Bug 1466606 - P2. Re-use code to determine NextGetSample index. r?bryce
Previous commit added SetNextGetSampleIndexIfNeeded, let's re-use it for GetSample.
We continue to have an exception code as it caters for the most common code path, which is we retrieve one sample after the other.
MozReview-Commit-ID: HOQ53qwZj7w
--- a/dom/media/mediasource/TrackBuffersManager.cpp
+++ b/dom/media/mediasource/TrackBuffersManager.cpp
@@ -2457,27 +2457,16 @@ TrackBuffersManager::GetSample(TrackInfo
MediaResult& aResult)
{
MOZ_ASSERT(OnTaskQueue());
auto& trackData = GetTracksData(aTrack);
const TrackBuffer& track = GetTrackBuffer(aTrack);
aResult = NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA;
- if (!track.Length()) {
- aResult = NS_ERROR_DOM_MEDIA_END_OF_STREAM;
- return nullptr;
- }
-
- if (trackData.mNextGetSampleIndex.isNothing() &&
- trackData.mNextSampleTimecode == TimeUnit()) {
- // First demux, get first sample.
- trackData.mNextGetSampleIndex = Some(0u);
- }
-
if (trackData.mNextGetSampleIndex.isSome()) {
if (trackData.mNextGetSampleIndex.ref() >= track.Length()) {
aResult = NS_ERROR_DOM_MEDIA_END_OF_STREAM;
return nullptr;
}
const MediaRawData* sample =
GetSample(aTrack,
trackData.mNextGetSampleIndex.ref(),
@@ -2514,51 +2503,42 @@ TrackBuffersManager::GetSample(TrackInfo
// Next sample isn't available yet. Use estimates.
trackData.mNextSampleTimecode = nextSampleTimecode;
trackData.mNextSampleTime = nextSampleTime;
}
aResult = NS_OK;
return p.forget();
}
- if (trackData.mNextSampleTimecode >
- track.LastElement()->mTimecode + track.LastElement()->mDuration) {
- // The next element is past our last sample. We're done.
- trackData.mNextGetSampleIndex = Some(uint32_t(track.Length()));
- aResult = NS_ERROR_DOM_MEDIA_END_OF_STREAM;
+ aResult = SetNextGetSampleIndexIfNeeded(aTrack, aFuzz);
+
+ if (NS_FAILED(aResult)) {
return nullptr;
}
- // Our previous index has been overwritten, attempt to find the new one.
- int32_t pos = FindCurrentPosition(aTrack, aFuzz);
- if (pos < 0) {
- MSE_DEBUG("Couldn't find sample (pts:%" PRId64 " dts:%" PRId64 ")",
- trackData.mNextSampleTime.ToMicroseconds(),
- trackData.mNextSampleTimecode.ToMicroseconds());
- return nullptr;
- }
-
- const RefPtr<MediaRawData>& sample = track[pos];
+ MOZ_RELEASE_ASSERT(trackData.mNextGetSampleIndex.isSome() &&
+ trackData.mNextGetSampleIndex.ref() < track.Length());
+ const RefPtr<MediaRawData>& sample =
+ track[trackData.mNextGetSampleIndex.ref()];
RefPtr<MediaRawData> p = sample->Clone();
if (!p) {
// OOM
aResult = MediaResult(NS_ERROR_OUT_OF_MEMORY, __func__);
return nullptr;
}
// Find the previous keyframe to calculate the evictable amount.
- int32_t i = pos;
+ uint32_t i = trackData.mNextGetSampleIndex.ref();
for (; !track[i]->mKeyframe; i--) {
}
UpdateEvictionIndex(trackData, i);
- trackData.mNextGetSampleIndex = Some(uint32_t(pos)+1);
+ trackData.mNextGetSampleIndex.ref()++;
trackData.mNextSampleTimecode = sample->mTimecode + sample->mDuration;
trackData.mNextSampleTime = sample->GetEndTime();
- aResult = NS_OK;
return p.forget();
}
int32_t
TrackBuffersManager::FindCurrentPosition(TrackInfo::TrackType aTrack,
const TimeUnit& aFuzz) const
{
MOZ_ASSERT(OnTaskQueue());
@@ -2685,16 +2665,19 @@ TrackBuffersManager::SetNextGetSampleInd
// The next element is past our last sample. We're done.
trackData.mNextGetSampleIndex = Some(uint32_t(track.Length()));
return NS_ERROR_DOM_MEDIA_END_OF_STREAM;
}
int32_t pos = FindCurrentPosition(aTrack, aFuzz);
if (pos < 0) {
// Not found, must wait for more data.
+ MSE_DEBUG("Couldn't find sample (pts:%" PRId64 " dts:%" PRId64 ")",
+ trackData.mNextSampleTime.ToMicroseconds(),
+ trackData.mNextSampleTimecode.ToMicroseconds());
return NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA;
}
trackData.mNextGetSampleIndex = Some(uint32_t(pos));
return NS_OK;
}
void
TrackBuffersManager::TrackData::AddSizeOfResources(MediaSourceDecoder::ResourceSizes* aSizes) const