[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / chromecast / media / cma / pipeline / av_pipeline_impl.h
blob70395fc95d65fd845bf079cb249e2f12a8a0dadc
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_
8 #include <list>
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"
19 namespace media {
20 class AudioDecoderConfig;
21 class VideoDecoderConfig;
24 namespace chromecast {
25 namespace media {
26 class BrowserCdmCast;
27 class BufferingFrameProvider;
28 class BufferingState;
29 class CodedFrameProvider;
30 class DecoderBufferBase;
31 class MediaComponentDevice;
33 class AvPipelineImpl {
34 public:
35 // Pipeline states.
36 enum State {
37 kUninitialized,
38 kPlaying,
39 kFlushing,
40 kFlushed,
41 kStopped,
42 kError,
45 typedef base::Callback<
46 void(StreamId id,
47 const ::media::AudioDecoderConfig&,
48 const ::media::VideoDecoderConfig&)> UpdateConfigCB;
50 AvPipelineImpl(
51 MediaComponentDevice* media_component_device,
52 const UpdateConfigCB& update_config_cb);
53 ~AvPipelineImpl();
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.
63 bool Initialize();
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.
75 void Stop();
77 State GetState() const { return state_; }
78 void TransitionToState(State state);
80 void SetCdm(BrowserCdmCast* media_keys);
82 private:
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.
88 void OnEos();
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);
103 // Callbacks:
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_;
121 // Backends.
122 MediaComponentDevice* media_component_device_;
124 // AV pipeline state.
125 State state_;
127 // Buffering 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
138 // key id.
139 std::list<scoped_refptr<DecoderBufferBase> > non_playable_frames_;
141 // Buffer provider.
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.
148 bool pending_read_;
150 // Pending buffer.
151 scoped_refptr<DecoderBufferBase> pending_buffer_;
153 // Indicate if there is a frame being pushed to the audio device.
154 bool pending_push_;
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);
171 } // namespace media
172 } // namespace chromecast
174 #endif // CHROMECAST_MEDIA_CMA_BASE_AV_PIPELINE_IMPL_H_