-
Notifications
You must be signed in to change notification settings - Fork 3
/
Broadcaster.as
182 lines (161 loc) · 5.46 KB
/
Broadcaster.as
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
import flash.media.Camera;
import flash.media.Microphone;
import flash.events.Event;
// prevent sleep and lock
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
var nc:NetConnection;
var ns:NetStream;
var cam:Camera = Camera.getCamera();
cam.setMode(video.width, video.height, 10);
video.attachCamera(cam);
var mic:Microphone;
var format:TextFormat = new TextFormat();
format.size = 14;
format.bold = true;
btnConnect.addEventListener(MouseEvent.CLICK, connect);
btnConnect.setStyle("textFormat", format);
btnPublish.addEventListener(MouseEvent.CLICK, publish);
btnPublish.setStyle("textFormat", format);
// open their browser to red5 page
//r5logo.addEventListener(MouseEvent.CLICK, goThere);
var publishing:Boolean = false;
//
message.htmlText = '<b>Broadcaster</b><br/>By Paul Gregoire<br/>http://gregoire.org/<br/>';
// get settings
var so:SharedObject = SharedObject.getLocal("userData");
url.text = so.data.url == null ? "rtmp://192.168.1.2/live" : so.data.url;
streamName.text = so.data.streamName == null ? "livestream" : so.data.streamName;
videoWidth.text = so.data.width == null ? "320" : so.data.width;
videoHeight.text = so.data.height == null ? "240" : so.data.height;
fps.text = so.data.fps == null ? "12" : so.data.fps;
quality.text = so.data.quality == null ? "80" : so.data.quality;
sampleRate.text = so.data.sampleRate == null ? "11" : so.data.sampleRate;
gain.text = so.data.gain == null ? "80" : so.data.gain;
function log(msg:String):void {
message.text += msg + '\n';
}
function connect(evt:Event):void {
if (btnConnect.label === 'Connect') {
log('Connecting...');
// create the netConnection
nc = new NetConnection();
nc.objectEncoding = ObjectEncoding.AMF0;
// set it's client/focus to this
nc.client = this;
nc.proxyType = "best";
// add listeners for netstatus and security issues
nc.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
//nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
//nc.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
//nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.connect(url.text, null);
} else if (btnConnect.label === 'Disconnect') {
log('Disconnecting...');
if (nc.connected) {
nc.close();
}
}
}
function publish(evt:Event):void {
if (!publishing) {
btnPublish.label = "Unpublish";
publishing = true;
mic = Microphone.getMicrophone();
if (mic != null) {
log("Microphone: " + mic.name);
mic.rate = int(sampleRate.text);
mic.gain = Number(gain.text);
mic.setSilenceLevel(5);
mic.setLoopBack(false);
mic.setUseEchoSuppression(true);
//mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
//mic.addEventListener(StatusEvent.STATUS, statusHandler);
ns.attachAudio(mic);
}
if (cam != null) {
log("Camera: " + cam.name);
cam.setMode(int(videoWidth.text), int(videoHeight.text), Number(fps.text));
cam.setQuality(0, int(quality.text));
ns.attachCamera(cam);
video.attachCamera(cam);
}
if (mic != null || cam != null) {
ns.publish(streamName.text, "live");
}
// save current settings
var so:SharedObject = SharedObject.getLocal("userData");
so.data.url = url.text;
so.data.streamName = streamName.text;
so.data.width = videoWidth.text;
so.data.height = videoHeight.text;
so.data.fps = fps.text;
so.data.quality = quality.text;
so.data.sampleRate = sampleRate.text;
so.data.gain = gain.text;
so.flush();
} else {
btnPublish.label = "Publish";
publishing = false;
ns.attachCamera(null);
ns.attachAudio(null);
//ns.close();
}
}
function onBWDone():void {
// have to have this for an RTMP connection
log('onBWDone');
}
function onBWCheck(... rest):uint {
log('onBWCheck');
//have to return something, so returning anything :)
return 0;
}
function onStatus(evt:NetStatusEvent):void {
log("NetConnection.onStatus " + evt);
//traceObject(evt);
var desc:String;
if (evt.info !== '' || evt.info !== null) {
log("Code: " + evt.info.code);
switch (evt.info.code) {
case "NetConnection.Connect.Success":
btnConnect.label = "Disconnect";
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
//ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.client = this;
//
btnPublish.enabled = true;
btnPublish.label = "Publish";
publishing = false;
break;
case "NetConnection.Connect.Failed":
break;
case "NetConnection.Connect.Rejected":
desc = evt.info.description;
log("Description: " + desc);
break;
case "NetConnection.Connect.Closed":
btnConnect.label = 'Connect';
btnPublish.enabled = false;
break;
case "NetConnection.Connect.SSLHandshakeFailed":
log("SSL handshake failed");
desc = evt.info.description;
log("Description: " + desc);
break;
}
}
}
function goThere(e:MouseEvent){
var request:URLRequest = new URLRequest("https://github.com/Red5");
try {
navigateToURL(request, "_blank");
} catch (e:Error) {
log("Error opening url " + e.message);
}
}
stage.addEventListener(Event.DEACTIVATE, deactivateHandler);
function deactivateHandler(event:Event):void {
NativeApplication.nativeApplication.exit();
}