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 |
| 75 // Helper that calculates the buffer duration to use in |
| 76 // ApplyDurationDefaultOrEstimateIfNeeded(). |
| 77 base::TimeDelta GetDurationDefaultOrEstimate(); |
| 78 |
51 int track_num_; | 79 int track_num_; |
52 std::deque<scoped_refptr<StreamParserBuffer> > buffers_; | 80 std::deque<scoped_refptr<StreamParserBuffer> > buffers_; |
53 bool is_video_; | 81 bool is_video_; |
| 82 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; |
| 83 |
| 84 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. |
| 85 base::TimeDelta default_duration_; |
| 86 // If kNoTimestamp(), then a default value will be used. This estimate is |
| 87 // the maximum duration seen or derived so far for this track, and is valid |
| 88 // only if |default_duration_| is kNoTimestamp(). |
| 89 base::TimeDelta estimated_next_frame_duration_; |
54 }; | 90 }; |
55 | 91 |
56 typedef std::map<int, Track> TextTrackMap; | 92 typedef std::map<int, Track> TextTrackMap; |
57 | 93 |
58 public: | 94 public: |
59 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; | 95 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; |
60 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; | 96 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; |
61 | 97 |
62 WebMClusterParser(int64 timecode_scale, | 98 WebMClusterParser(int64 timecode_scale, |
63 int audio_track_num, | 99 int audio_track_num, |
| 100 base::TimeDelta audio_default_duration, |
64 int video_track_num, | 101 int video_track_num, |
| 102 base::TimeDelta video_default_duration, |
65 const WebMTracksParser::TextTracks& text_tracks, | 103 const WebMTracksParser::TextTracks& text_tracks, |
66 const std::set<int64>& ignored_tracks, | 104 const std::set<int64>& ignored_tracks, |
67 const std::string& audio_encryption_key_id, | 105 const std::string& audio_encryption_key_id, |
68 const std::string& video_encryption_key_id, | 106 const std::string& video_encryption_key_id, |
69 const LogCB& log_cb); | 107 const LogCB& log_cb); |
70 virtual ~WebMClusterParser(); | 108 virtual ~WebMClusterParser(); |
71 | 109 |
72 // Resets the parser state so it can accept a new cluster. | 110 // Resets the parser state so it can accept a new cluster. |
73 void Reset(); | 111 void Reset(); |
74 | 112 |
75 // Parses a WebM cluster element in |buf|. | 113 // Parses a WebM cluster element in |buf|. |
76 // | 114 // |
77 // Returns -1 if the parse fails. | 115 // Returns -1 if the parse fails. |
78 // Returns 0 if more data is needed. | 116 // Returns 0 if more data is needed. |
79 // Returns the number of bytes parsed on success. | 117 // Returns the number of bytes parsed on success. |
80 int Parse(const uint8* buf, int size); | 118 int Parse(const uint8* buf, int size); |
81 | 119 |
82 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } | 120 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } |
83 const BufferQueue& audio_buffers() const { return audio_.buffers(); } | 121 |
84 const BufferQueue& video_buffers() const { return video_.buffers(); } | 122 // Get the buffers resulting from Parse(). |
| 123 // If the parse reached the end of cluster and the last buffer was held aside |
| 124 // due to missing duration, the buffer is given an estimated duration and |
| 125 // included in the result. |
| 126 const BufferQueue& GetAudioBuffers(); |
| 127 const BufferQueue& GetVideoBuffers(); |
85 | 128 |
86 // Constructs and returns a subset of |text_track_map_| containing only | 129 // Constructs and returns a subset of |text_track_map_| containing only |
87 // tracks with non-empty buffer queues produced by the last Parse(). | 130 // 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 | 131 // The returned map is cleared by Parse() or Reset() and updated by the next |
89 // call to GetTextBuffers(). | 132 // call to GetTextBuffers(). |
90 const TextBufferQueueMap& GetTextBuffers(); | 133 const TextBufferQueueMap& GetTextBuffers(); |
91 | 134 |
92 // Returns true if the last Parse() call stopped at the end of a cluster. | 135 // Returns true if the last Parse() call stopped at the end of a cluster. |
93 bool cluster_ended() const { return cluster_ended_; } | 136 bool cluster_ended() const { return cluster_ended_; } |
94 | 137 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 TextBufferQueueMap text_buffers_map_; | 189 TextBufferQueueMap text_buffers_map_; |
147 | 190 |
148 LogCB log_cb_; | 191 LogCB log_cb_; |
149 | 192 |
150 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); | 193 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); |
151 }; | 194 }; |
152 | 195 |
153 } // namespace media | 196 } // namespace media |
154 | 197 |
155 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 198 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
OLD | NEW |