Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: media/base/pipeline.h

Issue 418143005: media: Introduce Renderer interface and RendererImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix media/BUILD.gn Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_BASE_PIPELINE_H_ 5 #ifndef MEDIA_BASE_PIPELINE_H_
6 #define MEDIA_BASE_PIPELINE_H_ 6 #define MEDIA_BASE_PIPELINE_H_
7 7
8 #include <string>
9
10 #include "base/gtest_prod_util.h" 8 #include "base/gtest_prod_util.h"
11 #include "base/memory/weak_ptr.h" 9 #include "base/memory/weak_ptr.h"
12 #include "base/synchronization/condition_variable.h"
13 #include "base/synchronization/lock.h" 10 #include "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h" 11 #include "base/threading/thread_checker.h"
15 #include "base/time/default_tick_clock.h" 12 #include "base/time/default_tick_clock.h"
16 #include "media/base/audio_renderer.h"
17 #include "media/base/buffering_state.h" 13 #include "media/base/buffering_state.h"
18 #include "media/base/demuxer.h" 14 #include "media/base/demuxer.h"
19 #include "media/base/media_export.h" 15 #include "media/base/media_export.h"
20 #include "media/base/pipeline_status.h" 16 #include "media/base/pipeline_status.h"
21 #include "media/base/ranges.h" 17 #include "media/base/ranges.h"
22 #include "media/base/serial_runner.h" 18 #include "media/base/serial_runner.h"
23 #include "media/base/video_rotation.h" 19 #include "media/base/video_rotation.h"
24 #include "ui/gfx/size.h" 20 #include "ui/gfx/size.h"
25 21
26 namespace base { 22 namespace base {
27 class SingleThreadTaskRunner; 23 class SingleThreadTaskRunner;
28 class TimeDelta; 24 class TimeDelta;
29 } 25 }
30 26
31 namespace media { 27 namespace media {
32 28
33 class FilterCollection; 29 class FilterCollection;
34 class MediaLog; 30 class MediaLog;
31 class Renderer;
35 class TextRenderer; 32 class TextRenderer;
36 class TextTrackConfig; 33 class TextTrackConfig;
37 class TimeDeltaInterpolator; 34 class TimeDeltaInterpolator;
38 class TimeSource;
39 class VideoRenderer;
40 35
41 // Metadata describing a pipeline once it has been initialized. 36 // Metadata describing a pipeline once it has been initialized.
42 struct PipelineMetadata { 37 struct PipelineMetadata {
43 PipelineMetadata() 38 PipelineMetadata()
44 : has_audio(false), has_video(false), video_rotation(VIDEO_ROTATION_0) {} 39 : has_audio(false), has_video(false), video_rotation(VIDEO_ROTATION_0) {}
45 40
46 bool has_audio; 41 bool has_audio;
47 bool has_video; 42 bool has_video;
48 gfx::Size natural_size; 43 gfx::Size natural_size;
49 VideoRotation video_rotation; 44 VideoRotation video_rotation;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 // been determined yet, then returns 0. 166 // been determined yet, then returns 0.
172 base::TimeDelta GetMediaDuration() const; 167 base::TimeDelta GetMediaDuration() const;
173 168
174 // Return true if loading progress has been made since the last time this 169 // Return true if loading progress has been made since the last time this
175 // method was called. 170 // method was called.
176 bool DidLoadingProgress(); 171 bool DidLoadingProgress();
177 172
178 // Gets the current pipeline statistics. 173 // Gets the current pipeline statistics.
179 PipelineStatistics GetStatistics() const; 174 PipelineStatistics GetStatistics() const;
180 175
181 void set_underflow_disabled_for_testing(bool disabled) { 176 void DisableUnderflowForTesting();
182 underflow_disabled_for_testing_ = disabled;
183 }
184 void SetTimeDeltaInterpolatorForTesting(TimeDeltaInterpolator* interpolator); 177 void SetTimeDeltaInterpolatorForTesting(TimeDeltaInterpolator* interpolator);
scherkus (not reviewing) 2014/08/21 21:17:00 can this get removed and for existing tests they c
xhwang 2014/08/22 19:11:31 Good point. Done!
185 void SetErrorForTesting(PipelineStatus status); 178 void SetErrorForTesting(PipelineStatus status);
186 179
187 private: 180 private:
188 FRIEND_TEST_ALL_PREFIXES(PipelineTest, GetBufferedTimeRanges); 181 FRIEND_TEST_ALL_PREFIXES(PipelineTest, GetBufferedTimeRanges);
189 FRIEND_TEST_ALL_PREFIXES(PipelineTest, EndedCallback); 182 FRIEND_TEST_ALL_PREFIXES(PipelineTest, EndedCallback);
190 FRIEND_TEST_ALL_PREFIXES(PipelineTest, AudioStreamShorterThanVideo); 183 FRIEND_TEST_ALL_PREFIXES(PipelineTest, AudioStreamShorterThanVideo);
191 friend class MediaLog; 184 friend class MediaLog;
192 185
193 // Pipeline states, as described above. 186 // Pipeline states, as described above.
194 enum State { 187 enum State {
195 kCreated, 188 kCreated,
196 kInitDemuxer, 189 kInitDemuxer,
197 kInitAudioRenderer, 190 kInitRenderer,
198 kInitVideoRenderer,
199 kSeeking, 191 kSeeking,
200 kPlaying, 192 kPlaying,
201 kStopping, 193 kStopping,
202 kStopped, 194 kStopped,
203 }; 195 };
204 196
205 // Updates |state_|. All state transitions should use this call. 197 // Updates |state_|. All state transitions should use this call.
206 void SetState(State next_state); 198 void SetState(State next_state);
207 199
208 static const char* GetStateString(State state); 200 static const char* GetStateString(State state);
(...skipping 12 matching lines...) Expand all
221 const TextTrackConfig& config) OVERRIDE; 213 const TextTrackConfig& config) OVERRIDE;
222 virtual void RemoveTextStream(DemuxerStream* text_stream) OVERRIDE; 214 virtual void RemoveTextStream(DemuxerStream* text_stream) OVERRIDE;
223 215
224 // Callback executed when a rendering error happened, initiating the teardown 216 // Callback executed when a rendering error happened, initiating the teardown
225 // sequence. 217 // sequence.
226 void OnError(PipelineStatus error); 218 void OnError(PipelineStatus error);
227 219
228 // Callback executed by filters to update statistics. 220 // Callback executed by filters to update statistics.
229 void OnUpdateStatistics(const PipelineStatistics& stats); 221 void OnUpdateStatistics(const PipelineStatistics& stats);
230 222
231 // Callback executed by audio renderer to update clock time.
232 void OnAudioTimeUpdate(base::TimeDelta time, base::TimeDelta max_time);
233
234 // Callback executed by video renderer to update clock time.
235 void OnVideoTimeUpdate(base::TimeDelta max_time);
236
237 // The following "task" methods correspond to the public methods, but these 223 // The following "task" methods correspond to the public methods, but these
238 // methods are run as the result of posting a task to the Pipeline's 224 // methods are run as the result of posting a task to the Pipeline's
239 // task runner. 225 // task runner.
240 void StartTask(); 226 void StartTask();
241 227
242 // Stops and destroys all filters, placing the pipeline in the kStopped state. 228 // Stops and destroys all filters, placing the pipeline in the kStopped state.
243 void StopTask(const base::Closure& stop_cb); 229 void StopTask(const base::Closure& stop_cb);
244 230
245 // Carries out stopping and destroying all filters, placing the pipeline in 231 // Carries out stopping and destroying all filters, placing the pipeline in
246 // the kStopped state. 232 // the kStopped state.
247 void ErrorChangedTask(PipelineStatus error); 233 void ErrorChangedTask(PipelineStatus error);
248 234
249 // Carries out notifying filters that the playback rate has changed. 235 // Carries out notifying filters that the playback rate has changed.
250 void PlaybackRateChangedTask(float playback_rate); 236 void PlaybackRateChangedTask(float playback_rate);
251 237
252 // Carries out notifying filters that the volume has changed. 238 // Carries out notifying filters that the volume has changed.
253 void VolumeChangedTask(float volume); 239 void VolumeChangedTask(float volume);
254 240
255 // Carries out notifying filters that we are seeking to a new timestamp. 241 // Carries out notifying filters that we are seeking to a new timestamp.
256 void SeekTask(base::TimeDelta time, const PipelineStatusCB& seek_cb); 242 void SeekTask(base::TimeDelta time, const PipelineStatusCB& seek_cb);
257 243
258 // Callbacks executed when a renderer has ended. 244 // Callbacks executed when a renderer has ended.
259 void OnAudioRendererEnded(); 245 void OnRendererEnded();
260 void OnVideoRendererEnded();
261 void OnTextRendererEnded(); 246 void OnTextRendererEnded();
262 void RunEndedCallbackIfNeeded(); 247 void RunEndedCallbackIfNeeded();
263 248
264 // Carries out adding a new text stream to the text renderer. 249 // Carries out adding a new text stream to the text renderer.
265 void AddTextStreamTask(DemuxerStream* text_stream, 250 void AddTextStreamTask(DemuxerStream* text_stream,
266 const TextTrackConfig& config); 251 const TextTrackConfig& config);
267 252
268 // Carries out removing a text stream from the text renderer. 253 // Carries out removing a text stream from the text renderer.
269 void RemoveTextStreamTask(DemuxerStream* text_stream); 254 void RemoveTextStreamTask(DemuxerStream* text_stream);
270 255
271 // Kicks off initialization for each media object, executing |done_cb| with 256 // Kicks off initialization for each media object, executing |done_cb| with
272 // the result when completed. 257 // the result when completed.
273 void InitializeDemuxer(const PipelineStatusCB& done_cb); 258 void InitializeDemuxer(const PipelineStatusCB& done_cb);
274 void InitializeAudioRenderer(const PipelineStatusCB& done_cb); 259 void InitializeRenderer(const PipelineStatusCB& done_cb);
275 void InitializeVideoRenderer(const PipelineStatusCB& done_cb);
276 260
277 // Kicks off destroying filters. Called by StopTask() and ErrorChangedTask(). 261 // Kicks off destroying filters. Called by StopTask() and ErrorChangedTask().
278 // When we start to tear down the pipeline, we will consider two cases: 262 // When we start to tear down the pipeline, we will consider two cases:
279 // 1. when pipeline has not been initialized, we will transit to stopping 263 // 1. when pipeline has not been initialized, we will transit to stopping
280 // state first. 264 // state first.
281 // 2. when pipeline has been initialized, we will first transit to pausing 265 // 2. when pipeline has been initialized, we will first transit to pausing
282 // => flushing => stopping => stopped state. 266 // => flushing => stopping => stopped state.
283 // This will remove the race condition during stop between filters. 267 // This will remove the race condition during stop between filters.
284 void TearDownPipeline(); 268 void TearDownPipeline();
285 269
286 // Compute the time corresponding to a byte offset. 270 // Compute the time corresponding to a byte offset.
287 base::TimeDelta TimeForByteOffset_Locked(int64 byte_offset) const; 271 base::TimeDelta TimeForByteOffset_Locked(int64 byte_offset) const;
288 272
289 void OnStateTransition(PipelineStatus status); 273 void OnStateTransition(PipelineStatus status);
290 void StateTransitionTask(PipelineStatus status); 274 void StateTransitionTask(PipelineStatus status);
291 275
292 // Initiates an asynchronous pause-flush-seek-preroll call sequence 276 // Initiates an asynchronous pause-flush-seek-preroll call sequence
293 // executing |done_cb| with the final status when completed. 277 // executing |done_cb| with the final status when completed.
294 void DoSeek(base::TimeDelta seek_timestamp, const PipelineStatusCB& done_cb); 278 void DoSeek(base::TimeDelta seek_timestamp, const PipelineStatusCB& done_cb);
295 279
296 // Initiates an asynchronous pause-flush-stop call sequence executing 280 // Initiates an asynchronous pause-flush-stop call sequence executing
297 // |done_cb| when completed. 281 // |done_cb| when completed.
298 void DoStop(const PipelineStatusCB& done_cb); 282 void DoStop(const PipelineStatusCB& done_cb);
299 void OnStopCompleted(PipelineStatus status); 283 void OnStopCompleted(PipelineStatus status);
300 284
301 // Collection of callback methods and helpers for tracking changes in 285 void BufferingStateChanged(BufferingState new_buffering_state);
302 // buffering state and transition from paused/underflow states and playing
303 // states.
304 //
305 // While in the kPlaying state:
306 // - A waiting to non-waiting transition indicates preroll has completed
307 // and StartPlayback() should be called
308 // - A non-waiting to waiting transition indicates underflow has occurred
309 // and PausePlayback() should be called
310 void BufferingStateChanged(BufferingState* buffering_state,
311 BufferingState new_buffering_state);
312 bool WaitingForEnoughData() const;
313 void PausePlayback();
314 void StartPlayback();
315
316 void PauseClockAndStopTicking_Locked();
317 void StartClockIfWaitingForTimeUpdate_Locked();
318 286
319 // Task runner used to execute pipeline tasks. 287 // Task runner used to execute pipeline tasks.
320 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 288 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
321 289
322 // MediaLog to which to log events. 290 // MediaLog to which to log events.
323 scoped_refptr<MediaLog> media_log_; 291 scoped_refptr<MediaLog> media_log_;
324 292
325 // Lock used to serialize access for the following data members. 293 // Lock used to serialize access for the following data members.
326 mutable base::Lock lock_; 294 mutable base::Lock lock_;
327 295
(...skipping 13 matching lines...) Expand all
341 float volume_; 309 float volume_;
342 310
343 // Current playback rate (>= 0.0f). This value is set immediately via 311 // Current playback rate (>= 0.0f). This value is set immediately via
344 // SetPlaybackRate() and a task is dispatched on the task runner to notify 312 // SetPlaybackRate() and a task is dispatched on the task runner to notify
345 // the filters. 313 // the filters.
346 float playback_rate_; 314 float playback_rate_;
347 315
348 // Current duration as reported by |demuxer_|. 316 // Current duration as reported by |demuxer_|.
349 base::TimeDelta duration_; 317 base::TimeDelta duration_;
350 318
351 // base::TickClock used by |interpolator_|.
352 base::DefaultTickClock default_tick_clock_;
353
354 // Tracks the most recent media time update and provides interpolated values
355 // as playback progresses.
356 scoped_ptr<TimeDeltaInterpolator> interpolator_;
357
358 enum InterpolationState {
359 // Audio (if present) is not rendering. Time isn't being interpolated.
360 INTERPOLATION_STOPPED,
361
362 // Audio (if present) is rendering. Time isn't being interpolated.
363 INTERPOLATION_WAITING_FOR_AUDIO_TIME_UPDATE,
364
365 // Audio (if present) is rendering. Time is being interpolated.
366 INTERPOLATION_STARTED,
367 };
368
369 InterpolationState interpolation_state_;
370
371 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that 319 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that
372 // the pipeline is operating correctly. Any other value indicates that the 320 // the pipeline is operating correctly. Any other value indicates that the
373 // pipeline is stopped or is stopping. Clients can call the Stop() method to 321 // pipeline is stopped or is stopping. Clients can call the Stop() method to
374 // reset the pipeline state, and restore this to PIPELINE_OK. 322 // reset the pipeline state, and restore this to PIPELINE_OK.
375 PipelineStatus status_; 323 PipelineStatus status_;
376 324
377 // The following data members are only accessed by tasks posted to 325 // The following data members are only accessed by tasks posted to
378 // |task_runner_|. 326 // |task_runner_|.
379 327
328 bool is_initialized_;
329
380 // Member that tracks the current state. 330 // Member that tracks the current state.
381 State state_; 331 State state_;
382 332
383 // The timestamp to start playback from after starting/seeking has completed. 333 // The timestamp to start playback from after starting/seeking has completed.
384 base::TimeDelta start_timestamp_; 334 base::TimeDelta start_timestamp_;
385 335
386 // Whether we've received the audio/video/text ended events. 336 // Whether we've received the audio/video/text ended events.
387 bool audio_ended_; 337 bool renderer_ended_;
388 bool video_ended_; 338 bool text_renderer_ended_;
389 bool text_ended_;
390
391 BufferingState audio_buffering_state_;
392 BufferingState video_buffering_state_;
393 339
394 // Temporary callback used for Start() and Seek(). 340 // Temporary callback used for Start() and Seek().
395 PipelineStatusCB seek_cb_; 341 PipelineStatusCB seek_cb_;
396 342
397 // Temporary callback used for Stop(). 343 // Temporary callback used for Stop().
398 base::Closure stop_cb_; 344 base::Closure stop_cb_;
399 345
400 // Permanent callbacks passed in via Start(). 346 // Permanent callbacks passed in via Start().
401 base::Closure ended_cb_; 347 base::Closure ended_cb_;
402 PipelineStatusCB error_cb_; 348 PipelineStatusCB error_cb_;
403 PipelineMetadataCB metadata_cb_; 349 PipelineMetadataCB metadata_cb_;
404 BufferingStateCB buffering_state_cb_; 350 BufferingStateCB buffering_state_cb_;
405 base::Closure duration_change_cb_; 351 base::Closure duration_change_cb_;
406 352
407 // Contains the demuxer and renderers to use when initializing. 353 // Contains the demuxer and renderers to use when initializing.
408 scoped_ptr<FilterCollection> filter_collection_; 354 scoped_ptr<FilterCollection> filter_collection_;
409 355
410 // Holds the initialized demuxer. Used for seeking. Owned by client. 356 // Holds the initialized demuxer. Used for seeking. Owned by client.
411 Demuxer* demuxer_; 357 Demuxer* demuxer_;
412 358
413 // Holds the initialized renderers. Used for setting the volume, 359 // Holds the initialized renderers. Used for setting the volume,
414 // playback rate, and determining when playback has finished. 360 // playback rate, and determining when playback has finished.
415 scoped_ptr<AudioRenderer> audio_renderer_; 361 scoped_ptr<Renderer> renderer_;
416 scoped_ptr<VideoRenderer> video_renderer_;
417 scoped_ptr<TextRenderer> text_renderer_; 362 scoped_ptr<TextRenderer> text_renderer_;
418 363
419 // Renderer-provided time source used to control playback.
420 TimeSource* time_source_;
421
422 PipelineStatistics statistics_; 364 PipelineStatistics statistics_;
423 365
424 scoped_ptr<SerialRunner> pending_callbacks_; 366 scoped_ptr<SerialRunner> pending_callbacks_;
425 367
426 bool underflow_disabled_for_testing_; 368 bool underflow_disabled_for_testing_;
369 TimeDeltaInterpolator* test_interpolator_;
427 370
428 base::ThreadChecker thread_checker_; 371 base::ThreadChecker thread_checker_;
429 372
430 // NOTE: Weak pointers must be invalidated before all other member variables. 373 // NOTE: Weak pointers must be invalidated before all other member variables.
431 base::WeakPtrFactory<Pipeline> weak_factory_; 374 base::WeakPtrFactory<Pipeline> weak_factory_;
432 375
433 DISALLOW_COPY_AND_ASSIGN(Pipeline); 376 DISALLOW_COPY_AND_ASSIGN(Pipeline);
434 }; 377 };
435 378
436 } // namespace media 379 } // namespace media
437 380
438 #endif // MEDIA_BASE_PIPELINE_H_ 381 #endif // MEDIA_BASE_PIPELINE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698