Skip to content

Commit

Permalink
Detect audio decoding failure and fall back to ulaw.
Browse files Browse the repository at this point in the history
  • Loading branch information
bp2008 committed Sep 20, 2021
1 parent 977c5bf commit ffbf191
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions ui3/ui3.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ function GetSpeechVoiceOptions()
}
return opt;
}
function GetAudioCodecOptions()
{
return ["\u03BC-law", "FLAC"];
}
var HTML5DelayCompensationOptions = {
None: "None",
Weak: "Weak",
Expand Down Expand Up @@ -993,9 +997,9 @@ var defaultSettings =
}
, {
key: "ui3_audio_codec"
, value: "FLAC"
, value: flac_supported ? "FLAC" : GetAudioCodecOptions()[0]
, inputType: "select"
, options: ["\u03BC-law", "FLAC"]
, options: GetAudioCodecOptions()
, label: 'Audio Codec' + (!flac_supported ? '<div class="settingDesc">(FLAC unavailable in this browser)</div>' : '')
, onChange: OnChange_ui3_audio_codec
, category: "Video Player"
Expand Down Expand Up @@ -20239,14 +20243,24 @@ function PcmAudioPlayer()
// break;
// }
//};
var decoderState = { lastReceivedAudioIndex: -1, nextPlayAudioIndex: 0, buffers: [] };
var decoderState = { lastReceivedAudioIndex: -1, nextPlayAudioIndex: 0, buffers: [], startTime: -1 };
this.DecodeAndPlayAudioData = function (audioData, sampleRate, setAudioCodecString)
{
if (!supported)
return;
if (sampleRate !== context.sampleRate)
NewContext(sampleRate);

// detect decoder stall
if (decoderState.nextPlayAudioIndex <= 1 && decoderState.lastReceivedAudioIndex > 20 && decoderState.startTime > -1 && performance.now() - decoderState.startTime > 5000)
{
console.log("FLAC decoder stall detected.", decoderState);
DoAudioDecodingFallback();
return;
}

if (decoderState.startTime === -1)
decoderState.startTime = performance.now();
decoderState.lastReceivedAudioIndex++;
var myIndex = decoderState.lastReceivedAudioIndex;
context.decodeAudioData(audioData.buffer, function (audioBuffer)
Expand All @@ -20258,6 +20272,7 @@ function PcmAudioPlayer()
{
console.log("Audio decode FAIL", arguments);
setAudioCodecString("flac (cannot decode)");
DoAudioDecodingFallback();
});
}
var PlayDecodedAudio = function ()
Expand Down Expand Up @@ -20496,6 +20511,16 @@ function CameraAudioMuteToggle()
if (pcmPlayer)
pcmPlayer.SetAudioVolumeFromSettings();
}
function DoAudioDecodingFallback()
{
var fallbackCodec = GetAudioCodecOptions()[0];
if (settings.ui3_audio_codec !== fallbackCodec)
{
toaster.Warning('"' + settings.ui3_audio_codec + '" audio decoder failed. Changing audio codec setting to "' + fallbackCodec + '".', 10000);
settings.ui3_audio_codec = fallbackCodec;
OnChange_ui3_audio_codec();
}
}
///////////////////////////////////////////////////////////////
// Volume Icon Helper /////////////////////////////////////////
///////////////////////////////////////////////////////////////
Expand Down

0 comments on commit ffbf191

Please sign in to comment.