mirror of
https://github.com/ossrs/srs.git
synced 2025-11-23 19:34:05 +08:00
This PR adds G.711 (PCMU/PCMA) audio codec support for WebRTC in SRS, enabling relay-only streaming of G.711 audio between WebRTC clients via WHIP/WHEP. G.711 is a widely-used, royalty-free audio codec with excellent compatibility across VoIP systems, IP cameras, and legacy telephony equipment. Fixes #4075 Many IP cameras, VoIP systems, and IoT devices use G.711 (PCMU/PCMA) as their default audio codec. Previously, SRS only supported Opus for WebRTC audio, requiring transcoding or rejecting G.711 streams entirely. This PR enables direct relay of G.711 audio streams in WebRTC, similar to how VP9/AV1 video codecs are supported. Enhanced WHIP/WHEP players with URL-based codec selection: ``` # Audio codec only http://localhost:8080/players/whip.html?acodec=pcmu http://localhost:8080/players/whip.html?acodec=pcma # Video + audio codecs http://localhost:8080/players/whip.html?vcodec=vp9&acodec=pcmu http://localhost:8080/players/whep.html?vcodec=h264&acodec=pcma # Backward compatible (codec = vcodec) http://localhost:8080/players/whip.html?codec=vp9 ``` Testing ```bash # Build and run unit tests cd trunk make utest -j && ./objs/srs_utest # Test with WHIP player # 1. Start SRS server ./objs/srs -c conf/rtc.conf # 2. Open WHIP publisher with PCMU audio http://localhost:8080/players/whip.html?acodec=pcmu # 3. Open WHEP player to receive stream http://localhost:8080/players/whep.html ``` ## Related Issues - Fixes #4075 - WebRTC G.711A Audio Codec Support - Related to #4548 - VP9 codec support (similar relay-only pattern)
This commit is contained in:
@@ -24,7 +24,7 @@ function update_nav() {
|
||||
$("#nav_vlc").attr("href", "vlc.html" + window.location.search);
|
||||
}
|
||||
|
||||
// Special extra params, such as auth_key.
|
||||
// Special extra params, such as auth_key, codec, vcodec, acodec.
|
||||
function user_extra_params(query, params, rtc) {
|
||||
var queries = params || [];
|
||||
|
||||
@@ -124,6 +124,9 @@ function build_default_whip_whep_url(query, apiPath) {
|
||||
console.log('?api=x to overwrite WebRTC API(1985).');
|
||||
console.log('?schema=http|https to overwrite WebRTC API protocol.');
|
||||
console.log(`?path=xxx to overwrite default ${apiPath}`);
|
||||
console.log('?codec=xxx to specify video codec (alias for vcodec, e.g., h264, vp9, av1)');
|
||||
console.log('?vcodec=xxx to specify video codec (e.g., h264, vp9, av1)');
|
||||
console.log('?acodec=xxx to specify audio codec (e.g., opus, pcmu, pcma)');
|
||||
|
||||
var server = (!query.server)? window.location.hostname:query.server;
|
||||
var vhost = (!query.vhost)? window.location.hostname:query.vhost;
|
||||
|
||||
@@ -41,11 +41,15 @@ function SrsRtcWhipWhepAsync() {
|
||||
// camera: boolean, whether capture video from camera, default to true.
|
||||
// screen: boolean, whether capture video from screen, default to false.
|
||||
// audio: boolean, whether play audio, default to true.
|
||||
// vcodec: string, video codec to use (e.g., 'h264', 'vp9', 'av1'), default to undefined.
|
||||
// acodec: string, audio codec to use (e.g., 'opus', 'pcmu', 'pcma'), default to undefined.
|
||||
self.publish = async function (url, options) {
|
||||
if (url.indexOf('/whip/') === -1) throw new Error(`invalid WHIP url ${url}`);
|
||||
const hasAudio = options?.audio ?? true;
|
||||
const useCamera = options?.camera ?? true;
|
||||
const useScreen = options?.screen ?? false;
|
||||
const vcodec = options?.vcodec;
|
||||
const acodec = options?.acodec;
|
||||
|
||||
if (!hasAudio && !useCamera && !useScreen) throw new Error(`The camera, screen and audio can't be false at the same time`);
|
||||
|
||||
@@ -91,6 +95,13 @@ function SrsRtcWhipWhepAsync() {
|
||||
|
||||
var offer = await self.pc.createOffer();
|
||||
await self.pc.setLocalDescription(offer);
|
||||
|
||||
// Filter codecs if specified
|
||||
if (vcodec || acodec) {
|
||||
offer.sdp = self.__internal.filterCodec(offer.sdp, vcodec, acodec);
|
||||
console.log(`Filtered codecs (vcodec=${vcodec}, acodec=${acodec}): ${offer.sdp}`);
|
||||
}
|
||||
|
||||
const answer = await new Promise(function (resolve, reject) {
|
||||
console.log(`Generated offer: ${offer.sdp}`);
|
||||
|
||||
@@ -119,15 +130,26 @@ function SrsRtcWhipWhepAsync() {
|
||||
// @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.
|
||||
// vcodec: string, video codec to use (e.g., 'h264', 'vp9', 'av1'), default to undefined.
|
||||
// acodec: string, audio codec to use (e.g., 'opus', 'pcmu', 'pcma'), default to undefined.
|
||||
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`);
|
||||
const vcodec = options?.vcodec;
|
||||
const acodec = options?.acodec;
|
||||
|
||||
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);
|
||||
|
||||
// Filter codecs if specified
|
||||
if (vcodec || acodec) {
|
||||
offer.sdp = self.__internal.filterCodec(offer.sdp, vcodec, acodec);
|
||||
console.log(`Filtered codecs (vcodec=${vcodec}, acodec=${acodec}): ${offer.sdp}`);
|
||||
}
|
||||
|
||||
const answer = await new Promise(function(resolve, reject) {
|
||||
console.log(`Generated offer: ${offer.sdp}`);
|
||||
|
||||
@@ -199,6 +221,43 @@ function SrsRtcWhipWhepAsync() {
|
||||
simulator: a.protocol + '//' + a.host + '/rtc/v1/nack/',
|
||||
};
|
||||
},
|
||||
filterCodec: (sdp, vcodec, acodec) => {
|
||||
// Filter video codec if specified
|
||||
if (vcodec) {
|
||||
const vcodecUpper = vcodec.toUpperCase();
|
||||
sdp = sdp.split('\n').filter(line => {
|
||||
// Keep all non-video lines
|
||||
if (!line.startsWith('a=rtpmap:') && !line.startsWith('a=rtcp-fb:') &&
|
||||
!line.startsWith('a=fmtp:')) {
|
||||
return true;
|
||||
}
|
||||
// For video codec lines, only keep the specified codec
|
||||
if (line.includes('video/')) {
|
||||
return line.toUpperCase().includes(vcodecUpper);
|
||||
}
|
||||
return true;
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
// Filter audio codec if specified
|
||||
if (acodec) {
|
||||
const acodecUpper = acodec.toUpperCase();
|
||||
sdp = sdp.split('\n').filter(line => {
|
||||
// Keep all non-audio lines
|
||||
if (!line.startsWith('a=rtpmap:') && !line.startsWith('a=rtcp-fb:') &&
|
||||
!line.startsWith('a=fmtp:')) {
|
||||
return true;
|
||||
}
|
||||
// For audio codec lines, only keep the specified codec
|
||||
if (line.includes('audio/')) {
|
||||
return line.toUpperCase().includes(acodecUpper);
|
||||
}
|
||||
return true;
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
return sdp;
|
||||
},
|
||||
};
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/ontrack
|
||||
|
||||
@@ -125,9 +125,17 @@ $(function(){
|
||||
|
||||
// For example: webrtc://r.ossrs.net/live/livestream
|
||||
var url = $("#txt_url").val();
|
||||
var query = parse_query_string();
|
||||
|
||||
// Support codec parameters: codec (alias for vcodec), vcodec, acodec
|
||||
var vcodec = query.vcodec || query.codec;
|
||||
var acodec = query.acodec;
|
||||
|
||||
sdk.play(url, {
|
||||
videoOnly: $('#ch_videoonly').prop('checked'),
|
||||
audioOnly: $('#ch_audioonly').prop('checked'),
|
||||
vcodec: vcodec,
|
||||
acodec: acodec
|
||||
}).then(function(session){
|
||||
$('#sessionid').html(session.sessionid);
|
||||
$('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
|
||||
|
||||
@@ -132,10 +132,18 @@ $(function(){
|
||||
|
||||
// For example: webrtc://r.ossrs.net/live/livestream
|
||||
var url = $("#txt_url").val();
|
||||
var query = parse_query_string();
|
||||
|
||||
// Support codec parameters: codec (alias for vcodec), vcodec, acodec
|
||||
var vcodec = query.vcodec || query.codec;
|
||||
var acodec = query.acodec;
|
||||
|
||||
sdk.publish(url, {
|
||||
camera: $('#ra_camera').prop('checked'),
|
||||
screen: $('#ra_screen').prop('checked'),
|
||||
audio: $('#ch_audio').prop('checked')
|
||||
audio: $('#ch_audio').prop('checked'),
|
||||
vcodec: vcodec,
|
||||
acodec: acodec
|
||||
}).then(function(session){
|
||||
$('#sessionid').html(session.sessionid);
|
||||
$('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
|
||||
|
||||
Reference in New Issue
Block a user