Skip to content

Commit

Permalink
webrtc: add option to disable audio effects (#1908) (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Jun 27, 2023
1 parent 4aef466 commit 79ee4e0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/core/webrtc_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func (s *webRTCHTTPServer) onRequest(ctx *gin.Context) {
audioCodec: ctx.Query("audio_codec"),
videoBitrate: ctx.Query("video_bitrate"),
audioBitrate: ctx.Query("audio_bitrate"),
audioVoice: ctx.Query("audio_voice") == "true",
})
if res.err != nil {
if res.errStatusCode != 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/core/webrtc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type webRTCSessionNewReq struct {
audioCodec string
videoBitrate string
audioBitrate string
audioVoice bool
res chan webRTCSessionNewRes
}

Expand Down
14 changes: 14 additions & 0 deletions internal/core/webrtc_publish_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@

audio bitrate (kbps):
<input id="audio_bitrate" type="text" value="32" />

<div>
<input id="audio_voice" type="checkbox" checked>
<label for="audio_voice">optimize for voice</label>
</div>
</div>
<div id="submit_line">
<button id="publish_confirm">publish</button>
Expand Down Expand Up @@ -226,12 +231,14 @@
const audioCodec = document.getElementById('audio_codec').value;
const videoBitrate = document.getElementById('video_bitrate').value;
const audioBitrate = document.getElementById('audio_bitrate').value;
const audioVoice = document.getElementById('audio_voice').checked;

const p = new URLSearchParams(window.location.search);
p.set('video_codec', videoCodec);
p.set('audio_codec', audioCodec);
p.set('video_bitrate', videoBitrate);
p.set('audio_bitrate', audioBitrate);
p.set('audio_voice', audioVoice ? 'true' : 'false');

fetch(new URL('whip', window.location.href) + '?' + p.toString(), {
method: 'POST',
Expand Down Expand Up @@ -362,6 +369,13 @@
audio = {
deviceId: audioId,
};

const voice = document.getElementById('audio_voice').checked;
if (!voice) {
audio.autoGainControl = false;
audio.echoCancellation = false;
audio.noiseSuppression = false;
}
}

navigator.mediaDevices.getUserMedia({ video, audio })
Expand Down
25 changes: 19 additions & 6 deletions internal/core/webrtc_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ func findOpusPayloadFormat(attributes []sdp.Attribute) int {
return 0
}

func editAnswer(offer *webrtc.SessionDescription, videoBitrateStr string, audioBitrateStr string) error {
func editAnswer(
offer *webrtc.SessionDescription,
videoBitrateStr string,
audioBitrateStr string,
audioVoice bool,
) error {
var sd sdp.SessionDescription
err := sd.Unmarshal([]byte(offer.SDP))
if err != nil {
Expand Down Expand Up @@ -97,10 +102,18 @@ func editAnswer(offer *webrtc.SessionDescription, videoBitrateStr string, audioB
if pl != 0 {
for i, attr := range media.Attributes {
if attr.Key == "fmtp" && strings.HasPrefix(attr.Value, strconv.FormatInt(int64(pl), 10)+" ") {
media.Attributes[i] = sdp.Attribute{
Key: "fmtp",
Value: strconv.FormatInt(int64(pl), 10) + " stereo=1;sprop-stereo=1;maxaveragebitrate=" +
strconv.FormatUint(audioBitrate*1024, 10),
if audioVoice {
media.Attributes[i] = sdp.Attribute{
Key: "fmtp",
Value: strconv.FormatInt(int64(pl), 10) + " minptime=10;useinbandfec=1;maxaveragebitrate=" +
strconv.FormatUint(audioBitrate*1024, 10),
}
} else {
media.Attributes[i] = sdp.Attribute{
Key: "fmtp",
Value: strconv.FormatInt(int64(pl), 10) + " stereo=1;sprop-stereo=1;maxaveragebitrate=" +
strconv.FormatUint(audioBitrate*1024, 10),
}
}
}
}
Expand Down Expand Up @@ -368,7 +381,7 @@ func (s *webRTCSession) runPublish() (int, error) {
tmp := pc.LocalDescription()
answer = *tmp

err = editAnswer(&answer, s.req.videoBitrate, s.req.audioBitrate)
err = editAnswer(&answer, s.req.videoBitrate, s.req.audioBitrate, s.req.audioVoice)
if err != nil {
return http.StatusBadRequest, err
}
Expand Down

0 comments on commit 79ee4e0

Please sign in to comment.