[99s-extend] Newbie, Cowboy + Websocket + Audio Recording

Juan Matías jmrepetti at gmail.com
Mon Sep 29 18:52:16 CEST 2014


Hello list, I hope this is the right place to ask this.

I'm learning Erlang, and I wanted to create a Cowboy app to record audio
from a web browser.

Based on the websocket example in the Cowboy source code, I get the user
mic input and send this input to the websocket.

I created a "recorder" module, which functionality is to save the data to
the a file.


*#rawe_handler.erl*-module(rawec_handler).
-behaviour(cowboy_websocket_handler).
......
init(_, _, _) ->
  case whereis(recorder) of
    undefined ->
        RecorderPid = recorder:start(),
        register(recorder, RecorderPid);
    _ -> ok
  end,
    {upgrade, protocol, cowboy_websocket}.
.....
websocket_handle(_Frame, Req, State) ->
  RecorderPid = whereis(recorder),
  RecorderPid ! {rec, _Frame/binary},
    {ok, Req, State}.

*#recorder.erl*
-module(recorder).

-export([start/0, recorder_fun/1]).
-compile([debug_info]).

recorder_fun(IoDevice) ->
  receive
    {rec, Data} ->
      ok = file:write(IoDevice, Data),
      io:format(Data),
      recorder_fun(IoDevice);
    {stop, _} ->
      %%Close file
      file:close(IoDevice)
    end.

start() ->
  {ok, IoDevice} = file:open("/tmp/test_binary.wav", [write,
binary]),
  spawn(recorder, recorder_fun, [IoDevice]).


When I start the console, and allow the microphone on the browser, I see
this error on the console:

=ERROR REPORT==== 29-Sep-2014::18:13:03 ===
Ranch listener http had connection process started with
cowboy_protocol:start_link/4 at <0.178.0> exit with reason:
*{[{reason,badarith},{mfa,{rawec_handler,websocket_handle,3*}},{stacktrace,[{rawec_handler,websocket_handle,3,[{file,"src/rawec_handler.erl"},{line,35}]},{cowboy_websocket,handler_call,7,[{file,"src/cowboy_websocket.erl"},{line,588}]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,435}]}]},{msg,{binary,<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0....(ETC,
DATA STREAM CONTINUES)

Probably my approach to do this is totally wrong. I there any obvious
problem here?
Can someone point me to a right direction?. Maybe I should write directly
to a file in the *websocket_handle *funcion, but how can I keep a file
opened during the streaming?

The github repo is here: https://github.com/jmrepetti/rawec with the whole
source code if you want to take a look.


Thanks in advance,
Matias.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140929/84fe21a4/attachment.html>


More information about the Extend mailing list