Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebRTC: Add support for A/V only WHEP/WHEP player. v6.0.116 #3964

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions trunk/research/players/js/srs.sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,24 @@ function SrsRtcWhipWhepAsync() {
// See https://datatracker.ietf.org/doc/draft-ietf-wish-whip/
// @url The WebRTC url to publish with, for example:
// http://localhost:1985/rtc/v1/whip/?app=live&stream=livestream
self.publish = async function (url) {
// @options The options to control playing, supports:
// videoOnly: boolean, whether only play video, default to false.
// audioOnly: boolean, whether only play audio, default to false.
self.publish = async function (url, options) {
if (url.indexOf('/whip/') === -1) throw new Error(`invalid WHIP url ${url}`);
if (options?.videoOnly && options?.audioOnly) throw new Error(`The videoOnly and audioOnly in options can't be true at the same time`);

self.pc.addTransceiver("audio", {direction: "sendonly"});
self.pc.addTransceiver("video", {direction: "sendonly"});
if (!options?.videoOnly) {
self.pc.addTransceiver("audio", {direction: "sendonly"});
} else {
self.constraints.audio = false;
}

if (!options?.audioOnly) {
self.pc.addTransceiver("video", {direction: "sendonly"});
} else {
self.constraints.video = false;
}

if (!navigator.mediaDevices && window.location.protocol === 'http:' && window.location.hostname !== 'localhost') {
throw new SrsError('HttpsRequiredError', `Please use HTTPS or localhost to publish, read https://github.com/ossrs/srs/issues/2762#issuecomment-983147576`);
Expand All @@ -548,7 +561,7 @@ function SrsRtcWhipWhepAsync() {
var offer = await self.pc.createOffer();
await self.pc.setLocalDescription(offer);
const answer = await new Promise(function (resolve, reject) {
console.log("Generated offer: ", offer);
console.log(`Generated offer: ${offer.sdp}`);

const xhr = new XMLHttpRequest();
xhr.onload = function() {
Expand All @@ -572,16 +585,20 @@ function SrsRtcWhipWhepAsync() {
// See https://datatracker.ietf.org/doc/draft-ietf-wish-whip/
// @url The WebRTC url to play with, for example:
// http://localhost:1985/rtc/v1/whep/?app=live&stream=livestream
self.play = async function(url) {
// @options The options to control playing, supports:
// videoOnly: boolean, whether only play video, default to false.
// audioOnly: boolean, whether only play audio, default to false.
self.play = async function(url, options) {
if (url.indexOf('/whip-play/') === -1 && url.indexOf('/whep/') === -1) throw new Error(`invalid WHEP url ${url}`);
if (options?.videoOnly && options?.audioOnly) throw new Error(`The videoOnly and audioOnly in options can't be true at the same time`);

self.pc.addTransceiver("audio", {direction: "recvonly"});
self.pc.addTransceiver("video", {direction: "recvonly"});
if (!options?.videoOnly) self.pc.addTransceiver("audio", {direction: "recvonly"});
if (!options?.audioOnly) self.pc.addTransceiver("video", {direction: "recvonly"});

var offer = await self.pc.createOffer();
await self.pc.setLocalDescription(offer);
const answer = await new Promise(function(resolve, reject) {
console.log("Generated offer: ", offer);
console.log(`Generated offer: ${offer.sdp}`);

const xhr = new XMLHttpRequest();
xhr.onload = function() {
Expand Down
21 changes: 17 additions & 4 deletions trunk/research/players/whep.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,23 @@
<button class="btn btn-primary" id="btn_play">Play</button>
</div>

<label></label>
<p></p>
<video id="rtc_media_player" controls autoplay></video>

<label></label>
<p></p>
<div class="form-inline">
Controls:
<label>
<input type="checkbox" id="ch_videoonly" style="margin-bottom: 8px"> Video Only
</label>
<label>
<input type="checkbox" id="ch_audioonly" style="margin-bottom: 8px"> Audio Only
</label>
</div>

SessionID: <span id='sessionid'></span>

<label></label>
<p></p>
Simulator: <a href='#' id='simulator-drop'>Drop</a>

<footer>
Expand Down Expand Up @@ -89,7 +99,10 @@

// For example: webrtc://r.ossrs.net/live/livestream
var url = $("#txt_url").val();
sdk.play(url).then(function(session){
sdk.play(url, {
videoOnly: $('#ch_videoonly').prop('checked'),
audioOnly: $('#ch_audioonly').prop('checked'),
}).then(function(session){
$('#sessionid').html(session.sessionid);
$('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
}).catch(function (reason) {
Expand Down
24 changes: 19 additions & 5 deletions trunk/research/players/whip.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,28 @@
<button class="btn btn-primary" id="btn_publish">Publish</button>
</div>

<label></label>
<p></p>
<video id="rtc_media_player" width="320" autoplay muted></video>

<label></label>
<p></p>
<div class="form-inline">
Controls:
<label>
<input type="checkbox" id="ch_videoonly" style="margin-bottom: 8px"> Video Only
</label>
<label>
<input type="checkbox" id="ch_audioonly" style="margin-bottom: 8px"> Audio Only
</label>
</div>

<p></p>
SessionID: <span id='sessionid'></span>

<label></label>
<p></p>
Audio: <span id='acodecs'></span><br/>
Video: <span id='vcodecs'></span>

<label></label>
<p></p>
Simulator: <a href='#' id='simulator-drop'>Drop</a>

<footer>
Expand Down Expand Up @@ -101,7 +112,10 @@

// For example: webrtc://r.ossrs.net/live/livestream
var url = $("#txt_url").val();
sdk.publish(url).then(function(session){
sdk.publish(url, {
videoOnly: $('#ch_videoonly').prop('checked'),
audioOnly: $('#ch_audioonly').prop('checked'),
}).then(function(session){
$('#sessionid').html(session.sessionid);
$('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
}).catch(function (reason) {
Expand Down
Loading