1 // Copyright (c) 2012 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_BASE_STREAM_PARSER_H_
6 #define MEDIA_BASE_STREAM_PARSER_H_
13 #include "base/callback_forward.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/time.h"
17 #include "media/base/demuxer_stream.h"
18 #include "media/base/eme_constants.h"
19 #include "media/base/media_export.h"
20 #include "media/base/media_log.h"
24 class AudioDecoderConfig
;
25 class StreamParserBuffer
;
26 class TextTrackConfig
;
27 class VideoDecoderConfig
;
29 // Abstract interface for parsing media byte streams.
30 class MEDIA_EXPORT StreamParser
{
32 typedef std::deque
<scoped_refptr
<StreamParserBuffer
> > BufferQueue
;
34 // Range of |TrackId| is dependent upon stream parsers. It is currently
35 // the key for the buffer's text track config in the applicable
36 // TextTrackConfigMap (which is passed in StreamParser::NewConfigCB), or
37 // 0 for other media types that currently allow at most one track.
38 // WebMTracksParser uses -1 as an invalid text track number.
39 // TODO(wolenetz/acolwell): Change to size_type while fixing stream parsers to
40 // emit validated track configuration and buffer vectors rather than max 1
41 // audio, max 1 video, and N text tracks in a map keyed by
42 // bytestream-specific-ranged track numbers. See http://crbug.com/341581.
45 // Map of text track ID to the track configuration.
46 typedef std::map
<TrackId
, TextTrackConfig
> TextTrackConfigMap
;
48 // Map of text track ID to decode-timestamp-ordered buffers for the track.
49 typedef std::map
<TrackId
, const BufferQueue
> TextBufferQueueMap
;
51 // Stream parameters passed in InitCB.
52 struct InitParameters
{
53 InitParameters(base::TimeDelta duration
);
56 base::TimeDelta duration
;
58 // Indicates the source time associated with presentation timestamp 0. A
59 // null Time is returned if no mapping to Time exists.
60 base::Time timeline_offset
;
62 // Indicates that timestampOffset should be updated based on the earliest
63 // end timestamp (audio or video) provided during each NewBuffersCB.
64 bool auto_update_timestamp_offset
;
66 // Indicates live stream.
67 DemuxerStream::Liveness liveness
;
70 // Indicates completion of parser initialization.
71 // params - Stream parameters.
72 typedef base::Callback
<void(const InitParameters
& params
)> InitCB
;
74 // Indicates when new stream configurations have been parsed.
75 // First parameter - The new audio configuration. If the config is not valid
76 // then it means that there isn't an audio stream.
77 // Second parameter - The new video configuration. If the config is not valid
78 // then it means that there isn't an audio stream.
79 // Third parameter - The new text tracks configuration. If the map is empty,
80 // then no text tracks were parsed from the stream.
81 // Return value - True if the new configurations are accepted.
82 // False if the new configurations are not supported
83 // and indicates that a parsing error should be signalled.
84 typedef base::Callback
<bool(const AudioDecoderConfig
&,
85 const VideoDecoderConfig
&,
86 const TextTrackConfigMap
&)> NewConfigCB
;
88 // New stream buffers have been parsed.
89 // First parameter - A queue of newly parsed audio buffers.
90 // Second parameter - A queue of newly parsed video buffers.
91 // Third parameter - A map of text track ids to queues of newly parsed inband
92 // text buffers. If the map is not empty, it must contain
93 // at least one track with a non-empty queue of text
95 // Return value - True indicates that the buffers are accepted.
96 // False if something was wrong with the buffers and a parsing
97 // error should be signalled.
98 typedef base::Callback
<bool(const BufferQueue
&,
100 const TextBufferQueueMap
&)> NewBuffersCB
;
102 // Signals the beginning of a new media segment.
103 typedef base::Callback
<void()> NewMediaSegmentCB
;
105 // A new potentially encrypted stream has been parsed.
106 // First parameter - The type of the initialization data associated with the
108 // Second parameter - The initialization data associated with the stream.
109 typedef base::Callback
<void(EmeInitDataType
, const std::vector
<uint8
>&)>
110 EncryptedMediaInitDataCB
;
113 virtual ~StreamParser();
115 // Initializes the parser with necessary callbacks. Must be called before any
116 // data is passed to Parse(). |init_cb| will be called once enough data has
117 // been parsed to determine the initial stream configurations, presentation
118 // start time, and duration. If |ignore_text_track| is true, then no text
119 // buffers should be passed later by the parser to |new_buffers_cb|.
121 const InitCB
& init_cb
,
122 const NewConfigCB
& config_cb
,
123 const NewBuffersCB
& new_buffers_cb
,
124 bool ignore_text_track
,
125 const EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
126 const NewMediaSegmentCB
& new_segment_cb
,
127 const base::Closure
& end_of_segment_cb
,
128 const LogCB
& log_cb
) = 0;
130 // Called when a seek occurs. This flushes the current parser state
131 // and puts the parser in a state where it can receive data for the new seek
133 virtual void Flush() = 0;
135 // Called when there is new data to parse.
137 // Returns true if the parse succeeds.
138 virtual bool Parse(const uint8
* buf
, int size
) = 0;
141 DISALLOW_COPY_AND_ASSIGN(StreamParser
);
144 // Appends to |merged_buffers| the provided buffers in decode-timestamp order.
145 // Any previous contents of |merged_buffers| is assumed to have lower
146 // decode timestamps versus the provided buffers. All provided buffer queues
147 // are assumed to already be in decode-timestamp order.
148 // Returns false if any of the provided audio/video/text buffers are found
149 // to not be in decode timestamp order, or have a decode timestamp less than
150 // the last buffer, if any, in |merged_buffers|. Partial results may exist
151 // in |merged_buffers| in this case. Returns true on success.
152 // No validation of media type within the various buffer queues is done here.
153 // TODO(wolenetz/acolwell): Merge incrementally in parsers to eliminate
154 // subtle issues with tie-breaking. See http://crbug.com/338484.
155 MEDIA_EXPORT
bool MergeBufferQueues(
156 const StreamParser::BufferQueue
& audio_buffers
,
157 const StreamParser::BufferQueue
& video_buffers
,
158 const StreamParser::TextBufferQueueMap
& text_buffers
,
159 StreamParser::BufferQueue
* merged_buffers
);
163 #endif // MEDIA_BASE_STREAM_PARSER_H_