-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamConnection.as
More file actions
191 lines (158 loc) · 5.94 KB
/
StreamConnection.as
File metadata and controls
191 lines (158 loc) · 5.94 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
package {
import InputController;
import flash.events.Event;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.events.StatusEvent;
import flash.events.NetStatusEvent;
import flash.events.ActivityEvent;
import flash.events.SecurityErrorEvent;
import flash.external.ExternalInterface;
import flash.utils.*;
public class StreamConnection extends NetConnection {
public static const DISCONNECTED = 'disconnected';
public static const CONNECTED = 'connected';
public static const STREAM_READY = 'stream_ready';
public static const STREAMING = 'streaming';
public var name:String;
public var source;
public var dest;
public var stream:NetStream;
public var streamName:String;
public var types:Object;
public var status:String;
public var input:InputController;
private var options:Object;
/**
* Initiates, monitors and maintains a single network onnection,
* as well as any NetStreams established over that connection.
* Acts as facade for local/device connections to use same API.
*/
public function StreamConnection( connName, src, dest, inputCtrl ) {
name = connName;
source = src;
this.dest = dest;
input = inputCtrl;
status = StreamConnection.DISCONNECTED;
types = {
source : determineResourceType( source ),
dest : determineResourceType( dest )
};
trace( '** Adding connection ', connName, types.source, '-->', types.dest );
initConnection();
}
/**
* Initiates network connection if needed and sets listeners
*/
function initConnection() {
var netConnRequired = true;
if ( sourceIsDevice() ) {
netConnRequired = connectDevice();
if ( ! netConnRequired ) {
return; }
}
addEventListener( NetStatusEvent.NET_STATUS, netMonitor );
addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityMonitor );
if ( types.source === 'Remote' ) {
connect( source );
}
else if ( types.dest === 'Remote' ) {
connect( dest );
}
else {
connect( null ); // Local
}
}
/**
* Initiates "connection" when one end is a cam/mic
*/
function connectDevice() {
source.addEventListener( StatusEvent.STATUS, deviceMonitor );
if ( ! input.activated() ) {
input.askPermission();
}
if ( types.dest === 'Video' ) {
initializeStream();
return false;
}
else {
return true;
}
}
/**
* Establishes a ready-to-play network stream and announces same.
*/
function initializeStream() {
trace( '* Initializing stream for', name );
if ( sourceIsDevice() ) {
initializeDeviceStream();
}
else {
stream = new NetStream( this );
stream.addEventListener( NetStatusEvent.NET_STATUS, netMonitor );
trace( 'Empty stream initialized for', name );
status = StreamConnection.STREAM_READY;
dispatchEvent( new Event( StreamConnection.STREAM_READY ));
}
}
/**
* Establishes stream for cam/mic if destination is not local
*/
function initializeDeviceStream() {
if ( source.muted ) {
return; }
if ( types.dest != 'Video' ) {
stream = new NetStream( this );
stream.addEventListener( NetStatusEvent.NET_STATUS, netMonitor );
trace( 'Empty stream initialized for', name );
}
status = StreamConnection.STREAM_READY;
dispatchEvent( new Event( StreamConnection.STREAM_READY ));
}
/**
* Monitors all network events for the connection and acts accordingly.
*/
function netMonitor( evt ):void {
trace( '* Net event on', name, '|', evt.info.code );
//ExternalInterface.call( 'tweetOff.streamDriver.updateStatus', name, evt.info );
switch ( evt.info.code ) {
case "NetConnection.Connect.Success":
status = StreamConnection.CONNECTED;
initializeStream();
break;
case "NetStream.Play.StreamNotFound":
trace( "Stream not found: " + streamName );
break;
}
}
/**
* Monitors cam/mic access and "connects" devices when they become available
*/
function deviceMonitor( evt:StatusEvent ):void {
trace( '* Activity on device', evt.target, '|', evt.code );
if ( evt.code.search( 'Unmuted' ) > -1 ) {
initializeStream();
}
}
function securityMonitor( event:SecurityErrorEvent ):void {
trace("securityErrorHandler: ", event );
}
function sourceIsDevice() {
return ( ["Camera", "Microphone"].indexOf( types.source ) !== -1 );
}
/**
* cam - Camera
* mic - Microphone
* local - 'local/path/to/file'
* network - 'protocol:[//host][:port]/appname[/instanceName]'
*/
private function determineResourceType( obj ) {
var type = getQualifiedClassName( obj ).split('::').pop();
trace(obj, type);
if ( type === 'String' ) {
type = ( obj.search('://') === -1 ) ? 'Local' : 'Remote';
}
return type;
}
}
}