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 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, 115 int flags, const uint8* data, int size, 116 const uint8* additional, int additional_size); 117 118 // Resets the Track objects associated with each text track. 119 void ResetTextTracks(); 120 121 // Search for the indicated track_num among the text tracks. Returns NULL 122 // if that track num is not a text track. 123 Track* FindTextTrack(int track_num); 124 125 double timecode_multiplier_; // Multiplier used to convert timecodes into 126 // microseconds. 127 std::set<int64> ignored_tracks_; 128 std::string audio_encryption_key_id_; 129 std::string video_encryption_key_id_; 130 131 WebMListParser parser_; 132 133 int64 last_block_timecode_; 134 scoped_ptr<uint8[]> block_data_; 135 int block_data_size_; 136 int64 block_duration_; 137 int64 block_add_id_; 138 scoped_ptr<uint8[]> block_additional_data_; 139 int block_additional_data_size_; 140 141 int64 cluster_timecode_; 142 base::TimeDelta cluster_start_time_; 143 bool cluster_ended_; 144 145 Track audio_; 146 Track video_; 147 TextTrackMap text_track_map_; 148 LogCB log_cb_; 149 150 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); 151 }; 152 153 } // namespace media 154 155 #endif // MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_ 156