2012-05-11 10:35:36 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
2012-04-29 20:11:26 -07:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-02-04 02:04:25 -08:00
|
|
|
#include "MediaStreamGraphImpl.h"
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
#include "AudioSegment.h"
|
|
|
|
#include "VideoSegment.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIAppShell.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsWidgetsCID.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "VideoUtils.h"
|
2012-06-18 19:30:09 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-31 05:17:21 -07:00
|
|
|
#include "TrackUnionStream.h"
|
2012-08-20 21:06:46 -07:00
|
|
|
#include "ImageContainer.h"
|
2012-11-15 19:25:26 -08:00
|
|
|
#include "AudioChannelCommon.h"
|
2013-01-13 14:46:57 -08:00
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
using namespace mozilla::layers;
|
2012-11-15 19:25:26 -08:00
|
|
|
using namespace mozilla::dom;
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gMediaStreamGraphLog;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The singleton graph instance.
|
|
|
|
*/
|
|
|
|
static MediaStreamGraphImpl* gGraph;
|
|
|
|
|
|
|
|
StreamTime
|
|
|
|
MediaStreamGraphImpl::GetDesiredBufferEnd(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
StreamTime current = mCurrentTime - aStream->mBufferStartTime;
|
2012-07-31 05:17:21 -07:00
|
|
|
return current +
|
2013-01-15 04:22:03 -08:00
|
|
|
MillisecondsToMediaTime(std::max(AUDIO_TARGET_MS, VIDEO_TARGET_MS));
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::FinishStream(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
if (aStream->mFinished)
|
|
|
|
return;
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p will finish", aStream));
|
|
|
|
aStream->mFinished = true;
|
|
|
|
// Force at least one more iteration of the control loop, since we rely
|
|
|
|
// on UpdateCurrentTime to notify our listeners once the stream end
|
|
|
|
// has been reached.
|
|
|
|
EnsureNextIteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::AddStream(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
aStream->mBufferStartTime = mCurrentTime;
|
|
|
|
*mStreams.AppendElement() = already_AddRefed<MediaStream>(aStream);
|
|
|
|
LOG(PR_LOG_DEBUG, ("Adding media stream %p to the graph", aStream));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::RemoveStream(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
// Remove references in mStreamUpdates before we allow aStream to die.
|
|
|
|
// Pending updates are not needed (since the main thread has already given
|
|
|
|
// up the stream) so we will just drop them.
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mStreamUpdates.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
if (mStreamUpdates[i].mStream == aStream) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mStreamUpdates[i].mStream = nullptr;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This unrefs the stream, probably destroying it
|
|
|
|
mStreams.RemoveElement(aStream);
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("Removing media stream %p from the graph", aStream));
|
|
|
|
}
|
|
|
|
|
2012-05-31 23:26:17 -07:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UpdateConsumptionState(SourceMediaStream* aStream)
|
|
|
|
{
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStreamListener::Consumption state =
|
|
|
|
aStream->mIsConsumed ? MediaStreamListener::CONSUMED
|
|
|
|
: MediaStreamListener::NOT_CONSUMED;
|
2012-05-31 23:26:17 -07:00
|
|
|
if (state != aStream->mLastConsumptionState) {
|
|
|
|
aStream->mLastConsumptionState = state;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < aStream->mListeners.Length(); ++j) {
|
2012-05-31 23:26:17 -07:00
|
|
|
MediaStreamListener* l = aStream->mListeners[j];
|
|
|
|
l->NotifyConsumptionChanged(this, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:40 -07:00
|
|
|
void
|
2012-07-20 12:36:03 -07:00
|
|
|
MediaStreamGraphImpl::ExtractPendingInput(SourceMediaStream* aStream,
|
|
|
|
GraphTime aDesiredUpToTime,
|
|
|
|
bool* aEnsureNextIteration)
|
2012-04-29 20:11:40 -07:00
|
|
|
{
|
|
|
|
bool finished;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(aStream->mMutex);
|
2012-12-07 03:06:55 -08:00
|
|
|
if (aStream->mPullEnabled && !aStream->mFinished &&
|
|
|
|
!aStream->mListeners.IsEmpty()) {
|
|
|
|
// Compute how much stream time we'll need assuming we don't block
|
|
|
|
// the stream at all between mBlockingDecisionsMadeUntilTime and
|
|
|
|
// aDesiredUpToTime.
|
|
|
|
StreamTime t =
|
|
|
|
GraphTimeToStreamTime(aStream, mStateComputedTime) +
|
|
|
|
(aDesiredUpToTime - mStateComputedTime);
|
2013-02-25 01:25:07 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Calling NotifyPull aStream=%p t=%f current end=%f", aStream,
|
|
|
|
MediaTimeToSeconds(t),
|
|
|
|
MediaTimeToSeconds(aStream->mBuffer.GetEnd())));
|
2012-12-07 03:06:55 -08:00
|
|
|
if (t > aStream->mBuffer.GetEnd()) {
|
|
|
|
*aEnsureNextIteration = true;
|
|
|
|
for (uint32_t j = 0; j < aStream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = aStream->mListeners[j];
|
|
|
|
{
|
|
|
|
MutexAutoUnlock unlock(aStream->mMutex);
|
|
|
|
l->NotifyPull(this, t);
|
|
|
|
}
|
2012-07-20 12:36:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-29 20:11:40 -07:00
|
|
|
finished = aStream->mUpdateFinished;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = aStream->mUpdateTracks.Length() - 1; i >= 0; --i) {
|
2012-04-29 20:11:40 -07:00
|
|
|
SourceMediaStream::TrackData* data = &aStream->mUpdateTracks[i];
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < aStream->mListeners.Length(); ++j) {
|
2012-04-29 20:12:50 -07:00
|
|
|
MediaStreamListener* l = aStream->mListeners[j];
|
|
|
|
TrackTicks offset = (data->mCommands & SourceMediaStream::TRACK_CREATE)
|
|
|
|
? data->mStart : aStream->mBuffer.FindTrack(data->mID)->GetSegment()->GetDuration();
|
|
|
|
l->NotifyQueuedTrackChanges(this, data->mID, data->mRate,
|
|
|
|
offset, data->mCommands, *data->mData);
|
|
|
|
}
|
2012-04-29 20:11:40 -07:00
|
|
|
if (data->mCommands & SourceMediaStream::TRACK_CREATE) {
|
|
|
|
MediaSegment* segment = data->mData.forget();
|
|
|
|
LOG(PR_LOG_DEBUG, ("SourceMediaStream %p creating track %d, rate %d, start %lld, initial end %lld",
|
2012-08-22 08:56:38 -07:00
|
|
|
aStream, data->mID, data->mRate, int64_t(data->mStart),
|
|
|
|
int64_t(segment->GetDuration())));
|
2012-04-29 20:11:40 -07:00
|
|
|
aStream->mBuffer.AddTrack(data->mID, data->mRate, data->mStart, segment);
|
|
|
|
// The track has taken ownership of data->mData, so let's replace
|
|
|
|
// data->mData with an empty clone.
|
|
|
|
data->mData = segment->CreateEmptyClone();
|
|
|
|
data->mCommands &= ~SourceMediaStream::TRACK_CREATE;
|
|
|
|
} else if (data->mData->GetDuration() > 0) {
|
|
|
|
MediaSegment* dest = aStream->mBuffer.FindTrack(data->mID)->GetSegment();
|
|
|
|
LOG(PR_LOG_DEBUG, ("SourceMediaStream %p track %d, advancing end from %lld to %lld",
|
|
|
|
aStream, data->mID,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t(dest->GetDuration()),
|
|
|
|
int64_t(dest->GetDuration() + data->mData->GetDuration())));
|
2012-04-29 20:11:40 -07:00
|
|
|
dest->AppendFrom(data->mData);
|
|
|
|
}
|
|
|
|
if (data->mCommands & SourceMediaStream::TRACK_END) {
|
|
|
|
aStream->mBuffer.FindTrack(data->mID)->SetEnded();
|
|
|
|
aStream->mUpdateTracks.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aStream->mBuffer.AdvanceKnownTracksTime(aStream->mUpdateKnownTracksTime);
|
|
|
|
}
|
|
|
|
if (finished) {
|
|
|
|
FinishStream(aStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UpdateBufferSufficiencyState(SourceMediaStream* aStream)
|
|
|
|
{
|
|
|
|
StreamTime desiredEnd = GetDesiredBufferEnd(aStream);
|
|
|
|
nsTArray<SourceMediaStream::ThreadAndRunnable> runnables;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(aStream->mMutex);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStream->mUpdateTracks.Length(); ++i) {
|
2012-04-29 20:11:40 -07:00
|
|
|
SourceMediaStream::TrackData* data = &aStream->mUpdateTracks[i];
|
2012-04-29 22:23:00 -07:00
|
|
|
if (data->mCommands & SourceMediaStream::TRACK_CREATE) {
|
|
|
|
// This track hasn't been created yet, so we have no sufficiency
|
|
|
|
// data. The track will be created in the next iteration of the
|
|
|
|
// control loop and then we'll fire insufficiency notifications
|
|
|
|
// if necessary.
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-29 20:13:42 -07:00
|
|
|
if (data->mCommands & SourceMediaStream::TRACK_END) {
|
|
|
|
// This track will end, so no point in firing not-enough-data
|
|
|
|
// callbacks.
|
2012-04-29 20:11:40 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
StreamBuffer::Track* track = aStream->mBuffer.FindTrack(data->mID);
|
2012-04-29 20:13:42 -07:00
|
|
|
// Note that track->IsEnded() must be false, otherwise we would have
|
|
|
|
// removed the track from mUpdateTracks already.
|
|
|
|
NS_ASSERTION(!track->IsEnded(), "What is this track doing here?");
|
2012-04-29 20:11:40 -07:00
|
|
|
data->mHaveEnough = track->GetEndTimeRoundDown() >= desiredEnd;
|
|
|
|
if (!data->mHaveEnough) {
|
|
|
|
runnables.MoveElementsFrom(data->mDispatchWhenNotEnough);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < runnables.Length(); ++i) {
|
2012-04-29 20:11:40 -07:00
|
|
|
runnables[i].mThread->Dispatch(runnables[i].mRunnable, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
StreamTime
|
|
|
|
MediaStreamGraphImpl::GraphTimeToStreamTime(MediaStream* aStream,
|
|
|
|
GraphTime aTime)
|
|
|
|
{
|
2012-07-31 05:17:21 -07:00
|
|
|
NS_ASSERTION(aTime <= mStateComputedTime,
|
2012-04-29 20:11:26 -07:00
|
|
|
"Don't ask about times where we haven't made blocking decisions yet");
|
|
|
|
if (aTime <= mCurrentTime) {
|
2013-01-15 04:22:03 -08:00
|
|
|
return std::max<StreamTime>(0, aTime - aStream->mBufferStartTime);
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
GraphTime t = mCurrentTime;
|
|
|
|
StreamTime s = t - aStream->mBufferStartTime;
|
|
|
|
while (t < aTime) {
|
|
|
|
GraphTime end;
|
|
|
|
if (!aStream->mBlocked.GetAt(t, &end)) {
|
2013-01-15 04:22:03 -08:00
|
|
|
s += std::min(aTime, end) - t;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
t = end;
|
|
|
|
}
|
2013-01-15 04:22:03 -08:00
|
|
|
return std::max<StreamTime>(0, s);
|
2013-02-04 02:04:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamTime
|
|
|
|
MediaStreamGraphImpl::GraphTimeToStreamTimeOptimistic(MediaStream* aStream,
|
|
|
|
GraphTime aTime)
|
|
|
|
{
|
|
|
|
GraphTime computedUpToTime = std::min(mStateComputedTime, aTime);
|
|
|
|
StreamTime s = GraphTimeToStreamTime(aStream, computedUpToTime);
|
|
|
|
return s + (aTime - computedUpToTime);
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
GraphTime
|
|
|
|
MediaStreamGraphImpl::StreamTimeToGraphTime(MediaStream* aStream,
|
2012-08-22 08:56:38 -07:00
|
|
|
StreamTime aTime, uint32_t aFlags)
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
if (aTime >= STREAM_TIME_MAX) {
|
|
|
|
return GRAPH_TIME_MAX;
|
|
|
|
}
|
|
|
|
MediaTime bufferElapsedToCurrentTime = mCurrentTime - aStream->mBufferStartTime;
|
|
|
|
if (aTime < bufferElapsedToCurrentTime ||
|
|
|
|
(aTime == bufferElapsedToCurrentTime && !(aFlags & INCLUDE_TRAILING_BLOCKED_INTERVAL))) {
|
|
|
|
return aTime + aStream->mBufferStartTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaTime streamAmount = aTime - bufferElapsedToCurrentTime;
|
|
|
|
NS_ASSERTION(streamAmount >= 0, "Can't answer queries before current time");
|
|
|
|
|
|
|
|
GraphTime t = mCurrentTime;
|
|
|
|
while (t < GRAPH_TIME_MAX) {
|
|
|
|
bool blocked;
|
|
|
|
GraphTime end;
|
2012-07-31 05:17:21 -07:00
|
|
|
if (t < mStateComputedTime) {
|
2012-04-29 20:11:26 -07:00
|
|
|
blocked = aStream->mBlocked.GetAt(t, &end);
|
2013-01-15 04:22:03 -08:00
|
|
|
end = std::min(end, mStateComputedTime);
|
2012-04-29 20:11:26 -07:00
|
|
|
} else {
|
|
|
|
blocked = false;
|
|
|
|
end = GRAPH_TIME_MAX;
|
|
|
|
}
|
|
|
|
if (blocked) {
|
|
|
|
t = end;
|
|
|
|
} else {
|
|
|
|
if (streamAmount == 0) {
|
|
|
|
// No more stream time to consume at time t, so we're done.
|
|
|
|
break;
|
|
|
|
}
|
2013-01-15 04:22:03 -08:00
|
|
|
MediaTime consume = std::min(end - t, streamAmount);
|
2012-04-29 20:11:26 -07:00
|
|
|
streamAmount -= consume;
|
|
|
|
t += consume;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphTime
|
|
|
|
MediaStreamGraphImpl::GetAudioPosition(MediaStream* aStream)
|
|
|
|
{
|
2012-07-31 05:17:22 -07:00
|
|
|
if (aStream->mAudioOutputStreams.IsEmpty()) {
|
2012-04-29 20:11:26 -07:00
|
|
|
return mCurrentTime;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t positionInFrames = aStream->mAudioOutputStreams[0].mStream->GetPositionInFrames();
|
2012-05-06 20:44:41 -07:00
|
|
|
if (positionInFrames < 0) {
|
|
|
|
return mCurrentTime;
|
|
|
|
}
|
2012-07-31 05:17:22 -07:00
|
|
|
return aStream->mAudioOutputStreams[0].mAudioPlaybackStartTime +
|
|
|
|
TicksToTimeRoundDown(aStream->mAudioOutputStreams[0].mStream->GetRate(),
|
2012-05-06 20:44:41 -07:00
|
|
|
positionInFrames);
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UpdateCurrentTime()
|
|
|
|
{
|
|
|
|
GraphTime prevCurrentTime = mCurrentTime;
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
GraphTime nextCurrentTime =
|
|
|
|
SecondsToMediaTime((now - mCurrentTimeStamp).ToSeconds()) + mCurrentTime;
|
2012-07-31 05:17:21 -07:00
|
|
|
if (mStateComputedTime < nextCurrentTime) {
|
2012-05-06 20:44:41 -07:00
|
|
|
LOG(PR_LOG_WARNING, ("Media graph global underrun detected"));
|
2013-02-04 02:04:25 -08:00
|
|
|
nextCurrentTime = mStateComputedTime;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
mCurrentTimeStamp = now;
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Updating current time to %f (real %f, mStateComputedTime %f)",
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaTimeToSeconds(nextCurrentTime),
|
|
|
|
(now - mInitialTimeStamp).ToSeconds(),
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaTimeToSeconds(mStateComputedTime)));
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
if (prevCurrentTime >= nextCurrentTime) {
|
|
|
|
NS_ASSERTION(prevCurrentTime == nextCurrentTime, "Time can't go backwards!");
|
|
|
|
// This could happen due to low clock resolution, maybe?
|
|
|
|
LOG(PR_LOG_DEBUG, ("Time did not advance"));
|
2012-05-28 04:58:34 -07:00
|
|
|
// There's not much left to do here, but the code below that notifies
|
|
|
|
// listeners that streams have ended still needs to run.
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStream* stream = mStreams[i];
|
|
|
|
|
|
|
|
// Calculate blocked time and fire Blocked/Unblocked events
|
|
|
|
GraphTime blockedTime = 0;
|
|
|
|
GraphTime t = prevCurrentTime;
|
|
|
|
while (t < nextCurrentTime) {
|
|
|
|
GraphTime end;
|
|
|
|
bool blocked = stream->mBlocked.GetAt(t, &end);
|
|
|
|
if (blocked) {
|
2013-01-15 04:22:03 -08:00
|
|
|
blockedTime += std::min(end, nextCurrentTime) - t;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
2013-01-02 05:49:18 -08:00
|
|
|
if (blocked != stream->mNotifiedBlocked) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < stream->mListeners.Length(); ++j) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStreamListener* l = stream->mListeners[j];
|
|
|
|
l->NotifyBlockingChanged(this,
|
|
|
|
blocked ? MediaStreamListener::BLOCKED : MediaStreamListener::UNBLOCKED);
|
|
|
|
}
|
2013-01-02 05:49:18 -08:00
|
|
|
stream->mNotifiedBlocked = blocked;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
t = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->AdvanceTimeVaryingValuesToCurrentTime(nextCurrentTime, blockedTime);
|
|
|
|
// Advance mBlocked last so that implementations of
|
|
|
|
// AdvanceTimeVaryingValuesToCurrentTime can rely on the value of mBlocked.
|
|
|
|
stream->mBlocked.AdvanceCurrentTime(nextCurrentTime);
|
|
|
|
|
2012-05-28 04:58:34 -07:00
|
|
|
if (blockedTime < nextCurrentTime - prevCurrentTime) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < stream->mListeners.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStreamListener* l = stream->mListeners[i];
|
|
|
|
l->NotifyOutput(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->mFinished && !stream->mNotifiedFinished &&
|
|
|
|
stream->mBufferStartTime + stream->GetBufferEnd() <= nextCurrentTime) {
|
|
|
|
stream->mNotifiedFinished = true;
|
2012-11-20 17:32:06 -08:00
|
|
|
stream->mLastPlayedVideoFrame.SetNull();
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < stream->mListeners.Length(); ++j) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStreamListener* l = stream->mListeners[j];
|
|
|
|
l->NotifyFinished(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p bufferStartTime=%f blockedTime=%f",
|
|
|
|
stream, MediaTimeToSeconds(stream->mBufferStartTime),
|
|
|
|
MediaTimeToSeconds(blockedTime)));
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentTime = nextCurrentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaStreamGraphImpl::WillUnderrun(MediaStream* aStream, GraphTime aTime,
|
|
|
|
GraphTime aEndBlockingDecisions, GraphTime* aEnd)
|
|
|
|
{
|
2012-07-31 05:17:21 -07:00
|
|
|
// Finished streams can't underrun. ProcessedMediaStreams also can't cause
|
|
|
|
// underrun currently, since we'll always be able to produce data for them
|
|
|
|
// unless they block on some other stream.
|
|
|
|
if (aStream->mFinished || aStream->AsProcessedStream()) {
|
2012-04-29 20:11:26 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GraphTime bufferEnd =
|
|
|
|
StreamTimeToGraphTime(aStream, aStream->GetBufferEnd(),
|
|
|
|
INCLUDE_TRAILING_BLOCKED_INTERVAL);
|
|
|
|
NS_ASSERTION(bufferEnd >= mCurrentTime, "Buffer underran");
|
|
|
|
// We should block after bufferEnd.
|
|
|
|
if (bufferEnd <= aTime) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p will block due to data underrun, "
|
|
|
|
"bufferEnd %f",
|
|
|
|
aStream, MediaTimeToSeconds(bufferEnd)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// We should keep blocking if we're currently blocked and we don't have
|
|
|
|
// data all the way through to aEndBlockingDecisions. If we don't have
|
|
|
|
// data all the way through to aEndBlockingDecisions, we'll block soon,
|
|
|
|
// but we might as well remain unblocked and play the data we've got while
|
|
|
|
// we can.
|
|
|
|
if (bufferEnd <= aEndBlockingDecisions && aStream->mBlocked.GetBefore(aTime)) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p will block due to speculative data underrun, "
|
|
|
|
"bufferEnd %f",
|
|
|
|
aStream, MediaTimeToSeconds(bufferEnd)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Reconsider decisions at bufferEnd
|
2013-01-15 04:22:03 -08:00
|
|
|
*aEnd = std::min(*aEnd, bufferEnd);
|
2012-04-29 20:11:26 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-28 21:34:17 -07:00
|
|
|
MediaStreamGraphImpl::MarkConsumed(MediaStream* aStream)
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
2012-10-28 21:34:17 -07:00
|
|
|
if (aStream->mIsConsumed) {
|
2012-07-31 05:17:21 -07:00
|
|
|
return;
|
2012-10-28 21:34:17 -07:00
|
|
|
}
|
|
|
|
aStream->mIsConsumed = true;
|
|
|
|
|
|
|
|
ProcessedMediaStream* ps = aStream->AsProcessedStream();
|
|
|
|
if (!ps) {
|
2012-07-31 05:17:21 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-10-28 21:34:17 -07:00
|
|
|
// Mark all the inputs to this stream as consumed
|
|
|
|
for (uint32_t i = 0; i < ps->mInputs.Length(); ++i) {
|
|
|
|
MarkConsumed(ps->mInputs[i]->mSource);
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UpdateStreamOrderForStream(nsTArray<MediaStream*>* aStack,
|
|
|
|
already_AddRefed<MediaStream> aStream)
|
|
|
|
{
|
|
|
|
nsRefPtr<MediaStream> stream = aStream;
|
|
|
|
NS_ASSERTION(!stream->mHasBeenOrdered, "stream should not have already been ordered");
|
|
|
|
if (stream->mIsOnOrderingStack) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = aStack->Length() - 1; ; --i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
aStack->ElementAt(i)->AsProcessedStream()->mInCycle = true;
|
|
|
|
if (aStack->ElementAt(i) == stream)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ProcessedMediaStream* ps = stream->AsProcessedStream();
|
|
|
|
if (ps) {
|
|
|
|
aStack->AppendElement(stream);
|
|
|
|
stream->mIsOnOrderingStack = true;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < ps->mInputs.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStream* source = ps->mInputs[i]->mSource;
|
|
|
|
if (!source->mHasBeenOrdered) {
|
|
|
|
nsRefPtr<MediaStream> s = source;
|
|
|
|
UpdateStreamOrderForStream(aStack, s.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aStack->RemoveElementAt(aStack->Length() - 1);
|
|
|
|
stream->mIsOnOrderingStack = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->mHasBeenOrdered = true;
|
|
|
|
*mStreams.AppendElement() = stream.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UpdateStreamOrder()
|
|
|
|
{
|
|
|
|
nsTArray<nsRefPtr<MediaStream> > oldStreams;
|
|
|
|
oldStreams.SwapElements(mStreams);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < oldStreams.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStream* stream = oldStreams[i];
|
|
|
|
stream->mHasBeenOrdered = false;
|
2012-08-29 04:20:45 -07:00
|
|
|
stream->mIsConsumed = false;
|
2012-07-31 05:17:21 -07:00
|
|
|
stream->mIsOnOrderingStack = false;
|
|
|
|
stream->mInBlockingSet = false;
|
|
|
|
ProcessedMediaStream* ps = stream->AsProcessedStream();
|
|
|
|
if (ps) {
|
|
|
|
ps->mInCycle = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<MediaStream*,10> stack;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < oldStreams.Length(); ++i) {
|
2012-10-28 21:34:17 -07:00
|
|
|
nsRefPtr<MediaStream>& s = oldStreams[i];
|
|
|
|
if (!s->mAudioOutputs.IsEmpty() || !s->mVideoOutputs.IsEmpty()) {
|
|
|
|
MarkConsumed(s);
|
|
|
|
}
|
|
|
|
if (!s->mHasBeenOrdered) {
|
|
|
|
UpdateStreamOrderForStream(&stack, s.forget());
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::RecomputeBlocking(GraphTime aEndBlockingDecisions)
|
|
|
|
{
|
|
|
|
bool blockingDecisionsWillChange = false;
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("Media graph %p computing blocking for time %f",
|
|
|
|
this, MediaTimeToSeconds(mStateComputedTime)));
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStream* stream = mStreams[i];
|
2012-07-31 05:17:21 -07:00
|
|
|
if (!stream->mInBlockingSet) {
|
|
|
|
// Compute a partition of the streams containing 'stream' such that we can
|
|
|
|
// compute the blocking status of each subset independently.
|
|
|
|
nsAutoTArray<MediaStream*,10> streamSet;
|
|
|
|
AddBlockingRelatedStreamsToSet(&streamSet, stream);
|
|
|
|
|
|
|
|
GraphTime end;
|
|
|
|
for (GraphTime t = mStateComputedTime;
|
|
|
|
t < aEndBlockingDecisions; t = end) {
|
|
|
|
end = GRAPH_TIME_MAX;
|
|
|
|
RecomputeBlockingAt(streamSet, t, aEndBlockingDecisions, &end);
|
|
|
|
if (end < GRAPH_TIME_MAX) {
|
|
|
|
blockingDecisionsWillChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
GraphTime end;
|
|
|
|
stream->mBlocked.GetAt(mCurrentTime, &end);
|
|
|
|
if (end < GRAPH_TIME_MAX) {
|
|
|
|
blockingDecisionsWillChange = true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Media graph %p computed blocking for interval %f to %f",
|
|
|
|
this, MediaTimeToSeconds(mStateComputedTime),
|
|
|
|
MediaTimeToSeconds(aEndBlockingDecisions)));
|
|
|
|
mStateComputedTime = aEndBlockingDecisions;
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
if (blockingDecisionsWillChange) {
|
|
|
|
// Make sure we wake up to notify listeners about these changes.
|
|
|
|
EnsureNextIteration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStreamGraphImpl::AddBlockingRelatedStreamsToSet(nsTArray<MediaStream*>* aStreams,
|
|
|
|
MediaStream* aStream)
|
|
|
|
{
|
|
|
|
if (aStream->mInBlockingSet)
|
|
|
|
return;
|
|
|
|
aStream->mInBlockingSet = true;
|
|
|
|
aStreams->AppendElement(aStream);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStream->mConsumers.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaInputPort* port = aStream->mConsumers[i];
|
|
|
|
if (port->mFlags & (MediaInputPort::FLAG_BLOCK_INPUT | MediaInputPort::FLAG_BLOCK_OUTPUT)) {
|
|
|
|
AddBlockingRelatedStreamsToSet(aStreams, port->mDest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProcessedMediaStream* ps = aStream->AsProcessedStream();
|
|
|
|
if (ps) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < ps->mInputs.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaInputPort* port = ps->mInputs[i];
|
|
|
|
if (port->mFlags & (MediaInputPort::FLAG_BLOCK_INPUT | MediaInputPort::FLAG_BLOCK_OUTPUT)) {
|
|
|
|
AddBlockingRelatedStreamsToSet(aStreams, port->mSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::MarkStreamBlocking(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
if (aStream->mBlockInThisPhase)
|
|
|
|
return;
|
|
|
|
aStream->mBlockInThisPhase = true;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStream->mConsumers.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaInputPort* port = aStream->mConsumers[i];
|
|
|
|
if (port->mFlags & MediaInputPort::FLAG_BLOCK_OUTPUT) {
|
|
|
|
MarkStreamBlocking(port->mDest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProcessedMediaStream* ps = aStream->AsProcessedStream();
|
|
|
|
if (ps) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < ps->mInputs.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaInputPort* port = ps->mInputs[i];
|
|
|
|
if (port->mFlags & MediaInputPort::FLAG_BLOCK_INPUT) {
|
|
|
|
MarkStreamBlocking(port->mSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::RecomputeBlockingAt(const nsTArray<MediaStream*>& aStreams,
|
|
|
|
GraphTime aTime,
|
2012-04-29 20:11:26 -07:00
|
|
|
GraphTime aEndBlockingDecisions,
|
|
|
|
GraphTime* aEnd)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStreams.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStream* stream = aStreams[i];
|
|
|
|
stream->mBlockInThisPhase = false;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStreams.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStream* stream = aStreams[i];
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
if (stream->mFinished) {
|
|
|
|
GraphTime endTime = StreamTimeToGraphTime(stream, stream->GetBufferEnd());
|
|
|
|
if (endTime <= aTime) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p is blocked due to being finished", stream));
|
2012-07-31 05:17:21 -07:00
|
|
|
// We'll block indefinitely
|
|
|
|
MarkStreamBlocking(stream);
|
|
|
|
*aEnd = aEndBlockingDecisions;
|
2012-04-29 20:11:26 -07:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p is finished, but not blocked yet (end at %f, with blocking at %f)",
|
|
|
|
stream, MediaTimeToSeconds(stream->GetBufferEnd()),
|
|
|
|
MediaTimeToSeconds(endTime)));
|
2013-01-15 04:22:03 -08:00
|
|
|
*aEnd = std::min(*aEnd, endTime);
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphTime end;
|
|
|
|
bool explicitBlock = stream->mExplicitBlockerCount.GetAt(aTime, &end) > 0;
|
2013-01-15 04:22:03 -08:00
|
|
|
*aEnd = std::min(*aEnd, end);
|
2012-04-29 20:11:26 -07:00
|
|
|
if (explicitBlock) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p is blocked due to explicit blocker", stream));
|
2012-07-31 05:17:21 -07:00
|
|
|
MarkStreamBlocking(stream);
|
2012-04-29 20:11:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool underrun = WillUnderrun(stream, aTime, aEndBlockingDecisions, aEnd);
|
|
|
|
if (underrun) {
|
2012-07-31 05:17:21 -07:00
|
|
|
// We'll block indefinitely
|
|
|
|
MarkStreamBlocking(stream);
|
|
|
|
*aEnd = aEndBlockingDecisions;
|
2012-04-29 20:11:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ASSERTION(*aEnd > aTime, "Failed to advance!");
|
2012-07-31 05:17:21 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStreams.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStream* stream = aStreams[i];
|
|
|
|
stream->mBlocked.SetAtAndAfter(aTime, stream->mBlockInThisPhase);
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
2012-09-19 17:47:51 -07:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::NotifyHasCurrentData(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
for (uint32_t j = 0; j < aStream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = aStream->mListeners[j];
|
|
|
|
l->NotifyHasCurrentData(this,
|
|
|
|
GraphTimeToStreamTime(aStream, mCurrentTime) < aStream->mBuffer.GetEnd());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
void
|
2012-07-31 05:17:22 -07:00
|
|
|
MediaStreamGraphImpl::CreateOrDestroyAudioStreams(GraphTime aAudioOutputStartTime,
|
|
|
|
MediaStream* aStream)
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
2012-07-31 05:17:22 -07:00
|
|
|
nsAutoTArray<bool,2> audioOutputStreamsFound;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStream->mAudioOutputStreams.Length(); ++i) {
|
2012-07-31 05:17:22 -07:00
|
|
|
audioOutputStreamsFound.AppendElement(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aStream->mAudioOutputs.IsEmpty()) {
|
|
|
|
for (StreamBuffer::TrackIter tracks(aStream->GetStreamBuffer(), MediaSegment::AUDIO);
|
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2012-07-31 05:17:22 -07:00
|
|
|
for (i = 0; i < audioOutputStreamsFound.Length(); ++i) {
|
|
|
|
if (aStream->mAudioOutputStreams[i].mTrackID == tracks->GetID()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < audioOutputStreamsFound.Length()) {
|
|
|
|
audioOutputStreamsFound[i] = true;
|
|
|
|
} else {
|
|
|
|
// No output stream created for this track yet. Check if it's time to
|
|
|
|
// create one.
|
|
|
|
GraphTime startTime =
|
|
|
|
StreamTimeToGraphTime(aStream, tracks->GetStartTimeRoundDown(),
|
|
|
|
INCLUDE_TRAILING_BLOCKED_INTERVAL);
|
|
|
|
if (startTime >= mStateComputedTime) {
|
|
|
|
// The stream wants to play audio, but nothing will play for the forseeable
|
|
|
|
// future, so don't create the stream.
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// XXX allocating a AudioStream could be slow so we're going to have to do
|
2012-07-31 05:17:22 -07:00
|
|
|
// something here ... preallocation, async allocation, multiplexing onto a single
|
|
|
|
// stream ...
|
|
|
|
MediaStream::AudioOutputStream* audioOutputStream =
|
|
|
|
aStream->mAudioOutputStreams.AppendElement();
|
|
|
|
audioOutputStream->mAudioPlaybackStartTime = aAudioOutputStartTime;
|
|
|
|
audioOutputStream->mBlockedAudioTime = 0;
|
2012-11-14 11:46:40 -08:00
|
|
|
audioOutputStream->mStream = AudioStream::AllocateStream();
|
2013-01-20 12:44:44 -08:00
|
|
|
// XXX for now, allocate stereo output. But we need to fix this to
|
|
|
|
// match the system's ideal channel configuration.
|
|
|
|
audioOutputStream->mStream->Init(2, tracks->GetRate(), AUDIO_CHANNEL_NORMAL);
|
2012-07-31 05:17:22 -07:00
|
|
|
audioOutputStream->mTrackID = tracks->GetID();
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = audioOutputStreamsFound.Length() - 1; i >= 0; --i) {
|
2012-07-31 05:17:22 -07:00
|
|
|
if (!audioOutputStreamsFound[i]) {
|
|
|
|
aStream->mAudioOutputStreams[i].mStream->Shutdown();
|
|
|
|
aStream->mAudioOutputStreams.RemoveElementAt(i);
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::PlayAudio(MediaStream* aStream,
|
|
|
|
GraphTime aFrom, GraphTime aTo)
|
|
|
|
{
|
2012-07-31 05:17:22 -07:00
|
|
|
if (aStream->mAudioOutputStreams.IsEmpty()) {
|
2012-04-29 20:11:26 -07:00
|
|
|
return;
|
2012-07-31 05:17:22 -07:00
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
// When we're playing multiple copies of this stream at the same time, they're
|
|
|
|
// perfectly correlated so adding volumes is the right thing to do.
|
|
|
|
float volume = 0.0f;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStream->mAudioOutputs.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
volume += aStream->mAudioOutputs[i].mVolume;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStream->mAudioOutputStreams.Length(); ++i) {
|
2012-07-31 05:17:22 -07:00
|
|
|
MediaStream::AudioOutputStream& audioOutput = aStream->mAudioOutputStreams[i];
|
|
|
|
StreamBuffer::Track* track = aStream->mBuffer.FindTrack(audioOutput.mTrackID);
|
|
|
|
AudioSegment* audio = track->Get<AudioSegment>();
|
2012-04-29 20:11:26 -07:00
|
|
|
|
2012-07-31 05:17:22 -07:00
|
|
|
// We don't update aStream->mBufferStartTime here to account for
|
|
|
|
// time spent blocked. Instead, we'll update it in UpdateCurrentTime after the
|
|
|
|
// blocked period has completed. But we do need to make sure we play from the
|
|
|
|
// right offsets in the stream buffer, even if we've already written silence for
|
|
|
|
// some amount of blocked time after the current time.
|
|
|
|
GraphTime t = aFrom;
|
|
|
|
while (t < aTo) {
|
|
|
|
GraphTime end;
|
|
|
|
bool blocked = aStream->mBlocked.GetAt(t, &end);
|
2013-01-15 04:22:03 -08:00
|
|
|
end = std::min(end, aTo);
|
2012-04-29 20:11:26 -07:00
|
|
|
|
2012-07-31 05:17:22 -07:00
|
|
|
AudioSegment output;
|
|
|
|
if (blocked) {
|
|
|
|
// Track total blocked time in aStream->mBlockedAudioTime so that
|
|
|
|
// the amount of silent samples we've inserted for blocking never gets
|
|
|
|
// more than one sample away from the ideal amount.
|
|
|
|
TrackTicks startTicks =
|
|
|
|
TimeToTicksRoundDown(track->GetRate(), audioOutput.mBlockedAudioTime);
|
|
|
|
audioOutput.mBlockedAudioTime += end - t;
|
|
|
|
TrackTicks endTicks =
|
|
|
|
TimeToTicksRoundDown(track->GetRate(), audioOutput.mBlockedAudioTime);
|
|
|
|
|
|
|
|
output.InsertNullDataAtStart(endTicks - startTicks);
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p writing blocking-silence samples for %f to %f",
|
2012-04-29 20:11:26 -07:00
|
|
|
aStream, MediaTimeToSeconds(t), MediaTimeToSeconds(end)));
|
2012-07-31 05:17:22 -07:00
|
|
|
} else {
|
|
|
|
TrackTicks startTicks =
|
|
|
|
track->TimeToTicksRoundDown(GraphTimeToStreamTime(aStream, t));
|
|
|
|
TrackTicks endTicks =
|
|
|
|
track->TimeToTicksRoundDown(GraphTimeToStreamTime(aStream, end));
|
|
|
|
|
|
|
|
// If startTicks is before the track start, then that part of 'audio'
|
|
|
|
// will just be silence, which is fine here. But if endTicks is after
|
|
|
|
// the track end, then 'audio' won't be long enough, so we'll need
|
|
|
|
// to explicitly play silence.
|
2013-01-15 04:22:03 -08:00
|
|
|
TrackTicks sliceEnd = std::min(endTicks, audio->GetDuration());
|
2012-07-31 05:17:22 -07:00
|
|
|
if (sliceEnd > startTicks) {
|
|
|
|
output.AppendSlice(*audio, startTicks, sliceEnd);
|
|
|
|
}
|
|
|
|
// Play silence where the track has ended
|
|
|
|
output.AppendNullData(endTicks - sliceEnd);
|
|
|
|
NS_ASSERTION(endTicks == sliceEnd || track->IsEnded(),
|
|
|
|
"Ran out of data but track not ended?");
|
|
|
|
output.ApplyVolume(volume);
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p writing samples for %f to %f (samples %lld to %lld)",
|
|
|
|
aStream, MediaTimeToSeconds(t), MediaTimeToSeconds(end),
|
|
|
|
startTicks, endTicks));
|
|
|
|
}
|
|
|
|
output.WriteTo(audioOutput.mStream);
|
|
|
|
t = end;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::PlayVideo(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
if (aStream->mVideoOutputs.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Display the next frame a bit early. This is better than letting the current
|
|
|
|
// frame be displayed for too long.
|
|
|
|
GraphTime framePosition = mCurrentTime + MEDIA_GRAPH_TARGET_PERIOD_MS;
|
|
|
|
NS_ASSERTION(framePosition >= aStream->mBufferStartTime, "frame position before buffer?");
|
|
|
|
StreamTime frameBufferTime = GraphTimeToStreamTime(aStream, framePosition);
|
|
|
|
|
2012-07-31 05:17:22 -07:00
|
|
|
TrackTicks start;
|
|
|
|
const VideoFrame* frame = nullptr;
|
|
|
|
StreamBuffer::Track* track;
|
|
|
|
for (StreamBuffer::TrackIter tracks(aStream->GetStreamBuffer(), MediaSegment::VIDEO);
|
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
|
|
|
VideoSegment* segment = tracks->Get<VideoSegment>();
|
|
|
|
TrackTicks thisStart;
|
|
|
|
const VideoFrame* thisFrame =
|
|
|
|
segment->GetFrameAt(tracks->TimeToTicksRoundDown(frameBufferTime), &thisStart);
|
|
|
|
if (thisFrame && thisFrame->GetImage()) {
|
|
|
|
start = thisStart;
|
|
|
|
frame = thisFrame;
|
|
|
|
track = tracks.get();
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
2012-07-31 05:17:22 -07:00
|
|
|
if (!frame || *frame == aStream->mLastPlayedVideoFrame)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStream %p writing video frame %p (%dx%d)",
|
|
|
|
aStream, frame->GetImage(), frame->GetIntrinsicSize().width,
|
|
|
|
frame->GetIntrinsicSize().height));
|
|
|
|
GraphTime startTime = StreamTimeToGraphTime(aStream,
|
|
|
|
track->TicksToTimeRoundDown(start), INCLUDE_TRAILING_BLOCKED_INTERVAL);
|
|
|
|
TimeStamp targetTime = mCurrentTimeStamp +
|
|
|
|
TimeDuration::FromMilliseconds(double(startTime - mCurrentTime));
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aStream->mVideoOutputs.Length(); ++i) {
|
2012-07-31 05:17:22 -07:00
|
|
|
VideoFrameContainer* output = aStream->mVideoOutputs[i];
|
|
|
|
output->SetCurrentFrame(frame->GetIntrinsicSize(), frame->GetImage(),
|
|
|
|
targetTime);
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(output, &VideoFrameContainer::Invalidate);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2012-11-20 17:32:06 -08:00
|
|
|
if (!aStream->mNotifiedFinished) {
|
|
|
|
aStream->mLastPlayedVideoFrame = *frame;
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::PrepareUpdatesToMainThreadState()
|
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStream* stream = mStreams[i];
|
|
|
|
StreamUpdate* update = mStreamUpdates.AppendElement();
|
|
|
|
update->mGraphUpdateIndex = stream->mGraphUpdateIndices.GetAt(mCurrentTime);
|
|
|
|
update->mStream = stream;
|
|
|
|
update->mNextMainThreadCurrentTime =
|
|
|
|
GraphTimeToStreamTime(stream, mCurrentTime);
|
|
|
|
update->mNextMainThreadFinished =
|
|
|
|
stream->mFinished &&
|
|
|
|
StreamTimeToGraphTime(stream, stream->GetBufferEnd()) <= mCurrentTime;
|
|
|
|
}
|
|
|
|
mUpdateRunnables.MoveElementsFrom(mPendingUpdateRunnables);
|
|
|
|
|
|
|
|
EnsureStableStateEventPosted();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::EnsureImmediateWakeUpLocked(MonitorAutoLock& aLock)
|
|
|
|
{
|
|
|
|
if (mWaitState == WAITSTATE_WAITING_FOR_NEXT_ITERATION ||
|
|
|
|
mWaitState == WAITSTATE_WAITING_INDEFINITELY) {
|
|
|
|
mWaitState = WAITSTATE_WAKING_UP;
|
|
|
|
aLock.Notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::EnsureNextIteration()
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
EnsureNextIterationLocked(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::EnsureNextIterationLocked(MonitorAutoLock& aLock)
|
|
|
|
{
|
|
|
|
if (mNeedAnotherIteration)
|
|
|
|
return;
|
|
|
|
mNeedAnotherIteration = true;
|
|
|
|
if (mWaitState == WAITSTATE_WAITING_INDEFINITELY) {
|
|
|
|
mWaitState = WAITSTATE_WAKING_UP;
|
|
|
|
aLock.Notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-13 14:46:57 -08:00
|
|
|
static GraphTime
|
|
|
|
RoundUpToAudioBlock(GraphTime aTime)
|
|
|
|
{
|
|
|
|
TrackRate rate = IdealAudioRate();
|
|
|
|
int64_t ticksAtIdealRate = (aTime*rate) >> MEDIA_TIME_FRAC_BITS;
|
|
|
|
// Round up to nearest block boundary
|
|
|
|
int64_t blocksAtIdealRate =
|
|
|
|
(ticksAtIdealRate + (WEBAUDIO_BLOCK_SIZE - 1)) >>
|
|
|
|
WEBAUDIO_BLOCK_SIZE_BITS;
|
|
|
|
// Round up to nearest MediaTime unit
|
|
|
|
return
|
|
|
|
((((blocksAtIdealRate + 1)*WEBAUDIO_BLOCK_SIZE) << MEDIA_TIME_FRAC_BITS)
|
|
|
|
+ rate - 1)/rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ProduceDataForStreamsBlockByBlock(uint32_t aStreamIndex,
|
|
|
|
GraphTime aFrom,
|
|
|
|
GraphTime aTo)
|
|
|
|
{
|
|
|
|
GraphTime t = aFrom;
|
|
|
|
while (t < aTo) {
|
|
|
|
GraphTime next = RoundUpToAudioBlock(t + 1);
|
|
|
|
for (uint32_t i = aStreamIndex; i < mStreams.Length(); ++i) {
|
|
|
|
ProcessedMediaStream* ps = mStreams[i]->AsProcessedStream();
|
|
|
|
if (ps) {
|
|
|
|
ps->ProduceOutput(t, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t = next;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(t == aTo, "Something went wrong with rounding to block boundaries");
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::RunThread()
|
|
|
|
{
|
|
|
|
nsTArray<MessageBlock> messageQueue;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
messageQueue.SwapElements(mMessageQueue);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(!messageQueue.IsEmpty(),
|
|
|
|
"Shouldn't have started a graph with empty message queue!");
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Update mCurrentTime to the min of the playing audio times, or using the
|
|
|
|
// wall-clock time change if no audio is playing.
|
|
|
|
UpdateCurrentTime();
|
|
|
|
|
|
|
|
// Calculate independent action times for each batch of messages (each
|
|
|
|
// batch corresponding to an event loop task). This isolates the performance
|
|
|
|
// of different scripts to some extent.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < messageQueue.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
mProcessingGraphUpdateIndex = messageQueue[i].mGraphUpdateIndex;
|
|
|
|
nsTArray<nsAutoPtr<ControlMessage> >& messages = messageQueue[i].mMessages;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < messages.Length(); ++j) {
|
2012-07-31 05:17:21 -07:00
|
|
|
messages[j]->Run();
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
messageQueue.Clear();
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
UpdateStreamOrder();
|
|
|
|
|
2012-07-20 12:36:03 -07:00
|
|
|
GraphTime endBlockingDecisions =
|
2013-01-13 14:46:57 -08:00
|
|
|
RoundUpToAudioBlock(mCurrentTime + MillisecondsToMediaTime(AUDIO_TARGET_MS));
|
2012-07-20 12:36:03 -07:00
|
|
|
bool ensureNextIteration = false;
|
2012-07-31 05:17:21 -07:00
|
|
|
|
|
|
|
// Grab pending stream input.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-04-29 20:11:40 -07:00
|
|
|
SourceMediaStream* is = mStreams[i]->AsSourceStream();
|
|
|
|
if (is) {
|
2012-05-31 23:26:17 -07:00
|
|
|
UpdateConsumptionState(is);
|
2012-07-20 12:36:03 -07:00
|
|
|
ExtractPendingInput(is, endBlockingDecisions, &ensureNextIteration);
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
// Figure out which streams are blocked and when.
|
2012-07-31 05:17:21 -07:00
|
|
|
GraphTime prevComputedTime = mStateComputedTime;
|
2012-07-20 12:36:03 -07:00
|
|
|
RecomputeBlocking(endBlockingDecisions);
|
2012-04-29 20:11:26 -07:00
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
// Play stream contents.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t audioStreamsActive = 0;
|
2012-04-29 20:11:26 -07:00
|
|
|
bool allBlockedForever = true;
|
2013-01-13 14:46:57 -08:00
|
|
|
// True when we've done ProduceOutput for all processed streams.
|
|
|
|
bool doneAllProducing = false;
|
2012-04-29 20:11:26 -07:00
|
|
|
// Figure out what each stream wants to do
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStream* stream = mStreams[i];
|
2013-01-13 14:46:57 -08:00
|
|
|
if (!doneAllProducing && !stream->IsFinishedOnGraphThread()) {
|
|
|
|
ProcessedMediaStream* ps = stream->AsProcessedStream();
|
|
|
|
if (ps) {
|
|
|
|
AudioNodeStream* n = stream->AsAudioNodeStream();
|
|
|
|
if (n) {
|
|
|
|
// Since an AudioNodeStream is present, go ahead and
|
|
|
|
// produce audio block by block for all the rest of the streams.
|
|
|
|
ProduceDataForStreamsBlockByBlock(i, prevComputedTime, mStateComputedTime);
|
|
|
|
doneAllProducing = true;
|
|
|
|
} else {
|
|
|
|
ps->ProduceOutput(prevComputedTime, mStateComputedTime);
|
|
|
|
NS_ASSERTION(stream->mBuffer.GetEnd() >=
|
|
|
|
GraphTimeToStreamTime(stream, mStateComputedTime),
|
|
|
|
"Stream did not produce enough data");
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
2012-09-19 17:47:51 -07:00
|
|
|
NotifyHasCurrentData(stream);
|
2012-07-31 05:17:22 -07:00
|
|
|
CreateOrDestroyAudioStreams(prevComputedTime, stream);
|
2012-07-31 05:17:21 -07:00
|
|
|
PlayAudio(stream, prevComputedTime, mStateComputedTime);
|
2012-07-31 05:17:22 -07:00
|
|
|
audioStreamsActive += stream->mAudioOutputStreams.Length();
|
2012-04-29 20:11:26 -07:00
|
|
|
PlayVideo(stream);
|
2012-04-29 20:11:40 -07:00
|
|
|
SourceMediaStream* is = stream->AsSourceStream();
|
|
|
|
if (is) {
|
|
|
|
UpdateBufferSufficiencyState(is);
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
GraphTime end;
|
|
|
|
if (!stream->mBlocked.GetAt(mCurrentTime, &end) || end < GRAPH_TIME_MAX) {
|
|
|
|
allBlockedForever = false;
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 12:36:03 -07:00
|
|
|
if (ensureNextIteration || !allBlockedForever || audioStreamsActive > 0) {
|
2012-04-29 20:11:26 -07:00
|
|
|
EnsureNextIteration();
|
|
|
|
}
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
// Send updates to the main thread and wait for the next control loop
|
|
|
|
// iteration.
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
2012-07-17 22:02:06 -07:00
|
|
|
// Not using MonitorAutoLock since we need to unlock in a way
|
|
|
|
// that doesn't match lexical scopes.
|
2012-08-09 04:29:47 -07:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-04-29 20:11:26 -07:00
|
|
|
PrepareUpdatesToMainThreadState();
|
|
|
|
if (mForceShutDown || (IsEmpty() && mMessageQueue.IsEmpty())) {
|
|
|
|
// Enter shutdown mode. The stable-state handler will detect this
|
|
|
|
// and complete shutdown. Destroy any streams immediately.
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStreamGraph %p waiting for main thread cleanup", this));
|
2012-07-17 22:02:06 -07:00
|
|
|
// Commit to shutting down this graph object.
|
2012-04-29 20:11:26 -07:00
|
|
|
mLifecycleState = LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP;
|
2012-08-09 04:29:47 -07:00
|
|
|
// No need to Destroy streams here. The main-thread owner of each
|
|
|
|
// stream is responsible for calling Destroy them.
|
2012-04-29 20:11:26 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT;
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
if (mNeedAnotherIteration) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t timeoutMS = MEDIA_GRAPH_TARGET_PERIOD_MS -
|
|
|
|
int64_t((now - mCurrentTimeStamp).ToMilliseconds());
|
2012-04-29 20:11:26 -07:00
|
|
|
// Make sure timeoutMS doesn't overflow 32 bits by waking up at
|
|
|
|
// least once a minute, if we need to wake up at all
|
2013-01-15 04:22:03 -08:00
|
|
|
timeoutMS = std::max<int64_t>(0, std::min<int64_t>(timeoutMS, 60*1000));
|
2012-08-22 08:56:38 -07:00
|
|
|
timeout = PR_MillisecondsToInterval(uint32_t(timeoutMS));
|
2012-04-29 20:11:26 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Waiting for next iteration; at %f, timeout=%f",
|
|
|
|
(now - mInitialTimeStamp).ToSeconds(), timeoutMS/1000.0));
|
|
|
|
mWaitState = WAITSTATE_WAITING_FOR_NEXT_ITERATION;
|
|
|
|
} else {
|
|
|
|
mWaitState = WAITSTATE_WAITING_INDEFINITELY;
|
|
|
|
}
|
|
|
|
if (timeout > 0) {
|
2012-07-17 22:02:06 -07:00
|
|
|
mMonitor.Wait(timeout);
|
2012-04-29 20:11:26 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Resuming after timeout; at %f, elapsed=%f",
|
|
|
|
(TimeStamp::Now() - mInitialTimeStamp).ToSeconds(),
|
|
|
|
(TimeStamp::Now() - now).ToSeconds()));
|
|
|
|
}
|
|
|
|
mWaitState = WAITSTATE_RUNNING;
|
|
|
|
mNeedAnotherIteration = false;
|
|
|
|
messageQueue.SwapElements(mMessageQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ApplyStreamUpdate(StreamUpdate* aUpdate)
|
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
MediaStream* stream = aUpdate->mStream;
|
|
|
|
if (!stream)
|
|
|
|
return;
|
|
|
|
stream->mMainThreadCurrentTime = aUpdate->mNextMainThreadCurrentTime;
|
|
|
|
stream->mMainThreadFinished = aUpdate->mNextMainThreadFinished;
|
2012-08-19 21:20:44 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = stream->mMainThreadListeners.Length() - 1; i >= 0; --i) {
|
2012-08-19 21:20:44 -07:00
|
|
|
stream->mMainThreadListeners[i]->NotifyMainThreadStateChanged();
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ShutdownThreads()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
|
|
|
|
// mGraph's thread is not running so it's OK to do whatever here
|
|
|
|
LOG(PR_LOG_DEBUG, ("Stopping threads for MediaStreamGraph %p", this));
|
|
|
|
|
|
|
|
if (mThread) {
|
|
|
|
mThread->Shutdown();
|
2012-07-30 07:20:58 -07:00
|
|
|
mThread = nullptr;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ForceShutDown()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
|
|
|
|
LOG(PR_LOG_DEBUG, ("MediaStreamGraph %p ForceShutdown", this));
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mForceShutDown = true;
|
|
|
|
EnsureImmediateWakeUpLocked(lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MediaStreamGraphThreadRunnable : public nsRunnable {
|
|
|
|
public:
|
2013-02-01 12:13:32 -08:00
|
|
|
explicit MediaStreamGraphThreadRunnable(MediaStreamGraphImpl* aGraph)
|
|
|
|
: mGraph(aGraph)
|
|
|
|
{
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-02-01 12:13:32 -08:00
|
|
|
mGraph->RunThread();
|
2012-04-29 20:11:26 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-01 12:13:32 -08:00
|
|
|
private:
|
|
|
|
MediaStreamGraphImpl* mGraph;
|
2012-04-29 20:11:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class MediaStreamGraphShutDownRunnable : public nsRunnable {
|
|
|
|
public:
|
|
|
|
MediaStreamGraphShutDownRunnable(MediaStreamGraphImpl* aGraph) : mGraph(aGraph) {}
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mGraph->mDetectedNotRunning,
|
|
|
|
"We should know the graph thread control loop isn't running!");
|
|
|
|
// mGraph's thread is not running so it's OK to do whatever here
|
|
|
|
if (mGraph->IsEmpty()) {
|
|
|
|
// mGraph is no longer needed, so delete it. If the graph is not empty
|
|
|
|
// then we must be in a forced shutdown and some later AppendMessage will
|
|
|
|
// detect that the manager has been emptied, and delete it.
|
|
|
|
delete mGraph;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(mGraph->mForceShutDown, "Not in forced shutdown?");
|
|
|
|
mGraph->mLifecycleState =
|
|
|
|
MediaStreamGraphImpl::LIFECYCLE_WAITING_FOR_STREAM_DESTRUCTION;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
MediaStreamGraphImpl* mGraph;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MediaStreamGraphStableStateRunnable : public nsRunnable {
|
|
|
|
public:
|
2013-02-01 12:20:32 -08:00
|
|
|
explicit MediaStreamGraphStableStateRunnable(MediaStreamGraphImpl* aGraph)
|
|
|
|
: mGraph(aGraph)
|
|
|
|
{
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-02-01 12:20:32 -08:00
|
|
|
if (mGraph) {
|
|
|
|
mGraph->RunInStableState();
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-01 12:20:32 -08:00
|
|
|
private:
|
|
|
|
MediaStreamGraphImpl* mGraph;
|
2012-04-29 20:11:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Control messages forwarded from main thread to graph manager thread
|
|
|
|
*/
|
|
|
|
class CreateMessage : public ControlMessage {
|
|
|
|
public:
|
|
|
|
CreateMessage(MediaStream* aStream) : ControlMessage(aStream) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->GraphImpl()->AddStream(mStream);
|
|
|
|
mStream->Init();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-18 19:30:09 -07:00
|
|
|
class MediaStreamGraphShutdownObserver MOZ_FINAL : public nsIObserver
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::RunInStableState()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable> > runnables;
|
2012-07-31 05:17:22 -07:00
|
|
|
// When we're doing a forced shutdown, pending control messages may be
|
|
|
|
// run on the main thread via RunDuringShutdown. Those messages must
|
|
|
|
// run without the graph monitor being held. So, we collect them here.
|
|
|
|
nsTArray<nsAutoPtr<ControlMessage> > controlMessagesToRunDuringShutdown;
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mPostedRunInStableStateEvent = false;
|
|
|
|
|
|
|
|
runnables.SwapElements(mUpdateRunnables);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mStreamUpdates.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
StreamUpdate* update = &mStreamUpdates[i];
|
|
|
|
if (update->mStream) {
|
|
|
|
ApplyStreamUpdate(update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mStreamUpdates.Clear();
|
|
|
|
|
|
|
|
if (mLifecycleState == LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP && mForceShutDown) {
|
2012-07-31 05:17:22 -07:00
|
|
|
// Defer calls to RunDuringShutdown() to happen while mMonitor is not held.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mMessageQueue.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
MessageBlock& mb = mMessageQueue[i];
|
2012-07-31 05:17:22 -07:00
|
|
|
controlMessagesToRunDuringShutdown.MoveElementsFrom(mb.mMessages);
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
mMessageQueue.Clear();
|
2012-07-31 05:17:22 -07:00
|
|
|
controlMessagesToRunDuringShutdown.MoveElementsFrom(mCurrentTaskMessageQueue);
|
2012-04-29 20:11:26 -07:00
|
|
|
// Stop MediaStreamGraph threads. Do not clear gGraph since
|
|
|
|
// we have outstanding DOM objects that may need it.
|
|
|
|
mLifecycleState = LIFECYCLE_WAITING_FOR_THREAD_SHUTDOWN;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphShutDownRunnable(this);
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLifecycleState == LIFECYCLE_THREAD_NOT_STARTED) {
|
|
|
|
mLifecycleState = LIFECYCLE_RUNNING;
|
|
|
|
// Start the thread now. We couldn't start it earlier because
|
|
|
|
// the graph might exit immediately on finding it has no streams. The
|
|
|
|
// first message for a new graph must create a stream.
|
2013-02-01 12:13:32 -08:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphThreadRunnable(this);
|
2012-04-29 20:11:26 -07:00
|
|
|
NS_NewThread(getter_AddRefs(mThread), event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrentTaskMessageQueue.IsEmpty()) {
|
|
|
|
if (mLifecycleState == LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP && IsEmpty()) {
|
|
|
|
// Complete shutdown. First, ensure that this graph is no longer used.
|
|
|
|
// A new graph graph will be created if one is needed.
|
2013-02-04 09:27:54 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Disconnecting MediaStreamGraph %p", this));
|
|
|
|
if (this == gGraph) {
|
|
|
|
// null out gGraph if that's the graph being shut down
|
|
|
|
gGraph = nullptr;
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
// Asynchronously clean up old graph. We don't want to do this
|
|
|
|
// synchronously because it spins the event loop waiting for threads
|
|
|
|
// to shut down, and we don't want to do that in a stable state handler.
|
|
|
|
mLifecycleState = LIFECYCLE_WAITING_FOR_THREAD_SHUTDOWN;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphShutDownRunnable(this);
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mLifecycleState <= LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP) {
|
|
|
|
MessageBlock* block = mMessageQueue.AppendElement();
|
|
|
|
block->mMessages.SwapElements(mCurrentTaskMessageQueue);
|
|
|
|
block->mGraphUpdateIndex = mGraphUpdatesSent;
|
|
|
|
++mGraphUpdatesSent;
|
|
|
|
EnsureNextIterationLocked(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLifecycleState == LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP) {
|
|
|
|
mLifecycleState = LIFECYCLE_RUNNING;
|
|
|
|
// Revive the MediaStreamGraph since we have more messages going to it.
|
|
|
|
// Note that we need to put messages into its queue before reviving it,
|
|
|
|
// or it might exit immediately.
|
2013-02-01 12:13:32 -08:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphThreadRunnable(this);
|
2012-04-29 20:11:26 -07:00
|
|
|
mThread->Dispatch(event, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDetectedNotRunning = mLifecycleState > LIFECYCLE_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we get a new current time in the next event loop task
|
|
|
|
mPostedRunInStableState = false;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < runnables.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
runnables[i]->Run();
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < controlMessagesToRunDuringShutdown.Length(); ++i) {
|
2012-07-31 05:17:22 -07:00
|
|
|
controlMessagesToRunDuringShutdown[i]->RunDuringShutdown();
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::EnsureRunInStableState()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "main thread only");
|
|
|
|
|
|
|
|
if (mPostedRunInStableState)
|
|
|
|
return;
|
|
|
|
mPostedRunInStableState = true;
|
2013-02-01 12:20:32 -08:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphStableStateRunnable(this);
|
2012-04-29 20:11:26 -07:00
|
|
|
nsCOMPtr<nsIAppShell> appShell = do_GetService(kAppShellCID);
|
|
|
|
if (appShell) {
|
|
|
|
appShell->RunInStableState(event);
|
|
|
|
} else {
|
|
|
|
NS_ERROR("Appshell already destroyed?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::EnsureStableStateEventPosted()
|
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (mPostedRunInStableStateEvent)
|
|
|
|
return;
|
|
|
|
mPostedRunInStableStateEvent = true;
|
2013-02-01 12:20:32 -08:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphStableStateRunnable(this);
|
2012-04-29 20:11:26 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::AppendMessage(ControlMessage* aMessage)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "main thread only");
|
2012-08-09 04:30:09 -07:00
|
|
|
NS_ASSERTION(!aMessage->GetStream() ||
|
|
|
|
!aMessage->GetStream()->IsDestroyed(),
|
|
|
|
"Stream already destroyed");
|
2012-04-29 20:11:26 -07:00
|
|
|
|
|
|
|
if (mDetectedNotRunning &&
|
|
|
|
mLifecycleState > LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP) {
|
|
|
|
// The graph control loop is not running and main thread cleanup has
|
|
|
|
// happened. From now on we can't append messages to mCurrentTaskMessageQueue,
|
2012-07-31 05:17:21 -07:00
|
|
|
// because that will never be processed again, so just RunDuringShutdown
|
2012-04-29 20:11:26 -07:00
|
|
|
// this message.
|
|
|
|
// This should only happen during forced shutdown.
|
2012-07-31 05:17:21 -07:00
|
|
|
aMessage->RunDuringShutdown();
|
2012-04-29 20:11:26 -07:00
|
|
|
delete aMessage;
|
|
|
|
if (IsEmpty()) {
|
2013-02-04 09:29:14 -08:00
|
|
|
if (gGraph == this) {
|
|
|
|
gGraph = nullptr;
|
|
|
|
delete this;
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentTaskMessageQueue.AppendElement(aMessage);
|
|
|
|
EnsureRunInStableState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::Init()
|
|
|
|
{
|
|
|
|
MediaStreamGraphImpl* graph = GraphImpl();
|
|
|
|
mBlocked.SetAtAndAfter(graph->mCurrentTime, true);
|
|
|
|
mExplicitBlockerCount.SetAtAndAfter(graph->mCurrentTime, true);
|
2012-07-31 05:17:21 -07:00
|
|
|
mExplicitBlockerCount.SetAtAndAfter(graph->mStateComputedTime, false);
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamGraphImpl*
|
|
|
|
MediaStream::GraphImpl()
|
|
|
|
{
|
2013-02-01 11:43:36 -08:00
|
|
|
return mGraph;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStreamGraph*
|
|
|
|
MediaStream::Graph()
|
|
|
|
{
|
2013-02-01 11:43:36 -08:00
|
|
|
return mGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::SetGraphImpl(MediaStreamGraphImpl* aGraph)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mGraph, "Should only be called once");
|
|
|
|
mGraph = aGraph;
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamTime
|
|
|
|
MediaStream::GraphTimeToStreamTime(GraphTime aTime)
|
|
|
|
{
|
|
|
|
return GraphImpl()->GraphTimeToStreamTime(this, aTime);
|
|
|
|
}
|
|
|
|
|
2013-02-04 02:04:26 -08:00
|
|
|
StreamTime
|
|
|
|
MediaStream::GraphTimeToStreamTimeOptimistic(GraphTime aTime)
|
|
|
|
{
|
|
|
|
return GraphImpl()->GraphTimeToStreamTimeOptimistic(this, aTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphTime
|
|
|
|
MediaStream::StreamTimeToGraphTime(StreamTime aTime)
|
|
|
|
{
|
|
|
|
return GraphImpl()->StreamTimeToGraphTime(this, aTime, 0);
|
|
|
|
}
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
void
|
|
|
|
MediaStream::FinishOnGraphThread()
|
|
|
|
{
|
|
|
|
GraphImpl()->FinishStream(this);
|
|
|
|
}
|
|
|
|
|
2013-01-06 18:31:30 -08:00
|
|
|
void
|
|
|
|
MediaStream::RemoveAllListenersImpl()
|
|
|
|
{
|
|
|
|
for (int32_t i = mListeners.Length() - 1; i >= 0; --i) {
|
|
|
|
nsRefPtr<MediaStreamListener> listener = mListeners[i].forget();
|
|
|
|
listener->NotifyRemoved(GraphImpl());
|
|
|
|
}
|
|
|
|
mListeners.Clear();
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
void
|
|
|
|
MediaStream::DestroyImpl()
|
|
|
|
{
|
2013-01-06 18:31:30 -08:00
|
|
|
RemoveAllListenersImpl();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = mConsumers.Length() - 1; i >= 0; --i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
mConsumers[i]->Disconnect();
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAudioOutputStreams.Length(); ++i) {
|
2012-07-31 05:17:22 -07:00
|
|
|
mAudioOutputStreams[i].mStream->Shutdown();
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
2012-07-31 05:17:22 -07:00
|
|
|
mAudioOutputStreams.Clear();
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::Destroy()
|
|
|
|
{
|
2012-10-25 18:39:05 -07:00
|
|
|
// Keep this stream alive until we leave this method
|
|
|
|
nsRefPtr<MediaStream> kungFuDeathGrip = this;
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream) : ControlMessage(aStream) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->DestroyImpl();
|
|
|
|
mStream->GraphImpl()->RemoveStream(mStream);
|
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void RunDuringShutdown()
|
|
|
|
{ Run(); }
|
2012-04-29 20:11:26 -07:00
|
|
|
};
|
2012-07-30 07:20:58 -07:00
|
|
|
mWrapper = nullptr;
|
2012-10-25 15:07:59 -07:00
|
|
|
GraphImpl()->AppendMessage(new Message(this));
|
2012-10-25 18:39:05 -07:00
|
|
|
// Message::RunDuringShutdown may have removed this stream from the graph,
|
|
|
|
// but our kungFuDeathGrip above will have kept this stream alive if
|
|
|
|
// necessary.
|
2012-10-25 16:08:38 -07:00
|
|
|
mMainThreadDestroyed = true;
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::AddAudioOutput(void* aKey)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, void* aKey) : ControlMessage(aStream), mKey(aKey) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->AddAudioOutputImpl(mKey);
|
|
|
|
}
|
|
|
|
void* mKey;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::SetAudioOutputVolumeImpl(void* aKey, float aVolume)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAudioOutputs.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
if (mAudioOutputs[i].mKey == aKey) {
|
|
|
|
mAudioOutputs[i].mVolume = aVolume;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ERROR("Audio output key not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::SetAudioOutputVolume(void* aKey, float aVolume)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, void* aKey, float aVolume) :
|
|
|
|
ControlMessage(aStream), mKey(aKey), mVolume(aVolume) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->SetAudioOutputVolumeImpl(mKey, mVolume);
|
|
|
|
}
|
|
|
|
void* mKey;
|
|
|
|
float mVolume;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aKey, aVolume));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveAudioOutputImpl(void* aKey)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAudioOutputs.Length(); ++i) {
|
2012-04-29 20:11:26 -07:00
|
|
|
if (mAudioOutputs[i].mKey == aKey) {
|
|
|
|
mAudioOutputs.RemoveElementAt(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ERROR("Audio output key not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveAudioOutput(void* aKey)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, void* aKey) :
|
|
|
|
ControlMessage(aStream), mKey(aKey) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->RemoveAudioOutputImpl(mKey);
|
|
|
|
}
|
|
|
|
void* mKey;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::AddVideoOutput(VideoFrameContainer* aContainer)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, VideoFrameContainer* aContainer) :
|
|
|
|
ControlMessage(aStream), mContainer(aContainer) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->AddVideoOutputImpl(mContainer.forget());
|
|
|
|
}
|
|
|
|
nsRefPtr<VideoFrameContainer> mContainer;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aContainer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveVideoOutput(VideoFrameContainer* aContainer)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, VideoFrameContainer* aContainer) :
|
|
|
|
ControlMessage(aStream), mContainer(aContainer) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->RemoveVideoOutputImpl(mContainer);
|
|
|
|
}
|
|
|
|
nsRefPtr<VideoFrameContainer> mContainer;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aContainer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
MediaStream::ChangeExplicitBlockerCount(int32_t aDelta)
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2012-08-22 08:56:38 -07:00
|
|
|
Message(MediaStream* aStream, int32_t aDelta) :
|
2012-04-29 20:11:26 -07:00
|
|
|
ControlMessage(aStream), mDelta(aDelta) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->ChangeExplicitBlockerCountImpl(
|
2012-07-31 05:17:21 -07:00
|
|
|
mStream->GraphImpl()->mStateComputedTime, mDelta);
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mDelta;
|
2012-04-29 20:11:26 -07:00
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aDelta));
|
|
|
|
}
|
|
|
|
|
2012-05-24 03:37:14 -07:00
|
|
|
void
|
|
|
|
MediaStream::AddListenerImpl(already_AddRefed<MediaStreamListener> aListener)
|
|
|
|
{
|
|
|
|
MediaStreamListener* listener = *mListeners.AppendElement() = aListener;
|
|
|
|
listener->NotifyBlockingChanged(GraphImpl(),
|
2013-01-02 05:49:18 -08:00
|
|
|
mNotifiedBlocked ? MediaStreamListener::BLOCKED : MediaStreamListener::UNBLOCKED);
|
2012-05-24 03:37:14 -07:00
|
|
|
if (mNotifiedFinished) {
|
|
|
|
listener->NotifyFinished(GraphImpl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
void
|
|
|
|
MediaStream::AddListener(MediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamListener* aListener) :
|
|
|
|
ControlMessage(aStream), mListener(aListener) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->AddListenerImpl(mListener.forget());
|
|
|
|
}
|
|
|
|
nsRefPtr<MediaStreamListener> mListener;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aListener));
|
|
|
|
}
|
|
|
|
|
2013-01-06 18:31:30 -08:00
|
|
|
void
|
|
|
|
MediaStream::RemoveListenerImpl(MediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
// wouldn't need this if we could do it in the opposite order
|
|
|
|
nsRefPtr<MediaStreamListener> listener(aListener);
|
|
|
|
mListeners.RemoveElement(aListener);
|
|
|
|
listener->NotifyRemoved(GraphImpl());
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
void
|
|
|
|
MediaStream::RemoveListener(MediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamListener* aListener) :
|
|
|
|
ControlMessage(aStream), mListener(aListener) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
2012-04-29 20:11:26 -07:00
|
|
|
{
|
|
|
|
mStream->RemoveListenerImpl(mListener);
|
|
|
|
}
|
|
|
|
nsRefPtr<MediaStreamListener> mListener;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aListener));
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:40 -07:00
|
|
|
void
|
2012-05-22 23:01:15 -07:00
|
|
|
SourceMediaStream::DestroyImpl()
|
2012-04-29 20:11:40 -07:00
|
|
|
{
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2012-05-22 23:01:15 -07:00
|
|
|
mDestroyed = true;
|
|
|
|
}
|
|
|
|
MediaStream::DestroyImpl();
|
|
|
|
}
|
|
|
|
|
2012-07-20 12:36:03 -07:00
|
|
|
void
|
|
|
|
SourceMediaStream::SetPullEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mPullEnabled = aEnabled;
|
|
|
|
if (mPullEnabled && !mDestroyed) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 23:01:15 -07:00
|
|
|
void
|
|
|
|
SourceMediaStream::AddTrack(TrackID aID, TrackRate aRate, TrackTicks aStart,
|
|
|
|
MediaSegment* aSegment)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
TrackData* data = mUpdateTracks.AppendElement();
|
|
|
|
data->mID = aID;
|
|
|
|
data->mRate = aRate;
|
|
|
|
data->mStart = aStart;
|
|
|
|
data->mCommands = TRACK_CREATE;
|
|
|
|
data->mData = aSegment;
|
|
|
|
data->mHaveEnough = false;
|
|
|
|
if (!mDestroyed) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
|
|
|
}
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
|
|
|
|
2013-02-25 01:25:07 -08:00
|
|
|
bool
|
2012-04-29 20:11:40 -07:00
|
|
|
SourceMediaStream::AppendToTrack(TrackID aID, MediaSegment* aSegment)
|
|
|
|
{
|
2012-05-22 23:01:15 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2012-10-24 16:21:32 -07:00
|
|
|
// ::EndAllTrackAndFinished() can end these before the sources notice
|
2013-02-25 01:25:07 -08:00
|
|
|
bool appended = false;
|
2012-10-24 16:21:32 -07:00
|
|
|
if (!mFinished) {
|
|
|
|
TrackData *track = FindDataForTrack(aID);
|
|
|
|
if (track) {
|
|
|
|
track->mData->AppendFrom(aSegment);
|
2013-02-25 01:25:07 -08:00
|
|
|
appended = true;
|
2012-10-24 16:21:32 -07:00
|
|
|
} else {
|
2013-02-25 01:25:07 -08:00
|
|
|
aSegment->Clear();
|
|
|
|
}
|
2012-05-22 23:01:15 -07:00
|
|
|
}
|
|
|
|
if (!mDestroyed) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
2013-02-25 01:25:07 -08:00
|
|
|
return appended;
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourceMediaStream::HaveEnoughBuffered(TrackID aID)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2012-05-08 22:53:49 -07:00
|
|
|
TrackData *track = FindDataForTrack(aID);
|
|
|
|
if (track) {
|
|
|
|
return track->mHaveEnough;
|
|
|
|
}
|
2013-02-25 01:25:07 -08:00
|
|
|
return false;
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::DispatchWhenNotEnoughBuffered(TrackID aID,
|
|
|
|
nsIThread* aSignalThread, nsIRunnable* aSignalRunnable)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
TrackData* data = FindDataForTrack(aID);
|
2012-05-08 22:53:49 -07:00
|
|
|
if (!data) {
|
2013-02-25 01:25:07 -08:00
|
|
|
aSignalThread->Dispatch(aSignalRunnable, 0);
|
2012-05-08 22:53:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:40 -07:00
|
|
|
if (data->mHaveEnough) {
|
|
|
|
data->mDispatchWhenNotEnough.AppendElement()->Init(aSignalThread, aSignalRunnable);
|
|
|
|
} else {
|
|
|
|
aSignalThread->Dispatch(aSignalRunnable, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::EndTrack(TrackID aID)
|
|
|
|
{
|
2012-05-22 23:01:15 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2012-10-24 16:21:32 -07:00
|
|
|
// ::EndAllTrackAndFinished() can end these before the sources call this
|
|
|
|
if (!mFinished) {
|
|
|
|
TrackData *track = FindDataForTrack(aID);
|
|
|
|
if (track) {
|
|
|
|
track->mCommands |= TRACK_END;
|
|
|
|
}
|
2012-05-22 23:01:15 -07:00
|
|
|
}
|
|
|
|
if (!mDestroyed) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::AdvanceKnownTracksTime(StreamTime aKnownTime)
|
|
|
|
{
|
2012-05-22 23:01:15 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mUpdateKnownTracksTime = aKnownTime;
|
|
|
|
if (!mDestroyed) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-24 16:21:32 -07:00
|
|
|
SourceMediaStream::FinishWithLockHeld()
|
2012-04-29 20:11:40 -07:00
|
|
|
{
|
2012-05-22 23:01:15 -07:00
|
|
|
mUpdateFinished = true;
|
|
|
|
if (!mDestroyed) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
2012-04-29 20:11:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 16:21:32 -07:00
|
|
|
void
|
|
|
|
SourceMediaStream::EndAllTrackAndFinish()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
for (uint32_t i = 0; i < mUpdateTracks.Length(); ++i) {
|
|
|
|
SourceMediaStream::TrackData* data = &mUpdateTracks[i];
|
|
|
|
data->mCommands |= TRACK_END;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FinishWithLockHeld();
|
|
|
|
// we will call NotifyFinished() to let GetUserMedia know
|
|
|
|
}
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
void
|
|
|
|
MediaInputPort::Init()
|
|
|
|
{
|
|
|
|
LOG(PR_LOG_DEBUG, ("Adding MediaInputPort %p (from %p to %p) to the graph",
|
|
|
|
this, mSource, mDest));
|
|
|
|
mSource->AddConsumer(this);
|
|
|
|
mDest->AddInput(this);
|
2012-08-23 05:46:20 -07:00
|
|
|
// mPortCount decremented via MediaInputPort::Destroy's message
|
2012-07-31 05:17:21 -07:00
|
|
|
++mDest->GraphImpl()->mPortCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaInputPort::Disconnect()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mSource == !mDest,
|
|
|
|
"mSource must either both be null or both non-null");
|
|
|
|
if (!mSource)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mSource->RemoveConsumer(this);
|
|
|
|
mSource = nullptr;
|
|
|
|
mDest->RemoveInput(this);
|
|
|
|
mDest = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaInputPort::InputInterval
|
|
|
|
MediaInputPort::GetNextInputInterval(GraphTime aTime)
|
|
|
|
{
|
|
|
|
InputInterval result = { GRAPH_TIME_MAX, GRAPH_TIME_MAX, false };
|
|
|
|
GraphTime t = aTime;
|
|
|
|
GraphTime end;
|
|
|
|
for (;;) {
|
|
|
|
if (!mDest->mBlocked.GetAt(t, &end))
|
|
|
|
break;
|
|
|
|
if (end == GRAPH_TIME_MAX)
|
|
|
|
return result;
|
|
|
|
t = end;
|
|
|
|
}
|
|
|
|
result.mStart = t;
|
|
|
|
GraphTime sourceEnd;
|
|
|
|
result.mInputIsBlocked = mSource->mBlocked.GetAt(t, &sourceEnd);
|
2013-01-15 04:22:03 -08:00
|
|
|
result.mEnd = std::min(end, sourceEnd);
|
2012-07-31 05:17:21 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaInputPort::Destroy()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaInputPort* aPort)
|
2012-08-23 05:46:20 -07:00
|
|
|
: ControlMessage(nullptr), mPort(aPort) {}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mPort->Disconnect();
|
2012-08-23 05:46:20 -07:00
|
|
|
--mPort->GraphImpl()->mPortCount;
|
2012-07-31 05:17:21 -07:00
|
|
|
NS_RELEASE(mPort);
|
|
|
|
}
|
|
|
|
virtual void RunDuringShutdown()
|
|
|
|
{
|
|
|
|
Run();
|
|
|
|
}
|
|
|
|
// This does not need to be strongly referenced; the graph is holding
|
|
|
|
// a strong reference to the port, which we will remove. This will be the
|
|
|
|
// last message for the port.
|
|
|
|
MediaInputPort* mPort;
|
|
|
|
};
|
2012-08-23 05:46:20 -07:00
|
|
|
GraphImpl()->AppendMessage(new Message(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamGraphImpl*
|
|
|
|
MediaInputPort::GraphImpl()
|
|
|
|
{
|
2013-02-01 11:49:58 -08:00
|
|
|
return mGraph;
|
2012-08-23 05:46:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamGraph*
|
|
|
|
MediaInputPort::Graph()
|
|
|
|
{
|
2013-02-01 11:49:58 -08:00
|
|
|
return mGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaInputPort::SetGraphImpl(MediaStreamGraphImpl* aGraph)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mGraph, "Should only be called once");
|
|
|
|
mGraph = aGraph;
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
|
|
|
|
2012-11-22 14:25:05 -08:00
|
|
|
already_AddRefed<MediaInputPort>
|
2012-08-22 08:56:38 -07:00
|
|
|
ProcessedMediaStream::AllocateInputPort(MediaStream* aStream, uint32_t aFlags)
|
2012-07-31 05:17:21 -07:00
|
|
|
{
|
2012-11-22 14:25:05 -08:00
|
|
|
// This method creates two references to the MediaInputPort: one for
|
|
|
|
// the main thread, and one for the MediaStreamGraph.
|
2012-07-31 05:17:21 -07:00
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaInputPort* aPort)
|
|
|
|
: ControlMessage(aPort->GetDestination()),
|
|
|
|
mPort(aPort) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mPort->Init();
|
2012-11-22 14:25:05 -08:00
|
|
|
// The graph holds its reference implicitly
|
|
|
|
mPort.forget();
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
2012-11-22 14:25:05 -08:00
|
|
|
nsRefPtr<MediaInputPort> mPort;
|
2012-07-31 05:17:21 -07:00
|
|
|
};
|
2012-11-22 14:25:05 -08:00
|
|
|
nsRefPtr<MediaInputPort> port = new MediaInputPort(aStream, this, aFlags);
|
2013-02-01 11:49:58 -08:00
|
|
|
port->SetGraphImpl(GraphImpl());
|
2012-07-31 05:17:21 -07:00
|
|
|
GraphImpl()->AppendMessage(new Message(port));
|
2012-11-22 14:25:05 -08:00
|
|
|
return port.forget();
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessedMediaStream::Finish()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(ProcessedMediaStream* aStream)
|
|
|
|
: ControlMessage(aStream) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mStream->GraphImpl()->FinishStream(mStream);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessedMediaStream::SetAutofinish(bool aAutofinish)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(ProcessedMediaStream* aStream, bool aAutofinish)
|
|
|
|
: ControlMessage(aStream), mAutofinish(aAutofinish) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
2013-02-04 02:04:26 -08:00
|
|
|
static_cast<ProcessedMediaStream*>(mStream)->SetAutofinishImpl(mAutofinish);
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
|
|
|
bool mAutofinish;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aAutofinish));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessedMediaStream::DestroyImpl()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = mInputs.Length() - 1; i >= 0; --i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
mInputs[i]->Disconnect();
|
|
|
|
}
|
|
|
|
MediaStream::DestroyImpl();
|
|
|
|
}
|
2012-04-29 20:11:26 -07:00
|
|
|
|
2012-05-09 20:30:34 -07:00
|
|
|
/**
|
|
|
|
* We make the initial mCurrentTime nonzero so that zero times can have
|
|
|
|
* special meaning if necessary.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t INITIAL_CURRENT_TIME = 1;
|
2012-05-09 20:30:34 -07:00
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
MediaStreamGraphImpl::MediaStreamGraphImpl()
|
2012-07-31 05:17:21 -07:00
|
|
|
: mCurrentTime(INITIAL_CURRENT_TIME)
|
|
|
|
, mStateComputedTime(INITIAL_CURRENT_TIME)
|
2012-04-29 20:11:26 -07:00
|
|
|
, mProcessingGraphUpdateIndex(0)
|
2012-07-31 05:17:21 -07:00
|
|
|
, mPortCount(0)
|
2012-04-29 20:11:26 -07:00
|
|
|
, mMonitor("MediaStreamGraphImpl")
|
|
|
|
, mLifecycleState(LIFECYCLE_THREAD_NOT_STARTED)
|
|
|
|
, mWaitState(WAITSTATE_RUNNING)
|
|
|
|
, mNeedAnotherIteration(false)
|
|
|
|
, mForceShutDown(false)
|
|
|
|
, mPostedRunInStableStateEvent(false)
|
|
|
|
, mDetectedNotRunning(false)
|
|
|
|
, mPostedRunInStableState(false)
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gMediaStreamGraphLog) {
|
|
|
|
gMediaStreamGraphLog = PR_NewLogModule("MediaStreamGraph");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mCurrentTimeStamp = mInitialTimeStamp = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(MediaStreamGraphShutdownObserver, nsIObserver)
|
|
|
|
|
|
|
|
static bool gShutdownObserverRegistered = false;
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaStreamGraphShutdownObserver::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
if (gGraph) {
|
|
|
|
gGraph->ForceShutDown();
|
|
|
|
}
|
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
|
|
|
gShutdownObserverRegistered = false;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamGraph*
|
|
|
|
MediaStreamGraph::GetInstance()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
|
|
|
|
|
|
|
|
if (!gGraph) {
|
|
|
|
if (!gShutdownObserverRegistered) {
|
|
|
|
gShutdownObserverRegistered = true;
|
|
|
|
nsContentUtils::RegisterShutdownObserver(new MediaStreamGraphShutdownObserver());
|
|
|
|
}
|
|
|
|
|
|
|
|
gGraph = new MediaStreamGraphImpl();
|
|
|
|
LOG(PR_LOG_DEBUG, ("Starting up MediaStreamGraph %p", gGraph));
|
|
|
|
}
|
|
|
|
|
|
|
|
return gGraph;
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:40 -07:00
|
|
|
SourceMediaStream*
|
2013-02-15 00:01:58 -08:00
|
|
|
MediaStreamGraph::CreateSourceStream(DOMMediaStream* aWrapper)
|
2012-04-29 20:11:40 -07:00
|
|
|
{
|
|
|
|
SourceMediaStream* stream = new SourceMediaStream(aWrapper);
|
|
|
|
NS_ADDREF(stream);
|
2013-02-01 11:43:36 -08:00
|
|
|
MediaStreamGraphImpl* graph = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
stream->SetGraphImpl(graph);
|
|
|
|
graph->AppendMessage(new CreateMessage(stream));
|
2012-04-29 20:11:40 -07:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
ProcessedMediaStream*
|
2013-02-15 00:01:58 -08:00
|
|
|
MediaStreamGraph::CreateTrackUnionStream(DOMMediaStream* aWrapper)
|
2012-07-31 05:17:21 -07:00
|
|
|
{
|
|
|
|
TrackUnionStream* stream = new TrackUnionStream(aWrapper);
|
|
|
|
NS_ADDREF(stream);
|
2013-02-01 11:43:36 -08:00
|
|
|
MediaStreamGraphImpl* graph = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
stream->SetGraphImpl(graph);
|
|
|
|
graph->AppendMessage(new CreateMessage(stream));
|
2012-07-31 05:17:21 -07:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2013-01-13 14:46:57 -08:00
|
|
|
AudioNodeStream*
|
|
|
|
MediaStreamGraph::CreateAudioNodeStream(AudioNodeEngine* aEngine)
|
|
|
|
{
|
|
|
|
AudioNodeStream* stream = new AudioNodeStream(aEngine);
|
|
|
|
NS_ADDREF(stream);
|
2013-02-01 11:43:36 -08:00
|
|
|
MediaStreamGraphImpl* graph = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
stream->SetGraphImpl(graph);
|
|
|
|
graph->AppendMessage(new CreateMessage(stream));
|
2013-01-13 14:46:57 -08:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:26 -07:00
|
|
|
}
|