Files
palemoon27/dom/media/mediasink/DecodedAudioDataSink.cpp
T
roytam1 147c5fa2eb import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1150437 - Make gmp-clearkey consistently Apache2 licensed. r=edwin,jwwang,kentuckyfriedtakahe (3c954c73dd)
- Bug 1223486 - Ensure WrapTexCoord returns 0.0 instead of 1.0 when given a negative whole number. r=Bas (b4af8eb524)
- Bug 1160914 - Make gmp-clearkey's decoders threadsafe refcounted, to handle DecodingComplete while GMPVideoHost::CreateFrame() is waiting. r=edwin (03359c7730)
- Bug 1206763 - Enable SkiaGL canvas on gonk r=mattwoodrow (1588079b23)
- Decouple CairoImage from ImageContainer. (bug 1222910, r=roc) (a93a4dad82)
- Decouple D3D11ShareHandleImage from ImageContainer. (bug 1222910, r=mattwoodrow) (9642ef1c53)
- Bug 1194753 - Wait longer when decoding the first frame of D3D9 DXVA video before deciding it is invalid. r=cpearce (865fe1d2f3)
- Decouple D3D9SurfaceImage from ImageContainer. (bug 1222910, r=mattwoodrow) (55bc5abffc)
- Bug 1191534 - Implement BlitImageToFramebuffer for IOSurface. r=jgilbert (0da6aff77f)
- Decouple EGLImageImage from ImageContainer. (bug 1222910, r=snorp) (cffb7aadcb)
- Decouple SurfaceTextureImage from ImageContainer. (bug 1222910, r=snorp) (5b920014d0)
- Decouple MacIOSurfaceImage from ImageContainer. (bug 1222910, r=mstange) (ffab69155c)
- Decouple SharedRGBImage and PlanarYCbCrImage from ImageContainer. (bug 1222910, r=mattwoodrow) (a9f6e23788)
- Remove Gonk usage of ImageContainer::CreateImage. (bug 1222910, r=sotaro) (3f460b901d)
- Remove CreateImage() from ImageContainer, ImageFactory, and ImageClient. (bug 1222910, r=roc) (873d5d5e47)
- Bug 1212795: P1. Ensure the check if HW acceleration is allowed is performed on the main thread. r=jwwang (7cac257e5a)
- Bug 1137529: Prefer Apple's VDA hardware acceleration for Hi-Def videos. r=rillian (a3ffc48e5e)
- Bug 1212795: P2. Ensure all frameworks required for video decoding on mac are loaded. r=jwwang (b903b94c60)
- Bug 1212795: P3. Make AppleDecoderModule detects if the required modules are loaded. r=jwwang (fb132e36b0)
- Bug 1221991 - [1.3] Make SupportsMimeType a const function. r=jya (2450030808)
- Bug 1221991 - [2.3] Split DecoderLoop into functions, simplify decoding state and clean up style. r=snorp (7c0e665cf7)
- Bug 1221991 - [3.1] Manage module state via accessor functions. r=snorp (491b00f74b)
- Bug 1221991 - [4.2] Add AndroidDecoderModule logging. r=snorp (7213505016)
- Bug 1153110: Don't prefer VDA for 720p and above. r=rillian (50d5fbfa7c)
- remove PR_LOGGING (44f4efbaa2)
- Bug 1169653 - Limit use of the speech recognition API in JS to certified apps or apps with the proper flags set. r=smaug (c93a8949b8)
- Bug 1133633: Part2. Enable async decoding on mac. r=mattmoodrow (c9740168bd)
- Bug 1154896: Report decoding errors back to MP4Reader. r=rillian (26291f72e1)
- Bug 1154896: Part2. Ignore kVTDecodeInfo_FrameDropped flag. r=rillian (bfca4c264e)
- Bug 1198094: P1. Limit rate at which InputExhausted could be called by mac decoder. r=rillian (e5819c1666)
- Bug 1214678 - Allow Apple media decoders to work on iOS r=jya (c475eff0be)
- Bug 1219140 - Remove AskMediaCodecAndWait() r=bwu (dbe8d364ec)
- fix includes (432c3419b0)
- adapted Bug 1159509 - Support audio AMR-NB for Gonk in MP4Reader. r=jya (adaddc5c21)
- Bug 1154512 - Remove MediaTaskQueue::SyncDispatch() from PDM. r=cpearce (5bddd082bd)
- Bug 1204622 - release codec listener at reader task queue. r=jya (72b062b5d7)
- Bug 1097498 - Wait fence for the graphic buffer. r=sotaro (c72f301939)
- Bug 1198664 - Refactor: move common behaviors to base class. r=bwu,jya (a754813c5b)
- Bug 1198664 - Use looper to process decoder tasks. r=bwu (768315de13)
- Bug 1174721 - Use AudioCompactor for GonkAudioDecoderManager. r=sotaro (768e68c946)
- Bug 1199809 - Refactor: use RAII to help manage output buffer lifecycle. r=sotaro (9a6e0dceb7)
- Bug 1133955 - Record the last seek time to decide the direction to seek. r=sotaro (d0bcdbe68b)
- Bug 1210045 - Fix GonkVideoDecoderManager shutdown during initialization r=bwu (b379248ff1)
- Bug 1214997 - Use MozPromise in MediaCodecProxy and OMXCodecProxy r=bwu (ab9a92d640)
- Bug 1199809 - Make a copy of output buffer after flush(). r=sotaro (17465ecaea)
- Bug 1216895 - assert that decoder methods are run on correct thread. r=jya (b8c106c2ca)
- Bug 1185018 - Part 1 of 1 - Made speech recognition services language dependent and removed assumption of a single service. r=smaug (f83acf82b4)
- Bug 1207416: Add Telemetry for time spent in different Load states in WebRTC r=gcp (e3959b6760)
- Bug 1219480 - Replace PRLogModuleInfo with LazyLogModule in the media directory. r=rillian (a9b13ef334)
- Bug 1196558: Don't assert should output task not be dispatched. r=edwin (a2569bca73)
- Bug 1199193: Ensure DrainComplete() is called once all decoded frames have been output. r=rillian (760fb42753)
- Bug 1079621 - Change non-fatal errors to warnings. r=rillian (20c85e7e56)
- Bug 1185234 - Implement SpeechRecognition::interimResults. r=smaug (b3d635d8c0)
- Bug 1171850 - Remove 'models' dir from MODELSPS_DEST as the dir 'models' now comes from MODELSPS_FILES. r=gps (5515511e3a)
- Bug 1183503 - Rename model and dictionary files for recognition in preparation for localization. r=smaug (32c78610af)
- Bug 1185235 - Implement SpeechRecognition::maxAlternatives. r=smaug (91eac7f30f)
- Bug 1177514 - Remove final text of 'ERROR' on recognition error, should be signaled by SpeechRecognitionError. r=smaug (c8d7b198e0)
- Bug 1187791 - Part 1 of 1 - SpeechRecognition::maxAlternatives can't throw; so, rm webidl throws specifier. r=smaug (e295b2bde5)
- Bug 1213131: [vpx] Configure libvpx decoder to use multi-threads decoding. r=kentuckyfriedtakahe (f38ba0ef82)
- Bug 1206845: Prevent overflows in MediaCache. r=roc (e8a228dcd3)
- Bug 1205825 - part 1 - don't reacquire the media cache's monitor in MediaCacheStream::FlushPartialBlockInternal; r=roc (6342592be3)
- Bug 1205825 - part 2 - call Get*CachedData*Internal variants from MediaCacheStream::GetCachedRanges; r=roc (f229c8f757)
2023-01-10 13:45:00 +08:00

