| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016 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_FILTERS_MEDIA_SOURCE_STATE_H_ | |
| 6 #define MEDIA_FILTERS_MEDIA_SOURCE_STATE_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "media/base/audio_codecs.h" | |
| 13 #include "media/base/demuxer.h" | |
| 14 #include "media/base/demuxer_stream.h" | |
| 15 #include "media/base/media_export.h" | |
| 16 #include "media/base/media_log.h" | |
| 17 #include "media/base/stream_parser.h" | |
| 18 #include "media/base/stream_parser_buffer.h" | |
| 19 #include "media/base/video_codecs.h" | |
| 20 | |
| 21 namespace media { | |
| 22 | |
| 23 using base::TimeDelta; | |
| 24 | |
| 25 class ChunkDemuxerStream; | |
| 26 class FrameProcessor; | |
| 27 | |
| 28 // Contains state belonging to a source id. | |
| 29 class MEDIA_EXPORT MediaSourceState { | |
| 30 public: | |
| 31 // Callback signature used to create ChunkDemuxerStreams. | |
| 32 typedef base::Callback<ChunkDemuxerStream*(DemuxerStream::Type)> | |
| 33 CreateDemuxerStreamCB; | |
| 34 | |
| 35 typedef base::Callback<void(ChunkDemuxerStream*, const TextTrackConfig&)> | |
| 36 NewTextTrackCB; | |
| 37 | |
| 38 MediaSourceState(std::unique_ptr<StreamParser> stream_parser, | |
| 39 std::unique_ptr<FrameProcessor> frame_processor, | |
| 40 const CreateDemuxerStreamCB& create_demuxer_stream_cb, | |
| 41 const scoped_refptr<MediaLog>& media_log); | |
| 42 | |
| 43 ~MediaSourceState(); | |
| 44 | |
| 45 void Init(const StreamParser::InitCB& init_cb, | |
| 46 const std::string& expected_codecs, | |
| 47 const StreamParser::EncryptedMediaInitDataCB& | |
| 48 encrypted_media_init_data_cb, | |
| 49 const NewTextTrackCB& new_text_track_cb); | |
| 50 | |
| 51 // Appends new data to the StreamParser. | |
| 52 // Returns true if the data was successfully appended. Returns false if an | |
| 53 // error occurred. |*timestamp_offset| is used and possibly updated by the | |
| 54 // append. |append_window_start| and |append_window_end| correspond to the MSE | |
| 55 // spec's similarly named source buffer attributes that are used in coded | |
| 56 // frame processing. | |
| 57 bool Append(const uint8_t* data, | |
| 58 size_t length, | |
| 59 TimeDelta append_window_start, | |
| 60 TimeDelta append_window_end, | |
| 61 TimeDelta* timestamp_offset); | |
| 62 | |
| 63 // Aborts the current append sequence and resets the parser. | |
| 64 void ResetParserState(TimeDelta append_window_start, | |
| 65 TimeDelta append_window_end, | |
| 66 TimeDelta* timestamp_offset); | |
| 67 | |
| 68 // Calls Remove(|start|, |end|, |duration|) on all | |
| 69 // ChunkDemuxerStreams managed by this object. | |
| 70 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration); | |
| 71 | |
| 72 // If the buffer is full, attempts to try to free up space, as specified in | |
| 73 // the "Coded Frame Eviction Algorithm" in the Media Source Extensions Spec. | |
| 74 // Returns false iff buffer is still full after running eviction. | |
| 75 // https://w3c.github.io/media-source/#sourcebuffer-coded-frame-eviction | |
| 76 bool EvictCodedFrames(DecodeTimestamp media_time, size_t newDataSize); | |
| 77 | |
| 78 // Returns true if currently parsing a media segment, or false otherwise. | |
| 79 bool parsing_media_segment() const { return parsing_media_segment_; } | |
| 80 | |
| 81 // Sets |frame_processor_|'s sequence mode to |sequence_mode|. | |
| 82 void SetSequenceMode(bool sequence_mode); | |
| 83 | |
| 84 // Signals the coded frame processor to update its group start timestamp to be | |
| 85 // |timestamp_offset| if it is in sequence append mode. | |
| 86 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset); | |
| 87 | |
| 88 // Returns the range of buffered data in this source, capped at |duration|. | |
| 89 // |ended| - Set to true if end of stream has been signaled and the special | |
| 90 // end of stream range logic needs to be executed. | |
| 91 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const; | |
| 92 | |
| 93 // Returns the highest PTS of currently buffered frames in this source, or | |
| 94 // base::TimeDelta() if none of the streams contain buffered data. | |
| 95 TimeDelta GetHighestPresentationTimestamp() const; | |
| 96 | |
| 97 // Returns the highest buffered duration across all streams managed | |
| 98 // by this object. | |
| 99 // Returns TimeDelta() if none of the streams contain buffered data. | |
| 100 TimeDelta GetMaxBufferedDuration() const; | |
| 101 | |
| 102 // Helper methods that call methods with similar names on all the | |
| 103 // ChunkDemuxerStreams managed by this object. | |
| 104 void StartReturningData(); | |
| 105 void AbortReads(); | |
| 106 void Seek(TimeDelta seek_time); | |
| 107 void CompletePendingReadIfPossible(); | |
| 108 void OnSetDuration(TimeDelta duration); | |
| 109 void MarkEndOfStream(); | |
| 110 void UnmarkEndOfStream(); | |
| 111 void Shutdown(); | |
| 112 // Sets the memory limit on each stream of a specific type. | |
| 113 // |memory_limit| is the maximum number of bytes each stream of type |type| | |
| 114 // is allowed to hold in its buffer. | |
| 115 void SetMemoryLimits(DemuxerStream::Type type, size_t memory_limit); | |
| 116 bool IsSeekWaitingForData() const; | |
| 117 | |
| 118 using RangesList = std::vector<Ranges<TimeDelta>>; | |
| 119 static Ranges<TimeDelta> ComputeRangesIntersection( | |
| 120 const RangesList& active_ranges, | |
| 121 bool ended); | |
| 122 | |
| 123 void SetTracksWatcher(const Demuxer::MediaTracksUpdatedCB& tracks_updated_cb); | |
| 124 | |
| 125 private: | |
| 126 // State advances through this list. The intent is to ensure at least one | |
| 127 // config is received prior to parser calling initialization callback, and | |
| 128 // that such initialization callback occurs at most once per parser. | |
| 129 enum State { | |
| 130 UNINITIALIZED = 0, | |
| 131 PENDING_PARSER_CONFIG, | |
| 132 PENDING_PARSER_INIT, | |
| 133 PARSER_INITIALIZED | |
| 134 }; | |
| 135 | |
| 136 // Called by the |stream_parser_| when a new initialization segment is | |
| 137 // encountered. | |
| 138 // Returns true on a successful call. Returns false if an error occurred while | |
| 139 // processing decoder configurations. | |
| 140 bool OnNewConfigs(std::string expected_codecs, | |
| 141 std::unique_ptr<MediaTracks> tracks, | |
| 142 const StreamParser::TextTrackConfigMap& text_configs); | |
| 143 | |
| 144 // Called by the |stream_parser_| at the beginning of a new media segment. | |
| 145 void OnNewMediaSegment(); | |
| 146 | |
| 147 // Called by the |stream_parser_| at the end of a media segment. | |
| 148 void OnEndOfMediaSegment(); | |
| 149 | |
| 150 // Called by the |stream_parser_| when new buffers have been parsed. | |
| 151 // It processes the new buffers using |frame_processor_|, which includes | |
| 152 // appending the processed frames to associated demuxer streams for each | |
| 153 // frame's track. | |
| 154 // Returns true on a successful call. Returns false if an error occurred while | |
| 155 // processing the buffers. | |
| 156 bool OnNewBuffers(const StreamParser::BufferQueueMap& buffer_queue_map); | |
| 157 | |
| 158 void OnSourceInitDone(const StreamParser::InitParameters& params); | |
| 159 | |
| 160 // Sets memory limits for all demuxer streams. | |
| 161 void SetStreamMemoryLimits(); | |
| 162 | |
| 163 // Tracks the number of MEDIA_LOGs emitted for segments missing expected audio | |
| 164 // or video blocks. Useful to prevent log spam. | |
| 165 int num_missing_track_logs_ = 0; | |
| 166 | |
| 167 CreateDemuxerStreamCB create_demuxer_stream_cb_; | |
| 168 NewTextTrackCB new_text_track_cb_; | |
| 169 | |
| 170 // During Append(), if OnNewBuffers() coded frame processing updates the | |
| 171 // timestamp offset then |*timestamp_offset_during_append_| is also updated | |
| 172 // so Append()'s caller can know the new offset. This pointer is only non-NULL | |
| 173 // during the lifetime of an Append() call. | |
| 174 TimeDelta* timestamp_offset_during_append_; | |
| 175 | |
| 176 // During Append(), coded frame processing triggered by OnNewBuffers() | |
| 177 // requires these two attributes. These are only valid during the lifetime of | |
| 178 // an Append() call. | |
| 179 TimeDelta append_window_start_during_append_; | |
| 180 TimeDelta append_window_end_during_append_; | |
| 181 | |
| 182 // Keeps track of whether a media segment is being parsed. | |
| 183 bool parsing_media_segment_; | |
| 184 | |
| 185 // Valid only while |parsing_media_segment_| is true. These flags enable | |
| 186 // warning when the parsed media segment doesn't have frames for some track. | |
| 187 std::map<StreamParser::TrackId, bool> media_segment_has_data_for_track_; | |
| 188 | |
| 189 // The object used to parse appended data. | |
| 190 std::unique_ptr<StreamParser> stream_parser_; | |
| 191 | |
| 192 // Note that ChunkDemuxerStreams are created and owned by the parent | |
| 193 // ChunkDemuxer. They are not owned by |this|. | |
| 194 using DemuxerStreamMap = std::map<StreamParser::TrackId, ChunkDemuxerStream*>; | |
| 195 DemuxerStreamMap audio_streams_; | |
| 196 DemuxerStreamMap video_streams_; | |
| 197 DemuxerStreamMap text_streams_; | |
| 198 | |
| 199 std::unique_ptr<FrameProcessor> frame_processor_; | |
| 200 scoped_refptr<MediaLog> media_log_; | |
| 201 StreamParser::InitCB init_cb_; | |
| 202 | |
| 203 State state_; | |
| 204 | |
| 205 // During Append(), OnNewConfigs() will trigger the initialization segment | |
| 206 // received algorithm. Note, the MSE spec explicitly disallows this algorithm | |
| 207 // during an Abort(), since Abort() is allowed only to emit coded frames, and | |
| 208 // only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment). So we | |
| 209 // also have a flag here that indicates if Append is in progress and we can | |
| 210 // invoke this callback. | |
| 211 Demuxer::MediaTracksUpdatedCB init_segment_received_cb_; | |
| 212 bool append_in_progress_ = false; | |
| 213 bool first_init_segment_received_ = false; | |
| 214 | |
| 215 std::vector<AudioCodec> expected_audio_codecs_; | |
| 216 std::vector<VideoCodec> expected_video_codecs_; | |
| 217 | |
| 218 // Indicates that timestampOffset should be updated automatically during | |
| 219 // OnNewBuffers() based on the earliest end timestamp of the buffers provided. | |
| 220 // TODO(wolenetz): Refactor this function while integrating April 29, 2014 | |
| 221 // changes to MSE spec. See http://crbug.com/371499. | |
| 222 bool auto_update_timestamp_offset_; | |
| 223 | |
| 224 DISALLOW_COPY_AND_ASSIGN(MediaSourceState); | |
| 225 }; | |
| 226 | |
| 227 } // namespace media | |
| 228 | |
| 229 #endif // MEDIA_FILTERS_MEDIA_SOURCE_STATE_H_ | |
| OLD | NEW |