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"
17 #include "chromecast/public/media/stream_id.h"
20 class AudioDecoderConfig
;
21 class VideoDecoderConfig
;
24 namespace chromecast
{
27 class BufferingFrameProvider
;
29 class CodedFrameProvider
;
30 class DecoderBufferBase
;
31 class MediaComponentDevice
;
33 class AvPipelineImpl
{
45 typedef base::Callback
<
47 const ::media::AudioDecoderConfig
&,
48 const ::media::VideoDecoderConfig
&)> UpdateConfigCB
;
51 MediaComponentDevice
* media_component_device
,
52 const UpdateConfigCB
& update_config_cb
);
55 // Setting the frame provider or the client must be done in the
56 // |kUninitialized| state.
57 void SetCodedFrameProvider(scoped_ptr
<CodedFrameProvider
> frame_provider
,
58 size_t max_buffer_size
,
59 size_t max_frame_size
);
60 void SetClient(const AvPipelineClient
& client
);
62 // Initialize the pipeline.
65 // Setup the pipeline and ensure samples are available for the given media
66 // time, then start rendering samples.
67 bool StartPlayingFrom(base::TimeDelta time
,
68 const scoped_refptr
<BufferingState
>& buffering_state
);
70 // Flush any remaining samples in the pipeline.
71 // Invoke |done_cb| when flush is completed.
72 void Flush(const base::Closure
& done_cb
);
74 // Tear down the pipeline and release the hardware resources.
77 State
GetState() const { return state_
; }
78 void TransitionToState(State state
);
80 void SetCdm(BrowserCdmCast
* media_keys
);
83 // Callback invoked when the CDM state has changed in a way that might
84 // impact media playback.
85 void OnCdmStateChange();
87 // Callback invoked when playback has reached the end of stream.
90 // Feed the pipeline, getting the frames from |frame_provider_|.
91 void FetchBufferIfNeeded();
93 // Callback invoked when receiving a new frame from |frame_provider_|.
94 void OnNewFrame(const scoped_refptr
<DecoderBufferBase
>& buffer
,
95 const ::media::AudioDecoderConfig
& audio_config
,
96 const ::media::VideoDecoderConfig
& video_config
);
98 // Process a pending buffer.
99 void ProcessPendingBuffer();
101 void OnFramePushed(MediaComponentDevice::FrameStatus status
);
104 // - when BrowserCdm updated its state.
105 // - when BrowserCdm has been destroyed.
106 void OnCdmStateChanged();
107 void OnCdmDestroyed();
109 // Callback invoked when a frame has been buffered by |frame_provider_|
110 // which is a BufferingFrameProvider.
111 void OnFrameBuffered(const scoped_refptr
<DecoderBufferBase
>& buffer
,
112 bool is_at_max_capacity
);
113 void UpdatePlayableFrames();
115 base::ThreadChecker thread_checker_
;
117 UpdateConfigCB update_config_cb_
;
119 AvPipelineClient client_
;
122 MediaComponentDevice
* media_component_device_
;
124 // AV pipeline state.
128 // Can be NULL if there is no buffering strategy.
129 scoped_refptr
<BufferingState
> buffering_state_
;
131 // |buffered_time_| is the maximum timestamp of buffered frames.
132 // |playable_buffered_time_| is the maximum timestamp of buffered and
133 // playable frames (i.e. the key id is available for those frames).
134 base::TimeDelta buffered_time_
;
135 base::TimeDelta playable_buffered_time_
;
137 // List of frames buffered but not playable right away due to a missing
139 std::list
<scoped_refptr
<DecoderBufferBase
> > non_playable_frames_
;
142 scoped_ptr
<BufferingFrameProvider
> frame_provider_
;
144 // Indicate whether the frame fetching process is active.
145 bool enable_feeding_
;
147 // Indicate whether there is a pending buffer read.
151 scoped_refptr
<DecoderBufferBase
> pending_buffer_
;
153 // Indicate if there is a frame being pushed to the audio device.
156 // The media time is retrieved at regular intervals.
157 // Indicate whether time update is enabled.
158 bool enable_time_update_
;
159 bool pending_time_update_task_
;
161 // Decryption keys, if available.
162 BrowserCdmCast
* media_keys_
;
163 int media_keys_callback_id_
;
165 base::WeakPtr
<AvPipelineImpl
> weak_this_
;
166 base::WeakPtrFactory
<AvPipelineImpl
> weak_factory_
;
168 DISALLOW_COPY_AND_ASSIGN(AvPipelineImpl
);
172 } // namespace chromecast
174 #endif // CHROMECAST_MEDIA_CMA_BASE_AV_PIPELINE_IMPL_H_