Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / formats / webm / webm_cluster_parser.h
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.
4
5 #ifndef MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
6 #define MEDIA_FORMATS_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.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"
20
21 namespace media {
22
23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient {
24  public:
25   typedef StreamParser::TrackId TrackId;
26
27  private:
28   // Helper class that manages per-track state.
29   class Track {
30    public:
31     Track(int track_num, bool is_video);
32     ~Track();
33
34     int track_num() const { return track_num_; }
35     const std::deque<scoped_refptr<StreamParserBuffer> >& buffers() const {
36       return buffers_;
37     }
38
39     bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer);
40
41     // Clears all buffer state.
42     void Reset();
43
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;
49
50    private:
51     int track_num_;
52     std::deque<scoped_refptr<StreamParserBuffer> > buffers_;
53     bool is_video_;
54   };
55
56   typedef std::map<int, Track> TextTrackMap;
57
58  public:
59   typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
60   typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap;
61
62   WebMClusterParser(int64 timecode_scale,
63                     int audio_track_num,
64                     int video_track_num,
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,
69                     const LogCB& log_cb);
70   virtual ~WebMClusterParser();
71
72   // Resets the parser state so it can accept a new cluster.
73   void Reset();
74
75   // Parses a WebM cluster element in |buf|.
76   //
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);
81
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(); }
85
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();
91
92   // Returns true if the last Parse() call stopped at the end of a cluster.
93   bool cluster_ended() const { return cluster_ended_; }
94
95  private:
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;
101
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);
109
110   // Resets the Track objects associated with each text track.
111   void ResetTextTracks();
112
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);
116
117   double timecode_multiplier_;  // Multiplier used to convert timecodes into
118                                 // microseconds.
119   std::set<int64> ignored_tracks_;
120   std::string audio_encryption_key_id_;
121   std::string video_encryption_key_id_;
122
123   WebMListParser parser_;
124
125   int64 last_block_timecode_;
126   scoped_ptr<uint8[]> block_data_;
127   int block_data_size_;
128   int64 block_duration_;
129   int64 block_add_id_;
130   scoped_ptr<uint8[]> block_additional_data_;
131   int block_additional_data_size_;
132   int64 discard_padding_;
133   bool discard_padding_set_;
134
135   int64 cluster_timecode_;
136   base::TimeDelta cluster_start_time_;
137   bool cluster_ended_;
138
139   Track audio_;
140   Track video_;
141   TextTrackMap text_track_map_;
142
143   // Subset of |text_track_map_| maintained by GetTextBuffers(), and cleared by
144   // ResetTextTracks(). Callers of GetTextBuffers() get a const-ref to this
145   // member.
146   TextBufferQueueMap text_buffers_map_;
147
148   LogCB log_cb_;
149
150   DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser);
151 };
152
153 }  // namespace media
154
155 #endif  // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_