2014-10-11 06:02:59 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* 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 "MediaStreamGraphImpl.h"
|
|
|
|
#include "mozilla/MathAlgorithms.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
|
|
|
|
#include "AudioSegment.h"
|
|
|
|
#include "VideoSegment.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIAppShell.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsWidgetsCID.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "TrackUnionStream.h"
|
|
|
|
#include "ImageContainer.h"
|
|
|
|
#include "AudioChannelService.h"
|
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "AudioNodeExternalInputStream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "DOMMediaStream.h"
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
#include "AudioOutputObserver.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-10-25 10:24:36 -07:00
|
|
|
#ifdef STREAM_LOG
|
|
|
|
#undef STREAM_LOG
|
|
|
|
#endif
|
|
|
|
|
2014-10-11 06:02:59 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gTrackUnionStreamLog;
|
|
|
|
#define STREAM_LOG(type, msg) PR_LOG(gTrackUnionStreamLog, type, msg)
|
|
|
|
#else
|
|
|
|
#define STREAM_LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TrackUnionStream::TrackUnionStream(DOMMediaStream* aWrapper) :
|
|
|
|
ProcessedMediaStream(aWrapper),
|
|
|
|
mFilterCallback(nullptr)
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gTrackUnionStreamLog) {
|
|
|
|
gTrackUnionStreamLog = PR_NewLogModule("TrackUnionStream");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackUnionStream::RemoveInput(MediaInputPort* aPort)
|
|
|
|
{
|
|
|
|
for (int32_t i = mTrackMap.Length() - 1; i >= 0; --i) {
|
|
|
|
if (mTrackMap[i].mInputPort == aPort) {
|
|
|
|
EndTrack(i);
|
|
|
|
mTrackMap.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProcessedMediaStream::RemoveInput(aPort);
|
|
|
|
}
|
|
|
|
void TrackUnionStream::ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (IsFinishedOnGraphThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsAutoTArray<bool,8> mappedTracksFinished;
|
|
|
|
nsAutoTArray<bool,8> mappedTracksWithMatchingInputTracks;
|
|
|
|
for (uint32_t i = 0; i < mTrackMap.Length(); ++i) {
|
|
|
|
mappedTracksFinished.AppendElement(true);
|
|
|
|
mappedTracksWithMatchingInputTracks.AppendElement(false);
|
|
|
|
}
|
2014-11-20 10:36:00 -08:00
|
|
|
bool allFinished = !mInputs.IsEmpty();
|
|
|
|
bool allHaveCurrentData = !mInputs.IsEmpty();
|
2014-10-11 06:02:59 -07:00
|
|
|
for (uint32_t i = 0; i < mInputs.Length(); ++i) {
|
|
|
|
MediaStream* stream = mInputs[i]->GetSource();
|
|
|
|
if (!stream->IsFinishedOnGraphThread()) {
|
|
|
|
// XXX we really should check whether 'stream' has finished within time aTo,
|
|
|
|
// not just that it's finishing when all its queued data eventually runs
|
|
|
|
// out.
|
|
|
|
allFinished = false;
|
|
|
|
}
|
|
|
|
if (!stream->HasCurrentData()) {
|
|
|
|
allHaveCurrentData = false;
|
|
|
|
}
|
|
|
|
for (StreamBuffer::TrackIter tracks(stream->GetStreamBuffer());
|
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
|
|
|
bool found = false;
|
|
|
|
for (uint32_t j = 0; j < mTrackMap.Length(); ++j) {
|
|
|
|
TrackMapEntry* map = &mTrackMap[j];
|
|
|
|
if (map->mInputPort == mInputs[i] && map->mInputTrackID == tracks->GetID()) {
|
|
|
|
bool trackFinished;
|
|
|
|
StreamBuffer::Track* outputTrack = mBuffer.FindTrack(map->mOutputTrackID);
|
|
|
|
if (!outputTrack || outputTrack->IsEnded()) {
|
|
|
|
trackFinished = true;
|
|
|
|
} else {
|
|
|
|
CopyTrackData(tracks.get(), j, aFrom, aTo, &trackFinished);
|
|
|
|
}
|
|
|
|
mappedTracksFinished[j] = trackFinished;
|
|
|
|
mappedTracksWithMatchingInputTracks[j] = true;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found && (!mFilterCallback || mFilterCallback(tracks.get()))) {
|
|
|
|
bool trackFinished = false;
|
|
|
|
uint32_t mapIndex = AddTrack(mInputs[i], tracks.get(), aFrom);
|
|
|
|
CopyTrackData(tracks.get(), mapIndex, aFrom, aTo, &trackFinished);
|
|
|
|
mappedTracksFinished.AppendElement(trackFinished);
|
|
|
|
mappedTracksWithMatchingInputTracks.AppendElement(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int32_t i = mTrackMap.Length() - 1; i >= 0; --i) {
|
|
|
|
if (mappedTracksFinished[i]) {
|
|
|
|
EndTrack(i);
|
|
|
|
} else {
|
|
|
|
allFinished = false;
|
|
|
|
}
|
|
|
|
if (!mappedTracksWithMatchingInputTracks[i]) {
|
|
|
|
mTrackMap.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allFinished && mAutofinish && (aFlags & ALLOW_FINISH)) {
|
|
|
|
// All streams have finished and won't add any more tracks, and
|
|
|
|
// all our tracks have actually finished and been removed from our map,
|
|
|
|
// so we're finished now.
|
|
|
|
FinishOnGraphThread();
|
|
|
|
} else {
|
|
|
|
mBuffer.AdvanceKnownTracksTime(GraphTimeToStreamTime(aTo));
|
|
|
|
}
|
|
|
|
if (allHaveCurrentData) {
|
|
|
|
// We can make progress if we're not blocked
|
|
|
|
mHasCurrentData = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consumers may specify a filtering callback to apply to every input track.
|
|
|
|
// Returns true to allow the track to act as an input; false to reject it entirely.
|
|
|
|
|
|
|
|
void TrackUnionStream::SetTrackIDFilter(TrackIDFilterCallback aCallback)
|
|
|
|
{
|
|
|
|
mFilterCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward SetTrackEnabled(output_track_id, enabled) to the Source MediaStream,
|
|
|
|
// translating the output track ID into the correct ID in the source.
|
|
|
|
void TrackUnionStream::ForwardTrackEnabled(TrackID aOutputID, bool aEnabled)
|
|
|
|
{
|
|
|
|
for (int32_t i = mTrackMap.Length() - 1; i >= 0; --i) {
|
|
|
|
if (mTrackMap[i].mOutputTrackID == aOutputID) {
|
|
|
|
mTrackMap[i].mInputPort->GetSource()->
|
|
|
|
SetTrackEnabled(mTrackMap[i].mInputTrackID, aEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TrackUnionStream::AddTrack(MediaInputPort* aPort, StreamBuffer::Track* aTrack,
|
|
|
|
GraphTime aFrom)
|
|
|
|
{
|
|
|
|
// Use the ID of the source track if it's not already assigned to a track,
|
|
|
|
// otherwise allocate a new unique ID.
|
|
|
|
TrackID id = aTrack->GetID();
|
|
|
|
TrackID maxTrackID = 0;
|
|
|
|
for (uint32_t i = 0; i < mTrackMap.Length(); ++i) {
|
|
|
|
TrackID outID = mTrackMap[i].mOutputTrackID;
|
|
|
|
maxTrackID = std::max(maxTrackID, outID);
|
|
|
|
}
|
|
|
|
// Note: we might have removed it here, but it might still be in the
|
|
|
|
// StreamBuffer if the TrackUnionStream sees its input stream flip from
|
|
|
|
// A to B, where both A and B have a track with the same ID
|
|
|
|
while (1) {
|
|
|
|
// search until we find one not in use here, and not in mBuffer
|
|
|
|
if (!mBuffer.FindTrack(id)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
id = ++maxTrackID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round up the track start time so the track, if anything, starts a
|
|
|
|
// little later than the true time. This means we'll have enough
|
|
|
|
// samples in our input stream to go just beyond the destination time.
|
2014-09-17 22:20:43 -07:00
|
|
|
StreamTime outputStart = GraphTimeToStreamTime(aFrom);
|
2014-10-11 06:02:59 -07:00
|
|
|
|
|
|
|
nsAutoPtr<MediaSegment> segment;
|
|
|
|
segment = aTrack->GetSegment()->CreateEmptyClone();
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
2014-09-17 16:51:13 -07:00
|
|
|
l->NotifyQueuedTrackChanges(Graph(), id, outputStart,
|
2014-10-11 06:02:59 -07:00
|
|
|
MediaStreamListener::TRACK_EVENT_CREATED,
|
|
|
|
*segment);
|
|
|
|
}
|
|
|
|
segment->AppendNullData(outputStart);
|
|
|
|
StreamBuffer::Track* track =
|
2014-09-17 22:13:16 -07:00
|
|
|
&mBuffer.AddTrack(id, outputStart, segment.forget());
|
2014-10-11 06:02:59 -07:00
|
|
|
STREAM_LOG(PR_LOG_DEBUG, ("TrackUnionStream %p adding track %d for input stream %p track %d, start ticks %lld",
|
|
|
|
this, id, aPort->GetSource(), aTrack->GetID(),
|
|
|
|
(long long)outputStart));
|
|
|
|
|
|
|
|
TrackMapEntry* map = mTrackMap.AppendElement();
|
|
|
|
map->mEndOfConsumedInputTicks = 0;
|
|
|
|
map->mEndOfLastInputIntervalInInputStream = -1;
|
|
|
|
map->mEndOfLastInputIntervalInOutputStream = -1;
|
|
|
|
map->mInputPort = aPort;
|
|
|
|
map->mInputTrackID = aTrack->GetID();
|
|
|
|
map->mOutputTrackID = track->GetID();
|
|
|
|
map->mSegment = aTrack->GetSegment()->CreateEmptyClone();
|
|
|
|
return mTrackMap.Length() - 1;
|
|
|
|
}
|
2014-09-17 22:13:15 -07:00
|
|
|
|
2014-10-11 06:02:59 -07:00
|
|
|
void TrackUnionStream::EndTrack(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
StreamBuffer::Track* outputTrack = mBuffer.FindTrack(mTrackMap[aIndex].mOutputTrackID);
|
|
|
|
if (!outputTrack || outputTrack->IsEnded())
|
|
|
|
return;
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
2014-09-17 22:20:43 -07:00
|
|
|
StreamTime offset = outputTrack->GetSegment()->GetDuration();
|
2014-10-11 06:02:59 -07:00
|
|
|
nsAutoPtr<MediaSegment> segment;
|
|
|
|
segment = outputTrack->GetSegment()->CreateEmptyClone();
|
2014-09-17 16:51:13 -07:00
|
|
|
l->NotifyQueuedTrackChanges(Graph(), outputTrack->GetID(), offset,
|
2014-10-11 06:02:59 -07:00
|
|
|
MediaStreamListener::TRACK_EVENT_ENDED,
|
|
|
|
*segment);
|
|
|
|
}
|
|
|
|
outputTrack->SetEnded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackUnionStream::CopyTrackData(StreamBuffer::Track* aInputTrack,
|
|
|
|
uint32_t aMapIndex, GraphTime aFrom, GraphTime aTo,
|
|
|
|
bool* aOutputTrackFinished)
|
|
|
|
{
|
|
|
|
TrackMapEntry* map = &mTrackMap[aMapIndex];
|
|
|
|
StreamBuffer::Track* outputTrack = mBuffer.FindTrack(map->mOutputTrackID);
|
|
|
|
MOZ_ASSERT(outputTrack && !outputTrack->IsEnded(), "Can't copy to ended track");
|
|
|
|
|
|
|
|
MediaSegment* segment = map->mSegment;
|
|
|
|
MediaStream* source = map->mInputPort->GetSource();
|
|
|
|
|
|
|
|
GraphTime next;
|
|
|
|
*aOutputTrackFinished = false;
|
|
|
|
for (GraphTime t = aFrom; t < aTo; t = next) {
|
|
|
|
MediaInputPort::InputInterval interval = map->mInputPort->GetNextInputInterval(t);
|
|
|
|
interval.mEnd = std::min(interval.mEnd, aTo);
|
|
|
|
StreamTime inputEnd = source->GraphTimeToStreamTime(interval.mEnd);
|
2014-09-17 22:20:43 -07:00
|
|
|
StreamTime inputTrackEndPoint = STREAM_TIME_MAX;
|
2014-10-11 06:02:59 -07:00
|
|
|
|
|
|
|
if (aInputTrack->IsEnded() &&
|
2014-09-17 22:13:13 -07:00
|
|
|
aInputTrack->GetEnd() <= inputEnd) {
|
2014-09-17 16:51:13 -07:00
|
|
|
inputTrackEndPoint = aInputTrack->GetEnd();
|
2014-10-11 06:02:59 -07:00
|
|
|
*aOutputTrackFinished = true;
|
|
|
|
}
|
|
|
|
|
2014-10-29 07:47:28 -07:00
|
|
|
if (interval.mStart >= interval.mEnd) {
|
2014-10-11 06:02:59 -07:00
|
|
|
break;
|
2014-10-29 07:47:28 -07:00
|
|
|
}
|
2014-09-17 22:20:43 -07:00
|
|
|
StreamTime ticks = interval.mEnd - interval.mStart;
|
2014-10-11 06:02:59 -07:00
|
|
|
next = interval.mEnd;
|
|
|
|
|
2014-09-17 22:13:17 -07:00
|
|
|
StreamTime outputStart = outputTrack->GetEnd();
|
2014-10-11 06:02:59 -07:00
|
|
|
|
|
|
|
if (interval.mInputIsBlocked) {
|
|
|
|
// Maybe the input track ended?
|
|
|
|
segment->AppendNullData(ticks);
|
|
|
|
STREAM_LOG(PR_LOG_DEBUG+1, ("TrackUnionStream %p appending %lld ticks of null data to track %d",
|
|
|
|
this, (long long)ticks, outputTrack->GetID()));
|
|
|
|
} else {
|
2014-09-17 22:13:17 -07:00
|
|
|
MOZ_ASSERT(outputTrack->GetEnd() == GraphTimeToStreamTime(interval.mStart),
|
|
|
|
"Samples missing");
|
|
|
|
StreamTime inputStart = source->GraphTimeToStreamTime(interval.mStart);
|
|
|
|
segment->AppendSlice(*aInputTrack->GetSegment(),
|
|
|
|
std::min(inputTrackEndPoint, inputStart),
|
|
|
|
std::min(inputTrackEndPoint, inputEnd));
|
2014-10-11 06:02:59 -07:00
|
|
|
}
|
|
|
|
ApplyTrackDisabling(outputTrack->GetID(), segment);
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
|
|
|
l->NotifyQueuedTrackChanges(Graph(), outputTrack->GetID(),
|
2014-09-17 22:13:17 -07:00
|
|
|
outputStart, 0, *segment);
|
2014-10-11 06:02:59 -07:00
|
|
|
}
|
|
|
|
outputTrack->GetSegment()->AppendFrom(segment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|