-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamController.as
More file actions
253 lines (218 loc) · 8.63 KB
/
StreamController.as
File metadata and controls
253 lines (218 loc) · 8.63 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package {
import StreamView;
import StreamConnection;
import InputController;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.NetStatusEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.media.Video;
import flash.media.Camera;
import flash.external.ExternalInterface;
public class StreamController extends EventDispatcher {
var queue:Object;
var connections:Object;
public var input:InputController;
var view:StreamView;
var clock:Timer;
const STREAM_EXT = 'sdp';
/**
* Starts/stops individual streams and coordinates behaviors between them.
*/
public function StreamController( viewObj, inputCtrl:InputController=null ) {
view = viewObj;
input = inputCtrl || new InputController();
connections = {};
queue = {};
setClock();
}
/**
* Establishes a named 2-way stream connection
*/
function addConnection( connName, src, dest ) {
var conn = connections[connName] = new StreamConnection( connName, src, dest, input );
conn.addEventListener( NetStatusEvent.NET_STATUS, monitor );
conn.addEventListener( StreamConnection.DISCONNECTED, monitor );
conn.addEventListener( StreamConnection.CONNECTED, monitor );
conn.addEventListener( StreamConnection.STREAM_READY, monitor );
conn.addEventListener( StreamConnection.STREAMING, monitor );
}
/**
* Removes a connection entirely
*/
function dropConnection( connName ) {
trace('** Dropping connection', connName );
stop( connName );
delete connections[connName];
}
/**
* Publishes stream from source and plays stream at dest
*/
public function start( connName, streamName='' ) {
var conn = connections[connName],
type = conn.types
;
if ( ! conn.streamName ) {
conn.streamName = streamName || connName + '.' + STREAM_EXT;
trace('conn.streamName', conn.streamName.toString() );
}
trace( '** Starting stream', streamName, 'on connection', connName );
if ( conn.status !== StreamConnection.STREAM_READY ) {
trace( '* Stream ', connName, 'not ready. Queuing.' );
queueStream( connName );
return;
}
// Cam --> local video
if ( conn.sourceIsDevice() && type.dest === 'Video' ) {
trace('connecting', conn.dest, conn.source);
conn.dest.attachCamera( conn.source );
//conn.dest.attachAudio( conn.source );
}
// Cam --> media server
else if ( conn.sourceIsDevice() && type.dest === 'Remote' ) {
conn.stream.attachCamera( conn.source );
//conn.stream.attachAudio( conn.source );
conn.stream.publish( conn.streamName );
}
// TODO: Can three below can be consolidated?
// Media server --> local video
else if ( type.source === 'Remote' && type.dest === 'Video' ) {
conn.dest.attachNetStream( conn.stream );
conn.stream.play( conn.streamName );
}
// Local file --> local video
else if ( type.source === 'Local' && type.dest === 'Video' ) {
conn.dest.attachNetStream( conn.stream );
conn.stream.play( conn.streamName );
}
// Local file --> media server
else if ( type.source === 'Local' && type.dest === 'Remote' ) {
conn.dest.attachNetStream( conn.stream );
conn.stream.play( conn.streamName );
}
conn.status = StreamConnection.STREAMING;
}
/**
* Stops stream at both ends
*/
public function stop( connName, streamName='' ) {
var conn = connections[connName],
type = conn.types
;
trace( '** Stopping stream', streamName, 'on connection', connName );
if ( conn.status !== StreamConnection.STREAMING ) {
trace( '* Stream ', connName, 'not streaming? Not cool.');
}
// Cam -> local video
if ( conn.sourceIsDevice() && type.dest === 'Video' ) {
conn.dest.attachCamera( false );
//conn.dest.attachAudio( false );
}
// Cam -> media server
else if ( conn.sourceIsDevice() && type.dest === 'Remote' ) {
conn.stream.attachCamera( false );
//conn.stream.attachAudio( false );
conn.stream.publish( false );
}
// Remote -> Video | Local -> Video | Local -> Remote
else {
conn.dest.attachNetStream( false );
conn.stream.close( conn.streamName );
};
conn.status = StreamConnection.STREAM_READY;
}
/**
* Sends metadata to an active stream
*/
function sendData( connName, data ) {
connections[connName].stream.send( "@setDataFrame", "onMetaData", data );
}
/**
* Removes connection and places it in queue.
* Connection should have a stream object before this point.
*/
function queueStream( connName ) {
queue[connName] = connections[connName];
delete connections[connName];
trace( '*', connName, 'stream queued' );
}
/**
* Transfers queued stream into active connections and starts.
*/
function startQueuedStream( connName, checkSources=true ) {
var connName, queuedConn,
toDelete = []
;
trace( '* Start queued connection', connName );
if ( ! queue[connName] ) {
return; }
connections[connName] = queue[connName];
delete queue[connName];
trace( '* Unqueued', connName );
if ( checkSources && queue.length > 0 ) {
startAllStreamsUsingSource( connections[connName].source );
}
start( connName );
}
/**
* Starts any streams using the specified source.
*/
function startAllStreamsUsingSource( src ) {
var conn;
for ( conn in queue ) {
if ( conn.source == src ) {
startQueuedStream( conn.name, false );
}
}
}
/**
* Simple timer used to regularly broadcast network status
*/
function setClock( interval=1000 ) {
clock = new Timer( interval );
if ( input.cam ) {
clock.addEventListener( TimerEvent.TIMER, view.updateCamInfo );
}
clock.addEventListener( TimerEvent.TIMER, broadcastConnStatus );
clock.start();
}
/**
* Sends network status updates to view
*/
function broadcastConnStatus( evt:TimerEvent ):void {
var conn;
for ( conn in connections ) {
view.updateStreamInfo( connections[conn] );
/*ExternalInterface.call( 'tweetOff.streamDriver.updateStatus', connections[conn].status );*/
}
}
/**
* Monitors StreamConnection and NetStatus events
*/
function monitor( evt:* ) {
var connName = evt.target.name;
if ( evt.type === StreamConnection.STREAM_READY ) {
trace( '** STREAM READY', evt.target.name, '|', evt.target );
( queue[connName] ) ? startQueuedStream( connName ) : start( connName );
}
else if ( evt.type === NetStatusEvent.NET_STATUS ) {
if ( evt.info.level === 'error' ) {
handleConnectionError( evt );
}
// Broadcast to javascript
ExternalInterface.call( driverCall( 'streamMonitor' ), connName, evt.info );
}
}
function handleConnectionError( evt:NetStatusEvent ) {
// Connection.Failed
// Connection.Rejected
// Record.Failed
// Record.NoAccess
// Publish.BadName - shouldn't come up if stream naming conventions are strong
}
function driverCall( method ) {
return view.options.driver + '.' + method;
}
}
}