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

Side by Side Diff: chromecast/media/cma/backend/media_component_device_default.cc

Issue 1257013003: Load CMA backend from shared library (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: DCHECK on posted task Created 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 #include "chromecast/media/cma/backend/media_component_device_default.h" 5 #include "chromecast/media/cma/backend/media_component_device_default.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback_helpers.h" 8 #include "base/callback_helpers.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
11 #include "chromecast/media/cma/base/decoder_buffer_base.h" 11 #include "chromecast/public/media/cast_decoder_buffer.h"
12 #include "chromecast/public/media/media_pipeline_device_params.h"
13 #include "chromecast/public/task_runner.h"
12 #include "media/base/buffers.h" 14 #include "media/base/buffers.h"
13 15
14 namespace chromecast { 16 namespace chromecast {
15 namespace media { 17 namespace media {
16 18
17 namespace { 19 namespace {
18 20
19 // Maximum number of frames that can be buffered. 21 // Maximum number of frames that can be buffered.
20 const size_t kMaxFrameCount = 20; 22 const size_t kMaxFrameCount = 20;
21 23
24 // Wraps base::Closure in the chromecast/public TaskRunner interface.
25 class ClosureTask : public TaskRunner::Task {
26 public:
27 ClosureTask(const base::Closure& cb) : cb_(cb) {}
28 void Run() override { cb_.Run(); }
29
30 private:
31 base::Closure cb_;
32 };
33
34 // Returns whether or not transitioning from |state1| to |state2| is valid.
35 inline static bool IsValidStateTransition(MediaComponentDevice::State state1,
36 MediaComponentDevice::State state2) {
37 if (state2 == state1)
38 return true;
39
40 // All states can transition to |kStateError|.
41 if (state2 == MediaComponentDevice::kStateError)
42 return true;
43
44 // All the other valid FSM transitions.
45 switch (state1) {
46 case MediaComponentDevice::kStateUninitialized:
47 return state2 == MediaComponentDevice::kStateIdle;
48 case MediaComponentDevice::kStateIdle:
49 return state2 == MediaComponentDevice::kStateRunning ||
50 state2 == MediaComponentDevice::kStatePaused ||
51 state2 == MediaComponentDevice::kStateUninitialized;
52 case MediaComponentDevice::kStatePaused:
53 return state2 == MediaComponentDevice::kStateIdle ||
54 state2 == MediaComponentDevice::kStateRunning;
55 case MediaComponentDevice::kStateRunning:
56 return state2 == MediaComponentDevice::kStateIdle ||
57 state2 == MediaComponentDevice::kStatePaused;
58 case MediaComponentDevice::kStateError:
59 return state2 == MediaComponentDevice::kStateUninitialized;
60
61 default:
62 return false;
63 }
64 }
65
22 } // namespace 66 } // namespace
23 67
24 MediaComponentDeviceDefault::DefaultDecoderBuffer::DefaultDecoderBuffer() 68 MediaComponentDeviceDefault::DefaultDecoderBuffer::DefaultDecoderBuffer()
25 : size(0) { 69 : size(0) {
26 } 70 }
27 71
28 MediaComponentDeviceDefault::DefaultDecoderBuffer::~DefaultDecoderBuffer() { 72 MediaComponentDeviceDefault::DefaultDecoderBuffer::~DefaultDecoderBuffer() {
29 } 73 }
30 74
31 MediaComponentDeviceDefault::MediaComponentDeviceDefault( 75 MediaComponentDeviceDefault::MediaComponentDeviceDefault(
76 const MediaPipelineDeviceParams& params,
32 MediaClockDevice* media_clock_device) 77 MediaClockDevice* media_clock_device)
33 : media_clock_device_(media_clock_device), 78 : task_runner_(params.task_runner),
79 media_clock_device_(media_clock_device),
34 state_(kStateUninitialized), 80 state_(kStateUninitialized),
35 rendering_time_(::media::kNoTimestamp()), 81 rendering_time_(::media::kNoTimestamp()),
36 decoded_frame_count_(0), 82 decoded_frame_count_(0),
37 decoded_byte_count_(0), 83 decoded_byte_count_(0),
38 scheduled_rendering_task_(false), 84 scheduled_rendering_task_(false),
39 weak_factory_(this) { 85 weak_factory_(this) {
40 weak_this_ = weak_factory_.GetWeakPtr(); 86 weak_this_ = weak_factory_.GetWeakPtr();
41 DetachFromThread(); 87 thread_checker_.DetachFromThread();
42 } 88 }
43 89
44 MediaComponentDeviceDefault::~MediaComponentDeviceDefault() { 90 MediaComponentDeviceDefault::~MediaComponentDeviceDefault() {
45 } 91 }
46 92
47 void MediaComponentDeviceDefault::SetClient(const Client& client) { 93 void MediaComponentDeviceDefault::SetClient(Client* client) {
48 DCHECK(CalledOnValidThread()); 94 DCHECK(thread_checker_.CalledOnValidThread());
49 client_ = client; 95 client_.reset(client);
50 } 96 }
51 97
52 MediaComponentDevice::State MediaComponentDeviceDefault::GetState() const { 98 MediaComponentDevice::State MediaComponentDeviceDefault::GetState() const {
53 DCHECK(CalledOnValidThread()); 99 DCHECK(thread_checker_.CalledOnValidThread());
54 return state_; 100 return state_;
55 } 101 }
56 102
57 bool MediaComponentDeviceDefault::SetState(State new_state) { 103 bool MediaComponentDeviceDefault::SetState(State new_state) {
58 DCHECK(CalledOnValidThread()); 104 DCHECK(thread_checker_.CalledOnValidThread());
59 if (!MediaComponentDevice::IsValidStateTransition(state_, new_state)) 105 DCHECK(IsValidStateTransition(state_, new_state));
60 return false;
61 state_ = new_state; 106 state_ = new_state;
62 107
63 if (state_ == kStateIdle) { 108 if (state_ == kStateIdle) {
64 // Back to the idle state: reset a bunch of parameters. 109 // Back to the idle state: reset a bunch of parameters.
65 is_eos_ = false; 110 is_eos_ = false;
66 rendering_time_ = ::media::kNoTimestamp(); 111 rendering_time_ = ::media::kNoTimestamp();
67 decoded_frame_count_ = 0; 112 decoded_frame_count_ = 0;
68 decoded_byte_count_ = 0; 113 decoded_byte_count_ = 0;
69 frames_.clear(); 114 frames_.clear();
70 pending_buffer_ = scoped_refptr<DecoderBufferBase>(); 115 pending_buffer_ = scoped_ptr<CastDecoderBuffer>();
71 frame_pushed_cb_.Reset(); 116 frame_pushed_cb_.reset();
72 return true; 117 return true;
73 } 118 }
74 119
75 if (state_ == kStateRunning) { 120 if (state_ == kStateRunning) {
76 if (!scheduled_rendering_task_) { 121 if (!scheduled_rendering_task_) {
77 scheduled_rendering_task_ = true; 122 scheduled_rendering_task_ = true;
78 base::ThreadTaskRunnerHandle::Get()->PostTask( 123 task_runner_->PostTask(
79 FROM_HERE, 124 new ClosureTask(
80 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_)); 125 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_)),
126 0);
81 } 127 }
82 return true; 128 return true;
83 } 129 }
84 130
85 return true; 131 return true;
86 } 132 }
87 133
88 bool MediaComponentDeviceDefault::SetStartPts(base::TimeDelta time) { 134 bool MediaComponentDeviceDefault::SetStartPts(int64_t time_microseconds) {
89 DCHECK(CalledOnValidThread()); 135 DCHECK(thread_checker_.CalledOnValidThread());
90 DCHECK_EQ(state_, kStateIdle); 136 DCHECK_EQ(state_, kStateIdle);
91 rendering_time_ = time; 137 rendering_time_ = base::TimeDelta::FromMicroseconds(time_microseconds);
92 return true; 138 return true;
93 } 139 }
94 140
95 MediaComponentDevice::FrameStatus MediaComponentDeviceDefault::PushFrame( 141 MediaComponentDevice::FrameStatus MediaComponentDeviceDefault::PushFrame(
96 const scoped_refptr<DecryptContext>& decrypt_context, 142 CastKeySystem cast_key_system,
97 const scoped_refptr<DecoderBufferBase>& buffer, 143 CastDecoderBuffer* buffer_in,
98 const FrameStatusCB& completion_cb) { 144 FrameStatusCB* completion_cb_in) {
99 DCHECK(CalledOnValidThread()); 145 DCHECK(thread_checker_.CalledOnValidThread());
100 DCHECK(state_ == kStatePaused || state_ == kStateRunning); 146 DCHECK(state_ == kStatePaused || state_ == kStateRunning);
101 DCHECK(!is_eos_); 147 DCHECK(!is_eos_);
102 DCHECK(!pending_buffer_.get()); 148 DCHECK(!pending_buffer_.get());
103 DCHECK(buffer.get()); 149 DCHECK(buffer_in);
104 150
151 scoped_ptr<FrameStatusCB> completion_cb(completion_cb_in);
152
153 scoped_ptr<CastDecoderBuffer> buffer(buffer_in);
105 if (buffer->end_of_stream()) { 154 if (buffer->end_of_stream()) {
106 is_eos_ = true; 155 is_eos_ = true;
107 return kFrameSuccess; 156 return kFrameSuccess;
108 } 157 }
109 158
110 if (frames_.size() > kMaxFrameCount) { 159 if (frames_.size() > kMaxFrameCount) {
111 pending_buffer_ = buffer; 160 pending_buffer_ = buffer.Pass();
112 frame_pushed_cb_ = completion_cb; 161 frame_pushed_cb_ = completion_cb.Pass();
113 return kFramePending; 162 return kFramePending;
114 } 163 }
115 164
116 DefaultDecoderBuffer fake_buffer; 165 DefaultDecoderBuffer fake_buffer;
117 fake_buffer.size = buffer->data_size(); 166 fake_buffer.size = buffer->data_size();
118 fake_buffer.pts = buffer->timestamp(); 167 fake_buffer.pts = base::TimeDelta::FromMicroseconds(buffer->timestamp());
119 frames_.push_back(fake_buffer); 168 frames_.push_back(fake_buffer);
120 return kFrameSuccess; 169 return kFrameSuccess;
121 } 170 }
122 171
123 base::TimeDelta MediaComponentDeviceDefault::GetRenderingTime() const { 172 MediaComponentDeviceDefault::RenderingDelay
124 return rendering_time_; 173 MediaComponentDeviceDefault::GetRenderingDelay() const {
125 }
126
127 base::TimeDelta MediaComponentDeviceDefault::GetRenderingDelay() const {
128 NOTIMPLEMENTED(); 174 NOTIMPLEMENTED();
129 return ::media::kNoTimestamp(); 175 return RenderingDelay();
130 } 176 }
131 177
132 void MediaComponentDeviceDefault::RenderTask() { 178 void MediaComponentDeviceDefault::RenderTask() {
133 scheduled_rendering_task_ = false; 179 scheduled_rendering_task_ = false;
134 180
135 if (state_ != kStateRunning) 181 if (state_ != kStateRunning)
136 return; 182 return;
137 183
138 base::TimeDelta media_time = media_clock_device_->GetTime(); 184 base::TimeDelta media_time =
185 base::TimeDelta::FromMicroseconds(media_clock_device_->GetTime());
139 if (media_time == ::media::kNoTimestamp()) { 186 if (media_time == ::media::kNoTimestamp()) {
140 scheduled_rendering_task_ = true; 187 scheduled_rendering_task_ = true;
141 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 188 task_runner_->PostTask(
142 FROM_HERE, 189 new ClosureTask(
143 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_), 190 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_)),
144 base::TimeDelta::FromMilliseconds(50)); 191 50);
145 return; 192 return;
146 } 193 }
147 194
148 while (!frames_.empty() && frames_.front().pts <= media_time) { 195 while (!frames_.empty() && frames_.front().pts <= media_time) {
149 rendering_time_ = frames_.front().pts; 196 rendering_time_ = frames_.front().pts;
150 decoded_frame_count_++; 197 decoded_frame_count_++;
151 decoded_byte_count_ += frames_.front().size; 198 decoded_byte_count_ += frames_.front().size;
152 frames_.pop_front(); 199 frames_.pop_front();
153 if (pending_buffer_.get()) { 200 if (pending_buffer_.get()) {
154 DefaultDecoderBuffer fake_buffer; 201 DefaultDecoderBuffer fake_buffer;
155 fake_buffer.size = pending_buffer_->data_size(); 202 fake_buffer.size = pending_buffer_->data_size();
156 fake_buffer.pts = pending_buffer_->timestamp(); 203 fake_buffer.pts =
204 base::TimeDelta::FromMicroseconds(pending_buffer_->timestamp());
157 frames_.push_back(fake_buffer); 205 frames_.push_back(fake_buffer);
158 pending_buffer_ = scoped_refptr<DecoderBufferBase>(); 206 pending_buffer_ = scoped_ptr<CastDecoderBuffer>();
159 base::ResetAndReturn(&frame_pushed_cb_).Run(kFrameSuccess); 207 frame_pushed_cb_->Run(kFrameSuccess);
208 frame_pushed_cb_.reset();
160 } 209 }
161 } 210 }
162 211
163 if (frames_.empty() && is_eos_) { 212 if (frames_.empty() && is_eos_) {
164 if (!client_.eos_cb.is_null()) 213 if (client_) {
165 client_.eos_cb.Run(); 214 client_->OnEndOfStream();
215 }
166 return; 216 return;
167 } 217 }
168 218
169 scheduled_rendering_task_ = true; 219 scheduled_rendering_task_ = true;
170 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 220 task_runner_->PostTask(
171 FROM_HERE, 221 new ClosureTask(
172 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_), 222 base::Bind(&MediaComponentDeviceDefault::RenderTask, weak_this_)),
173 base::TimeDelta::FromMilliseconds(50)); 223 50);
174 } 224 }
175 225
176 bool MediaComponentDeviceDefault::GetStatistics(Statistics* stats) const { 226 bool MediaComponentDeviceDefault::GetStatistics(Statistics* stats) const {
177 if (state_ != kStateRunning) 227 if (state_ != kStateRunning)
178 return false; 228 return false;
179 229
180 // Note: what is returned here is not the number of samples but the number of 230 // Note: what is returned here is not the number of samples but the number of
181 // frames. The value is different for audio. 231 // frames. The value is different for audio.
182 stats->decoded_bytes = decoded_byte_count_; 232 stats->decoded_bytes = decoded_byte_count_;
183 stats->decoded_samples = decoded_frame_count_; 233 stats->decoded_samples = decoded_frame_count_;
184 stats->dropped_samples = 0; 234 stats->dropped_samples = 0;
185 return true; 235 return true;
186 } 236 }
187 237
188 } // namespace media 238 } // namespace media
189 } // namespace chromecast 239 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698