OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 5 #ifndef MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
6 #define MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 6 #define MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace media { | 21 namespace media { |
22 | 22 |
23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { | 23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { |
24 public: | 24 public: |
25 typedef StreamParser::TrackId TrackId; | 25 typedef StreamParser::TrackId TrackId; |
26 | 26 |
27 private: | 27 private: |
28 // Helper class that manages per-track state. | 28 // Helper class that manages per-track state. |
29 class Track { | 29 class Track { |
30 public: | 30 public: |
31 Track(int track_num, bool is_video); | 31 Track(int track_num, bool is_video, base::TimeDelta default_duration); |
32 ~Track(); | 32 ~Track(); |
33 | 33 |
34 int track_num() const { return track_num_; } | 34 int track_num() const { return track_num_; } |
35 const std::deque<scoped_refptr<StreamParserBuffer> >& buffers() const { | 35 const std::deque<scoped_refptr<StreamParserBuffer> >& buffers() const { |
36 return buffers_; | 36 return buffers_; |
37 } | 37 } |
38 | 38 |
| 39 // If |last_added_buffer_missing_duration_| is set, updates its duration |
| 40 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets |
| 41 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing |
| 42 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or |
| 43 // otherwise adds |buffer| to |buffers_|. |
39 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 44 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
40 | 45 |
41 // Clears all buffer state. | 46 // If |last_added_buffer_missing_duration_| is set, updates its duration |
| 47 // to be the first non-kNoTimestamp() value of |default_duration_|, |
| 48 // |estimated_next_frame_duration_|, or an arbitrary default, then adds it |
| 49 // to |buffers_| and unsets |last_added_buffer_missing_duration_|. (This |
| 50 // method helps stream parser emit all buffers in a media segment before |
| 51 // signaling end of segment.) |
| 52 void ApplyDurationDefaultOrEstimateIfNeeded(); |
| 53 |
| 54 // Clears all buffer state, except a possibly held-aside buffer that is |
| 55 // missing duration. |
| 56 void ClearBuffersButKeepLastIfMissingDuration(); |
| 57 |
| 58 // Clears all buffer state, including any possibly held-aside buffer that |
| 59 // was missing duration. |
42 void Reset(); | 60 void Reset(); |
43 | 61 |
44 // Helper function used to inspect block data to determine if the | 62 // Helper function used to inspect block data to determine if the |
45 // block is a keyframe. | 63 // block is a keyframe. |
46 // |data| contains the bytes in the block. | 64 // |data| contains the bytes in the block. |
47 // |size| indicates the number of bytes in |data|. | 65 // |size| indicates the number of bytes in |data|. |
48 bool IsKeyframe(const uint8* data, int size) const; | 66 bool IsKeyframe(const uint8* data, int size) const; |
49 | 67 |
50 private: | 68 private: |
| 69 // Helper that sanity-checks |buffer| duration, updates |
| 70 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. |
| 71 // Returns false if |buffer| failed sanity check and therefore was not added |
| 72 // to |buffers_|. Returns true otherwise. |
| 73 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
| 74 |
51 int track_num_; | 75 int track_num_; |
52 std::deque<scoped_refptr<StreamParserBuffer> > buffers_; | 76 std::deque<scoped_refptr<StreamParserBuffer> > buffers_; |
53 bool is_video_; | 77 bool is_video_; |
| 78 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; |
| 79 |
| 80 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. |
| 81 base::TimeDelta default_duration_; |
| 82 // If kNoTimestamp(), then a default value will be used. This estimate is |
| 83 // the maximum duration seen or derived so far for this track, and is valid |
| 84 // only if |default_duration_| is kNoTimestamp(). |
| 85 base::TimeDelta estimated_next_frame_duration_; |
54 }; | 86 }; |
55 | 87 |
56 typedef std::map<int, Track> TextTrackMap; | 88 typedef std::map<int, Track> TextTrackMap; |
57 | 89 |
58 public: | 90 public: |
59 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; | 91 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; |
60 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; | 92 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; |
61 | 93 |
62 WebMClusterParser(int64 timecode_scale, | 94 WebMClusterParser(int64 timecode_scale, |
63 int audio_track_num, | 95 int audio_track_num, |
| 96 base::TimeDelta audio_default_duration, |
64 int video_track_num, | 97 int video_track_num, |
| 98 base::TimeDelta video_default_duration, |
65 const WebMTracksParser::TextTracks& text_tracks, | 99 const WebMTracksParser::TextTracks& text_tracks, |
66 const std::set<int64>& ignored_tracks, | 100 const std::set<int64>& ignored_tracks, |
67 const std::string& audio_encryption_key_id, | 101 const std::string& audio_encryption_key_id, |
68 const std::string& video_encryption_key_id, | 102 const std::string& video_encryption_key_id, |
69 const LogCB& log_cb); | 103 const LogCB& log_cb); |
70 virtual ~WebMClusterParser(); | 104 virtual ~WebMClusterParser(); |
71 | 105 |
72 // Resets the parser state so it can accept a new cluster. | 106 // Resets the parser state so it can accept a new cluster. |
73 void Reset(); | 107 void Reset(); |
74 | 108 |
75 // Parses a WebM cluster element in |buf|. | 109 // Parses a WebM cluster element in |buf|. |
76 // | 110 // |
77 // Returns -1 if the parse fails. | 111 // Returns -1 if the parse fails. |
78 // Returns 0 if more data is needed. | 112 // Returns 0 if more data is needed. |
79 // Returns the number of bytes parsed on success. | 113 // Returns the number of bytes parsed on success. |
80 int Parse(const uint8* buf, int size); | 114 int Parse(const uint8* buf, int size); |
81 | 115 |
82 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } | 116 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } |
83 const BufferQueue& audio_buffers() const { return audio_.buffers(); } | 117 |
84 const BufferQueue& video_buffers() const { return video_.buffers(); } | 118 // Get the buffers resulting from Parse(). |
| 119 // If the parse reached the end of cluster and the last buffer was held aside |
| 120 // due to missing duration, the buffer is given an estimated duration and |
| 121 // included in the result. |
| 122 const BufferQueue& GetAudioBuffers(); |
| 123 const BufferQueue& GetVideoBuffers(); |
85 | 124 |
86 // Constructs and returns a subset of |text_track_map_| containing only | 125 // Constructs and returns a subset of |text_track_map_| containing only |
87 // tracks with non-empty buffer queues produced by the last Parse(). | 126 // 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 | 127 // The returned map is cleared by Parse() or Reset() and updated by the next |
89 // call to GetTextBuffers(). | 128 // call to GetTextBuffers(). |
90 const TextBufferQueueMap& GetTextBuffers(); | 129 const TextBufferQueueMap& GetTextBuffers(); |
91 | 130 |
92 // Returns true if the last Parse() call stopped at the end of a cluster. | 131 // Returns true if the last Parse() call stopped at the end of a cluster. |
93 bool cluster_ended() const { return cluster_ended_; } | 132 bool cluster_ended() const { return cluster_ended_; } |
94 | 133 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 TextBufferQueueMap text_buffers_map_; | 185 TextBufferQueueMap text_buffers_map_; |
147 | 186 |
148 LogCB log_cb_; | 187 LogCB log_cb_; |
149 | 188 |
150 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); | 189 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); |
151 }; | 190 }; |
152 | 191 |
153 } // namespace media | 192 } // namespace media |
154 | 193 |
155 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 194 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
OLD | NEW |