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.
4
5#ifndef MEDIA_BASE_STREAM_PARSER_H_
6#define MEDIA_BASE_STREAM_PARSER_H_
7
8#include <deque>
9#include <map>
10#include <string>
11
12#include "base/callback_forward.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/time/time.h"
16#include "media/base/media_export.h"
17#include "media/base/media_log.h"
18
19namespace media {
20
21class AudioDecoderConfig;
22class StreamParserBuffer;
23class TextTrackConfig;
24class VideoDecoderConfig;
25
26// Abstract interface for parsing media byte streams.
27class MEDIA_EXPORT StreamParser {
28 public:
29  typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
30  typedef std::map<int, TextTrackConfig> TextTrackConfigMap;
31
32  StreamParser();
33  virtual ~StreamParser();
34
35  // Indicates completion of parser initialization.
36  // First parameter - Indicates initialization success. Set to true if
37  //                   initialization was successful. False if an error
38  //                   occurred.
39  // Second parameter -  Indicates the stream duration. Only contains a valid
40  //                     value if the first parameter is true.
41  typedef base::Callback<void(bool, base::TimeDelta)> InitCB;
42
43  // Indicates when new stream configurations have been parsed.
44  // First parameter - The new audio configuration. If the config is not valid
45  //                   then it means that there isn't an audio stream.
46  // Second parameter - The new video configuration. If the config is not valid
47  //                    then it means that there isn't an audio stream.
48  // Third parameter - The new text tracks configuration.  If the map is empty,
49  //                   then no text tracks were parsed from the stream.
50  // Return value - True if the new configurations are accepted.
51  //                False if the new configurations are not supported
52  //                and indicates that a parsing error should be signalled.
53  typedef base::Callback<bool(const AudioDecoderConfig&,
54                              const VideoDecoderConfig&,
55                              const TextTrackConfigMap&)> NewConfigCB;
56
57  // New stream buffers have been parsed.
58  // First parameter - A queue of newly parsed audio buffers.
59  // Second parameter - A queue of newly parsed video buffers.
60  // Return value - True indicates that the buffers are accepted.
61  //                False if something was wrong with the buffers and a parsing
62  //                error should be signalled.
63  typedef base::Callback<bool(const BufferQueue&,
64                              const BufferQueue&)> NewBuffersCB;
65
66  // New stream buffers of inband text have been parsed.
67  // First parameter - The id of the text track to which these cues will
68  //                   be added.
69  // Second parameter - A queue of newly parsed buffers.
70  // Return value - True indicates that the buffers are accepted.
71  //                False if something was wrong with the buffers and a parsing
72  //                error should be signalled.
73  typedef base::Callback<bool(int, const BufferQueue&)> NewTextBuffersCB;
74
75  // Signals the beginning of a new media segment.
76  typedef base::Callback<void()> NewMediaSegmentCB;
77
78  // A new potentially encrypted stream has been parsed.
79  // First parameter - The type of the initialization data associated with the
80  //                   stream.
81  // Second parameter - The initialization data associated with the stream.
82  typedef base::Callback<void(const std::string&,
83                              const std::vector<uint8>&)> NeedKeyCB;
84
85  // Initialize the parser with necessary callbacks. Must be called before any
86  // data is passed to Parse(). |init_cb| will be called once enough data has
87  // been parsed to determine the initial stream configurations, presentation
88  // start time, and duration.
89  virtual void Init(const InitCB& init_cb,
90                    const NewConfigCB& config_cb,
91                    const NewBuffersCB& new_buffers_cb,
92                    const NewTextBuffersCB& text_cb,
93                    const NeedKeyCB& need_key_cb,
94                    const NewMediaSegmentCB& new_segment_cb,
95                    const base::Closure& end_of_segment_cb,
96                    const LogCB& log_cb) = 0;
97
98  // Called when a seek occurs. This flushes the current parser state
99  // and puts the parser in a state where it can receive data for the new seek
100  // point.
101  virtual void Flush() = 0;
102
103  // Called when there is new data to parse.
104  //
105  // Returns true if the parse succeeds.
106  virtual bool Parse(const uint8* buf, int size) = 0;
107
108 private:
109  DISALLOW_COPY_AND_ASSIGN(StreamParser);
110};
111
112}  // namespace media
113
114#endif  // MEDIA_BASE_STREAM_PARSER_H_
115