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> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "media/base/audio_decoder_config.h" | 14 #include "media/base/audio_decoder_config.h" |
15 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
16 #include "media/base/media_log.h" | 16 #include "media/base/media_log.h" |
17 #include "media/base/stream_parser.h" | 17 #include "media/base/stream_parser.h" |
18 #include "media/base/stream_parser_buffer.h" | 18 #include "media/base/stream_parser_buffer.h" |
19 #include "media/formats/webm/webm_parser.h" | 19 #include "media/formats/webm/webm_parser.h" |
20 #include "media/formats/webm/webm_tracks_parser.h" | 20 #include "media/formats/webm/webm_tracks_parser.h" |
21 | 21 |
22 namespace media { | 22 namespace media { |
23 | 23 |
24 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { | 24 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { |
25 public: | 25 public: |
26 typedef StreamParser::TrackId TrackId; | 26 typedef StreamParser::TrackId TrackId; |
27 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; | 27 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; |
28 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; | 28 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; |
29 | 29 |
30 // Arbitrarily-chosen numbers to estimate the duration of a buffer if none is | 30 // Numbers chosen to estimate the duration of a buffer if none is set and |
31 // set and there is not enough information to get a better estimate. | 31 // there is not enough information to get a better estimate. |
32 enum { | 32 enum { |
33 kDefaultAudioBufferDurationInMs = 23, // Common 1k samples @44.1kHz | 33 // Common 1k samples @44.1kHz |
34 kDefaultVideoBufferDurationInMs = 42 // Low 24fps to reduce stalls | 34 kDefaultAudioBufferDurationInMs = 23, |
35 // Chosen to represent 16fps duration, which will prevent MSE stalls in | |
wolenetz
2015/04/15 02:55:23
nit: insert blank line to break the two comment+it
chcunningham
2015/04/16 18:04:16
Done.
| |
36 // videos with frame-rates as low as 8fps. | |
37 kDefaultVideoBufferDurationInMs = 63 | |
35 }; | 38 }; |
36 | 39 |
37 // Opus packets encode the duration and other parameters in the 5 most | 40 // Opus packets encode the duration and other parameters in the 5 most |
38 // significant bits of the first byte. The index in this array corresponds | 41 // significant bits of the first byte. The index in this array corresponds |
39 // to the duration of each frame of the packet in microseconds. See | 42 // to the duration of each frame of the packet in microseconds. See |
40 // https://tools.ietf.org/html/rfc6716#page-14 | 43 // https://tools.ietf.org/html/rfc6716#page-14 |
41 static const uint16_t kOpusFrameDurationsMu[]; | 44 static const uint16_t kOpusFrameDurationsMu[]; |
42 | 45 |
43 private: | 46 private: |
44 // Helper class that manages per-track state. | 47 // Helper class that manages per-track state. |
(...skipping 20 matching lines...) Expand all Loading... | |
65 const BufferQueue& ready_buffers() const { return ready_buffers_; } | 68 const BufferQueue& ready_buffers() const { return ready_buffers_; } |
66 | 69 |
67 // If |last_added_buffer_missing_duration_| is set, updates its duration | 70 // If |last_added_buffer_missing_duration_| is set, updates its duration |
68 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets | 71 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets |
69 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing | 72 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing |
70 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or | 73 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or |
71 // otherwise adds |buffer| to |buffers_|. | 74 // otherwise adds |buffer| to |buffers_|. |
72 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 75 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
73 | 76 |
74 // If |last_added_buffer_missing_duration_| is set, updates its duration to | 77 // If |last_added_buffer_missing_duration_| is set, updates its duration to |
75 // be non-kNoTimestamp() value of |estimated_next_frame_duration_| or an | 78 // be non-kNoTimestamp() value of |estimated_next_frame_duration_| or a |
76 // arbitrary default, then adds it to |buffers_| and unsets | 79 // hard-coded default, then adds it to |buffers_| and unsets |
77 // |last_added_buffer_missing_duration_|. (This method helps stream parser | 80 // |last_added_buffer_missing_duration_|. (This method helps stream parser |
78 // emit all buffers in a media segment before signaling end of segment.) | 81 // emit all buffers in a media segment before signaling end of segment.) |
79 void ApplyDurationEstimateIfNeeded(); | 82 void ApplyDurationEstimateIfNeeded(); |
80 | 83 |
81 // Clears |ready_buffers_| (use ExtractReadyBuffers() to fill it again). | 84 // Clears |ready_buffers_| (use ExtractReadyBuffers() to fill it again). |
82 // Leaves as-is |buffers_| and any possibly held-aside buffer that is | 85 // Leaves as-is |buffers_| and any possibly held-aside buffer that is |
83 // missing duration. | 86 // missing duration. |
84 void ClearReadyBuffers(); | 87 void ClearReadyBuffers(); |
85 | 88 |
86 // Clears all buffer state, including any possibly held-aside buffer that | 89 // Clears all buffer state, including any possibly held-aside buffer that |
(...skipping 13 matching lines...) Expand all Loading... | |
100 // Helper that sanity-checks |buffer| duration, updates | 103 // Helper that sanity-checks |buffer| duration, updates |
101 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. | 104 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. |
102 // Returns false if |buffer| failed sanity check and therefore was not added | 105 // Returns false if |buffer| failed sanity check and therefore was not added |
103 // to |buffers_|. Returns true otherwise. | 106 // to |buffers_|. Returns true otherwise. |
104 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 107 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
105 | 108 |
106 // Helper that calculates the buffer duration to use in | 109 // Helper that calculates the buffer duration to use in |
107 // ApplyDurationEstimateIfNeeded(). | 110 // ApplyDurationEstimateIfNeeded(). |
108 base::TimeDelta GetDurationEstimate(); | 111 base::TimeDelta GetDurationEstimate(); |
109 | 112 |
113 // Counts the number of estimated durations used in this track. Used to | |
114 // prevent log spam for MEDIA_LOG()s about estimated duration. | |
115 int num_duration_estimates_; | |
116 | |
110 int track_num_; | 117 int track_num_; |
111 bool is_video_; | 118 bool is_video_; |
112 | 119 |
113 // Parsed track buffers, each with duration and in (decode) timestamp order, | 120 // Parsed track buffers, each with duration and in (decode) timestamp order, |
114 // that have not yet been extracted into |ready_buffers_|. Note that up to | 121 // that have not yet been extracted into |ready_buffers_|. Note that up to |
115 // one additional buffer missing duration may be tracked by | 122 // one additional buffer missing duration may be tracked by |
116 // |last_added_buffer_missing_duration_|. | 123 // |last_added_buffer_missing_duration_|. |
117 BufferQueue buffers_; | 124 BufferQueue buffers_; |
118 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; | 125 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; |
119 | 126 |
120 // Buffers in (decode) timestamp order that were previously parsed into and | 127 // Buffers in (decode) timestamp order that were previously parsed into and |
121 // extracted from |buffers_|. Buffers are moved from |buffers_| to | 128 // extracted from |buffers_|. Buffers are moved from |buffers_| to |
122 // |ready_buffers_| by ExtractReadyBuffers() if they are below a specified | 129 // |ready_buffers_| by ExtractReadyBuffers() if they are below a specified |
123 // upper bound timestamp. Track users can therefore extract only those | 130 // upper bound timestamp. Track users can therefore extract only those |
124 // parsed buffers which are "ready" for emission (all before some maximum | 131 // parsed buffers which are "ready" for emission (all before some maximum |
125 // timestamp). | 132 // timestamp). |
126 BufferQueue ready_buffers_; | 133 BufferQueue ready_buffers_; |
127 | 134 |
128 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. | 135 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. |
129 base::TimeDelta default_duration_; | 136 base::TimeDelta default_duration_; |
130 | 137 |
131 // If kNoTimestamp(), then a default value will be used. This estimate is | 138 // If kNoTimestamp(), then a default value will be used. This estimate is |
132 // the maximum duration seen or derived so far for this track, and is valid | 139 // the maximum (for video), or minimum (for audio) duration seen so far for |
133 // only if |default_duration_| is kNoTimestamp(). | 140 // this track, and is used only if |default_duration_| is kNoTimestamp(). |
141 // TODO(chcunningham): Use maximum for audio too, adding checks to disable | |
142 // splicing when these estimates are observed in SourceBufferStream. | |
134 base::TimeDelta estimated_next_frame_duration_; | 143 base::TimeDelta estimated_next_frame_duration_; |
135 | 144 |
136 LogCB log_cb_; | 145 LogCB log_cb_; |
137 }; | 146 }; |
138 | 147 |
139 typedef std::map<int, Track> TextTrackMap; | 148 typedef std::map<int, Track> TextTrackMap; |
140 | 149 |
141 public: | 150 public: |
142 WebMClusterParser(int64 timecode_scale, | 151 WebMClusterParser(int64 timecode_scale, |
143 int audio_track_num, | 152 int audio_track_num, |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 DecodeTimestamp ready_buffer_upper_bound_; | 315 DecodeTimestamp ready_buffer_upper_bound_; |
307 | 316 |
308 LogCB log_cb_; | 317 LogCB log_cb_; |
309 | 318 |
310 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); | 319 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); |
311 }; | 320 }; |
312 | 321 |
313 } // namespace media | 322 } // namespace media |
314 | 323 |
315 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 324 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
OLD | NEW |