2013-12-11 09:03:30 +04:00
|
|
|
/* -*- 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/. */
|
2015-07-23 14:58:30 +03:00
|
|
|
|
2013-12-11 09:03:30 +04:00
|
|
|
#include "AudioStream.h"
|
2015-07-23 14:58:30 +03:00
|
|
|
#include "MediaQueue.h"
|
2015-08-18 06:55:01 +03:00
|
|
|
#include "DecodedAudioDataSink.h"
|
2015-07-23 14:58:30 +03:00
|
|
|
#include "VideoUtils.h"
|
|
|
|
|
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-07-28 06:52:05 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-12-11 09:03:30 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
extern PRLogModuleInfo* gMediaDecoderLog;
|
|
|
|
#define SINK_LOG(msg, ...) \
|
2015-08-18 06:55:01 +03:00
|
|
|
MOZ_LOG(gMediaDecoderLog, LogLevel::Debug, \
|
|
|
|
("DecodedAudioDataSink=%p " msg, this, ##__VA_ARGS__))
|
2015-05-14 05:15:05 +03:00
|
|
|
#define SINK_LOG_V(msg, ...) \
|
2015-08-18 06:55:01 +03:00
|
|
|
MOZ_LOG(gMediaDecoderLog, LogLevel::Verbose, \
|
|
|
|
("DecodedAudioDataSink=%p " msg, this, ##__VA_ARGS__))
|
|
|
|
|
|
|
|
namespace media {
|
2013-12-11 09:03:30 +04:00
|
|
|
|
2014-09-23 00:26:36 +04:00
|
|
|
// The amount of audio frames that is used to fuzz rounding errors.
|
|
|
|
static const int64_t AUDIO_FUZZ_FRAMES = 1;
|
|
|
|
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::DecodedAudioDataSink(MediaQueue<MediaData>& aAudioQueue,
|
|
|
|
int64_t aStartTime,
|
|
|
|
const AudioInfo& aInfo,
|
|
|
|
dom::AudioChannel aChannel)
|
|
|
|
: AudioSink(aAudioQueue)
|
|
|
|
, mMonitor("DecodedAudioDataSink::mMonitor")
|
2015-07-28 06:52:05 +03:00
|
|
|
, mState(AUDIOSINK_STATE_INIT)
|
|
|
|
, mAudioLoopScheduled(false)
|
2013-12-11 09:03:30 +04:00
|
|
|
, mStartTime(aStartTime)
|
|
|
|
, mWritten(0)
|
2014-10-30 23:10:00 +03:00
|
|
|
, mLastGoodPosition(0)
|
2013-12-11 09:03:30 +04:00
|
|
|
, mInfo(aInfo)
|
|
|
|
, mChannel(aChannel)
|
|
|
|
, mStopAudioThread(false)
|
|
|
|
, mPlaying(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-08-26 05:30:56 +03:00
|
|
|
DecodedAudioDataSink::~DecodedAudioDataSink()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-28 06:52:05 +03:00
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetState(State aState)
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
|
|
|
AssertOnAudioThread();
|
|
|
|
mPendingState = Some(aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::DispatchTask(already_AddRefed<nsIRunnable>&& event)
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-08-06 05:16:49 +03:00
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::OnAudioQueueEvent()
|
2015-08-06 05:16:49 +03:00
|
|
|
{
|
|
|
|
AssertOnAudioThread();
|
|
|
|
if (!mAudioLoopScheduled) {
|
|
|
|
AudioLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::ConnectListener()
|
2015-08-06 05:16:49 +03:00
|
|
|
{
|
|
|
|
AssertOnAudioThread();
|
|
|
|
mPushListener = AudioQueue().PushEvent().Connect(
|
2015-08-18 06:55:01 +03:00
|
|
|
mThread, this, &DecodedAudioDataSink::OnAudioQueueEvent);
|
2015-08-06 05:16:49 +03:00
|
|
|
mFinishListener = AudioQueue().FinishEvent().Connect(
|
2015-08-18 06:55:01 +03:00
|
|
|
mThread, this, &DecodedAudioDataSink::OnAudioQueueEvent);
|
2015-08-06 05:16:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::DisconnectListener()
|
2015-08-06 05:16:49 +03:00
|
|
|
{
|
|
|
|
AssertOnAudioThread();
|
|
|
|
mPushListener.Disconnect();
|
|
|
|
mFinishListener.Disconnect();
|
|
|
|
}
|
|
|
|
|
2015-07-28 06:52:05 +03:00
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::ScheduleNextLoop()
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
|
|
|
AssertOnAudioThread();
|
|
|
|
if (mAudioLoopScheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mAudioLoopScheduled = true;
|
2015-08-18 06:55:01 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethod(this, &DecodedAudioDataSink::AudioLoop);
|
2015-07-28 06:52:05 +03:00
|
|
|
DispatchTask(r.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::ScheduleNextLoopCrossThread()
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
|
|
|
AssertNotOnAudioThread();
|
2015-08-18 06:55:01 +03:00
|
|
|
nsRefPtr<DecodedAudioDataSink> self = this;
|
2015-07-28 06:52:05 +03:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2015-07-22 04:54:06 +03:00
|
|
|
nsRefPtr<GenericPromise>
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::Init()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-22 04:54:06 +03:00
|
|
|
nsRefPtr<GenericPromise> p = mEndPromise.Ensure(__func__);
|
2013-12-11 09:03:30 +04:00
|
|
|
nsresult rv = NS_NewNamedThread("Media Audio",
|
|
|
|
getter_AddRefs(mThread),
|
|
|
|
nullptr,
|
2015-08-03 21:39:45 +03:00
|
|
|
SharedThreadPool::kStackSize);
|
2013-12-11 09:03:30 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-07-22 04:54:06 +03:00
|
|
|
mEndPromise.Reject(rv, __func__);
|
|
|
|
return p;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
2014-09-29 09:31:00 +04:00
|
|
|
|
2015-07-28 06:52:05 +03:00
|
|
|
ScheduleNextLoopCrossThread();
|
2015-07-22 04:54:06 +03:00
|
|
|
return p;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::GetPosition()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-24 09:24:42 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2014-10-30 23:10:00 +03:00
|
|
|
|
|
|
|
int64_t pos;
|
|
|
|
if (mAudioStream &&
|
|
|
|
(pos = mAudioStream->GetPosition()) >= 0) {
|
|
|
|
// Update the last good position when we got a good one.
|
|
|
|
mLastGoodPosition = pos;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
2014-10-30 23:10:00 +03:00
|
|
|
|
2015-07-09 06:07:57 +03:00
|
|
|
return mStartTime + mLastGoodPosition;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2015-02-27 00:37:03 +03:00
|
|
|
bool
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::HasUnplayedFrames()
|
2015-02-27 00:37:03 +03:00
|
|
|
{
|
2015-07-24 09:24:42 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-02-27 00:37:03 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2013-12-11 09:03:30 +04:00
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::Shutdown()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->Cancel();
|
|
|
|
}
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
2015-08-18 06:55:01 +03:00
|
|
|
nsRefPtr<DecodedAudioDataSink> self = this;
|
2015-07-31 08:26:06 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
|
|
|
|
self->mStopAudioThread = true;
|
|
|
|
if (!self->mAudioLoopScheduled) {
|
|
|
|
self->AudioLoop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
DispatchTask(r.forget());
|
2013-12-11 09:03:30 +04:00
|
|
|
|
|
|
|
mThread->Shutdown();
|
|
|
|
mThread = nullptr;
|
2015-07-22 04:54:15 +03:00
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->Shutdown();
|
|
|
|
mAudioStream = nullptr;
|
|
|
|
}
|
2015-07-28 06:52:05 +03:00
|
|
|
|
|
|
|
// 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());
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetVolume(double aVolume)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertNotOnAudioThread();
|
2015-08-18 06:55:01 +03:00
|
|
|
nsRefPtr<DecodedAudioDataSink> self = this;
|
2015-07-31 08:26:06 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
|
|
|
|
if (self->mState == AUDIOSINK_STATE_PLAYING) {
|
|
|
|
self->mAudioStream->SetVolume(aVolume);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
DispatchTask(r.forget());
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetPlaybackRate(double aPlaybackRate)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertNotOnAudioThread();
|
|
|
|
MOZ_ASSERT(aPlaybackRate != 0, "Don't set the playbackRate to 0 on AudioStream");
|
2015-08-18 06:55:01 +03:00
|
|
|
nsRefPtr<DecodedAudioDataSink> self = this;
|
2015-07-31 08:26:06 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
|
|
|
|
if (self->mState == AUDIOSINK_STATE_PLAYING) {
|
|
|
|
self->mAudioStream->SetPlaybackRate(aPlaybackRate);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
DispatchTask(r.forget());
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetPreservesPitch(bool aPreservesPitch)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertNotOnAudioThread();
|
2015-08-18 06:55:01 +03:00
|
|
|
nsRefPtr<DecodedAudioDataSink> self = this;
|
2015-07-31 08:26:06 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
|
|
|
|
if (self->mState == AUDIOSINK_STATE_PLAYING) {
|
|
|
|
self->mAudioStream->SetPreservesPitch(aPreservesPitch);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
DispatchTask(r.forget());
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetPlaying(bool aPlaying)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertNotOnAudioThread();
|
2015-08-18 06:55:01 +03:00
|
|
|
nsRefPtr<DecodedAudioDataSink> self = this;
|
2015-07-31 08:26:06 +03:00
|
|
|
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());
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::InitializeAudioStream()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
|
|
|
// 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());
|
2014-09-29 09:31:00 +04:00
|
|
|
nsresult rv = audioStream->Init(mInfo.mChannels, mInfo.mRate,
|
|
|
|
mChannel, AudioStream::HighLatency);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
audioStream->Shutdown();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-12-11 09:03:30 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
mAudioStream = audioStream;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::Drain()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertOnAudioThread();
|
2013-12-11 09:03:30 +04:00
|
|
|
MOZ_ASSERT(mPlaying && !mAudioStream->IsPaused());
|
|
|
|
// If the media was too short to trigger the start of the audio stream,
|
|
|
|
// start it now.
|
|
|
|
mAudioStream->Start();
|
2015-07-31 08:26:06 +03:00
|
|
|
mAudioStream->Drain();
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::Cleanup()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertOnAudioThread();
|
2015-07-22 04:54:06 +03:00
|
|
|
mEndPromise.Resolve(true, __func__);
|
2015-07-22 04:54:15 +03:00
|
|
|
// Since the promise if resolved asynchronously, we don't shutdown
|
|
|
|
// AudioStream here so MDSM::ResyncAudioClock can get the correct
|
|
|
|
// audio position.
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::ExpectMoreAudioData()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
|
|
|
return AudioQueue().GetSize() == 0 && !AudioQueue().IsFinished();
|
|
|
|
}
|
|
|
|
|
2015-07-28 06:49:42 +03:00
|
|
|
bool
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::WaitingForAudioToPlay()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertOnAudioThread();
|
2015-07-28 06:49:42 +03:00
|
|
|
// Return true if we're not playing, and we're not shutting down, or we're
|
2013-12-11 09:03:30 +04:00
|
|
|
// playing and we've got no audio to play.
|
2015-07-28 06:49:42 +03:00
|
|
|
if (!mStopAudioThread && (!mPlaying || ExpectMoreAudioData())) {
|
|
|
|
return true;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
2015-07-28 06:49:42 +03:00
|
|
|
return false;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::IsPlaybackContinuing()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertOnAudioThread();
|
2013-12-11 09:03:30 +04:00
|
|
|
// If we're shutting down, captured, or at EOS, break out and exit the audio
|
|
|
|
// thread.
|
|
|
|
if (mStopAudioThread || AudioQueue().AtEndOfStream()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-28 06:52:05 +03:00
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::AudioLoop()
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
|
|
|
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);
|
2015-08-06 05:16:49 +03:00
|
|
|
ConnectListener();
|
2015-07-28 06:52:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case AUDIOSINK_STATE_PLAYING: {
|
2015-07-31 08:26:06 +03:00
|
|
|
if (WaitingForAudioToPlay()) {
|
2015-08-06 05:16:49 +03:00
|
|
|
// OnAudioQueueEvent() will schedule next loop.
|
2015-07-31 08:26:06 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!IsPlaybackContinuing()) {
|
|
|
|
SetState(AUDIOSINK_STATE_COMPLETE);
|
|
|
|
break;
|
2015-07-28 06:52:05 +03:00
|
|
|
}
|
|
|
|
if (!PlayAudio()) {
|
|
|
|
SetState(AUDIOSINK_STATE_COMPLETE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Schedule next loop to play next sample.
|
|
|
|
ScheduleNextLoop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case AUDIOSINK_STATE_COMPLETE: {
|
2015-08-06 05:16:49 +03:00
|
|
|
DisconnectListener();
|
2015-07-28 06:52:05 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 06:49:59 +03:00
|
|
|
bool
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::PlayAudio()
|
2015-07-28 06:49:59 +03:00
|
|
|
{
|
|
|
|
// 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
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::FinishAudioLoop()
|
2015-07-28 06:49:59 +03:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
AssertOnAudioThread();
|
2015-07-28 06:49:59 +03:00
|
|
|
MOZ_ASSERT(mStopAudioThread || AudioQueue().AtEndOfStream());
|
|
|
|
if (!mStopAudioThread && mPlaying) {
|
|
|
|
Drain();
|
|
|
|
}
|
|
|
|
SINK_LOG("AudioLoop complete");
|
|
|
|
Cleanup();
|
|
|
|
SINK_LOG("AudioLoop exit");
|
|
|
|
}
|
|
|
|
|
2013-12-11 09:03:30 +04:00
|
|
|
uint32_t
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::PlaySilence(uint32_t aFrames)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
|
|
|
// 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
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::PlayFromAudioQueue()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
|
|
|
AssertOnAudioThread();
|
|
|
|
NS_ASSERTION(!mAudioStream->IsPaused(), "Don't play when paused");
|
2015-07-27 19:21:33 +03:00
|
|
|
nsRefPtr<AudioData> audio =
|
|
|
|
dont_AddRef(AudioQueue().PopFront().take()->As<AudioData>());
|
2014-09-15 01:03:00 +04:00
|
|
|
|
2013-12-11 09:03:30 +04:00
|
|
|
SINK_LOG_V("playing %u frames of audio at time %lld",
|
2014-08-19 17:12:00 +04:00
|
|
|
audio->mFrames, audio->mTime);
|
2015-07-14 16:21:27 +03:00
|
|
|
if (audio->mRate == mInfo.mRate && audio->mChannels == mInfo.mChannels) {
|
|
|
|
mAudioStream->Write(audio->mAudioData, 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);
|
|
|
|
}
|
2013-12-11 09:03:30 +04:00
|
|
|
|
|
|
|
StartAudioStreamPlaybackIfNeeded();
|
|
|
|
|
|
|
|
return audio->mFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::StartAudioStreamPlaybackIfNeeded()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
|
|
|
// 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
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::WriteSilence(uint32_t aFrames)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
|
|
|
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
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::GetEndTime() const
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
|
|
|
CheckedInt64 playedUsecs = FramesToUsecs(mWritten, mInfo.mRate) + mStartTime;
|
|
|
|
if (!playedUsecs.isValid()) {
|
|
|
|
NS_WARNING("Int overflow calculating audio end time");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return playedUsecs.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::AssertOnAudioThread()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-23 14:58:30 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mThread);
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2015-07-28 06:52:05 +03:00
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::AssertNotOnAudioThread()
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() != mThread);
|
|
|
|
}
|
|
|
|
|
2015-08-18 06:55:01 +03:00
|
|
|
} // namespace media
|
2013-12-11 09:03:30 +04:00
|
|
|
} // namespace mozilla
|