2015-05-09 20:38:15 -07: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 "DecodedStream.h"
|
|
|
|
#include "MediaStreamGraph.h"
|
2015-07-05 20:36:15 -07:00
|
|
|
#include "AudioSegment.h"
|
|
|
|
#include "VideoSegment.h"
|
|
|
|
#include "MediaQueue.h"
|
|
|
|
#include "MediaData.h"
|
|
|
|
#include "SharedBuffer.h"
|
|
|
|
#include "VideoUtils.h"
|
2015-05-09 20:38:15 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class DecodedStreamGraphListener : public MediaStreamListener {
|
|
|
|
typedef MediaStreamListener::MediaStreamGraphEvent MediaStreamGraphEvent;
|
|
|
|
public:
|
2015-05-09 21:07:14 -07:00
|
|
|
explicit DecodedStreamGraphListener(MediaStream* aStream)
|
|
|
|
: mMutex("DecodedStreamGraphListener::mMutex")
|
2015-05-09 20:38:15 -07:00
|
|
|
, mStream(aStream)
|
|
|
|
, mLastOutputTime(aStream->StreamTimeToMicroseconds(aStream->GetCurrentTime()))
|
|
|
|
, mStreamFinishedOnMainThread(false) {}
|
|
|
|
|
|
|
|
void NotifyOutput(MediaStreamGraph* aGraph, GraphTime aCurrentTime) override
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mStream) {
|
|
|
|
mLastOutputTime = mStream->StreamTimeToMicroseconds(mStream->GraphTimeToStreamTime(aCurrentTime));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyEvent(MediaStreamGraph* aGraph, MediaStreamGraphEvent event) override
|
|
|
|
{
|
|
|
|
if (event == EVENT_FINISHED) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(this, &DecodedStreamGraphListener::DoNotifyFinished);
|
|
|
|
aGraph->DispatchToMainThreadAfterStreamStateUpdate(event.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoNotifyFinished()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mStreamFinishedOnMainThread = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GetLastOutputTime()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mLastOutputTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Forget()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mStream = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFinishedOnMainThread()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mStreamFinishedOnMainThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex mMutex;
|
|
|
|
// Members below are protected by mMutex.
|
|
|
|
nsRefPtr<MediaStream> mStream;
|
|
|
|
int64_t mLastOutputTime; // microseconds
|
|
|
|
bool mStreamFinishedOnMainThread;
|
|
|
|
};
|
|
|
|
|
2015-06-08 01:51:39 -07:00
|
|
|
static void
|
|
|
|
UpdateStreamBlocking(MediaStream* aStream, bool aBlocking)
|
|
|
|
{
|
|
|
|
int32_t delta = aBlocking ? 1 : -1;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
aStream->ChangeExplicitBlockerCount(delta);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethodWithArg<int32_t>(
|
|
|
|
aStream, &MediaStream::ChangeExplicitBlockerCount, delta);
|
|
|
|
AbstractThread::MainThread()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 20:36:26 -07:00
|
|
|
DecodedStreamData::DecodedStreamData(SourceMediaStream* aStream, bool aPlaying)
|
2015-05-09 20:38:15 -07:00
|
|
|
: mAudioFramesWritten(0)
|
|
|
|
, mNextVideoTime(-1)
|
|
|
|
, mNextAudioTime(-1)
|
|
|
|
, mStreamInitialized(false)
|
|
|
|
, mHaveSentFinish(false)
|
|
|
|
, mHaveSentFinishAudio(false)
|
|
|
|
, mHaveSentFinishVideo(false)
|
|
|
|
, mStream(aStream)
|
2015-07-05 20:36:26 -07:00
|
|
|
, mPlaying(aPlaying)
|
2015-05-09 20:38:15 -07:00
|
|
|
, mEOSVideoCompensation(false)
|
|
|
|
{
|
2015-05-09 21:07:14 -07:00
|
|
|
mListener = new DecodedStreamGraphListener(mStream);
|
2015-05-09 20:38:15 -07:00
|
|
|
mStream->AddListener(mListener);
|
2015-07-05 20:36:26 -07:00
|
|
|
|
|
|
|
// Block the stream if we are not playing.
|
|
|
|
if (!aPlaying) {
|
|
|
|
UpdateStreamBlocking(mStream, true);
|
|
|
|
}
|
2015-05-09 20:38:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DecodedStreamData::~DecodedStreamData()
|
|
|
|
{
|
|
|
|
mListener->Forget();
|
|
|
|
mStream->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodedStreamData::IsFinished() const
|
|
|
|
{
|
|
|
|
return mListener->IsFinishedOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
2015-06-08 01:51:36 -07:00
|
|
|
DecodedStreamData::GetPosition() const
|
2015-05-09 20:38:15 -07:00
|
|
|
{
|
2015-06-08 01:51:36 -07:00
|
|
|
return mListener->GetLastOutputTime();
|
2015-05-09 20:38:15 -07:00
|
|
|
}
|
|
|
|
|
2015-06-08 01:51:39 -07:00
|
|
|
void
|
|
|
|
DecodedStreamData::SetPlaying(bool aPlaying)
|
|
|
|
{
|
|
|
|
if (mPlaying != aPlaying) {
|
|
|
|
mPlaying = aPlaying;
|
|
|
|
UpdateStreamBlocking(mStream, !mPlaying);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 20:38:15 -07:00
|
|
|
class OutputStreamListener : public MediaStreamListener {
|
|
|
|
typedef MediaStreamListener::MediaStreamGraphEvent MediaStreamGraphEvent;
|
|
|
|
public:
|
2015-05-09 21:07:14 -07:00
|
|
|
OutputStreamListener(DecodedStream* aDecodedStream, MediaStream* aStream)
|
|
|
|
: mDecodedStream(aDecodedStream), mStream(aStream) {}
|
2015-05-09 20:38:15 -07:00
|
|
|
|
|
|
|
void NotifyEvent(MediaStreamGraph* aGraph, MediaStreamGraphEvent event) override
|
|
|
|
{
|
|
|
|
if (event == EVENT_FINISHED) {
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethod(
|
|
|
|
this, &OutputStreamListener::DoNotifyFinished);
|
|
|
|
aGraph->DispatchToMainThreadAfterStreamStateUpdate(r.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Forget()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-09 21:07:14 -07:00
|
|
|
mDecodedStream = nullptr;
|
2015-05-09 20:38:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DoNotifyFinished()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-05 20:34:47 -07:00
|
|
|
if (mDecodedStream) {
|
|
|
|
// Remove the finished stream so it won't block the decoded stream.
|
|
|
|
mDecodedStream->Remove(mStream);
|
2015-05-09 20:38:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main thread only
|
2015-05-09 21:07:14 -07:00
|
|
|
DecodedStream* mDecodedStream;
|
2015-05-09 20:38:15 -07:00
|
|
|
nsRefPtr<MediaStream> mStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
OutputStreamData::~OutputStreamData()
|
|
|
|
{
|
|
|
|
mListener->Forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-09 21:07:14 -07:00
|
|
|
OutputStreamData::Init(DecodedStream* aDecodedStream, ProcessedMediaStream* aStream)
|
2015-05-09 20:38:15 -07:00
|
|
|
{
|
|
|
|
mStream = aStream;
|
2015-05-09 21:07:14 -07:00
|
|
|
mListener = new OutputStreamListener(aDecodedStream, aStream);
|
2015-05-09 20:38:15 -07:00
|
|
|
aStream->AddListener(mListener);
|
|
|
|
}
|
|
|
|
|
2015-07-27 09:21:33 -07:00
|
|
|
DecodedStream::DecodedStream(MediaQueue<MediaData>& aAudioQueue,
|
|
|
|
MediaQueue<MediaData>& aVideoQueue)
|
2015-07-01 21:10:10 -07:00
|
|
|
: mMonitor("DecodedStream::mMonitor")
|
2015-07-05 20:36:26 -07:00
|
|
|
, mPlaying(false)
|
2015-07-03 18:30:15 -07:00
|
|
|
, mAudioQueue(aAudioQueue)
|
|
|
|
, mVideoQueue(aVideoQueue)
|
2015-05-09 21:07:14 -07:00
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
void
|
|
|
|
DecodedStream::StartPlayback(int64_t aStartTime, const MediaInfo& aInfo)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
if (mStartTime.isNothing()) {
|
|
|
|
mStartTime.emplace(aStartTime);
|
|
|
|
mInfo = aInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecodedStream::StopPlayback()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
mStartTime.reset();
|
|
|
|
}
|
|
|
|
|
2015-05-09 20:48:05 -07:00
|
|
|
void
|
|
|
|
DecodedStream::DestroyData()
|
|
|
|
{
|
2015-05-09 21:07:14 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-01 21:10:10 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-05-09 21:26:03 -07:00
|
|
|
|
|
|
|
// Avoid the redundant blocking to output stream.
|
|
|
|
if (!mData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All streams are having their SourceMediaStream disconnected, so they
|
|
|
|
// need to be explicitly blocked again.
|
|
|
|
auto& outputStreams = OutputStreams();
|
|
|
|
for (int32_t i = outputStreams.Length() - 1; i >= 0; --i) {
|
|
|
|
OutputStreamData& os = outputStreams[i];
|
|
|
|
// Explicitly remove all existing ports.
|
|
|
|
// This is not strictly necessary but it's good form.
|
|
|
|
MOZ_ASSERT(os.mPort, "Double-delete of the ports!");
|
|
|
|
os.mPort->Destroy();
|
|
|
|
os.mPort = nullptr;
|
|
|
|
// During cycle collection, nsDOMMediaStream can be destroyed and send
|
|
|
|
// its Destroy message before this decoder is destroyed. So we have to
|
|
|
|
// be careful not to send any messages after the Destroy().
|
|
|
|
if (os.mStream->IsDestroyed()) {
|
|
|
|
// Probably the DOM MediaStream was GCed. Clean up.
|
|
|
|
outputStreams.RemoveElementAt(i);
|
|
|
|
} else {
|
|
|
|
os.mStream->ChangeExplicitBlockerCount(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 20:48:05 -07:00
|
|
|
mData = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-05 20:36:26 -07:00
|
|
|
void
|
|
|
|
DecodedStream::RecreateData()
|
|
|
|
{
|
|
|
|
nsRefPtr<DecodedStream> self = this;
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([self] () -> void {
|
|
|
|
self->RecreateData(nullptr);
|
|
|
|
});
|
|
|
|
AbstractThread::MainThread()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2015-05-09 20:48:05 -07:00
|
|
|
void
|
2015-06-08 01:51:36 -07:00
|
|
|
DecodedStream::RecreateData(MediaStreamGraph* aGraph)
|
2015-05-09 20:48:05 -07:00
|
|
|
{
|
2015-05-09 21:07:14 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-01 21:10:10 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-05-27 23:16:01 -07:00
|
|
|
MOZ_ASSERT((aGraph && !mData && OutputStreams().IsEmpty()) || // first time
|
|
|
|
(!aGraph && mData)); // 2nd time and later
|
2015-05-25 19:21:41 -07:00
|
|
|
|
2015-05-27 23:16:01 -07:00
|
|
|
if (!aGraph) {
|
|
|
|
aGraph = mData->mStream->Graph();
|
|
|
|
}
|
|
|
|
auto source = aGraph->CreateSourceStream(nullptr);
|
|
|
|
DestroyData();
|
2015-07-05 20:36:26 -07:00
|
|
|
mData.reset(new DecodedStreamData(source, mPlaying));
|
2015-05-25 19:21:41 -07:00
|
|
|
|
|
|
|
// Note that the delay between removing ports in DestroyDecodedStream
|
|
|
|
// and adding new ones won't cause a glitch since all graph operations
|
|
|
|
// between main-thread stable states take effect atomically.
|
|
|
|
auto& outputStreams = OutputStreams();
|
|
|
|
for (int32_t i = outputStreams.Length() - 1; i >= 0; --i) {
|
|
|
|
OutputStreamData& os = outputStreams[i];
|
|
|
|
MOZ_ASSERT(!os.mStream->IsDestroyed(), "Should've been removed in DestroyData()");
|
|
|
|
Connect(&os);
|
|
|
|
}
|
2015-05-09 20:48:05 -07:00
|
|
|
}
|
|
|
|
|
2015-05-09 20:57:46 -07:00
|
|
|
nsTArray<OutputStreamData>&
|
|
|
|
DecodedStream::OutputStreams()
|
|
|
|
{
|
2015-07-05 20:34:47 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-09 21:07:14 -07:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2015-05-09 20:57:46 -07:00
|
|
|
return mOutputStreams;
|
|
|
|
}
|
|
|
|
|
2015-07-24 05:28:17 -07:00
|
|
|
bool
|
|
|
|
DecodedStream::HasConsumers() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
return mOutputStreams.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:07:14 -07:00
|
|
|
ReentrantMonitor&
|
2015-05-27 23:17:30 -07:00
|
|
|
DecodedStream::GetReentrantMonitor() const
|
2015-05-09 21:07:14 -07:00
|
|
|
{
|
|
|
|
return mMonitor;
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:16:38 -07:00
|
|
|
void
|
|
|
|
DecodedStream::Connect(OutputStreamData* aStream)
|
|
|
|
{
|
2015-05-09 21:26:03 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2015-05-09 21:16:38 -07:00
|
|
|
NS_ASSERTION(!aStream->mPort, "Already connected?");
|
|
|
|
|
|
|
|
// The output stream must stay in sync with the decoded stream, so if
|
|
|
|
// either stream is blocked, we block the other.
|
|
|
|
aStream->mPort = aStream->mStream->AllocateInputPort(mData->mStream,
|
|
|
|
MediaInputPort::FLAG_BLOCK_INPUT | MediaInputPort::FLAG_BLOCK_OUTPUT);
|
|
|
|
// Unblock the output stream now. While it's connected to DecodedStream,
|
|
|
|
// DecodedStream is responsible for controlling blocking.
|
|
|
|
aStream->mStream->ChangeExplicitBlockerCount(-1);
|
|
|
|
}
|
|
|
|
|
2015-05-25 19:21:53 -07:00
|
|
|
void
|
|
|
|
DecodedStream::Connect(ProcessedMediaStream* aStream, bool aFinishWhenEnded)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-01 21:10:10 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-05-25 19:21:53 -07:00
|
|
|
|
2015-07-05 20:36:26 -07:00
|
|
|
if (!mData) {
|
|
|
|
RecreateData(aStream->Graph());
|
|
|
|
}
|
|
|
|
|
2015-05-25 19:21:53 -07:00
|
|
|
OutputStreamData* os = OutputStreams().AppendElement();
|
|
|
|
os->Init(this, aStream);
|
|
|
|
Connect(os);
|
|
|
|
if (aFinishWhenEnded) {
|
|
|
|
// Ensure that aStream finishes the moment mDecodedStream does.
|
|
|
|
aStream->SetAutofinish(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 20:34:47 -07:00
|
|
|
void
|
|
|
|
DecodedStream::Remove(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
|
|
|
|
auto& streams = OutputStreams();
|
|
|
|
for (int32_t i = streams.Length() - 1; i >= 0; --i) {
|
|
|
|
auto& os = streams[i];
|
|
|
|
MediaStream* p = os.mStream.get();
|
|
|
|
if (p == aStream) {
|
|
|
|
if (os.mPort) {
|
|
|
|
os.mPort->Destroy();
|
|
|
|
os.mPort = nullptr;
|
|
|
|
}
|
|
|
|
streams.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 01:51:39 -07:00
|
|
|
void
|
|
|
|
DecodedStream::SetPlaying(bool aPlaying)
|
|
|
|
{
|
2015-07-01 21:10:10 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-07-05 20:36:26 -07:00
|
|
|
mPlaying = aPlaying;
|
|
|
|
if (mData) {
|
|
|
|
mData->SetPlaying(aPlaying);
|
|
|
|
}
|
2015-06-08 01:51:39 -07:00
|
|
|
}
|
|
|
|
|
2015-07-05 20:36:15 -07:00
|
|
|
void
|
2015-07-11 01:41:39 -07:00
|
|
|
DecodedStream::InitTracks()
|
2015-07-05 20:36:15 -07:00
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
if (mData->mStreamInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceMediaStream* sourceStream = mData->mStream;
|
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
if (mInfo.HasAudio()) {
|
|
|
|
TrackID audioTrackId = mInfo.mAudio.mTrackId;
|
2015-07-05 20:36:15 -07:00
|
|
|
AudioSegment* audio = new AudioSegment();
|
2015-07-11 01:41:39 -07:00
|
|
|
sourceStream->AddAudioTrack(audioTrackId, mInfo.mAudio.mRate, 0, audio,
|
2015-07-05 20:36:15 -07:00
|
|
|
SourceMediaStream::ADDTRACK_QUEUED);
|
2015-07-11 01:41:39 -07:00
|
|
|
mData->mNextAudioTime = mStartTime.ref();
|
2015-07-05 20:36:15 -07:00
|
|
|
}
|
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
if (mInfo.HasVideo()) {
|
|
|
|
TrackID videoTrackId = mInfo.mVideo.mTrackId;
|
2015-07-05 20:36:15 -07:00
|
|
|
VideoSegment* video = new VideoSegment();
|
|
|
|
sourceStream->AddTrack(videoTrackId, 0, video,
|
|
|
|
SourceMediaStream::ADDTRACK_QUEUED);
|
2015-07-11 01:41:39 -07:00
|
|
|
mData->mNextVideoTime = mStartTime.ref();
|
2015-07-05 20:36:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sourceStream->FinishAddTracks();
|
|
|
|
mData->mStreamInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SendStreamAudio(DecodedStreamData* aStream, int64_t aStartTime,
|
2015-07-27 09:21:33 -07:00
|
|
|
MediaData* aData, AudioSegment* aOutput,
|
2015-07-05 20:36:15 -07:00
|
|
|
uint32_t aRate, double aVolume)
|
|
|
|
{
|
2015-07-27 09:21:33 -07:00
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
AudioData* audio = aData->As<AudioData>();
|
2015-07-05 20:36:15 -07:00
|
|
|
// This logic has to mimic AudioSink closely to make sure we write
|
|
|
|
// the exact same silences
|
|
|
|
CheckedInt64 audioWrittenOffset = aStream->mAudioFramesWritten +
|
|
|
|
UsecsToFrames(aStartTime, aRate);
|
2015-07-27 09:21:33 -07:00
|
|
|
CheckedInt64 frameOffset = UsecsToFrames(audio->mTime, aRate);
|
2015-07-05 20:36:15 -07:00
|
|
|
|
|
|
|
if (!audioWrittenOffset.isValid() ||
|
|
|
|
!frameOffset.isValid() ||
|
|
|
|
// ignore packet that we've already processed
|
2015-07-27 09:21:33 -07:00
|
|
|
frameOffset.value() + audio->mFrames <= audioWrittenOffset.value()) {
|
2015-07-05 20:36:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audioWrittenOffset.value() < frameOffset.value()) {
|
|
|
|
int64_t silentFrames = frameOffset.value() - audioWrittenOffset.value();
|
|
|
|
// Write silence to catch up
|
|
|
|
AudioSegment silence;
|
|
|
|
silence.InsertNullDataAtStart(silentFrames);
|
|
|
|
aStream->mAudioFramesWritten += silentFrames;
|
|
|
|
audioWrittenOffset += silentFrames;
|
|
|
|
aOutput->AppendFrom(&silence);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(audioWrittenOffset.value() >= frameOffset.value());
|
|
|
|
|
|
|
|
int64_t offset = audioWrittenOffset.value() - frameOffset.value();
|
2015-07-27 09:21:33 -07:00
|
|
|
size_t framesToWrite = audio->mFrames - offset;
|
2015-07-05 20:36:15 -07:00
|
|
|
|
2015-07-27 09:21:33 -07:00
|
|
|
audio->EnsureAudioBuffer();
|
|
|
|
nsRefPtr<SharedBuffer> buffer = audio->mAudioBuffer;
|
2015-07-05 20:36:15 -07:00
|
|
|
AudioDataValue* bufferData = static_cast<AudioDataValue*>(buffer->Data());
|
|
|
|
nsAutoTArray<const AudioDataValue*, 2> channels;
|
2015-07-27 09:21:33 -07:00
|
|
|
for (uint32_t i = 0; i < audio->mChannels; ++i) {
|
|
|
|
channels.AppendElement(bufferData + i * audio->mFrames + offset);
|
2015-07-05 20:36:15 -07:00
|
|
|
}
|
|
|
|
aOutput->AppendFrames(buffer.forget(), channels, framesToWrite);
|
|
|
|
aStream->mAudioFramesWritten += framesToWrite;
|
|
|
|
aOutput->ApplyVolume(aVolume);
|
|
|
|
|
2015-07-27 09:21:33 -07:00
|
|
|
aStream->mNextAudioTime = audio->GetEndTime();
|
2015-07-05 20:36:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-11 01:41:39 -07:00
|
|
|
DecodedStream::SendAudio(double aVolume, bool aIsSameOrigin)
|
2015-07-05 20:36:15 -07:00
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
if (!mInfo.HasAudio()) {
|
2015-07-05 20:36:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioSegment output;
|
2015-07-11 01:41:39 -07:00
|
|
|
uint32_t rate = mInfo.mAudio.mRate;
|
2015-07-27 09:21:33 -07:00
|
|
|
nsAutoTArray<nsRefPtr<MediaData>,10> audio;
|
2015-07-11 01:41:39 -07:00
|
|
|
TrackID audioTrackId = mInfo.mAudio.mTrackId;
|
2015-07-05 20:36:15 -07:00
|
|
|
SourceMediaStream* sourceStream = mData->mStream;
|
|
|
|
|
|
|
|
// It's OK to hold references to the AudioData because AudioData
|
|
|
|
// is ref-counted.
|
2015-07-03 18:30:15 -07:00
|
|
|
mAudioQueue.GetElementsAfter(mData->mNextAudioTime, &audio);
|
2015-07-05 20:36:15 -07:00
|
|
|
for (uint32_t i = 0; i < audio.Length(); ++i) {
|
2015-07-11 01:41:39 -07:00
|
|
|
SendStreamAudio(mData.get(), mStartTime.ref(), audio[i], &output, rate, aVolume);
|
2015-07-05 20:36:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aIsSameOrigin) {
|
|
|
|
output.ReplaceWithDisabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
// |mNextAudioTime| is updated as we process each audio sample in
|
|
|
|
// SendStreamAudio(). This is consistent with how |mNextVideoTime|
|
|
|
|
// is updated for video samples.
|
|
|
|
if (output.GetDuration() > 0) {
|
|
|
|
sourceStream->AppendToTrack(audioTrackId, &output);
|
|
|
|
}
|
|
|
|
|
2015-07-03 18:30:15 -07:00
|
|
|
if (mAudioQueue.IsFinished() && !mData->mHaveSentFinishAudio) {
|
2015-07-05 20:36:15 -07:00
|
|
|
sourceStream->EndTrack(audioTrackId);
|
|
|
|
mData->mHaveSentFinishAudio = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
WriteVideoToMediaStream(MediaStream* aStream,
|
|
|
|
layers::Image* aImage,
|
|
|
|
int64_t aEndMicroseconds,
|
|
|
|
int64_t aStartMicroseconds,
|
|
|
|
const mozilla::gfx::IntSize& aIntrinsicSize,
|
|
|
|
VideoSegment* aOutput)
|
|
|
|
{
|
|
|
|
nsRefPtr<layers::Image> image = aImage;
|
|
|
|
StreamTime duration =
|
|
|
|
aStream->MicrosecondsToStreamTimeRoundDown(aEndMicroseconds) -
|
|
|
|
aStream->MicrosecondsToStreamTimeRoundDown(aStartMicroseconds);
|
|
|
|
aOutput->AppendFrame(image.forget(), duration, aIntrinsicSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ZeroDurationAtLastChunk(VideoSegment& aInput)
|
|
|
|
{
|
|
|
|
// Get the last video frame's start time in VideoSegment aInput.
|
|
|
|
// If the start time is equal to the duration of aInput, means the last video
|
|
|
|
// frame's duration is zero.
|
|
|
|
StreamTime lastVideoStratTime;
|
|
|
|
aInput.GetLastFrame(&lastVideoStratTime);
|
|
|
|
return lastVideoStratTime == aInput.GetDuration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-11 01:41:39 -07:00
|
|
|
DecodedStream::SendVideo(bool aIsSameOrigin)
|
2015-07-05 20:36:15 -07:00
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
if (!mInfo.HasVideo()) {
|
2015-07-05 20:36:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoSegment output;
|
2015-07-11 01:41:39 -07:00
|
|
|
TrackID videoTrackId = mInfo.mVideo.mTrackId;
|
2015-07-27 09:21:33 -07:00
|
|
|
nsAutoTArray<nsRefPtr<MediaData>, 10> video;
|
2015-07-05 20:36:15 -07:00
|
|
|
SourceMediaStream* sourceStream = mData->mStream;
|
|
|
|
|
|
|
|
// It's OK to hold references to the VideoData because VideoData
|
|
|
|
// is ref-counted.
|
2015-07-03 18:30:15 -07:00
|
|
|
mVideoQueue.GetElementsAfter(mData->mNextVideoTime, &video);
|
2015-07-05 20:36:15 -07:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < video.Length(); ++i) {
|
2015-07-27 09:21:33 -07:00
|
|
|
VideoData* v = video[i]->As<VideoData>();
|
2015-07-05 20:36:15 -07:00
|
|
|
|
|
|
|
if (mData->mNextVideoTime < v->mTime) {
|
|
|
|
// Write last video frame to catch up. mLastVideoImage can be null here
|
|
|
|
// which is fine, it just means there's no video.
|
|
|
|
|
|
|
|
// TODO: |mLastVideoImage| should come from the last image rendered
|
|
|
|
// by the state machine. This will avoid the black frame when capture
|
|
|
|
// happens in the middle of playback (especially in th middle of a
|
|
|
|
// video frame). E.g. if we have a video frame that is 30 sec long
|
|
|
|
// and capture happens at 15 sec, we'll have to append a black frame
|
|
|
|
// that is 15 sec long.
|
|
|
|
WriteVideoToMediaStream(sourceStream, mData->mLastVideoImage, v->mTime,
|
|
|
|
mData->mNextVideoTime, mData->mLastVideoImageDisplaySize, &output);
|
|
|
|
mData->mNextVideoTime = v->mTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mData->mNextVideoTime < v->GetEndTime()) {
|
|
|
|
WriteVideoToMediaStream(sourceStream, v->mImage,
|
|
|
|
v->GetEndTime(), mData->mNextVideoTime, v->mDisplay, &output);
|
|
|
|
mData->mNextVideoTime = v->GetEndTime();
|
|
|
|
mData->mLastVideoImage = v->mImage;
|
|
|
|
mData->mLastVideoImageDisplaySize = v->mDisplay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the output is not empty.
|
|
|
|
if (output.GetLastFrame()) {
|
|
|
|
mData->mEOSVideoCompensation = ZeroDurationAtLastChunk(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aIsSameOrigin) {
|
|
|
|
output.ReplaceWithDisabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.GetDuration() > 0) {
|
|
|
|
sourceStream->AppendToTrack(videoTrackId, &output);
|
|
|
|
}
|
|
|
|
|
2015-07-03 18:30:15 -07:00
|
|
|
if (mVideoQueue.IsFinished() && !mData->mHaveSentFinishVideo) {
|
2015-07-05 20:36:15 -07:00
|
|
|
if (mData->mEOSVideoCompensation) {
|
|
|
|
VideoSegment endSegment;
|
|
|
|
// Calculate the deviation clock time from DecodedStream.
|
|
|
|
int64_t deviation_usec = sourceStream->StreamTimeToMicroseconds(1);
|
|
|
|
WriteVideoToMediaStream(sourceStream, mData->mLastVideoImage,
|
|
|
|
mData->mNextVideoTime + deviation_usec, mData->mNextVideoTime,
|
|
|
|
mData->mLastVideoImageDisplaySize, &endSegment);
|
|
|
|
mData->mNextVideoTime += deviation_usec;
|
|
|
|
MOZ_ASSERT(endSegment.GetDuration() > 0);
|
|
|
|
if (!aIsSameOrigin) {
|
|
|
|
endSegment.ReplaceWithDisabled();
|
|
|
|
}
|
|
|
|
sourceStream->AppendToTrack(videoTrackId, &endSegment);
|
|
|
|
}
|
|
|
|
sourceStream->EndTrack(videoTrackId);
|
|
|
|
mData->mHaveSentFinishVideo = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-11 01:41:39 -07:00
|
|
|
DecodedStream::AdvanceTracks()
|
2015-07-05 20:36:15 -07:00
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
StreamTime endPosition = 0;
|
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
if (mInfo.HasAudio()) {
|
2015-07-05 20:36:15 -07:00
|
|
|
StreamTime audioEnd = mData->mStream->TicksToTimeRoundDown(
|
2015-07-11 01:41:39 -07:00
|
|
|
mInfo.mAudio.mRate, mData->mAudioFramesWritten);
|
2015-07-05 20:36:15 -07:00
|
|
|
endPosition = std::max(endPosition, audioEnd);
|
|
|
|
}
|
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
if (mInfo.HasVideo()) {
|
2015-07-05 20:36:15 -07:00
|
|
|
StreamTime videoEnd = mData->mStream->MicrosecondsToStreamTimeRoundDown(
|
2015-07-11 01:41:39 -07:00
|
|
|
mData->mNextVideoTime - mStartTime.ref());
|
2015-07-05 20:36:15 -07:00
|
|
|
endPosition = std::max(endPosition, videoEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mData->mHaveSentFinish) {
|
|
|
|
mData->mStream->AdvanceKnownTracksTime(endPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-07-11 01:41:39 -07:00
|
|
|
DecodedStream::SendData(double aVolume, bool aIsSameOrigin)
|
2015-07-05 20:36:15 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-07-11 01:41:39 -07:00
|
|
|
MOZ_ASSERT(mStartTime.isSome(), "Must be called after StartPlayback()");
|
2015-07-05 20:36:15 -07:00
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
InitTracks();
|
|
|
|
SendAudio(aVolume, aIsSameOrigin);
|
|
|
|
SendVideo(aIsSameOrigin);
|
|
|
|
AdvanceTracks();
|
2015-07-05 20:36:15 -07:00
|
|
|
|
2015-07-11 01:41:39 -07:00
|
|
|
bool finished = (!mInfo.HasAudio() || mAudioQueue.IsFinished()) &&
|
|
|
|
(!mInfo.HasVideo() || mVideoQueue.IsFinished());
|
2015-07-05 20:36:15 -07:00
|
|
|
|
|
|
|
if (finished && !mData->mHaveSentFinish) {
|
|
|
|
mData->mHaveSentFinish = true;
|
|
|
|
mData->mStream->Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
return finished;
|
|
|
|
}
|
|
|
|
|
2015-07-16 19:18:04 -07:00
|
|
|
int64_t
|
2015-07-11 01:41:39 -07:00
|
|
|
DecodedStream::AudioEndTime() const
|
2015-07-05 20:36:15 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-07-16 19:18:04 -07:00
|
|
|
if (mStartTime.isSome() && mInfo.HasAudio()) {
|
|
|
|
CheckedInt64 t = mStartTime.ref() +
|
|
|
|
FramesToUsecs(mData->mAudioFramesWritten, mInfo.mAudio.mRate);
|
|
|
|
if (t.isValid()) {
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2015-07-05 20:36:15 -07:00
|
|
|
}
|
|
|
|
|
2015-07-05 20:36:26 -07:00
|
|
|
int64_t
|
|
|
|
DecodedStream::GetPosition() const
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-07-11 02:20:28 -07:00
|
|
|
// This is only called after MDSM starts playback. So mStartTime is
|
|
|
|
// guaranteed to be something.
|
|
|
|
MOZ_ASSERT(mStartTime.isSome());
|
|
|
|
return mStartTime.ref() + mData->GetPosition();
|
2015-07-05 20:36:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodedStream::IsFinished() const
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
return mData->IsFinished();
|
|
|
|
}
|
|
|
|
|
2015-05-09 20:38:15 -07:00
|
|
|
} // namespace mozilla
|