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