Roll src/third_party/WebKit 787a07c:716df21 (svn 201034:201036)
[chromium-blink-merge.git] / media / filters / frame_processor.cc
blobfed25a7be52f5c12f91267c00e7f7c2b98fdcbda
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/frame_processor.h"
7 #include <cstdlib>
9 #include "base/stl_util.h"
10 #include "media/base/buffers.h"
11 #include "media/base/stream_parser_buffer.h"
13 namespace media {
15 const int kMaxDroppedPrerollWarnings = 10;
16 const int kMaxDtsBeyondPtsWarnings = 10;
18 // Helper class to capture per-track details needed by a frame processor. Some
19 // of this information may be duplicated in the short-term in the associated
20 // ChunkDemuxerStream and SourceBufferStream for a track.
21 // This parallels the MSE spec each of a SourceBuffer's Track Buffers at
22 // http://www.w3.org/TR/media-source/#track-buffers.
23 class MseTrackBuffer {
24 public:
25 explicit MseTrackBuffer(ChunkDemuxerStream* stream);
26 ~MseTrackBuffer();
28 // Get/set |last_decode_timestamp_|.
29 DecodeTimestamp last_decode_timestamp() const {
30 return last_decode_timestamp_;
32 void set_last_decode_timestamp(DecodeTimestamp timestamp) {
33 last_decode_timestamp_ = timestamp;
36 // Get/set |last_frame_duration_|.
37 base::TimeDelta last_frame_duration() const {
38 return last_frame_duration_;
40 void set_last_frame_duration(base::TimeDelta duration) {
41 last_frame_duration_ = duration;
44 // Gets |highest_presentation_timestamp_|.
45 base::TimeDelta highest_presentation_timestamp() const {
46 return highest_presentation_timestamp_;
49 // Get/set |needs_random_access_point_|.
50 bool needs_random_access_point() const {
51 return needs_random_access_point_;
53 void set_needs_random_access_point(bool needs_random_access_point) {
54 needs_random_access_point_ = needs_random_access_point;
57 // Gets a pointer to this track's ChunkDemuxerStream.
58 ChunkDemuxerStream* stream() const { return stream_; }
60 // Unsets |last_decode_timestamp_|, unsets |last_frame_duration_|,
61 // unsets |highest_presentation_timestamp_|, and sets
62 // |needs_random_access_point_| to true.
63 void Reset();
65 // If |highest_presentation_timestamp_| is unset or |timestamp| is greater
66 // than |highest_presentation_timestamp_|, sets
67 // |highest_presentation_timestamp_| to |timestamp|. Note that bidirectional
68 // prediction between coded frames can cause |timestamp| to not be
69 // monotonically increasing even though the decode timestamps are
70 // monotonically increasing.
71 void SetHighestPresentationTimestampIfIncreased(base::TimeDelta timestamp);
73 // Adds |frame| to the end of |processed_frames_|.
74 void EnqueueProcessedFrame(const scoped_refptr<StreamParserBuffer>& frame);
76 // Appends |processed_frames_|, if not empty, to |stream_| and clears
77 // |processed_frames_|. Returns false if append failed, true otherwise.
78 // |processed_frames_| is cleared in both cases.
79 bool FlushProcessedFrames();
81 private:
82 // The decode timestamp of the last coded frame appended in the current coded
83 // frame group. Initially kNoTimestamp(), meaning "unset".
84 DecodeTimestamp last_decode_timestamp_;
86 // The coded frame duration of the last coded frame appended in the current
87 // coded frame group. Initially kNoTimestamp(), meaning "unset".
88 base::TimeDelta last_frame_duration_;
90 // The highest presentation timestamp encountered in a coded frame appended
91 // in the current coded frame group. Initially kNoTimestamp(), meaning
92 // "unset".
93 base::TimeDelta highest_presentation_timestamp_;
95 // Keeps track of whether the track buffer is waiting for a random access
96 // point coded frame. Initially set to true to indicate that a random access
97 // point coded frame is needed before anything can be added to the track
98 // buffer.
99 bool needs_random_access_point_;
101 // Pointer to the stream associated with this track. The stream is not owned
102 // by |this|.
103 ChunkDemuxerStream* const stream_;
105 // Queue of processed frames that have not yet been appended to |stream_|.
106 // EnqueueProcessedFrame() adds to this queue, and FlushProcessedFrames()
107 // clears it.
108 StreamParser::BufferQueue processed_frames_;
110 DISALLOW_COPY_AND_ASSIGN(MseTrackBuffer);
113 MseTrackBuffer::MseTrackBuffer(ChunkDemuxerStream* stream)
114 : last_decode_timestamp_(kNoDecodeTimestamp()),
115 last_frame_duration_(kNoTimestamp()),
116 highest_presentation_timestamp_(kNoTimestamp()),
117 needs_random_access_point_(true),
118 stream_(stream) {
119 DCHECK(stream_);
122 MseTrackBuffer::~MseTrackBuffer() {
123 DVLOG(2) << __FUNCTION__ << "()";
126 void MseTrackBuffer::Reset() {
127 DVLOG(2) << __FUNCTION__ << "()";
129 last_decode_timestamp_ = kNoDecodeTimestamp();
130 last_frame_duration_ = kNoTimestamp();
131 highest_presentation_timestamp_ = kNoTimestamp();
132 needs_random_access_point_ = true;
135 void MseTrackBuffer::SetHighestPresentationTimestampIfIncreased(
136 base::TimeDelta timestamp) {
137 if (highest_presentation_timestamp_ == kNoTimestamp() ||
138 timestamp > highest_presentation_timestamp_) {
139 highest_presentation_timestamp_ = timestamp;
143 void MseTrackBuffer::EnqueueProcessedFrame(
144 const scoped_refptr<StreamParserBuffer>& frame) {
145 processed_frames_.push_back(frame);
148 bool MseTrackBuffer::FlushProcessedFrames() {
149 if (processed_frames_.empty())
150 return true;
152 bool result = stream_->Append(processed_frames_);
153 processed_frames_.clear();
155 DVLOG_IF(3, !result) << __FUNCTION__
156 << "(): Failure appending processed frames to stream";
158 return result;
161 FrameProcessor::FrameProcessor(const UpdateDurationCB& update_duration_cb,
162 const scoped_refptr<MediaLog>& media_log)
163 : group_start_timestamp_(kNoTimestamp()),
164 update_duration_cb_(update_duration_cb),
165 media_log_(media_log) {
166 DVLOG(2) << __FUNCTION__ << "()";
167 DCHECK(!update_duration_cb.is_null());
170 FrameProcessor::~FrameProcessor() {
171 DVLOG(2) << __FUNCTION__ << "()";
172 STLDeleteValues(&track_buffers_);
175 void FrameProcessor::SetSequenceMode(bool sequence_mode) {
176 DVLOG(2) << __FUNCTION__ << "(" << sequence_mode << ")";
178 // Per April 1, 2014 MSE spec editor's draft:
179 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/media-source.html#widl-SourceBuffer-mode
180 // Step 7: If the new mode equals "sequence", then set the group start
181 // timestamp to the group end timestamp.
182 if (sequence_mode) {
183 DCHECK(kNoTimestamp() != group_end_timestamp_);
184 group_start_timestamp_ = group_end_timestamp_;
187 // Step 8: Update the attribute to new mode.
188 sequence_mode_ = sequence_mode;
191 bool FrameProcessor::ProcessFrames(
192 const StreamParser::BufferQueue& audio_buffers,
193 const StreamParser::BufferQueue& video_buffers,
194 const StreamParser::TextBufferQueueMap& text_map,
195 base::TimeDelta append_window_start,
196 base::TimeDelta append_window_end,
197 bool* new_media_segment,
198 base::TimeDelta* timestamp_offset) {
199 StreamParser::BufferQueue frames;
200 if (!MergeBufferQueues(audio_buffers, video_buffers, text_map, &frames)) {
201 MEDIA_LOG(ERROR, media_log_) << "Parsed buffers not in DTS sequence";
202 return false;
205 DCHECK(!frames.empty());
207 // Implements the coded frame processing algorithm's outer loop for step 1.
208 // Note that ProcessFrame() implements an inner loop for a single frame that
209 // handles "jump to the Loop Top step to restart processing of the current
210 // coded frame" per April 1, 2014 MSE spec editor's draft:
211 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
212 // media-source.html#sourcebuffer-coded-frame-processing
213 // 1. For each coded frame in the media segment run the following steps:
214 for (StreamParser::BufferQueue::const_iterator frames_itr = frames.begin();
215 frames_itr != frames.end(); ++frames_itr) {
216 if (!ProcessFrame(*frames_itr, append_window_start, append_window_end,
217 timestamp_offset, new_media_segment)) {
218 FlushProcessedFrames();
219 return false;
223 if (!FlushProcessedFrames())
224 return false;
226 // 2. - 4. Are handled by the WebMediaPlayer / Pipeline / Media Element.
228 // Step 5:
229 update_duration_cb_.Run(group_end_timestamp_);
231 return true;
234 void FrameProcessor::SetGroupStartTimestampIfInSequenceMode(
235 base::TimeDelta timestamp_offset) {
236 DVLOG(2) << __FUNCTION__ << "(" << timestamp_offset.InSecondsF() << ")";
237 DCHECK(kNoTimestamp() != timestamp_offset);
238 if (sequence_mode_)
239 group_start_timestamp_ = timestamp_offset;
241 // Changes to timestampOffset should invalidate the preroll buffer.
242 audio_preroll_buffer_ = NULL;
245 bool FrameProcessor::AddTrack(StreamParser::TrackId id,
246 ChunkDemuxerStream* stream) {
247 DVLOG(2) << __FUNCTION__ << "(): id=" << id;
249 MseTrackBuffer* existing_track = FindTrack(id);
250 DCHECK(!existing_track);
251 if (existing_track) {
252 MEDIA_LOG(ERROR, media_log_) << "Failure adding track with duplicate ID "
253 << id;
254 return false;
257 track_buffers_[id] = new MseTrackBuffer(stream);
258 return true;
261 bool FrameProcessor::UpdateTrack(StreamParser::TrackId old_id,
262 StreamParser::TrackId new_id) {
263 DVLOG(2) << __FUNCTION__ << "() : old_id=" << old_id << ", new_id=" << new_id;
265 if (old_id == new_id || !FindTrack(old_id) || FindTrack(new_id)) {
266 MEDIA_LOG(ERROR, media_log_) << "Failure updating track id from " << old_id
267 << " to " << new_id;
268 return false;
271 track_buffers_[new_id] = track_buffers_[old_id];
272 CHECK_EQ(1u, track_buffers_.erase(old_id));
273 return true;
276 void FrameProcessor::SetAllTrackBuffersNeedRandomAccessPoint() {
277 for (TrackBufferMap::iterator itr = track_buffers_.begin();
278 itr != track_buffers_.end();
279 ++itr) {
280 itr->second->set_needs_random_access_point(true);
284 void FrameProcessor::Reset() {
285 DVLOG(2) << __FUNCTION__ << "()";
286 for (TrackBufferMap::iterator itr = track_buffers_.begin();
287 itr != track_buffers_.end(); ++itr) {
288 itr->second->Reset();
292 void FrameProcessor::OnPossibleAudioConfigUpdate(
293 const AudioDecoderConfig& config) {
294 DCHECK(config.IsValidConfig());
296 // Always clear the preroll buffer when a config update is received.
297 audio_preroll_buffer_ = NULL;
299 if (config.Matches(current_audio_config_))
300 return;
302 current_audio_config_ = config;
303 sample_duration_ = base::TimeDelta::FromSecondsD(
304 1.0 / current_audio_config_.samples_per_second());
307 MseTrackBuffer* FrameProcessor::FindTrack(StreamParser::TrackId id) {
308 TrackBufferMap::iterator itr = track_buffers_.find(id);
309 if (itr == track_buffers_.end())
310 return NULL;
312 return itr->second;
315 void FrameProcessor::NotifyNewMediaSegmentStarting(
316 DecodeTimestamp segment_timestamp) {
317 DVLOG(2) << __FUNCTION__ << "(" << segment_timestamp.InSecondsF() << ")";
319 for (TrackBufferMap::iterator itr = track_buffers_.begin();
320 itr != track_buffers_.end();
321 ++itr) {
322 itr->second->stream()->OnNewMediaSegment(segment_timestamp);
326 bool FrameProcessor::FlushProcessedFrames() {
327 DVLOG(2) << __FUNCTION__ << "()";
329 bool result = true;
330 for (TrackBufferMap::iterator itr = track_buffers_.begin();
331 itr != track_buffers_.end();
332 ++itr) {
333 if (!itr->second->FlushProcessedFrames())
334 result = false;
337 return result;
340 bool FrameProcessor::HandlePartialAppendWindowTrimming(
341 base::TimeDelta append_window_start,
342 base::TimeDelta append_window_end,
343 const scoped_refptr<StreamParserBuffer>& buffer) {
344 DCHECK(buffer->duration() > base::TimeDelta());
345 DCHECK_EQ(DemuxerStream::AUDIO, buffer->type());
346 DCHECK(buffer->is_key_frame());
348 const base::TimeDelta frame_end_timestamp =
349 buffer->timestamp() + buffer->duration();
351 // If the buffer is entirely before |append_window_start|, save it as preroll
352 // for the first buffer which overlaps |append_window_start|.
353 if (buffer->timestamp() < append_window_start &&
354 frame_end_timestamp <= append_window_start) {
355 audio_preroll_buffer_ = buffer;
356 return false;
359 // If the buffer is entirely after |append_window_end| there's nothing to do.
360 if (buffer->timestamp() >= append_window_end)
361 return false;
363 DCHECK(buffer->timestamp() >= append_window_start ||
364 frame_end_timestamp > append_window_start);
366 bool processed_buffer = false;
368 // If we have a preroll buffer see if we can attach it to the first buffer
369 // overlapping or after |append_window_start|.
370 if (audio_preroll_buffer_.get()) {
371 // We only want to use the preroll buffer if it directly precedes (less
372 // than one sample apart) the current buffer.
373 const int64 delta =
374 (audio_preroll_buffer_->timestamp() +
375 audio_preroll_buffer_->duration() - buffer->timestamp())
376 .InMicroseconds();
377 if (std::abs(delta) < sample_duration_.InMicroseconds()) {
378 DVLOG(1) << "Attaching audio preroll buffer ["
379 << audio_preroll_buffer_->timestamp().InSecondsF() << ", "
380 << (audio_preroll_buffer_->timestamp() +
381 audio_preroll_buffer_->duration()).InSecondsF() << ") to "
382 << buffer->timestamp().InSecondsF();
383 buffer->SetPrerollBuffer(audio_preroll_buffer_);
384 processed_buffer = true;
385 } else {
386 LIMITED_MEDIA_LOG(DEBUG, media_log_, num_dropped_preroll_warnings_,
387 kMaxDroppedPrerollWarnings)
388 << "Partial append window trimming dropping unused audio preroll "
389 "buffer with PTS "
390 << audio_preroll_buffer_->timestamp().InMicroseconds()
391 << "us that ends too far (" << delta
392 << "us) from next buffer with PTS "
393 << buffer->timestamp().InMicroseconds() << "us";
395 audio_preroll_buffer_ = NULL;
398 // See if a partial discard can be done around |append_window_start|.
399 if (buffer->timestamp() < append_window_start) {
400 DVLOG(1) << "Truncating buffer which overlaps append window start."
401 << " presentation_timestamp " << buffer->timestamp().InSecondsF()
402 << " frame_end_timestamp " << frame_end_timestamp.InSecondsF()
403 << " append_window_start " << append_window_start.InSecondsF();
405 // Mark the overlapping portion of the buffer for discard.
406 buffer->set_discard_padding(std::make_pair(
407 append_window_start - buffer->timestamp(), base::TimeDelta()));
409 // Adjust the timestamp of this buffer forward to |append_window_start| and
410 // decrease the duration to compensate. Adjust DTS by the same delta as PTS
411 // to help prevent spurious discontinuities when DTS > PTS.
412 base::TimeDelta pts_delta = append_window_start - buffer->timestamp();
413 buffer->set_timestamp(append_window_start);
414 buffer->SetDecodeTimestamp(buffer->GetDecodeTimestamp() + pts_delta);
415 buffer->set_duration(frame_end_timestamp - append_window_start);
416 processed_buffer = true;
419 // See if a partial discard can be done around |append_window_end|.
420 if (frame_end_timestamp > append_window_end) {
421 DVLOG(1) << "Truncating buffer which overlaps append window end."
422 << " presentation_timestamp " << buffer->timestamp().InSecondsF()
423 << " frame_end_timestamp " << frame_end_timestamp.InSecondsF()
424 << " append_window_end " << append_window_end.InSecondsF();
426 // Mark the overlapping portion of the buffer for discard.
427 buffer->set_discard_padding(
428 std::make_pair(buffer->discard_padding().first,
429 frame_end_timestamp - append_window_end));
431 // Decrease the duration of the buffer to remove the discarded portion.
432 buffer->set_duration(append_window_end - buffer->timestamp());
433 processed_buffer = true;
436 return processed_buffer;
439 bool FrameProcessor::ProcessFrame(
440 const scoped_refptr<StreamParserBuffer>& frame,
441 base::TimeDelta append_window_start,
442 base::TimeDelta append_window_end,
443 base::TimeDelta* timestamp_offset,
444 bool* new_media_segment) {
445 // Implements the loop within step 1 of the coded frame processing algorithm
446 // for a single input frame per April 1, 2014 MSE spec editor's draft:
447 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
448 // media-source.html#sourcebuffer-coded-frame-processing
450 while (true) {
451 // 1. Loop Top: Let presentation timestamp be a double precision floating
452 // point representation of the coded frame's presentation timestamp in
453 // seconds.
454 // 2. Let decode timestamp be a double precision floating point
455 // representation of the coded frame's decode timestamp in seconds.
456 // 3. Let frame duration be a double precision floating point representation
457 // of the coded frame's duration in seconds.
458 // We use base::TimeDelta and DecodeTimestamp instead of double.
459 base::TimeDelta presentation_timestamp = frame->timestamp();
460 DecodeTimestamp decode_timestamp = frame->GetDecodeTimestamp();
461 base::TimeDelta frame_duration = frame->duration();
463 DVLOG(3) << __FUNCTION__ << ": Processing frame "
464 << "Type=" << frame->type()
465 << ", TrackID=" << frame->track_id()
466 << ", PTS=" << presentation_timestamp.InSecondsF()
467 << ", DTS=" << decode_timestamp.InSecondsF()
468 << ", DUR=" << frame_duration.InSecondsF()
469 << ", RAP=" << frame->is_key_frame();
471 // Sanity check the timestamps.
472 if (presentation_timestamp == kNoTimestamp()) {
473 MEDIA_LOG(ERROR, media_log_) << "Unknown PTS for " << frame->GetTypeName()
474 << " frame";
475 return false;
477 if (decode_timestamp == kNoDecodeTimestamp()) {
478 MEDIA_LOG(ERROR, media_log_) << "Unknown DTS for " << frame->GetTypeName()
479 << " frame";
480 return false;
482 if (decode_timestamp.ToPresentationTime() > presentation_timestamp) {
483 // TODO(wolenetz): Determine whether DTS>PTS should really be allowed. See
484 // http://crbug.com/354518.
485 LIMITED_MEDIA_LOG(DEBUG, media_log_, num_dts_beyond_pts_warnings_,
486 kMaxDtsBeyondPtsWarnings)
487 << "Parsed " << frame->GetTypeName() << " frame has DTS "
488 << decode_timestamp.InMicroseconds()
489 << "us, which is after the frame's PTS "
490 << presentation_timestamp.InMicroseconds() << "us";
491 DVLOG(2) << __FUNCTION__ << ": WARNING: Frame DTS("
492 << decode_timestamp.InSecondsF() << ") > PTS("
493 << presentation_timestamp.InSecondsF()
494 << "), frame type=" << frame->GetTypeName();
497 // TODO(acolwell/wolenetz): All stream parsers must emit valid (positive)
498 // frame durations. For now, we allow non-negative frame duration.
499 // See http://crbug.com/351166.
500 if (frame_duration == kNoTimestamp()) {
501 MEDIA_LOG(ERROR, media_log_)
502 << "Unknown duration for " << frame->GetTypeName() << " frame at PTS "
503 << presentation_timestamp.InMicroseconds() << "us";
504 return false;
506 if (frame_duration < base::TimeDelta()) {
507 MEDIA_LOG(ERROR, media_log_)
508 << "Negative duration " << frame_duration.InMicroseconds()
509 << "us for " << frame->GetTypeName() << " frame at PTS "
510 << presentation_timestamp.InMicroseconds() << "us";
511 return false;
514 // 4. If mode equals "sequence" and group start timestamp is set, then run
515 // the following steps:
516 if (sequence_mode_ && group_start_timestamp_ != kNoTimestamp()) {
517 // 4.1. Set timestampOffset equal to group start timestamp -
518 // presentation timestamp.
519 *timestamp_offset = group_start_timestamp_ - presentation_timestamp;
521 DVLOG(3) << __FUNCTION__ << ": updated timestampOffset is now "
522 << timestamp_offset->InSecondsF();
524 // 4.2. Set group end timestamp equal to group start timestamp.
525 group_end_timestamp_ = group_start_timestamp_;
527 // 4.3. Set the need random access point flag on all track buffers to
528 // true.
529 SetAllTrackBuffersNeedRandomAccessPoint();
531 // 4.4. Unset group start timestamp.
532 group_start_timestamp_ = kNoTimestamp();
535 // 5. If timestampOffset is not 0, then run the following steps:
536 if (*timestamp_offset != base::TimeDelta()) {
537 // 5.1. Add timestampOffset to the presentation timestamp.
538 // Note: |frame| PTS is only updated if it survives discontinuity
539 // processing.
540 presentation_timestamp += *timestamp_offset;
542 // 5.2. Add timestampOffset to the decode timestamp.
543 // Frame DTS is only updated if it survives discontinuity processing.
544 decode_timestamp += *timestamp_offset;
547 // 6. Let track buffer equal the track buffer that the coded frame will be
548 // added to.
550 // Remap audio and video track types to their special singleton identifiers.
551 StreamParser::TrackId track_id = kAudioTrackId;
552 switch (frame->type()) {
553 case DemuxerStream::AUDIO:
554 break;
555 case DemuxerStream::VIDEO:
556 track_id = kVideoTrackId;
557 break;
558 case DemuxerStream::TEXT:
559 track_id = frame->track_id();
560 break;
561 case DemuxerStream::UNKNOWN:
562 case DemuxerStream::NUM_TYPES:
563 DCHECK(false) << ": Invalid frame type " << frame->type();
564 return false;
567 MseTrackBuffer* track_buffer = FindTrack(track_id);
568 if (!track_buffer) {
569 MEDIA_LOG(ERROR, media_log_)
570 << "Unknown track with type " << frame->GetTypeName()
571 << ", frame processor track id " << track_id
572 << ", and parser track id " << frame->track_id();
573 return false;
576 // 7. If last decode timestamp for track buffer is set and decode timestamp
577 // is less than last decode timestamp
578 // OR
579 // If last decode timestamp for track buffer is set and the difference
580 // between decode timestamp and last decode timestamp is greater than 2
581 // times last frame duration:
582 DecodeTimestamp last_decode_timestamp =
583 track_buffer->last_decode_timestamp();
584 if (last_decode_timestamp != kNoDecodeTimestamp()) {
585 base::TimeDelta dts_delta = decode_timestamp - last_decode_timestamp;
586 if (dts_delta < base::TimeDelta() ||
587 dts_delta > 2 * track_buffer->last_frame_duration()) {
588 // 7.1. If mode equals "segments": Set group end timestamp to
589 // presentation timestamp.
590 // If mode equals "sequence": Set group start timestamp equal to
591 // the group end timestamp.
592 if (!sequence_mode_) {
593 group_end_timestamp_ = presentation_timestamp;
594 // This triggers a discontinuity so we need to treat the next frames
595 // appended within the append window as if they were the beginning of
596 // a new segment.
597 *new_media_segment = true;
598 } else {
599 DVLOG(3) << __FUNCTION__ << " : Sequence mode discontinuity, GETS: "
600 << group_end_timestamp_.InSecondsF();
601 DCHECK(kNoTimestamp() != group_end_timestamp_);
602 group_start_timestamp_ = group_end_timestamp_;
605 // 7.2. - 7.5.:
606 Reset();
608 // 7.6. Jump to the Loop Top step above to restart processing of the
609 // current coded frame.
610 DVLOG(3) << __FUNCTION__ << ": Discontinuity: reprocessing frame";
611 continue;
615 // 9. Let frame end timestamp equal the sum of presentation timestamp and
616 // frame duration.
617 base::TimeDelta frame_end_timestamp =
618 presentation_timestamp + frame_duration;
620 // 10. If presentation timestamp is less than appendWindowStart, then set
621 // the need random access point flag to true, drop the coded frame, and
622 // jump to the top of the loop to start processing the next coded
623 // frame.
624 // Note: We keep the result of partial discard of a buffer that overlaps
625 // |append_window_start| and does not end after |append_window_end|.
626 // 11. If frame end timestamp is greater than appendWindowEnd, then set the
627 // need random access point flag to true, drop the coded frame, and jump
628 // to the top of the loop to start processing the next coded frame.
629 frame->set_timestamp(presentation_timestamp);
630 frame->SetDecodeTimestamp(decode_timestamp);
631 if (track_buffer->stream()->supports_partial_append_window_trimming() &&
632 HandlePartialAppendWindowTrimming(append_window_start,
633 append_window_end,
634 frame)) {
635 // |frame| has been partially trimmed or had preroll added. Though
636 // |frame|'s duration may have changed, do not update |frame_duration|
637 // here, so |track_buffer|'s last frame duration update uses original
638 // frame duration and reduces spurious discontinuity detection.
639 decode_timestamp = frame->GetDecodeTimestamp();
640 presentation_timestamp = frame->timestamp();
641 frame_end_timestamp = frame->timestamp() + frame->duration();
644 if (presentation_timestamp < append_window_start ||
645 frame_end_timestamp > append_window_end) {
646 track_buffer->set_needs_random_access_point(true);
647 DVLOG(3) << "Dropping frame that is outside append window.";
648 return true;
651 // Note: This step is relocated, versus April 1 spec, to allow append window
652 // processing to first filter coded frames shifted by |timestamp_offset_| in
653 // such a way that their PTS is negative.
654 // 8. If the presentation timestamp or decode timestamp is less than the
655 // presentation start time, then run the end of stream algorithm with the
656 // error parameter set to "decode", and abort these steps.
657 DCHECK(presentation_timestamp >= base::TimeDelta());
658 if (decode_timestamp < DecodeTimestamp()) {
659 // B-frames may still result in negative DTS here after being shifted by
660 // |timestamp_offset_|.
661 MEDIA_LOG(ERROR, media_log_)
662 << frame->GetTypeName() << " frame with PTS "
663 << presentation_timestamp.InMicroseconds() << "us has negative DTS "
664 << decode_timestamp.InMicroseconds()
665 << "us after applying timestampOffset, handling any discontinuity, "
666 "and filtering against append window";
667 return false;
670 // 12. If the need random access point flag on track buffer equals true,
671 // then run the following steps:
672 if (track_buffer->needs_random_access_point()) {
673 // 12.1. If the coded frame is not a random access point, then drop the
674 // coded frame and jump to the top of the loop to start processing
675 // the next coded frame.
676 if (!frame->is_key_frame()) {
677 DVLOG(3) << __FUNCTION__
678 << ": Dropping frame that is not a random access point";
679 return true;
682 // 12.2. Set the need random access point flag on track buffer to false.
683 track_buffer->set_needs_random_access_point(false);
686 // We now have a processed buffer to append to the track buffer's stream.
687 // If it is the first in a new media segment or following a discontinuity,
688 // notify all the track buffers' streams that a new segment is beginning.
689 if (*new_media_segment) {
690 // First, complete the append to track buffer streams of previous media
691 // segment's frames, if any.
692 if (!FlushProcessedFrames())
693 return false;
695 *new_media_segment = false;
697 // TODO(acolwell/wolenetz): This should be changed to a presentation
698 // timestamp. See http://crbug.com/402502
699 NotifyNewMediaSegmentStarting(decode_timestamp);
702 DVLOG(3) << __FUNCTION__ << ": Sending processed frame to stream, "
703 << "PTS=" << presentation_timestamp.InSecondsF()
704 << ", DTS=" << decode_timestamp.InSecondsF();
706 // Steps 13-18: Note, we optimize by appending groups of contiguous
707 // processed frames for each track buffer at end of ProcessFrames() or prior
708 // to NotifyNewMediaSegmentStarting().
709 // TODO(wolenetz): Refactor SourceBufferStream to conform to spec GC timing.
710 // See http://crbug.com/371197.
711 track_buffer->EnqueueProcessedFrame(frame);
713 // 19. Set last decode timestamp for track buffer to decode timestamp.
714 track_buffer->set_last_decode_timestamp(decode_timestamp);
716 // 20. Set last frame duration for track buffer to frame duration.
717 track_buffer->set_last_frame_duration(frame_duration);
719 // 21. If highest presentation timestamp for track buffer is unset or frame
720 // end timestamp is greater than highest presentation timestamp, then
721 // set highest presentation timestamp for track buffer to frame end
722 // timestamp.
723 track_buffer->SetHighestPresentationTimestampIfIncreased(
724 frame_end_timestamp);
726 // 22. If frame end timestamp is greater than group end timestamp, then set
727 // group end timestamp equal to frame end timestamp.
728 if (frame_end_timestamp > group_end_timestamp_)
729 group_end_timestamp_ = frame_end_timestamp;
730 DCHECK(group_end_timestamp_ >= base::TimeDelta());
732 return true;
735 NOTREACHED();
736 return false;
739 } // namespace media