Refactored not to expose raw pointers on ProxyList class.
[chromium-blink-merge.git] / chromecast / media / cma / backend / media_component_device_default.cc
blob4c367dd69de61481ffab1a627c81a8ea31423a13
1 // Copyright 2015 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 #include "chromecast/media/cma/backend/media_component_device_default.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chromecast/public/media/cast_decoder_buffer.h"
12 #include "chromecast/public/media/decrypt_context.h"
13 #include "chromecast/public/media/media_pipeline_device_params.h"
14 #include "chromecast/public/task_runner.h"
15 #include "media/base/buffers.h"
17 namespace chromecast {
18 namespace media {
20 namespace {
22 // Maximum number of frames that can be buffered.
23 const size_t kMaxFrameCount = 20;
25 // Wraps base::Closure in the chromecast/public TaskRunner interface.
26 class ClosureTask : public TaskRunner::Task {
27 public:
28 ClosureTask(const base::Closure& cb) : cb_(cb) {}
29 void Run() override { cb_.Run(); }
31 private:
32 base::Closure cb_;
35 // Returns whether or not transitioning from |state1| to |state2| is valid.
36 inline static bool IsValidStateTransition(MediaComponentDevice::State state1,
37 MediaComponentDevice::State state2) {
38 if (state2 == state1)
39 return true;
41 // All states can transition to |kStateError|.
42 if (state2 == MediaComponentDevice::kStateError)
43 return true;
45 // All the other valid FSM transitions.
46 switch (state1) {
47 case MediaComponentDevice::kStateUninitialized:
48 return state2 == MediaComponentDevice::kStateIdle;
49 case MediaComponentDevice::kStateIdle:
50 return state2 == MediaComponentDevice::kStateRunning ||
51 state2 == MediaComponentDevice::kStatePaused ||
52 state2 == MediaComponentDevice::kStateUninitialized;
53 case MediaComponentDevice::kStatePaused:
54 return state2 == MediaComponentDevice::kStateIdle ||
55 state2 == MediaComponentDevice::kStateRunning;
56 case MediaComponentDevice::kStateRunning:
57 return state2 == MediaComponentDevice::kStateIdle ||
58 state2 == MediaComponentDevice::kStatePaused;
59 case MediaComponentDevice::kStateError:
60 return state2 == MediaComponentDevice::kStateUninitialized;
62 default:
63 return false;
67 } // namespace
69 MediaComponentDeviceDefault::DefaultDecoderBuffer::DefaultDecoderBuffer()
70 : size(0) {
73 MediaComponentDeviceDefault::DefaultDecoderBuffer::~DefaultDecoderBuffer() {
76 MediaComponentDeviceDefault::MediaComponentDeviceDefault(
77 const MediaPipelineDeviceParams& params,
78 MediaClockDevice* media_clock_device)
79 : task_runner_(params.task_runner),
80 media_clock_device_(media_clock_device),
81 state_(kStateUninitialized),
82 rendering_time_(::media::kNoTimestamp()),
83 decoded_frame_count_(0),
84 decoded_byte_count_(0),
85 scheduled_rendering_task_(false),
86 weak_factory_(this) {
87 weak_this_ = weak_factory_.GetWeakPtr();
88 thread_checker_.DetachFromThread();
91 MediaComponentDeviceDefault::~MediaComponentDeviceDefault() {
94 void MediaComponentDeviceDefault::SetClient(Client* client) {
95 DCHECK(thread_checker_.CalledOnValidThread());
96 client_.reset(client);
99 MediaComponentDevice::State MediaComponentDeviceDefault::GetState() const {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 return state_;
104 bool MediaComponentDeviceDefault::SetState(State new_state) {
105 DCHECK(thread_checker_.CalledOnValidThread());
106 DCHECK(IsValidStateTransition(state_, new_state));
107 state_ = new_state;
109 if (state_ == kStateIdle) {
110 // Back to the idle state: reset a bunch of parameters.
111 is_eos_ = false;
112 rendering_time_ = ::media::kNoTimestamp();
113 decoded_frame_count_ = 0;
114 decoded_byte_count_ = 0;
115 frames_.clear();
116 pending_buffer_ = scoped_ptr<CastDecoderBuffer>();
117 frame_pushed_cb_.reset();
118 return true;
121 if (state_ == kStateRunning) {
122 if (!scheduled_rendering_task_) {
123 scheduled_rendering_task_ = true;
124 task_runner_->PostTask(
125 new ClosureTask(
126 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_)),
129 return true;
132 return true;
135 bool MediaComponentDeviceDefault::SetStartPts(int64_t time_microseconds) {
136 DCHECK(thread_checker_.CalledOnValidThread());
137 DCHECK_EQ(state_, kStateIdle);
138 rendering_time_ = base::TimeDelta::FromMicroseconds(time_microseconds);
139 return true;
142 MediaComponentDevice::FrameStatus MediaComponentDeviceDefault::PushFrame(
143 DecryptContext* decrypt_context_in,
144 CastDecoderBuffer* buffer_in,
145 FrameStatusCB* completion_cb_in) {
146 DCHECK(thread_checker_.CalledOnValidThread());
147 DCHECK(state_ == kStatePaused || state_ == kStateRunning);
148 DCHECK(!is_eos_);
149 DCHECK(!pending_buffer_.get());
150 DCHECK(buffer_in);
152 scoped_ptr<DecryptContext> decrypt_context(decrypt_context_in);
153 scoped_ptr<FrameStatusCB> completion_cb(completion_cb_in);
155 scoped_ptr<CastDecoderBuffer> buffer(buffer_in);
156 if (buffer->end_of_stream()) {
157 is_eos_ = true;
158 return kFrameSuccess;
161 if (frames_.size() > kMaxFrameCount) {
162 pending_buffer_ = buffer.Pass();
163 frame_pushed_cb_ = completion_cb.Pass();
164 return kFramePending;
167 DefaultDecoderBuffer fake_buffer;
168 fake_buffer.size = buffer->data_size();
169 fake_buffer.pts = base::TimeDelta::FromMicroseconds(buffer->timestamp());
170 frames_.push_back(fake_buffer);
171 return kFrameSuccess;
174 MediaComponentDeviceDefault::RenderingDelay
175 MediaComponentDeviceDefault::GetRenderingDelay() const {
176 NOTIMPLEMENTED();
177 return RenderingDelay();
180 void MediaComponentDeviceDefault::RenderTask() {
181 scheduled_rendering_task_ = false;
183 if (state_ != kStateRunning)
184 return;
186 base::TimeDelta media_time = base::TimeDelta::FromMicroseconds(
187 media_clock_device_->GetTimeMicroseconds());
188 if (media_time == ::media::kNoTimestamp()) {
189 scheduled_rendering_task_ = true;
190 task_runner_->PostTask(
191 new ClosureTask(
192 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_)),
193 50);
194 return;
197 while (!frames_.empty() && frames_.front().pts <= media_time) {
198 rendering_time_ = frames_.front().pts;
199 decoded_frame_count_++;
200 decoded_byte_count_ += frames_.front().size;
201 frames_.pop_front();
202 if (pending_buffer_.get()) {
203 DefaultDecoderBuffer fake_buffer;
204 fake_buffer.size = pending_buffer_->data_size();
205 fake_buffer.pts =
206 base::TimeDelta::FromMicroseconds(pending_buffer_->timestamp());
207 frames_.push_back(fake_buffer);
208 pending_buffer_ = scoped_ptr<CastDecoderBuffer>();
209 frame_pushed_cb_->Run(kFrameSuccess);
210 frame_pushed_cb_.reset();
214 if (frames_.empty() && is_eos_) {
215 if (client_) {
216 client_->OnEndOfStream();
218 return;
221 scheduled_rendering_task_ = true;
222 task_runner_->PostTask(
223 new ClosureTask(
224 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_)),
225 50);
228 bool MediaComponentDeviceDefault::GetStatistics(Statistics* stats) const {
229 if (state_ != kStateRunning)
230 return false;
232 // Note: what is returned here is not the number of samples but the number of
233 // frames. The value is different for audio.
234 stats->decoded_bytes = decoded_byte_count_;
235 stats->decoded_samples = decoded_frame_count_;
236 stats->dropped_samples = 0;
237 return true;
240 } // namespace media
241 } // namespace chromecast