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

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

Powered by Google App Engine
This is Rietveld 408576698