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_
11 #include "base/callback_forward.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time.h"
15 #include "media/base/media_export.h"
19 class AudioDecoderConfig
;
20 class StreamParserBuffer
;
21 class VideoDecoderConfig
;
23 // Abstract interface for parsing media byte streams.
24 class MEDIA_EXPORT StreamParser
{
26 typedef std::deque
<scoped_refptr
<StreamParserBuffer
> > BufferQueue
;
29 virtual ~StreamParser();
31 // Indicates completion of parser initialization.
32 // First parameter - Indicates initialization success. Set to true if
33 // initialization was successful. False if an error
35 // Second parameter - Indicates the stream duration. Only contains a valid
36 // value if the first parameter is true.
37 typedef base::Callback
<void(bool, base::TimeDelta
)> InitCB
;
39 // Indicates when new stream configurations have been parsed.
40 // First parameter - The new audio configuration. If the config is not valid
41 // then it means that there isn't an audio stream.
42 // Second parameter - The new video configuration. If the config is not valid
43 // then it means that there isn't an audio stream.
44 // Return value - True if the new configurations are accepted.
45 // False if the new configurations are not supported
46 // and indicates that a parsing error should be signalled.
47 typedef base::Callback
<bool(const AudioDecoderConfig
&,
48 const VideoDecoderConfig
&)> NewConfigCB
;
50 // New stream buffers have been parsed.
51 // First parameter - A queue of newly parsed buffers.
52 // Return value - True indicates that the buffers are accepted.
53 // False if something was wrong with the buffers and a parsing
54 // error should be signalled.
55 typedef base::Callback
<bool(const BufferQueue
&)> NewBuffersCB
;
57 // Signals the beginning of a new media segment.
58 // First parameter - The earliest timestamp of all the streams in the segment.
59 typedef base::Callback
<void(base::TimeDelta
)> NewMediaSegmentCB
;
61 // A new potentially encrypted stream has been parsed.
62 // First parameter - The type of the initialization data associated with the
64 // Second parameter - The initialization data associated with the stream.
65 // Third parameter - Number of bytes of the initialization data.
66 // Return value - True indicates that the initialization data is accepted.
67 // False if something was wrong with the initialization data
68 // and a parsing error should be signalled.
69 typedef base::Callback
<bool(const std::string
&,
70 scoped_array
<uint8
>, int)> NeedKeyCB
;
72 // Initialize the parser with necessary callbacks. Must be called before any
73 // data is passed to Parse(). |init_cb| will be called once enough data has
74 // been parsed to determine the initial stream configurations, presentation
75 // start time, and duration.
76 virtual void Init(const InitCB
& init_cb
,
77 const NewConfigCB
& config_cb
,
78 const NewBuffersCB
& audio_cb
,
79 const NewBuffersCB
& video_cb
,
80 const NeedKeyCB
& need_key_cb
,
81 const NewMediaSegmentCB
& new_segment_cb
,
82 const base::Closure
& end_of_segment_cb
) = 0;
84 // Called when a seek occurs. This flushes the current parser state
85 // and puts the parser in a state where it can receive data for the new seek
87 virtual void Flush() = 0;
89 // Called when there is new data to parse.
91 // Returns true if the parse succeeds.
92 virtual bool Parse(const uint8
* buf
, int size
) = 0;
95 DISALLOW_COPY_AND_ASSIGN(StreamParser
);
100 #endif // MEDIA_BASE_STREAM_PARSER_H_