534 lines
14 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AudioStream.h"
#include "MediaQueue.h"
#include "DecodedAudioDataSink.h"
#include "VideoUtils.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/DebugOnly.h"
namespace mozilla {
extern LazyLogModule gMediaDecoderLog;
#define SINK_LOG(msg, ...) \
MOZ_LOG(gMediaDecoderLog, LogLevel::Debug, \
("DecodedAudioDataSink=%p " msg, this, ##__VA_ARGS__))
#define SINK_LOG_V(msg, ...) \
MOZ_LOG(gMediaDecoderLog, LogLevel::Verbose, \
("DecodedAudioDataSink=%p " msg, this, ##__VA_ARGS__))
namespace media {
// The amount of audio frames that is used to fuzz rounding errors.
static const int64_t AUDIO_FUZZ_FRAMES = 1;
DecodedAudioDataSink::DecodedAudioDataSink(MediaQueue<MediaData>& aAudioQueue,
int64_t aStartTime,
const AudioInfo& aInfo,
dom::AudioChannel aChannel)
: AudioSink(aAudioQueue)
, mMonitor("DecodedAudioDataSink::mMonitor")
, mState(AUDIOSINK_STATE_INIT)
, mAudioLoopScheduled(false)
, mStartTime(aStartTime)
, mWritten(0)
, mLastGoodPosition(0)
, mInfo(aInfo)
, mChannel(aChannel)
, mStopAudioThread(false)
, mPlaying(true)
{
}
DecodedAudioDataSink::~DecodedAudioDataSink()
{
}
void
DecodedAudioDataSink::SetState(State aState)
{
AssertOnAudioThread();
mPendingState = Some(aState);
}
void
DecodedAudioDataSink::DispatchTask(already_AddRefed<nsIRunnable>&& event)
{
DebugOnly<nsresult> rv = mThread->Dispatch(Move(event), NS_DISPATCH_NORMAL);
// There isn't much we can do if Dispatch() fails.
// Just assert it to keep things simple.
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
void
DecodedAudioDataSink::OnAudioQueueEvent()
{
AssertOnAudioThread();
if (!mAudioLoopScheduled) {
AudioLoop();
}
}
void
DecodedAudioDataSink::ConnectListener()
{
AssertOnAudioThread();
mPushListener = AudioQueue().PushEvent().Connect(
mThread, this, &DecodedAudioDataSink::OnAudioQueueEvent);
mFinishListener = AudioQueue().FinishEvent().Connect(
mThread, this, &DecodedAudioDataSink::OnAudioQueueEvent);
}
void
DecodedAudioDataSink::DisconnectListener()
{
AssertOnAudioThread();
mPushListener.Disconnect();
mFinishListener.Disconnect();
}
void
DecodedAudioDataSink::ScheduleNextLoop()
{
AssertOnAudioThread();
if (mAudioLoopScheduled) {
return;
}
mAudioLoopScheduled = true;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethod(this, &DecodedAudioDataSink::AudioLoop);
DispatchTask(r.forget());
}
void
DecodedAudioDataSink::ScheduleNextLoopCrossThread()
{
AssertNotOnAudioThread();
RefPtr<DecodedAudioDataSink> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([self] () {
// Do nothing if there is already a pending task waiting for its turn.
if (!self->mAudioLoopScheduled) {
self->AudioLoop();
}
});
DispatchTask(r.forget());
}
RefPtr<GenericPromise>
DecodedAudioDataSink::Init()
{
RefPtr<GenericPromise> p = mEndPromise.Ensure(__func__);
nsresult rv = NS_NewNamedThread("Media Audio",
getter_AddRefs(mThread),
nullptr,
SharedThreadPool::kStackSize);
if (NS_FAILED(rv)) {
mEndPromise.Reject(rv, __func__);
return p;
}
ScheduleNextLoopCrossThread();
return p;
}
int64_t
DecodedAudioDataSink::GetPosition()
{
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
int64_t pos;
if (mAudioStream &&
(pos = mAudioStream->GetPosition()) >= 0) {
// Update the last good position when we got a good one.
mLastGoodPosition = pos;
}
return mStartTime + mLastGoodPosition;
}
bool
DecodedAudioDataSink::HasUnplayedFrames()
{
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
// Experimentation suggests that GetPositionInFrames() is zero-indexed,
// so we need to add 1 here before comparing it to mWritten.
return mAudioStream && mAudioStream->GetPositionInFrames() + 1 < mWritten;
}
void
DecodedAudioDataSink::Shutdown()
{
{
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
if (mAudioStream) {
mAudioStream->Cancel();
}
}
RefPtr<DecodedAudioDataSink> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
self->mStopAudioThread = true;
if (!self->mAudioLoopScheduled) {
self->AudioLoop();
}
});
DispatchTask(r.forget());
mThread->Shutdown();
mThread = nullptr;
if (mAudioStream) {
mAudioStream->Shutdown();
mAudioStream = nullptr;
}
// Should've reached the final state after shutdown.
MOZ_ASSERT(mState == AUDIOSINK_STATE_SHUTDOWN ||
mState == AUDIOSINK_STATE_ERROR);
// Should have no pending state change.
MOZ_ASSERT(mPendingState.isNothing());
}
void
DecodedAudioDataSink::SetVolume(double aVolume)
{
AssertNotOnAudioThread();
RefPtr<DecodedAudioDataSink> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
if (self->mState == AUDIOSINK_STATE_PLAYING) {
self->mAudioStream->SetVolume(aVolume);
}
});
DispatchTask(r.forget());
}
void
DecodedAudioDataSink::SetPlaybackRate(double aPlaybackRate)
{
AssertNotOnAudioThread();
MOZ_ASSERT(aPlaybackRate != 0, "Don't set the playbackRate to 0 on AudioStream");
RefPtr<DecodedAudioDataSink> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
if (self->mState == AUDIOSINK_STATE_PLAYING) {
self->mAudioStream->SetPlaybackRate(aPlaybackRate);
}
});
DispatchTask(r.forget());
}
void
DecodedAudioDataSink::SetPreservesPitch(bool aPreservesPitch)
{
AssertNotOnAudioThread();
RefPtr<DecodedAudioDataSink> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
if (self->mState == AUDIOSINK_STATE_PLAYING) {
self->mAudioStream->SetPreservesPitch(aPreservesPitch);
}
});
DispatchTask(r.forget());
}
void
DecodedAudioDataSink::SetPlaying(bool aPlaying)
{
AssertNotOnAudioThread();
RefPtr<DecodedAudioDataSink> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
if (self->mState != AUDIOSINK_STATE_PLAYING ||
self->mPlaying == aPlaying) {
return;
}
self->mPlaying = aPlaying;
// pause/resume AudioStream as necessary.
if (!aPlaying && !self->mAudioStream->IsPaused()) {
self->mAudioStream->Pause();
} else if (aPlaying && self->mAudioStream->IsPaused()) {
self->mAudioStream->Resume();
}
// Wake up the audio loop to play next sample.
if (aPlaying && !self->mAudioLoopScheduled) {
self->AudioLoop();
}
});
DispatchTask(r.forget());
}
nsresult
DecodedAudioDataSink::InitializeAudioStream()
{
// AudioStream initialization can block for extended periods in unusual
// circumstances, so we take care to drop the decoder monitor while
// initializing.
RefPtr<AudioStream> audioStream(new AudioStream());
nsresult rv = audioStream->Init(mInfo.mChannels, mInfo.mRate, mChannel);
if (NS_FAILED(rv)) {
audioStream->Shutdown();
return rv;
}
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
mAudioStream = audioStream;
return NS_OK;
}
void
DecodedAudioDataSink::Drain()
{
AssertOnAudioThread();
MOZ_ASSERT(mPlaying && !mAudioStream->IsPaused());
// If the media was too short to trigger the start of the audio stream,
// start it now.
mAudioStream->Start();
mAudioStream->Drain();
}
void
DecodedAudioDataSink::Cleanup()
{
AssertOnAudioThread();
mEndPromise.Resolve(true, __func__);
// Since the promise if resolved asynchronously, we don't shutdown
// AudioStream here so MDSM::ResyncAudioClock can get the correct
// audio position.
}
bool
DecodedAudioDataSink::ExpectMoreAudioData()
{
return AudioQueue().GetSize() == 0 && !AudioQueue().IsFinished();
}
bool
DecodedAudioDataSink::WaitingForAudioToPlay()
{
AssertOnAudioThread();
// Return true if we're not playing, and we're not shutting down, or we're
// playing and we've got no audio to play.
if (!mStopAudioThread && (!mPlaying || ExpectMoreAudioData())) {
return true;
}
return false;
}
bool
DecodedAudioDataSink::IsPlaybackContinuing()
{
AssertOnAudioThread();
// If we're shutting down, captured, or at EOS, break out and exit the audio
// thread.
if (mStopAudioThread || AudioQueue().AtEndOfStream()) {
return false;
}
return true;
}
void
DecodedAudioDataSink::AudioLoop()
{
AssertOnAudioThread();
mAudioLoopScheduled = false;
switch (mState) {
case AUDIOSINK_STATE_INIT: {
SINK_LOG("AudioLoop started");
nsresult rv = InitializeAudioStream();
if (NS_FAILED(rv)) {
NS_WARNING("Initializing AudioStream failed.");
mEndPromise.Reject(rv, __func__);
SetState(AUDIOSINK_STATE_ERROR);
break;
}
SetState(AUDIOSINK_STATE_PLAYING);
ConnectListener();
break;
}
case AUDIOSINK_STATE_PLAYING: {
if (WaitingForAudioToPlay()) {
// OnAudioQueueEvent() will schedule next loop.
break;
}
if (!IsPlaybackContinuing()) {
SetState(AUDIOSINK_STATE_COMPLETE);
break;
}
if (!PlayAudio()) {
SetState(AUDIOSINK_STATE_COMPLETE);
break;
}
// Schedule next loop to play next sample.
ScheduleNextLoop();
break;
}
case AUDIOSINK_STATE_COMPLETE: {
DisconnectListener();
FinishAudioLoop();
SetState(AUDIOSINK_STATE_SHUTDOWN);
break;
}
case AUDIOSINK_STATE_SHUTDOWN:
break;
case AUDIOSINK_STATE_ERROR:
break;
} // end of switch
// We want mState to stay stable during AudioLoop to keep things simple.
// Therefore, we only do state transition at the end of AudioLoop.
if (mPendingState.isSome()) {
MOZ_ASSERT(mState != mPendingState.ref());
SINK_LOG("change mState, %d -> %d", mState, mPendingState.ref());
mState = mPendingState.ref();
mPendingState.reset();
// Schedule next loop when state changes.
ScheduleNextLoop();
}
}
bool
DecodedAudioDataSink::PlayAudio()
{
// See if there's a gap in the audio. If there is, push silence into the
// audio hardware, so we can play across the gap.
// Calculate the timestamp of the next chunk of audio in numbers of
// samples.
NS_ASSERTION(AudioQueue().GetSize() > 0, "Should have data to play");
CheckedInt64 sampleTime = UsecsToFrames(AudioQueue().PeekFront()->mTime, mInfo.mRate);
// Calculate the number of frames that have been pushed onto the audio hardware.
CheckedInt64 playedFrames = UsecsToFrames(mStartTime, mInfo.mRate) +
static_cast<int64_t>(mWritten);
CheckedInt64 missingFrames = sampleTime - playedFrames;
if (!missingFrames.isValid() || !sampleTime.isValid()) {
NS_WARNING("Int overflow adding in AudioLoop");
return false;
}
if (missingFrames.value() > AUDIO_FUZZ_FRAMES) {
// The next audio chunk begins some time after the end of the last chunk
// we pushed to the audio hardware. We must push silence into the audio
// hardware so that the next audio chunk begins playback at the correct
// time.
missingFrames = std::min<int64_t>(UINT32_MAX, missingFrames.value());
mWritten += PlaySilence(static_cast<uint32_t>(missingFrames.value()));
} else {
mWritten += PlayFromAudioQueue();
}
return true;
}
void
DecodedAudioDataSink::FinishAudioLoop()
{
AssertOnAudioThread();
MOZ_ASSERT(mStopAudioThread || AudioQueue().AtEndOfStream());
if (!mStopAudioThread && mPlaying) {
Drain();
}
SINK_LOG("AudioLoop complete");
Cleanup();
SINK_LOG("AudioLoop exit");
}
uint32_t
DecodedAudioDataSink::PlaySilence(uint32_t aFrames)
{
// Maximum number of bytes we'll allocate and write at once to the audio
// hardware when the audio stream contains missing frames and we're
// writing silence in order to fill the gap. We limit our silence-writes
// to 32KB in order to avoid allocating an impossibly large chunk of
// memory if we encounter a large chunk of silence.
const uint32_t SILENCE_BYTES_CHUNK = 32 * 1024;
AssertOnAudioThread();
NS_ASSERTION(!mAudioStream->IsPaused(), "Don't play when paused");
uint32_t maxFrames = SILENCE_BYTES_CHUNK / mInfo.mChannels / sizeof(AudioDataValue);
uint32_t frames = std::min(aFrames, maxFrames);
SINK_LOG_V("playing %u frames of silence", aFrames);
WriteSilence(frames);
return frames;
}
uint32_t
DecodedAudioDataSink::PlayFromAudioQueue()
{
AssertOnAudioThread();
NS_ASSERTION(!mAudioStream->IsPaused(), "Don't play when paused");
RefPtr<AudioData> audio =
dont_AddRef(AudioQueue().PopFront().take()->As<AudioData>());
SINK_LOG_V("playing %u frames of audio at time %lld",
audio->mFrames, audio->mTime);
if (audio->mRate == mInfo.mRate && audio->mChannels == mInfo.mChannels) {
mAudioStream->Write(audio->mAudioData.get(), audio->mFrames);
} else {
SINK_LOG_V("mismatched sample format mInfo=[%uHz/%u channels] audio=[%uHz/%u channels]",
mInfo.mRate, mInfo.mChannels, audio->mRate, audio->mChannels);
PlaySilence(audio->mFrames);
}
StartAudioStreamPlaybackIfNeeded();
return audio->mFrames;
}
void
DecodedAudioDataSink::StartAudioStreamPlaybackIfNeeded()
{
// This value has been chosen empirically.
const uint32_t MIN_WRITE_BEFORE_START_USECS = 200000;
// We want to have enough data in the buffer to start the stream.
if (static_cast<double>(mAudioStream->GetWritten()) / mAudioStream->GetRate() >=
static_cast<double>(MIN_WRITE_BEFORE_START_USECS) / USECS_PER_S) {
mAudioStream->Start();
}
}
void
DecodedAudioDataSink::WriteSilence(uint32_t aFrames)
{
uint32_t numSamples = aFrames * mInfo.mChannels;
nsAutoTArray<AudioDataValue, 1000> buf;
buf.SetLength(numSamples);
memset(buf.Elements(), 0, numSamples * sizeof(AudioDataValue));
mAudioStream->Write(buf.Elements(), aFrames);
StartAudioStreamPlaybackIfNeeded();
}
int64_t
DecodedAudioDataSink::GetEndTime() const
{
CheckedInt64 playedUsecs = FramesToUsecs(mWritten, mInfo.mRate) + mStartTime;
if (!playedUsecs.isValid()) {
NS_WARNING("Int overflow calculating audio end time");
return -1;
}
return playedUsecs.value();
}
void
DecodedAudioDataSink::AssertOnAudioThread()
{
MOZ_ASSERT(NS_GetCurrentThread() == mThread);
}
void
DecodedAudioDataSink::AssertNotOnAudioThread()
{
MOZ_ASSERT(NS_GetCurrentThread() != mThread);
}
} // namespace media
} // namespace mozilla