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 #ifndef MEDIA_FILTERS_FRAME_PROCESSOR_H_
6 #define MEDIA_FILTERS_FRAME_PROCESSOR_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/time/time.h"
13 #include "media/base/media_export.h"
14 #include "media/base/media_log.h"
15 #include "media/base/stream_parser.h"
16 #include "media/filters/chunk_demuxer.h"
22 // Helper class that implements Media Source Extension's coded frame processing
24 class MEDIA_EXPORT FrameProcessor
{
26 typedef base::Callback
<void(base::TimeDelta
)> UpdateDurationCB
;
28 // TODO(wolenetz/acolwell): Ensure that all TrackIds are coherent and unique
29 // for each track buffer. For now, special track identifiers are used for each
30 // of audio and video here, and text TrackIds are assumed to be non-negative.
31 // See http://crbug.com/341581.
37 FrameProcessor(const UpdateDurationCB
& update_duration_cb
,
38 const scoped_refptr
<MediaLog
>& media_log
);
41 // Get/set the current append mode, which if true means "sequence" and if
42 // false means "segments".
43 // See http://www.w3.org/TR/media-source/#widl-SourceBuffer-mode.
44 bool sequence_mode() { return sequence_mode_
; }
45 void SetSequenceMode(bool sequence_mode
);
47 // Processes buffers in |audio_buffers|, |video_buffers|, and |text_map|.
48 // Returns true on success or false on failure which indicates decode error.
49 // |append_window_start| and |append_window_end| correspond to the MSE spec's
50 // similarly named source buffer attributes that are used in coded frame
52 // |*new_media_segment| tracks whether the next buffers processed within the
53 // append window represent the start of a new media segment. This method may
54 // both use and update this flag.
55 // Uses |*timestamp_offset| according to the coded frame processing algorithm,
56 // including updating it as required in 'sequence' mode frame processing.
57 bool ProcessFrames(const StreamParser::BufferQueue
& audio_buffers
,
58 const StreamParser::BufferQueue
& video_buffers
,
59 const StreamParser::TextBufferQueueMap
& text_map
,
60 base::TimeDelta append_window_start
,
61 base::TimeDelta append_window_end
,
62 bool* new_media_segment
,
63 base::TimeDelta
* timestamp_offset
);
65 // Signals the frame processor to update its group start timestamp to be
66 // |timestamp_offset| if it is in sequence append mode.
67 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset
);
69 // Adds a new track with unique track ID |id|.
70 // If |id| has previously been added, returns false to indicate error.
71 // Otherwise, returns true, indicating future ProcessFrames() will emit
72 // frames for the track |id| to |stream|.
73 bool AddTrack(StreamParser::TrackId id
, ChunkDemuxerStream
* stream
);
75 // Updates the internal mapping of TrackId to track buffer for the track
76 // buffer formerly associated with |old_id| to be associated with |new_id|.
77 // Returns false to indicate failure due to either no existing track buffer
78 // for |old_id| or collision with previous track buffer already mapped to
79 // |new_id|. Otherwise returns true.
80 bool UpdateTrack(StreamParser::TrackId old_id
, StreamParser::TrackId new_id
);
82 // Sets the need random access point flag on all track buffers to true.
83 void SetAllTrackBuffersNeedRandomAccessPoint();
85 // Resets state for the coded frame processing algorithm as described in steps
86 // 2-5 of the MSE Reset Parser State algorithm described at
87 // http://www.w3.org/TR/media-source/#sourcebuffer-reset-parser-state
90 // Must be called when the audio config is updated. Used to manage when
91 // the preroll buffer is cleared and the allowed "fudge" factor between
93 void OnPossibleAudioConfigUpdate(const AudioDecoderConfig
& config
);
96 typedef std::map
<StreamParser::TrackId
, MseTrackBuffer
*> TrackBufferMap
;
98 // If |track_buffers_| contains |id|, returns a pointer to the associated
99 // MseTrackBuffer. Otherwise, returns NULL.
100 MseTrackBuffer
* FindTrack(StreamParser::TrackId id
);
102 // Signals all track buffers' streams that a new media segment is starting
103 // with decode timestamp |segment_timestamp|.
104 void NotifyNewMediaSegmentStarting(DecodeTimestamp segment_timestamp
);
106 // Helper that signals each track buffer to append any processed, but not yet
107 // appended, frames to its stream. Returns true on success, or false if one or
108 // more of the appends failed.
109 bool FlushProcessedFrames();
111 // Handles partial append window trimming of |buffer|. Returns true if the
112 // given |buffer| can be partially trimmed or have preroll added; otherwise,
115 // If |buffer| overlaps |append_window_start|, the portion of |buffer| before
116 // |append_window_start| will be marked for post-decode discard. Further, if
117 // |audio_preroll_buffer_| exists and abuts |buffer|, it will be set as
118 // preroll on |buffer| and |audio_preroll_buffer_| will be cleared. If the
119 // preroll buffer does not abut |buffer|, it will be discarded unused.
121 // Likewise, if |buffer| overlaps |append_window_end|, the portion of |buffer|
122 // after |append_window_end| will be marked for post-decode discard.
124 // If |buffer| lies entirely before |append_window_start|, and thus would
125 // normally be discarded, |audio_preroll_buffer_| will be set to |buffer| and
126 // the method will return false.
127 bool HandlePartialAppendWindowTrimming(
128 base::TimeDelta append_window_start
,
129 base::TimeDelta append_window_end
,
130 const scoped_refptr
<StreamParserBuffer
>& buffer
);
132 // Helper that processes one frame with the coded frame processing algorithm.
133 // Returns false on error or true on success.
134 bool ProcessFrame(const scoped_refptr
<StreamParserBuffer
>& frame
,
135 base::TimeDelta append_window_start
,
136 base::TimeDelta append_window_end
,
137 base::TimeDelta
* timestamp_offset
,
138 bool* new_media_segment
);
140 // TrackId-indexed map of each track's stream.
141 TrackBufferMap track_buffers_
;
143 // The last audio buffer seen by the frame processor that was removed because
144 // it was entirely before the start of the append window.
145 scoped_refptr
<StreamParserBuffer
> audio_preroll_buffer_
;
147 // The AudioDecoderConfig associated with buffers handed to ProcessFrames().
148 AudioDecoderConfig current_audio_config_
;
149 base::TimeDelta sample_duration_
;
151 // The AppendMode of the associated SourceBuffer.
152 // See SetSequenceMode() for interpretation of |sequence_mode_|.
153 // Per http://www.w3.org/TR/media-source/#widl-SourceBuffer-mode:
154 // Controls how a sequence of media segments are handled. This is initially
155 // set to false ("segments").
156 bool sequence_mode_
= false;
158 // Tracks the MSE coded frame processing variable of same name.
159 // Initially kNoTimestamp(), meaning "unset".
160 base::TimeDelta group_start_timestamp_
;
162 // Tracks the MSE coded frame processing variable of same name. It stores the
163 // highest coded frame end timestamp across all coded frames in the current
164 // coded frame group. It is set to 0 when the SourceBuffer object is created
165 // and gets updated by ProcessFrames().
166 base::TimeDelta group_end_timestamp_
;
168 UpdateDurationCB update_duration_cb_
;
170 // MediaLog for reporting messages and properties to debug content and engine.
171 scoped_refptr
<MediaLog
> media_log_
;
173 // Counters that limit spam to |media_log_| for frame processor warnings.
174 int num_dropped_preroll_warnings_
= 0;
175 int num_dts_beyond_pts_warnings_
= 0;
177 DISALLOW_COPY_AND_ASSIGN(FrameProcessor
);
182 #endif // MEDIA_FILTERS_FRAME_PROCESSOR_H_