Skip to content
This repository
Browse code

Merge branch 'master' of git@github.com:RJ/playdar-core

  • Loading branch information...
commit b49d253eb5d25c1cb5176e5ffa3a835b3002571f 2 parents 56d200a + 254ab61
James Wheare jwheare authored
2  contrib/player/README.txt
... ... @@ -0,0 +1,2 @@
  1 +A work in progress module that acts as a playback engine by enslaving mplayer.
  2 +
106 contrib/player/src/mplayer_master.erl
... ... @@ -0,0 +1,106 @@
  1 +-module(mplayer_master).
  2 +-behaviour(gen_server).
  3 +
  4 +% http://www.mplayerhq.hu/DOCS/tech/slave.txt
  5 +
  6 +%% API
  7 +-export([start_link/0, play/1, stop/0, send/1, np/0, get_property/1, pause/0,
  8 + volume/0]).
  9 +
  10 +%% gen_server callbacks
  11 +-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  12 + terminate/2, code_change/3]).
  13 +
  14 +-record(state, {port}).
  15 +
  16 +start_link() ->
  17 + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  18 +
  19 +play(File) -> gen_server:cast(?MODULE, {play, File}).
  20 +stop() -> send("stop").
  21 +pause() -> send("pause").
  22 +send(Cmd) -> gen_server:cast(?MODULE, {cmd, Cmd}).
  23 +np() -> get_property("path").
  24 +volume() -> get_property("volume").
  25 +%set_volume
  26 +get_property(P) -> gen_server:call(?MODULE, {get_property, P}).
  27 +
  28 +%% gen_server callbacks
  29 +
  30 +init([]) ->
  31 + Exe = "mplayer -slave -quiet -idle",
  32 + Port = open_port({spawn, Exe}, [{line, 1024}, use_stdio, stderr_to_stdout, hide]),
  33 + port_connect(Port, self()),
  34 + State = #state{port = Port},
  35 + {ok, State}.
  36 +
  37 +handle_call({get_property, P}, _From, State) ->
  38 + Cmd = io_lib:format("pausing_keep get_property ~s\n", [P]),
  39 + port_command(State#state.port, Cmd),
  40 + case receive_line(State) of
  41 + timeout -> {reply, timeout, State};
  42 + Resp ->
  43 + case Resp of
  44 + "Failed to get value of property" ++ _Rest ->
  45 + {reply, null, State};
  46 + Resp1 ->
  47 + % answers look like: 'ANS_<prop>=...'
  48 + Retval = lists:nthtail(length(P)+5, Resp1),
  49 + case Retval of
  50 + "(null)" ->
  51 + {reply, null, State};
  52 + _ ->
  53 + {reply, Retval, State}
  54 + end
  55 + end
  56 + end;
  57 +
  58 +handle_call(_Request, _From, State) ->
  59 + Reply = ok,
  60 + {reply, Reply, State}.
  61 +
  62 +handle_cast({play, File}, State) ->
  63 + F = io_lib:format("loadfile \"~s\"\n", [File]),
  64 + %io:format("Sending: ~s\n", [F]),
  65 + port_command(State#state.port, F),
  66 + {noreply, State};
  67 +
  68 +handle_cast({cmd, Cmd}, State) ->
  69 + CmdN = io_lib:format("pausing_keep ~s\n", [Cmd]),
  70 + port_command(State#state.port, CmdN),
  71 + {noreply, State};
  72 +
  73 +handle_cast(Msg, State) ->
  74 + io:format("Unhandled cast: ~p\n", [Msg]), %% TODO remove
  75 + {noreply, State}.
  76 +
  77 +handle_info({Port, {data, {_Flag, Line}}}, State = #state{port=Port}) ->
  78 + {noreply, handle_line(Line, State)};
  79 +
  80 +handle_info(Info, State) ->
  81 + io:format("Unhandled info: ~p\n", [Info]),
  82 + {noreply, State}.
  83 +
  84 +terminate(_Reason, State) ->
  85 + (catch port_command(State#state.port, "quit")),
  86 + ok.
  87 +code_change(_OldVsn, State, _Extra) ->
  88 + {ok, State}.
  89 +
  90 +%% ----------------
  91 +
  92 +receive_line(_State = #state{port=Port}) ->
  93 + receive
  94 + {Port, {data, {_Flag, Line}}} ->
  95 + Line
  96 + after 1000 ->
  97 + timeout
  98 + end.
  99 +
  100 +handle_line("Starting playback...", State) ->
  101 + io:format("PLAYBACK STARTING\n"),
  102 + State;
  103 +
  104 +handle_line(L, State) ->
  105 + io:format("UNHANDLED LINE: ~s\n", [L]),
  106 + State.

0 comments on commit b49d253

Please sign in to comment.
Something went wrong with that request. Please try again.