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