mirror of
https://github.com/ossrs/srs.git
synced 2025-02-15 04:42:04 +00:00
RTC: Refine js for player
This commit is contained in:
parent
07ea733901
commit
72f87e7cbc
1 changed files with 249 additions and 68 deletions
|
@ -58,61 +58,25 @@
|
||||||
</footer>
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var pc = null; // Global handler to do cleanup when replaying.
|
|
||||||
$(function(){
|
$(function(){
|
||||||
var startPlay = function() {
|
// Async-await-promise based SRS RTC Player.
|
||||||
$('#rtc_media_player').show();
|
function SrsRtcPlayerAsync() {
|
||||||
var urlObject = parse_rtmp_url($("#txt_url").val());
|
var self = {
|
||||||
var schema = window.location.protocol;
|
play: async function(apiUrl, streamUrl) {
|
||||||
|
self.pc.addTransceiver("audio", {direction: "recvonly"});
|
||||||
// Close PC when user replay.
|
self.pc.addTransceiver("video", {direction: "recvonly"});
|
||||||
if (pc) {
|
|
||||||
pc.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
pc = new RTCPeerConnection(null);
|
|
||||||
pc.onaddstream = function (event) {
|
|
||||||
console.log('Start play, event: ', event);
|
|
||||||
$('#rtc_media_player').prop('srcObject', event.stream);
|
|
||||||
};
|
|
||||||
new Promise(function(resolve, reject) {
|
|
||||||
pc.addTransceiver("audio", {direction: "recvonly"});
|
|
||||||
pc.addTransceiver("video", {direction: "recvonly"});
|
|
||||||
|
|
||||||
pc.createOffer(function(offer){
|
|
||||||
resolve(offer);
|
|
||||||
},function(reason){
|
|
||||||
reject(reason);
|
|
||||||
});
|
|
||||||
}).then(function(offer) {
|
|
||||||
return pc.setLocalDescription(offer).then(function(){ return offer; });
|
|
||||||
}).then(function(offer) {
|
|
||||||
return new Promise(function(resolve, reject) {
|
|
||||||
var port = urlObject.port || 1985;
|
|
||||||
|
|
||||||
// @see https://github.com/rtcdn/rtcdn-draft
|
|
||||||
var api = urlObject.user_query.play || '/rtc/v1/play/';
|
|
||||||
if (api.lastIndexOf('/') != api.length - 1) {
|
|
||||||
api += '/';
|
|
||||||
}
|
|
||||||
|
|
||||||
var url = schema + '//' + urlObject.server + ':' + port + api;
|
|
||||||
for (var key in urlObject.user_query) {
|
|
||||||
if (key != 'api' && key != 'play') {
|
|
||||||
url += '&' + key + '=' + urlObject.user_query[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Replace /rtc/v1/play/&k=v to /rtc/v1/play/?k=v
|
|
||||||
url = url.replace(api + '&', api + '?');
|
|
||||||
|
|
||||||
|
var offer = await self.pc.createOffer();
|
||||||
|
await self.pc.setLocalDescription(offer);
|
||||||
|
var session = await new Promise(function(resolve, reject) {
|
||||||
// @see https://github.com/rtcdn/rtcdn-draft
|
// @see https://github.com/rtcdn/rtcdn-draft
|
||||||
var data = {
|
var data = {
|
||||||
api: url, streamurl: urlObject.url, clientip: null, sdp: offer.sdp
|
api: apiUrl, streamurl: streamUrl, clientip: null, sdp: offer.sdp
|
||||||
};
|
};
|
||||||
console.log("Generated offer: ", data);
|
console.log("Generated offer: ", data);
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "POST", url: url, data: JSON.stringify(data),
|
type: "POST", url: apiUrl, data: JSON.stringify(data),
|
||||||
contentType:'application/json', dataType: 'json'
|
contentType:'application/json', dataType: 'json'
|
||||||
}).done(function(data) {
|
}).done(function(data) {
|
||||||
console.log("Got answer: ", data);
|
console.log("Got answer: ", data);
|
||||||
|
@ -120,20 +84,237 @@
|
||||||
reject(data); return;
|
reject(data); return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var simulator = schema + '//' + urlObject.server + ':' + port + '/rtc/v1/nack/';
|
resolve(data);
|
||||||
$('#sessionid').html(data.sessionid);
|
|
||||||
$('#simulator-drop').attr('href', simulator + '?drop=1&username=' + data.sessionid);
|
|
||||||
resolve(data.sdp);
|
|
||||||
}).fail(function(reason){
|
}).fail(function(reason){
|
||||||
reject(reason);
|
reject(reason);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}).then(function(answer) {
|
await self.pc.setRemoteDescription(
|
||||||
return pc.setRemoteDescription(new RTCSessionDescription({type: 'answer', sdp: answer}));
|
new RTCSessionDescription({type: 'answer', sdp: session.sdp})
|
||||||
|
);
|
||||||
|
return session;
|
||||||
|
},
|
||||||
|
close: function() {
|
||||||
|
self.pc.close();
|
||||||
|
},
|
||||||
|
// callbacks.
|
||||||
|
onaddstream: function (event) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.pc = new RTCPeerConnection(null);
|
||||||
|
self.pc.onaddstream = function (event) {
|
||||||
|
if (self.onaddstream) {
|
||||||
|
self.onaddstream(event);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Promise based SRS RTC Player.
|
||||||
|
function SrsRtcPlayerPromise() {
|
||||||
|
var self = {
|
||||||
|
play: function(apiUrl, streamUrl) {
|
||||||
|
self.pc.addTransceiver("audio", {direction: "recvonly"});
|
||||||
|
self.pc.addTransceiver("video", {direction: "recvonly"});
|
||||||
|
|
||||||
|
return self.pc.createOffer().then(function(offer) {
|
||||||
|
return self.pc.setLocalDescription(offer).then(function(){ return offer; });
|
||||||
|
}).then(function(offer) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
// @see https://github.com/rtcdn/rtcdn-draft
|
||||||
|
var data = {
|
||||||
|
api: apiUrl, streamurl: streamUrl, clientip: null, sdp: offer.sdp
|
||||||
|
};
|
||||||
|
console.log("Generated offer: ", data);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: "POST", url: apiUrl, data: JSON.stringify(data),
|
||||||
|
contentType:'application/json', dataType: 'json'
|
||||||
|
}).done(function(data) {
|
||||||
|
console.log("Got answer: ", data);
|
||||||
|
if (data.code) {
|
||||||
|
reject(data); return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(data);
|
||||||
|
}).fail(function(reason){
|
||||||
|
reject(reason);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}).then(function(session) {
|
||||||
|
return self.pc.setRemoteDescription(
|
||||||
|
new RTCSessionDescription({type: 'answer', sdp: session.sdp})
|
||||||
|
).then(function(){ return session; });
|
||||||
|
});
|
||||||
|
},
|
||||||
|
close: function() {
|
||||||
|
self.pc.close();
|
||||||
|
},
|
||||||
|
// callbacks.
|
||||||
|
onaddstream: function (event) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.pc = new RTCPeerConnection(null);
|
||||||
|
self.pc.onaddstream = function (event) {
|
||||||
|
if (self.onaddstream) {
|
||||||
|
self.onaddstream(event);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Callback based SRS RTC Player.
|
||||||
|
function SrsRtcPlayerCallbacks() {
|
||||||
|
var self = {
|
||||||
|
play: function(apiUrl, streamUrl, success, fail) {
|
||||||
|
self.pc.addTransceiver("audio", {direction: "recvonly"});
|
||||||
|
self.pc.addTransceiver("video", {direction: "recvonly"});
|
||||||
|
|
||||||
|
var onAnswer = function(session) {
|
||||||
|
var answer = session.sdp;
|
||||||
|
self.pc.setRemoteDescription(
|
||||||
|
new RTCSessionDescription({type: 'answer', sdp: answer})
|
||||||
|
).then(function(){
|
||||||
|
success(session);
|
||||||
}).catch(function(reason) {
|
}).catch(function(reason) {
|
||||||
pc.close(); $('#rtc_media_player').hide();
|
fail(reason);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
var onOfferDone = function (offer) {
|
||||||
|
// @see https://github.com/rtcdn/rtcdn-draft
|
||||||
|
var data = {
|
||||||
|
api: apiUrl, streamurl: streamUrl, clientip: null, sdp: offer.sdp
|
||||||
|
};
|
||||||
|
console.log("Generated offer: ", data);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: "POST", url: apiUrl, data: JSON.stringify(data),
|
||||||
|
contentType:'application/json', dataType: 'json'
|
||||||
|
}).done(function(data) {
|
||||||
|
console.log("Got answer: ", data);
|
||||||
|
if (data.code) {
|
||||||
|
fail(data); return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onAnswer(data);
|
||||||
|
}).fail(function(reason){
|
||||||
|
fail(reason);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
var onOffer = function(offer) {
|
||||||
|
self.pc.setLocalDescription(offer, function(){
|
||||||
|
onOfferDone(offer);
|
||||||
|
}, function(reason) {
|
||||||
|
fail(reason);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
self.pc.createOffer(function(offer){
|
||||||
|
onOffer(offer);
|
||||||
|
}, function(reason){
|
||||||
|
fail(reason);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
close: function() {
|
||||||
|
self.pc.close();
|
||||||
|
},
|
||||||
|
// callbacks.
|
||||||
|
onaddstream: function (event) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.pc = new RTCPeerConnection(null);
|
||||||
|
self.pc.onaddstream = function (event) {
|
||||||
|
if (self.onaddstream) {
|
||||||
|
self.onaddstream(event);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build RTC api url.
|
||||||
|
var prepareUrl = function () {
|
||||||
|
var apiUrl, streamUrl;
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
var urlObject = parse_rtmp_url($("#txt_url").val());
|
||||||
|
var schema = window.location.protocol;
|
||||||
|
var port = urlObject.port || 1985;
|
||||||
|
|
||||||
|
// @see https://github.com/rtcdn/rtcdn-draft
|
||||||
|
var api = urlObject.user_query.play || '/rtc/v1/play/';
|
||||||
|
if (api.lastIndexOf('/') !== api.length - 1) {
|
||||||
|
api += '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
apiUrl = schema + '//' + urlObject.server + ':' + port + api;
|
||||||
|
for (var key in urlObject.user_query) {
|
||||||
|
if (key !== 'api' && key !== 'play') {
|
||||||
|
apiUrl += '&' + key + '=' + urlObject.user_query[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Replace /rtc/v1/play/&k=v to /rtc/v1/play/?k=v
|
||||||
|
apiUrl = apiUrl.replace(api + '&', api + '?');
|
||||||
|
|
||||||
|
streamUrl = urlObject.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Start play with conf.
|
||||||
|
var playStream = function (conf) {
|
||||||
|
// Close PC when user replay.
|
||||||
|
if (sdk) {
|
||||||
|
sdk.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use Callback style.
|
||||||
|
if (true) {
|
||||||
|
if (true) {
|
||||||
|
sdk = new SrsRtcPlayerAsync();
|
||||||
|
} else {
|
||||||
|
sdk = new SrsRtcPlayerPromise();
|
||||||
|
}
|
||||||
|
sdk.onaddstream = function (event) {
|
||||||
|
console.log('Start play, event: ', event);
|
||||||
|
$('#rtc_media_player').prop('srcObject', event.stream);
|
||||||
|
};
|
||||||
|
|
||||||
|
sdk.play(conf.apiUrl, conf.streamUrl).then(function(session){
|
||||||
|
var simulator = conf.schema + '//' + conf.urlObject.server + ':' + conf.port + '/rtc/v1/nack/';
|
||||||
|
$('#sessionid').html(session.sessionid);
|
||||||
|
$('#simulator-drop').attr('href', simulator + '?drop=1&username=' + session.sessionid);
|
||||||
|
}).catch(function (reason) {
|
||||||
|
sdk.close();
|
||||||
|
$('#rtc_media_player').hide();
|
||||||
|
console.error(reason);
|
||||||
|
});
|
||||||
|
} else if (false) {
|
||||||
|
sdk = new SrsRtcPlayerCallbacks();
|
||||||
|
sdk.onaddstream = function (event) {
|
||||||
|
console.log('Start play, event: ', event);
|
||||||
|
$('#rtc_media_player').prop('srcObject', event.stream);
|
||||||
|
};
|
||||||
|
|
||||||
|
sdk.play(conf.apiUrl, conf.streamUrl, function (session) {
|
||||||
|
var simulator = conf.schema + '//' + conf.urlObject.server + ':' + conf.port + '/rtc/v1/nack/';
|
||||||
|
$('#sessionid').html(session.sessionid);
|
||||||
|
$('#simulator-drop').attr('href', simulator + '?drop=1&username=' + session.sessionid);
|
||||||
|
}, function (reason) {
|
||||||
|
sdk.close();
|
||||||
|
$('#rtc_media_player').hide();
|
||||||
throw reason;
|
throw reason;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var sdk = null; // Global handler to do cleanup when replaying.
|
||||||
|
var startPlay = function() {
|
||||||
|
$('#rtc_media_player').show();
|
||||||
|
var conf = prepareUrl();
|
||||||
|
playStream(conf);
|
||||||
};
|
};
|
||||||
|
|
||||||
$('#rtc_media_player').hide();
|
$('#rtc_media_player').hide();
|
||||||
|
|
Loading…
Reference in a new issue