forked from erlyvideo/erlyvideo-old
/
ems_flv_streams.erl
168 lines (142 loc) · 6.19 KB
/
ems_flv_streams.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
%%% @author Max Lapshin <max@maxidoors.ru> [http://erlyvideo.org]
%%% @copyright 2009 Max Lapshin
%%% @doc Endless http FLV streams registrator
%%% @reference See <a href="http://erlyvideo.org/" target="_top">http://erlyvideo.org/</a> for more information
%%% @end
%%%
%%% This file is part of erlyvideo.
%%%
%%% erlyvideo is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation, either version 3 of the License, or
%%% (at your option) any later version.
%%%
%%% erlyvideo is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with erlyvideo. If not, see <http://www.gnu.org/licenses/>.
%%%
%%%---------------------------------------------------------------------------------------
-module(ems_flv_streams).
-author('Max Lapshin <max@maxidoors.ru>').
-behaviour(gen_server).
-record(flv_streams, {
streams = []
}).
%% External API
-export([start_link/0, register/2, stream/1, command/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
%%--------------------------------------------------------------------
%% @spec (Name) -> undefined | {ok,Pid::pid()}
%%
%% @doc Find stream by name
%% @end
%%----------------------------------------------------------------------
stream(Name) ->
gen_server:call(?MODULE, {stream, Name}).
%%--------------------------------------------------------------------
%% @spec (Name,Command) -> undefined|Command
%%
%% @doc Send command to stream if exists
%% @end
%%----------------------------------------------------------------------
command(Name,Command) ->
case stream(Name) of
undefined -> undefined;
{ok, Pid, Client} -> Pid ! {Command, Client}
end.
%%--------------------------------------------------------------------
%% @spec (Name,Pid) -> ok
%%
%% @doc register stream under Name
%% @end
%%----------------------------------------------------------------------
register(Name, Pid) ->
gen_server:call(?MODULE, {register, Name, Pid}).
%%%------------------------------------------------------------------------
%%% Callback functions from gen_server
%%%------------------------------------------------------------------------
%%----------------------------------------------------------------------
%% @spec (Port::integer()) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%%
%% @doc Called by gen_server framework at process startup.
%% Create listening socket.
%% @end
%%----------------------------------------------------------------------
init([]) ->
{ok, #flv_streams{}}.
%%-------------------------------------------------------------------------
%% @spec (Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @doc Callback for synchronous server calls. If `{stop, ...}' tuple
%% is returned, the server is stopped and `terminate/2' is called.
%% @end
%% @private
%%-------------------------------------------------------------------------
handle_call({register, Name, Pid}, _From, #flv_streams{streams = Streams} = Server) ->
erlang:monitor(process, Pid),
{reply, ok, Server#flv_streams{streams = lists:keystore(Name, 1, Streams, {Name, Pid})}};
handle_call({stream, Name}, _From, #flv_streams{streams = Streams} = Server) ->
Reply = case proplists:get_value(Name, Streams) of
undefined -> undefined;
Pid -> {ok, Pid}
end,
{reply, Reply, Server};
handle_call(Request, _From, State) ->
{stop, {unknown_call, Request}, State}.
%%-------------------------------------------------------------------------
%% @spec (Msg, State) ->{noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @doc Callback for asyncrous server calls. If `{stop, ...}' tuple
%% is returned, the server is stopped and `terminate/2' is called.
%% @end
%% @private
%%-------------------------------------------------------------------------
handle_cast(_Msg, State) ->
{stop, {unknown_cast, _Msg}, State}.
%%-------------------------------------------------------------------------
%% @spec (Msg, State) ->{noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @doc Callback for messages sent directly to server's mailbox.
%% If `{stop, ...}' tuple is returned, the server is stopped and
%% `terminate/2' is called.
%% @end
%% @private
%%-------------------------------------------------------------------------
handle_info({'DOWN', _, process, Client, _Reason}, #flv_streams{streams = Streams} = Server) ->
{noreply, Server#flv_streams{streams = lists:keydelete(Client, 2, Streams)}};
handle_info(_Info, State) ->
{noreply, State}.
%%-------------------------------------------------------------------------
%% @spec (Reason, State) -> any
%% @doc Callback executed on server shutdown. It is only invoked if
%% `process_flag(trap_exit, true)' is set by the server process.
%% The return value is ignored.
%% @end
%% @private
%%-------------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%-------------------------------------------------------------------------
%% @spec (OldVsn, State, Extra) -> {ok, NewState}
%% @doc Convert process state when code is changed.
%% @end
%% @private
%%-------------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.