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.
5 #ifndef MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_
6 #define MEDIA_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_buffer.h"
17 #include "media/webm/webm_parser.h"
18 #include "media/webm/webm_tracks_parser.h"
22 class MEDIA_EXPORT WebMClusterParser
: public WebMParserClient
{
24 // Helper class that manages per-track state.
27 Track(int track_num
, bool is_video
);
30 int track_num() const { return track_num_
; }
31 const std::deque
<scoped_refptr
<StreamParserBuffer
> >& buffers() const {
35 bool AddBuffer(const scoped_refptr
<StreamParserBuffer
>& buffer
);
37 // Clears all buffer state.
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;
48 std::deque
<scoped_refptr
<StreamParserBuffer
> > buffers_
;
52 typedef std::map
<int, Track
> TextTrackMap
;
55 typedef std::deque
<scoped_refptr
<StreamParserBuffer
> > BufferQueue
;
57 class MEDIA_EXPORT TextTrackIterator
{
59 explicit TextTrackIterator(const TextTrackMap
& text_track_map
);
60 TextTrackIterator(const TextTrackIterator
& rhs
);
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
);
69 TextTrackIterator
& operator=(const TextTrackIterator
&);
71 TextTrackMap::const_iterator iterator_
;
72 const TextTrackMap::const_iterator iterator_end_
;
75 WebMClusterParser(int64 timecode_scale
,
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
,
83 virtual ~WebMClusterParser();
85 // Resets the parser state so it can accept a new cluster.
88 // Parses a WebM cluster element in |buf|.
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
);
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(); }
99 // Returns an iterator object, allowing each text track to be visited.
100 TextTrackIterator
CreateTextTrackIterator() const;
102 // Returns true if the last Parse() call stopped at the end of a cluster.
103 bool cluster_ended() const { return cluster_ended_
; }
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
;
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
);
118 // Resets the Track objects associated with each text track.
119 void ResetTextTracks();
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
);
125 double timecode_multiplier_
; // Multiplier used to convert timecodes into
127 std::set
<int64
> ignored_tracks_
;
128 std::string audio_encryption_key_id_
;
129 std::string video_encryption_key_id_
;
131 WebMListParser parser_
;
133 int64 last_block_timecode_
;
134 scoped_ptr
<uint8
[]> block_data_
;
135 int block_data_size_
;
136 int64 block_duration_
;
138 scoped_ptr
<uint8
[]> block_additional_data_
;
139 int block_additional_data_size_
;
141 int64 cluster_timecode_
;
142 base::TimeDelta cluster_start_time_
;
147 TextTrackMap text_track_map_
;
150 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser
);
155 #endif // MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_