2012-07-31 05:17:21 -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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_TRACKUNIONSTREAM_H_
|
|
|
|
#define MOZILLA_TRACKUNIONSTREAM_H_
|
|
|
|
|
|
|
|
#include "MediaStreamGraph.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2012-07-31 05:17:21 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
#define LOG(type, msg) PR_LOG(gMediaStreamGraphLog, type, msg)
|
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See MediaStreamGraph::CreateTrackUnionStream.
|
|
|
|
* This file is only included by MediaStreamGraph.cpp so it's OK to put the
|
|
|
|
* entire implementation in this header file.
|
|
|
|
*/
|
|
|
|
class TrackUnionStream : public ProcessedMediaStream {
|
|
|
|
public:
|
2013-02-15 00:01:58 -08:00
|
|
|
TrackUnionStream(DOMMediaStream* aWrapper) :
|
2012-07-31 05:17:21 -07:00
|
|
|
ProcessedMediaStream(aWrapper),
|
|
|
|
mMaxTrackID(0) {}
|
|
|
|
|
|
|
|
virtual void RemoveInput(MediaInputPort* aPort)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = mTrackMap.Length() - 1; i >= 0; --i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
if (mTrackMap[i].mInputPort == aPort) {
|
|
|
|
EndTrack(i);
|
|
|
|
mTrackMap.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProcessedMediaStream::RemoveInput(aPort);
|
|
|
|
}
|
|
|
|
virtual void ProduceOutput(GraphTime aFrom, GraphTime aTo)
|
|
|
|
{
|
|
|
|
nsAutoTArray<bool,8> mappedTracksFinished;
|
|
|
|
nsAutoTArray<bool,8> mappedTracksWithMatchingInputTracks;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mTrackMap.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
mappedTracksFinished.AppendElement(true);
|
|
|
|
mappedTracksWithMatchingInputTracks.AppendElement(false);
|
|
|
|
}
|
|
|
|
bool allFinished = true;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mInputs.Length(); ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStream* stream = mInputs[i]->GetSource();
|
|
|
|
if (!stream->IsFinishedOnGraphThread()) {
|
|
|
|
allFinished = false;
|
|
|
|
}
|
|
|
|
for (StreamBuffer::TrackIter tracks(stream->GetStreamBuffer());
|
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
|
|
|
bool found = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < mTrackMap.Length(); ++j) {
|
2012-07-31 05:17:21 -07:00
|
|
|
TrackMapEntry* map = &mTrackMap[j];
|
2012-11-02 03:42:59 -07:00
|
|
|
if (map->mInputPort == mInputs[i] && map->mInputTrackID == tracks->GetID()) {
|
2012-07-31 05:17:21 -07:00
|
|
|
bool trackFinished;
|
2012-11-02 03:42:59 -07:00
|
|
|
StreamBuffer::Track* outputTrack = mBuffer.FindTrack(map->mOutputTrackID);
|
|
|
|
if (!outputTrack || outputTrack->IsEnded()) {
|
2012-07-31 05:17:21 -07:00
|
|
|
trackFinished = true;
|
|
|
|
} else {
|
2012-11-02 03:42:59 -07:00
|
|
|
CopyTrackData(tracks.get(), j, aFrom, aTo, &trackFinished);
|
2012-07-31 05:17:21 -07:00
|
|
|
}
|
|
|
|
mappedTracksFinished[j] = trackFinished;
|
|
|
|
mappedTracksWithMatchingInputTracks[j] = true;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
bool trackFinished = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mapIndex = AddTrack(mInputs[i], tracks.get(), aFrom);
|
2012-11-02 03:42:59 -07:00
|
|
|
CopyTrackData(tracks.get(), mapIndex, aFrom, aTo, &trackFinished);
|
2012-07-31 05:17:21 -07:00
|
|
|
mappedTracksFinished.AppendElement(trackFinished);
|
|
|
|
mappedTracksWithMatchingInputTracks.AppendElement(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = mTrackMap.Length() - 1; i >= 0; --i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
if (mappedTracksFinished[i]) {
|
|
|
|
EndTrack(i);
|
|
|
|
} else {
|
|
|
|
allFinished = false;
|
|
|
|
}
|
|
|
|
if (!mappedTracksWithMatchingInputTracks[i]) {
|
|
|
|
mTrackMap.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allFinished && mAutofinish) {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
mBuffer.AdvanceKnownTracksTime(GraphTimeToStreamTime(aTo));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Only non-ended tracks are allowed to persist in this map.
|
|
|
|
struct TrackMapEntry {
|
|
|
|
MediaInputPort* mInputPort;
|
2012-11-02 03:42:59 -07:00
|
|
|
// We keep track IDs instead of track pointers because
|
|
|
|
// tracks can be removed without us being notified (e.g.
|
|
|
|
// when a finished track is forgotten.) When we need a Track*,
|
|
|
|
// we call StreamBuffer::FindTrack, which will return null if
|
|
|
|
// the track has been deleted.
|
|
|
|
TrackID mInputTrackID;
|
|
|
|
TrackID mOutputTrackID;
|
2012-07-31 05:17:21 -07:00
|
|
|
nsAutoPtr<MediaSegment> mSegment;
|
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t AddTrack(MediaInputPort* aPort, StreamBuffer::Track* aTrack,
|
2012-07-31 05:17:21 -07:00
|
|
|
GraphTime aFrom)
|
|
|
|
{
|
|
|
|
// Use the ID of the source track if we can, otherwise allocate a new
|
|
|
|
// unique ID
|
2013-01-15 04:22:03 -08:00
|
|
|
TrackID id = std::max(mMaxTrackID + 1, aTrack->GetID());
|
2012-07-31 05:17:21 -07:00
|
|
|
mMaxTrackID = id;
|
|
|
|
|
|
|
|
TrackRate rate = aTrack->GetRate();
|
|
|
|
// 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.
|
|
|
|
TrackTicks outputStart = TimeToTicksRoundUp(rate, GraphTimeToStreamTime(aFrom));
|
|
|
|
|
|
|
|
nsAutoPtr<MediaSegment> segment;
|
|
|
|
segment = aTrack->GetSegment()->CreateEmptyClone();
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStreamListener* l = mListeners[j];
|
|
|
|
l->NotifyQueuedTrackChanges(Graph(), id, rate, outputStart,
|
|
|
|
MediaStreamListener::TRACK_EVENT_CREATED,
|
|
|
|
*segment);
|
|
|
|
}
|
|
|
|
segment->AppendNullData(outputStart);
|
|
|
|
StreamBuffer::Track* track =
|
|
|
|
&mBuffer.AddTrack(id, rate, outputStart, segment.forget());
|
|
|
|
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->mInputPort = aPort;
|
2012-11-02 03:42:59 -07:00
|
|
|
map->mInputTrackID = aTrack->GetID();
|
|
|
|
map->mOutputTrackID = track->GetID();
|
2012-07-31 05:17:21 -07:00
|
|
|
map->mSegment = aTrack->GetSegment()->CreateEmptyClone();
|
|
|
|
return mTrackMap.Length() - 1;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
void EndTrack(uint32_t aIndex)
|
2012-07-31 05:17:21 -07:00
|
|
|
{
|
2012-11-02 03:42:59 -07:00
|
|
|
StreamBuffer::Track* outputTrack = mBuffer.FindTrack(mTrackMap[aIndex].mOutputTrackID);
|
|
|
|
if (!outputTrack || outputTrack->IsEnded())
|
2012-07-31 05:17:21 -07:00
|
|
|
return;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStreamListener* l = mListeners[j];
|
|
|
|
TrackTicks offset = outputTrack->GetSegment()->GetDuration();
|
|
|
|
nsAutoPtr<MediaSegment> segment;
|
|
|
|
segment = outputTrack->GetSegment()->CreateEmptyClone();
|
|
|
|
l->NotifyQueuedTrackChanges(Graph(), outputTrack->GetID(),
|
|
|
|
outputTrack->GetRate(), offset,
|
|
|
|
MediaStreamListener::TRACK_EVENT_ENDED,
|
|
|
|
*segment);
|
|
|
|
}
|
|
|
|
outputTrack->SetEnded();
|
|
|
|
}
|
2012-11-02 03:42:59 -07:00
|
|
|
void CopyTrackData(StreamBuffer::Track* aInputTrack,
|
|
|
|
uint32_t aMapIndex, GraphTime aFrom, GraphTime aTo,
|
2012-07-31 05:17:21 -07:00
|
|
|
bool* aOutputTrackFinished)
|
|
|
|
{
|
|
|
|
TrackMapEntry* map = &mTrackMap[aMapIndex];
|
2012-11-02 03:42:59 -07:00
|
|
|
StreamBuffer::Track* outputTrack = mBuffer.FindTrack(map->mOutputTrackID);
|
|
|
|
MOZ_ASSERT(outputTrack && !outputTrack->IsEnded(), "Can't copy to ended track");
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
TrackRate rate = outputTrack->GetRate();
|
|
|
|
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);
|
2013-01-15 04:22:03 -08:00
|
|
|
interval.mEnd = std::min(interval.mEnd, aTo);
|
2012-07-31 05:17:21 -07:00
|
|
|
if (interval.mStart >= interval.mEnd)
|
|
|
|
break;
|
|
|
|
next = interval.mEnd;
|
|
|
|
|
|
|
|
// Ticks >= startTicks and < endTicks are in the interval
|
|
|
|
StreamTime outputEnd = GraphTimeToStreamTime(interval.mEnd);
|
|
|
|
TrackTicks startTicks = outputTrack->GetEnd();
|
|
|
|
#ifdef DEBUG
|
|
|
|
StreamTime outputStart = GraphTimeToStreamTime(interval.mStart);
|
|
|
|
#endif
|
|
|
|
NS_ASSERTION(startTicks == TimeToTicksRoundUp(rate, outputStart),
|
|
|
|
"Samples missing");
|
|
|
|
TrackTicks endTicks = TimeToTicksRoundUp(rate, outputEnd);
|
|
|
|
TrackTicks ticks = endTicks - startTicks;
|
|
|
|
// StreamTime inputStart = source->GraphTimeToStreamTime(interval.mStart);
|
|
|
|
StreamTime inputEnd = source->GraphTimeToStreamTime(interval.mEnd);
|
|
|
|
TrackTicks inputTrackEndPoint = TRACK_TICKS_MAX;
|
|
|
|
|
2012-11-02 03:42:59 -07:00
|
|
|
if (aInputTrack->IsEnded()) {
|
|
|
|
TrackTicks inputEndTicks = aInputTrack->TimeToTicksRoundDown(inputEnd);
|
|
|
|
if (aInputTrack->GetEnd() <= inputEndTicks) {
|
|
|
|
inputTrackEndPoint = aInputTrack->GetEnd();
|
2012-07-31 05:17:21 -07:00
|
|
|
*aOutputTrackFinished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interval.mInputIsBlocked) {
|
|
|
|
// Maybe the input track ended?
|
|
|
|
segment->AppendNullData(ticks);
|
|
|
|
LOG(PR_LOG_DEBUG, ("TrackUnionStream %p appending %lld ticks of null data to track %d",
|
|
|
|
this, (long long)ticks, outputTrack->GetID()));
|
|
|
|
} else {
|
|
|
|
// Figuring out which samples to use from the input stream is tricky
|
|
|
|
// because its start time and our start time may differ by a fraction
|
|
|
|
// of a tick. Assuming the input track hasn't ended, we have to ensure
|
|
|
|
// that 'ticks' samples are gathered, even though a tick boundary may
|
|
|
|
// occur between outputStart and outputEnd but not between inputStart
|
|
|
|
// and inputEnd.
|
|
|
|
// We'll take the latest samples we can.
|
|
|
|
TrackTicks inputEndTicks = TimeToTicksRoundUp(rate, inputEnd);
|
|
|
|
TrackTicks inputStartTicks = inputEndTicks - ticks;
|
2012-11-02 03:42:59 -07:00
|
|
|
segment->AppendSlice(*aInputTrack->GetSegment(),
|
2013-01-15 04:22:03 -08:00
|
|
|
std::min(inputTrackEndPoint, inputStartTicks),
|
|
|
|
std::min(inputTrackEndPoint, inputEndTicks));
|
2012-07-31 05:17:21 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("TrackUnionStream %p appending %lld ticks of input data to track %d",
|
2013-01-15 04:22:03 -08:00
|
|
|
this, (long long)(std::min(inputTrackEndPoint, inputEndTicks) - std::min(inputTrackEndPoint, inputStartTicks)),
|
2012-07-31 05:17:21 -07:00
|
|
|
outputTrack->GetID()));
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
2012-07-31 05:17:21 -07:00
|
|
|
MediaStreamListener* l = mListeners[j];
|
|
|
|
l->NotifyQueuedTrackChanges(Graph(), outputTrack->GetID(),
|
|
|
|
outputTrack->GetRate(), startTicks, 0,
|
|
|
|
*segment);
|
|
|
|
}
|
|
|
|
outputTrack->GetSegment()->AppendFrom(segment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<TrackMapEntry> mTrackMap;
|
|
|
|
TrackID mMaxTrackID;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZILLA_MEDIASTREAMGRAPH_H_ */
|