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
3 // found in the LICENSE file.
5 #ifndef CHROMECAST_MEDIA_CMA_BASE_AV_PIPELINE_IMPL_H_
6 #define CHROMECAST_MEDIA_CMA_BASE_AV_PIPELINE_IMPL_H_
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "chromecast/media/cma/backend/media_component_device.h"
16 #include "chromecast/media/cma/pipeline/av_pipeline_client.h"
19 class AudioDecoderConfig
;
20 class VideoDecoderConfig
;
23 namespace chromecast
{
26 class BufferingFrameProvider
;
28 class CodedFrameProvider
;
29 class DecoderBufferBase
;
30 class MediaComponentDevice
;
32 class AvPipelineImpl
{
44 typedef base::Callback
<
45 void(const ::media::AudioDecoderConfig
&,
46 const ::media::VideoDecoderConfig
&)> UpdateConfigCB
;
49 MediaComponentDevice
* media_component_device
,
50 const UpdateConfigCB
& update_config_cb
);
53 // Setting the frame provider or the client must be done in the
54 // |kUninitialized| state.
55 void SetCodedFrameProvider(scoped_ptr
<CodedFrameProvider
> frame_provider
,
56 size_t max_buffer_size
,
57 size_t max_frame_size
);
58 void SetClient(const AvPipelineClient
& client
);
60 // Initialize the pipeline.
63 // Setup the pipeline and ensure samples are available for the given media
64 // time, then start rendering samples.
65 bool StartPlayingFrom(base::TimeDelta time
,
66 const scoped_refptr
<BufferingState
>& buffering_state
);
68 // Flush any remaining samples in the pipeline.
69 // Invoke |done_cb| when flush is completed.
70 void Flush(const base::Closure
& done_cb
);
72 // Tear down the pipeline and release the hardware resources.
75 State
GetState() const { return state_
; }
76 void TransitionToState(State state
);
78 void SetCdm(BrowserCdmCast
* media_keys
);
81 // Callback invoked when the CDM state has changed in a way that might
82 // impact media playback.
83 void OnCdmStateChange();
85 // Callback invoked when playback has reached the end of stream.
88 // Feed the pipeline, getting the frames from |frame_provider_|.
89 void FetchBufferIfNeeded();
91 // Callback invoked when receiving a new frame from |frame_provider_|.
92 void OnNewFrame(const scoped_refptr
<DecoderBufferBase
>& buffer
,
93 const ::media::AudioDecoderConfig
& audio_config
,
94 const ::media::VideoDecoderConfig
& video_config
);
96 // Process a pending buffer.
97 void ProcessPendingBuffer();
99 void OnFramePushed(MediaComponentDevice::FrameStatus status
);
102 // - when BrowserCdm updated its state.
103 // - when BrowserCdm has been destroyed.
104 void OnCdmStateChanged();
105 void OnCdmDestroyed();
107 // Callback invoked when a frame has been buffered by |frame_provider_|
108 // which is a BufferingFrameProvider.
109 void OnFrameBuffered(const scoped_refptr
<DecoderBufferBase
>& buffer
,
110 bool is_at_max_capacity
);
111 void UpdatePlayableFrames();
113 base::ThreadChecker thread_checker_
;
115 UpdateConfigCB update_config_cb_
;
117 AvPipelineClient client_
;
120 MediaComponentDevice
* media_component_device_
;
122 // AV pipeline state.
126 // Can be NULL if there is no buffering strategy.
127 scoped_refptr
<BufferingState
> buffering_state_
;
129 // |buffered_time_| is the maximum timestamp of buffered frames.
130 // |playable_buffered_time_| is the maximum timestamp of buffered and
131 // playable frames (i.e. the key id is available for those frames).
132 base::TimeDelta buffered_time_
;
133 base::TimeDelta playable_buffered_time_
;
135 // List of frames buffered but not playable right away due to a missing
137 std::list
<scoped_refptr
<DecoderBufferBase
> > non_playable_frames_
;
140 scoped_ptr
<BufferingFrameProvider
> frame_provider_
;
142 // Indicate whether the frame fetching process is active.
143 bool enable_feeding_
;
145 // Indicate whether there is a pending buffer read.
149 scoped_refptr
<DecoderBufferBase
> pending_buffer_
;
151 // Indicate if there is a frame being pushed to the audio device.
154 // The media time is retrieved at regular intervals.
155 // Indicate whether time update is enabled.
156 bool enable_time_update_
;
157 bool pending_time_update_task_
;
159 // Decryption keys, if available.
160 BrowserCdmCast
* media_keys_
;
161 int media_keys_callback_id_
;
163 base::WeakPtr
<AvPipelineImpl
> weak_this_
;
164 base::WeakPtrFactory
<AvPipelineImpl
> weak_factory_
;
166 DISALLOW_COPY_AND_ASSIGN(AvPipelineImpl
);
170 } // namespace chromecast
172 #endif // CHROMECAST_MEDIA_CMA_BASE_AV_PIPELINE_IMPL_H_