-
Notifications
You must be signed in to change notification settings - Fork 2
/
streamingtest.js
175 lines (168 loc) · 5.96 KB
/
streamingtest.js
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
var janus = null;
var streaming = null;
var opaqueId = "streamingtest-"+Janus.randomString(12);
var bitrateTimer = null;
var selectedStream = null;
$(document).ready(function() {
// Initialize the library (all console debuggers enabled)
Janus.init({debug: "all", callback: function() {
// Make sure the browser supports WebRTC
if(!Janus.isWebrtcSupported()) {
alert("No WebRTC support... ");
return;
}
// Create session
janus = new Janus(
{
server: "http://192.168.1.100:8088/janus", //enter your janus gateway IP address
success: function() {
// Attach to streaming plugin
janus.attach(
{
plugin: "janus.plugin.streaming",
opaqueId: opaqueId,
success: function(pluginHandle) {
$('#watch').html("Watch").click(startStream);
streaming = pluginHandle;
Janus.log("Plugin attached! (" + streaming.getPlugin() + ", id=" + streaming.getId() + ")");
//get available streams and select the one we need
updateStreamsList();
},
error: function(error) {
Janus.error(" -- Error attaching plugin... ", error);
alert("Error attaching plugin... " + error);
},
onmessage: function(msg, jsep) {
Janus.debug(" ::: Got a message :::");
Janus.debug(msg);
var result = msg["result"];
if(result !== null && result !== undefined) {
if(result["status"] !== undefined && result["status"] !== null) {
var status = result["status"];
if(status === 'starting')
Janus.log("Starting, please wait...");
else if(status === 'started')
Janus.log("Started");
else if(status === 'stopped')
stopStream();
}
} else if(msg["error"] !== undefined && msg["error"] !== null) {
alert(msg["error"]);
stopStream();
return;
}
if(jsep !== undefined && jsep !== null) {
Janus.debug("Handling SDP as well...");
Janus.debug(jsep);
// Offer from the plugin, let's answer
streaming.createAnswer(
{
jsep: jsep,
media: { audioSend: false, videoSend: false }, // We want recvonly audio/video
success: function(jsep) {
Janus.debug("Got SDP!");
Janus.debug(jsep);
var body = { "request": "start" };
streaming.send({"message": body, "jsep": jsep});
$('#watch').html("Stop").click(stopStream);
},
error: function(error) {
Janus.error("WebRTC error:", error);
alert("WebRTC error... " + JSON.stringify(error));
}
});
}
},
onremotestream: function(stream) {
Janus.debug(" ::: Got a remote stream :::");
Janus.debug(stream);
if($('#remotevideo').length === 0) {
$('#stream').append('<video class="rounded centered" id="remotevideo" width=640 height=480 autoplay/>');
// Show the stream and hide the spinner when we get a playing event
$("#remotevideo").on("playing", function () {
Janus.log("playng");
var videoTracks = stream.getVideoTracks();
if(videoTracks === null || videoTracks === undefined || videoTracks.length === 0){
alert("no videotraks ?!?");
return;
}
});
}
Janus.attachMediaStream($('#remotevideo').get(0), stream);
var videoTracks = stream.getVideoTracks();
if(videoTracks === null || videoTracks === undefined || videoTracks.length === 0) {
// No remote video
alert("No remote video available!");
}
if(videoTracks && videoTracks.length &&
(Janus.webRTCAdapter.browserDetails.browser === "chrome" ||
Janus.webRTCAdapter.browserDetails.browser === "firefox" ||
Janus.webRTCAdapter.browserDetails.browser === "safari")) {
bitrateTimer = setInterval(function() {
Janus.debug("Current bitrate is " + streaming.getBitrate());
}, 1000);
}
},
oncleanup: function() {
Janus.log(" ::: Got a cleanup notification :::");
alert("streaming cleanup");
$('#remotevideo').remove();
if(bitrateTimer !== null && bitrateTimer !== undefined)
clearInterval(bitrateTimer);
bitrateTimer = null;
$('#watch').html("Watch").click(startStream);
}
});
},
error: function(error) {
Janus.error(error);
alert(error, function() {
window.location.reload();
});
},
destroyed: function() {
window.location.reload();
}
});
}});
});
function updateStreamsList() {
var body = { "request": "list" };
Janus.debug("Sending message (" + JSON.stringify(body) + ")");
streaming.send({"message": body, success: function(result) {
if(result === null || result === undefined) {
alert("Got no response to our query for available streams");
return;
}
if(result["list"] !== undefined && result["list"] !== null) {
var list = result["list"];
Janus.log("Got a list of available streams");
Janus.debug(list);
for(var mp in list) {
Janus.debug(" >> [" + list[mp]["id"] + "] " + list[mp]["description"] + " (" + list[mp]["type"] + ")");
}
selectedStream = list[0]["id"];
}
}});
}
function startStream() {
$('#watch').off();
Janus.log("Selected video id #" + selectedStream);
if(selectedStream === undefined || selectedStream === null) {
alert("unselected stream");
return;
}
var body = { "request": "watch", id: parseInt(selectedStream) };
streaming.send({"message": body});
// No remote video yet
Janus.log("starting stream");
}
function stopStream() {
$('#watch').off();
var body = { "request": "stop" };
streaming.send({"message": body});
streaming.hangup();
if(bitrateTimer !== null && bitrateTimer !== undefined)
clearInterval(bitrateTimer);
bitrateTimer = null;
}