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_WEBM_WEBM_CLUSTER_PARSER_H_ 6 #define MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_ 7 8 #include <deque> 9 #include <map> 10 #include <set> 11 #include <string> 12 13 #include "base/memory/scoped_ptr.h" 14 #include "media/base/media_export.h" 15 #include "media/base/media_log.h" 16 #include "media/base/stream_parser_buffer.h" 17 #include "media/webm/webm_parser.h" 18 #include "media/webm/webm_tracks_parser.h" 19 20 namespace media { 21 22 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { 23 private: 24 // Helper class that manages per-track state. 25 class Track { 26 public: 27 Track(int track_num, bool is_video); 28 ~Track(); 29 30 int track_num() const { return track_num_; } 31 const std::deque<scoped_refptr<StreamParserBuffer> >& buffers() const { 32 return buffers_; 33 } 34 35 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); 36 37 // Clears all buffer state. 38 void Reset(); 39 40 // Helper function used to inspect block data to determine if the 41 // block is a keyframe. 42 // |data| contains the bytes in the block. 43 // |size| indicates the number of bytes in |data|. 44 bool IsKeyframe(const uint8* data, int size) const; 45 46 private: 47 int track_num_; 48 std::deque<scoped_refptr<StreamParserBuffer> > buffers_; 49 bool is_video_; 50 }; 51 52 typedef std::map<int, Track> TextTrackMap; 53 54 public: 55 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; 56 57 class MEDIA_EXPORT TextTrackIterator { 58 public: 59 explicit TextTrackIterator(const TextTrackMap& text_track_map); 60 TextTrackIterator(const TextTrackIterator& rhs); 61 ~TextTrackIterator(); 62 63 // To visit each text track. If the iterator is exhausted, it returns 64 // as parameters the values 0 and NULL, and the function returns false. 65 // Otherwise, it returns the buffers for the associated track, and the 66 // function returns true. 67 bool operator()(int* track_num, const BufferQueue** buffers); 68 private: 69 TextTrackIterator& operator=(const TextTrackIterator&); 70 71 TextTrackMap::const_iterator iterator_; 72 const TextTrackMap::const_iterator iterator_end_; 73 }; 74 75 WebMClusterParser(int64 timecode_scale, 76 int audio_track_num, 77 int video_track_num, 78 const WebMTracksParser::TextTracks& text_tracks, 79 const std::set<int64>& ignored_tracks, 80 const std::string& audio_encryption_key_id, 81 const std::string& video_encryption_key_id, 82 const LogCB& log_cb); 83 virtual ~WebMClusterParser(); 84 85 // Resets the parser state so it can accept a new cluster. 86 void Reset(); 87 88 // Parses a WebM cluster element in |buf|. 89 // 90 // Returns -1 if the parse fails. 91 // Returns 0 if more data is needed. 92 // Returns the number of bytes parsed on success. 93 int Parse(const uint8* buf, int size); 94 95 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } 96 const BufferQueue& audio_buffers() const { return audio_.buffers(); } 97 const BufferQueue& video_buffers() const { return video_.buffers(); } 98 99 // Returns an iterator object, allowing each text track to be visited. 100 TextTrackIterator CreateTextTrackIterator() const; 101 102 // Returns true if the last Parse() call stopped at the end of a cluster. 103 bool cluster_ended() const { return cluster_ended_; } 104 105 private: 106 // WebMParserClient methods. 107 virtual WebMParserClient* OnListStart(int id) OVERRIDE; 108 virtual bool OnListEnd(int id) OVERRIDE; 109 virtual bool OnUInt(int id, int64 val) OVERRIDE; 110 virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE; 111 112 bool ParseBlock(bool is_simple_block, const uint8* buf, int size, 113 const uint8* additional, int additional_size, int duration, 114 int64 discard_padding); 115 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, 116 int flags, const uint8* data, int size, 117 const uint8* additional, int additional_size, 118 int64 discard_padding); 119 120 // Resets the Track objects associated with each text track. 121 void ResetTextTracks(); 122 123 // Search for the indicated track_num among the text tracks. Returns NULL 124 // if that track num is not a text track. 125 Track* FindTextTrack(int track_num); 126 127 double timecode_multiplier_; // Multiplier used to convert timecodes into 128 // microseconds. 129 std::set<int64> ignored_tracks_; 130 std::string audio_encryption_key_id_; 131 std::string video_encryption_key_id_; 132 133 WebMListParser parser_; 134 135 int64 last_block_timecode_; 136 scoped_ptr<uint8[]> block_data_; 137 int block_data_size_; 138 int64 block_duration_; 139 int64 block_add_id_; 140 scoped_ptr<uint8[]> block_additional_data_; 141 int block_additional_data_size_; 142 int64 discard_padding_; 143 bool discard_padding_set_; 144 145 int64 cluster_timecode_; 146 base::TimeDelta cluster_start_time_; 147 bool cluster_ended_; 148 149 Track audio_; 150 Track video_; 151 TextTrackMap text_track_map_; 152 LogCB log_cb_; 153 154 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); 155 }; 156 157 } // namespace media 158 159 #endif // MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_ 160