1 // Copyright 2014 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_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
6 #define MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
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.h"
17 #include "media/base/stream_parser_buffer.h"
18 #include "media/formats/webm/webm_parser.h"
19 #include "media/formats/webm/webm_tracks_parser.h"
23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient {
25 typedef StreamParser::TrackId TrackId;
28 // Helper class that manages per-track state.
31 Track(int track_num, bool is_video);
34 int track_num() const { return track_num_; }
35 const std::deque<scoped_refptr<StreamParserBuffer> >& buffers() const {
39 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer);
41 // Clears all buffer state.
44 // Helper function used to inspect block data to determine if the
45 // block is a keyframe.
46 // |data| contains the bytes in the block.
47 // |size| indicates the number of bytes in |data|.
48 bool IsKeyframe(const uint8* data, int size) const;
52 std::deque<scoped_refptr<StreamParserBuffer> > buffers_;
56 typedef std::map<int, Track> TextTrackMap;
59 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
60 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap;
62 WebMClusterParser(int64 timecode_scale,
65 const WebMTracksParser::TextTracks& text_tracks,
66 const std::set<int64>& ignored_tracks,
67 const std::string& audio_encryption_key_id,
68 const std::string& video_encryption_key_id,
70 virtual ~WebMClusterParser();
72 // Resets the parser state so it can accept a new cluster.
75 // Parses a WebM cluster element in |buf|.
77 // Returns -1 if the parse fails.
78 // Returns 0 if more data is needed.
79 // Returns the number of bytes parsed on success.
80 int Parse(const uint8* buf, int size);
82 base::TimeDelta cluster_start_time() const { return cluster_start_time_; }
83 const BufferQueue& audio_buffers() const { return audio_.buffers(); }
84 const BufferQueue& video_buffers() const { return video_.buffers(); }
86 // Constructs and returns a subset of |text_track_map_| containing only
87 // tracks with non-empty buffer queues produced by the last Parse().
88 // The returned map is cleared by Parse() or Reset() and updated by the next
89 // call to GetTextBuffers().
90 const TextBufferQueueMap& GetTextBuffers();
92 // Returns true if the last Parse() call stopped at the end of a cluster.
93 bool cluster_ended() const { return cluster_ended_; }
96 // WebMParserClient methods.
97 virtual WebMParserClient* OnListStart(int id) OVERRIDE;
98 virtual bool OnListEnd(int id) OVERRIDE;
99 virtual bool OnUInt(int id, int64 val) OVERRIDE;
100 virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE;
102 bool ParseBlock(bool is_simple_block, const uint8* buf, int size,
103 const uint8* additional, int additional_size, int duration,
104 int64 discard_padding);
105 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration,
106 int flags, const uint8* data, int size,
107 const uint8* additional, int additional_size,
108 int64 discard_padding);
110 // Resets the Track objects associated with each text track.
111 void ResetTextTracks();
113 // Search for the indicated track_num among the text tracks. Returns NULL
114 // if that track num is not a text track.
115 Track* FindTextTrack(int track_num);
117 double timecode_multiplier_; // Multiplier used to convert timecodes into
119 std::set<int64> ignored_tracks_;
120 std::string audio_encryption_key_id_;
121 std::string video_encryption_key_id_;
123 WebMListParser parser_;
125 int64 last_block_timecode_;
126 scoped_ptr<uint8[]> block_data_;
127 int block_data_size_;
128 int64 block_duration_;
130 scoped_ptr<uint8[]> block_additional_data_;
131 int block_additional_data_size_;
132 int64 discard_padding_;
133 bool discard_padding_set_;
135 int64 cluster_timecode_;
136 base::TimeDelta cluster_start_time_;
141 TextTrackMap text_track_map_;
143 // Subset of |text_track_map_| maintained by GetTextBuffers(), and cleared by
144 // ResetTextTracks(). Callers of GetTextBuffers() get a const-ref to this
146 TextBufferQueueMap text_buffers_map_;
150 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser);
155 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_