2015-09-02 09:11:28 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "AudioSink.h"
|
|
|
|
#include "AudioSinkWrapper.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
AudioSinkWrapper::~AudioSinkWrapper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::Shutdown()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(!mIsStarted, "Must be called after playback stopped.");
|
|
|
|
mCreator = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MediaSink::PlaybackParams&
|
|
|
|
AudioSinkWrapper::GetPlaybackParams() const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
return mParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::SetPlaybackParams(const PlaybackParams& aParams)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
if (mAudioSink) {
|
2015-09-08 05:58:11 +03:00
|
|
|
mAudioSink->SetVolume(aParams.mVolume);
|
|
|
|
mAudioSink->SetPlaybackRate(aParams.mPlaybackRate);
|
|
|
|
mAudioSink->SetPreservesPitch(aParams.mPreservesPitch);
|
2015-09-02 09:11:28 +03:00
|
|
|
}
|
|
|
|
mParams = aParams;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GenericPromise>
|
2015-09-02 09:11:28 +03:00
|
|
|
AudioSinkWrapper::OnEnded(TrackType aType)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(mIsStarted, "Must be called after playback starts.");
|
|
|
|
if (aType == TrackInfo::kAudioTrack) {
|
|
|
|
return mEndPromise;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
AudioSinkWrapper::GetEndTime(TrackType aType) const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(mIsStarted, "Must be called after playback starts.");
|
|
|
|
if (aType == TrackInfo::kAudioTrack && mAudioSink) {
|
|
|
|
return mAudioSink->GetEndTime();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
2015-09-07 06:56:52 +03:00
|
|
|
AudioSinkWrapper::GetVideoPosition(TimeStamp aNow) const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(!mPlayStartTime.IsNull());
|
|
|
|
// Time elapsed since we started playing.
|
|
|
|
int64_t delta = (aNow - mPlayStartTime).ToMicroseconds();
|
|
|
|
// Take playback rate into account.
|
2015-09-08 05:58:11 +03:00
|
|
|
return mPlayDuration + delta * mParams.mPlaybackRate;
|
2015-09-07 06:56:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
AudioSinkWrapper::GetPosition(TimeStamp* aTimeStamp) const
|
2015-09-02 09:11:28 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(mIsStarted, "Must be called after playback starts.");
|
2015-09-07 06:56:52 +03:00
|
|
|
|
|
|
|
int64_t pos = -1;
|
|
|
|
TimeStamp t = TimeStamp::Now();
|
|
|
|
|
2015-09-07 06:58:11 +03:00
|
|
|
if (!mAudioEnded) {
|
|
|
|
// Rely on the audio sink to report playback position when it is not ended.
|
2015-09-07 06:56:52 +03:00
|
|
|
pos = mAudioSink->GetPosition();
|
|
|
|
} else if (!mPlayStartTime.IsNull()) {
|
|
|
|
// Calculate playback position using system clock if we are still playing.
|
|
|
|
pos = GetVideoPosition(t);
|
|
|
|
} else {
|
|
|
|
// Return how long we've played if we are not playing.
|
|
|
|
pos = mPlayDuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTimeStamp) {
|
|
|
|
*aTimeStamp = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
2015-09-02 09:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioSinkWrapper::HasUnplayedFrames(TrackType aType) const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
return mAudioSink ? mAudioSink->HasUnplayedFrames() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::SetVolume(double aVolume)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2015-09-08 05:58:11 +03:00
|
|
|
mParams.mVolume = aVolume;
|
2015-09-02 09:11:28 +03:00
|
|
|
if (mAudioSink) {
|
|
|
|
mAudioSink->SetVolume(aVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::SetPlaybackRate(double aPlaybackRate)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2015-09-08 05:58:11 +03:00
|
|
|
mParams.mPlaybackRate = aPlaybackRate;
|
2015-09-07 06:58:11 +03:00
|
|
|
if (!mAudioEnded) {
|
2015-09-07 06:56:52 +03:00
|
|
|
// Pass the playback rate to the audio sink. The underlying AudioStream
|
|
|
|
// will handle playback rate changes and report correct audio position.
|
2015-09-02 09:11:28 +03:00
|
|
|
mAudioSink->SetPlaybackRate(aPlaybackRate);
|
2015-09-07 06:56:52 +03:00
|
|
|
} else if (!mPlayStartTime.IsNull()) {
|
|
|
|
// Adjust playback duration and start time when we are still playing.
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
mPlayDuration = GetVideoPosition(now);
|
|
|
|
mPlayStartTime = now;
|
2015-09-02 09:11:28 +03:00
|
|
|
}
|
2015-09-07 06:56:52 +03:00
|
|
|
// Do nothing when not playing. Changes in playback rate will be taken into
|
|
|
|
// account by GetVideoPosition().
|
2015-09-02 09:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::SetPreservesPitch(bool aPreservesPitch)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2015-09-08 05:58:11 +03:00
|
|
|
mParams.mPreservesPitch = aPreservesPitch;
|
2015-09-02 09:11:28 +03:00
|
|
|
if (mAudioSink) {
|
|
|
|
mAudioSink->SetPreservesPitch(aPreservesPitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::SetPlaying(bool aPlaying)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
|
|
|
// Resume/pause matters only when playback started.
|
|
|
|
if (!mIsStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAudioSink) {
|
|
|
|
mAudioSink->SetPlaying(aPlaying);
|
|
|
|
}
|
2015-09-07 06:56:52 +03:00
|
|
|
|
|
|
|
if (aPlaying) {
|
|
|
|
MOZ_ASSERT(mPlayStartTime.IsNull());
|
|
|
|
mPlayStartTime = TimeStamp::Now();
|
|
|
|
} else {
|
|
|
|
// Remember how long we've played.
|
|
|
|
mPlayDuration = GetPosition();
|
|
|
|
// mPlayStartTime must be updated later since GetPosition()
|
|
|
|
// depends on the value of mPlayStartTime.
|
|
|
|
mPlayStartTime = TimeStamp();
|
|
|
|
}
|
2015-09-02 09:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::Start(int64_t aStartTime, const MediaInfo& aInfo)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(!mIsStarted, "playback already started.");
|
|
|
|
|
|
|
|
mIsStarted = true;
|
2015-09-07 06:56:52 +03:00
|
|
|
mPlayDuration = aStartTime;
|
|
|
|
mPlayStartTime = TimeStamp::Now();
|
2015-09-02 09:11:28 +03:00
|
|
|
|
2015-09-07 06:58:11 +03:00
|
|
|
// no audio is equivalent to audio ended before video starts.
|
|
|
|
mAudioEnded = !aInfo.HasAudio();
|
|
|
|
|
2015-09-07 06:56:52 +03:00
|
|
|
if (aInfo.HasAudio()) {
|
|
|
|
mAudioSink = mCreator->Create();
|
|
|
|
mEndPromise = mAudioSink->Init();
|
|
|
|
SetPlaybackParams(mParams);
|
2015-09-07 06:58:11 +03:00
|
|
|
|
|
|
|
mAudioSinkPromise.Begin(mEndPromise->Then(
|
|
|
|
mOwnerThread.get(), __func__, this,
|
|
|
|
&AudioSinkWrapper::OnAudioEnded,
|
|
|
|
&AudioSinkWrapper::OnAudioEnded));
|
2015-09-07 06:56:52 +03:00
|
|
|
}
|
2015-09-02 09:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSinkWrapper::Stop()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(mIsStarted, "playback not started.");
|
|
|
|
|
|
|
|
mIsStarted = false;
|
2015-09-07 06:58:11 +03:00
|
|
|
mAudioEnded = true;
|
2015-09-07 06:56:52 +03:00
|
|
|
|
|
|
|
if (mAudioSink) {
|
2015-09-07 06:58:11 +03:00
|
|
|
mAudioSinkPromise.DisconnectIfExists();
|
2015-09-07 06:56:52 +03:00
|
|
|
mAudioSink->Shutdown();
|
|
|
|
mAudioSink = nullptr;
|
|
|
|
mEndPromise = nullptr;
|
|
|
|
}
|
2015-09-02 09:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioSinkWrapper::IsStarted() const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
return mIsStarted;
|
|
|
|
}
|
|
|
|
|
2015-09-10 06:31:30 +03:00
|
|
|
bool
|
|
|
|
AudioSinkWrapper::IsPlaying() const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
return IsStarted() && !mPlayStartTime.IsNull();
|
|
|
|
}
|
|
|
|
|
2015-09-07 06:58:11 +03:00
|
|
|
void
|
|
|
|
AudioSinkWrapper::OnAudioEnded()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
mAudioSinkPromise.Complete();
|
|
|
|
mPlayDuration = GetPosition();
|
|
|
|
if (!mPlayStartTime.IsNull()) {
|
|
|
|
mPlayStartTime = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
mAudioEnded = true;
|
|
|
|
}
|
|
|
|
|
2015-09-02 09:11:28 +03:00
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|
|
|
|
|