This commit is contained in:
2023-10-29 13:17:28 +08:00
parent dee9051652
commit 336bcc662b
29 changed files with 3077 additions and 169 deletions

View File

@@ -3,7 +3,7 @@
<head>
<script type="text/javascript" src="./vconsole.min.js"></script>
<script>
// var vConsole = new VConsole();
var vConsole = new VConsole();
console.log('Hello world');
</script>
<meta charset="utf-8">

View File

@@ -1,4 +1,3 @@
/**
* The MIT License (MIT)
*
@@ -29,14 +28,66 @@
function SrsRtcPublisherAsync() {
var self = {};
self.cameraIndex=0
self.cameraDevices = []
self.url=""
// https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
self.constraints = {
audio: true,
video: {
deviceId:{
exact:"136e115ef37b7ec8361207a61d42cd39818381aa0e99728e35eb9746bedce6b4"
},
width: {ideal: 320, max: 576}
}
};
self.initDevices = async function () {
// await navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
}
function gotDevices(deviceInfos) {
let option = {}
// Handles being called several times to update labels. Preserve values.
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
const option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audioinput') {
console.log(option)
console.log(deviceInfo)
} else if (deviceInfo.kind === 'audiooutput') {
console.log(option)
console.log(deviceInfo)
} else if (deviceInfo.kind === 'videoinput') {
self.cameraDevices.push(deviceInfo)
} else {
console.log('Some other kind of source/device: ', deviceInfo);
}
}
}
function handleError(error) {
console.log('navigator.getUserMedia error: ', error);
}
self.changeCamera = async function(){
// console.log('navigator.getUserMedia')
// console.log(self.cameraDevices)
self.cameraIndex=self.cameraIndex+1==self.cameraDevices.length?0:self.cameraIndex+1
let deviceId={
exact:""
}
deviceId.exact=self.cameraDevices[self.cameraIndex].deviceId
self.constraints.video.deviceId=deviceId
self.publish(self.url)
console.log(self.constraints)
}
// @see https://github.com/rtcdn/rtcdn-draft
// @url The WebRTC url to play with, for example:
// webrtc://r.ossrs.net/live/livestream
@@ -59,12 +110,17 @@ function SrsRtcPublisherAsync() {
// or any other information, will pass-by in the query:
// webrtc://r.ossrs.net/live/livestream?vhost=xxx
// webrtc://r.ossrs.net/live/livestream?token=xxx
self.publish = async function (url) {
self.publish = async function (url,constraints={}) {
self.url=url;
var conf = self.__internal.prepareUrl(url);
self.pc.addTransceiver("audio", {direction: "sendonly"});
self.pc.addTransceiver("video", {direction: "sendonly"});
var stream = await navigator.mediaDevices.getUserMedia(self.constraints);
var stream = await navigator.mediaDevices.getUserMedia(constraints);
// @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
stream.getTracks().forEach(function (track) {
@@ -156,7 +212,7 @@ function SrsRtcPublisherAsync() {
return {
apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port,
tid: Number(parseInt(new Date().getTime()*Math.random()*100)).toString(16).slice(0, 7)
tid: Number(parseInt(new Date().getTime() * Math.random() * 100)).toString(16).slice(0, 7)
};
},
parse: function (url) {
@@ -300,14 +356,14 @@ function SrsRtcPlayerAsync() {
// or any other information, will pass-by in the query:
// webrtc://r.ossrs.net/live/livestream?vhost=xxx
// webrtc://r.ossrs.net/live/livestream?token=xxx
self.play = async function(url) {
self.play = async function (url) {
var conf = self.__internal.prepareUrl(url);
self.pc.addTransceiver("audio", {direction: "recvonly"});
self.pc.addTransceiver("video", {direction: "recvonly"});
var offer = await self.pc.createOffer();
await self.pc.setLocalDescription(offer);
var session = await new Promise(function(resolve, reject) {
var session = await new Promise(function (resolve, reject) {
// @see https://github.com/rtcdn/rtcdn-draft
var data = {
api: conf.apiUrl, tid: conf.tid, streamurl: conf.streamUrl,
@@ -317,15 +373,16 @@ function SrsRtcPlayerAsync() {
$.ajax({
type: "POST", url: conf.apiUrl, data: JSON.stringify(data),
contentType:'application/json', dataType: 'json'
}).done(function(data) {
contentType: 'application/json', dataType: 'json'
}).done(function (data) {
console.log("Got answer: ", data);
if (data.code) {
reject(data); return;
reject(data);
return;
}
resolve(data);
}).fail(function(reason){
}).fail(function (reason) {
reject(reason);
});
});
@@ -337,7 +394,7 @@ function SrsRtcPlayerAsync() {
};
// Close the player.
self.close = function() {
self.close = function () {
self.pc && self.pc.close();
self.pc = null;
};
@@ -383,7 +440,7 @@ function SrsRtcPlayerAsync() {
return {
apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port,
tid: Number(parseInt(new Date().getTime()*Math.random()*100)).toString(16).slice(0, 7)
tid: Number(parseInt(new Date().getTime() * Math.random() * 100)).toString(16).slice(0, 7)
};
},
parse: function (url) {
@@ -496,7 +553,7 @@ function SrsRtcPlayerAsync() {
self.stream = new MediaStream();
// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/ontrack
self.pc.ontrack = function(event) {
self.pc.ontrack = function (event) {
if (self.ontrack) {
self.ontrack(event);
}
@@ -511,7 +568,7 @@ function SrsRtcFormatSenders(senders, kind) {
var codecs = [];
senders.forEach(function (sender) {
var params = sender.getParameters();
params && params.codecs && params.codecs.forEach(function(c) {
params && params.codecs && params.codecs.forEach(function (c) {
if (kind && sender.track.kind !== kind) {
return;
}