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

Side by Side Diff: services/media/framework/engine.cc

Issue 1678433002: Motown: Remove LPCM optimizations, fix prepare, add flush, add ActiveMultistreamSink model/stage (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 10 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "services/media/framework/engine.h" 5 #include "services/media/framework/engine.h"
6 6
7 namespace mojo { 7 namespace mojo {
8 namespace media { 8 namespace media {
9 9
10 uint32_t Engine::Part::input_count() { 10 Engine::Engine() {}
11 DCHECK(stage_ != nullptr); 11
12 return stage_->input_count(); 12 Engine::~Engine() {
13 base::AutoLock lock(lock_);
13 } 14 }
14 15
15 Engine::Input Engine::Part::input(uint32_t index) { 16 void Engine::PrepareInput(const InputRef& input_ref) {
16 DCHECK(stage_ != nullptr && index < stage_->input_count());
17 return Input(stage_, index);
18 }
19
20 Engine::Input Engine::Part::input() {
21 DCHECK(stage_ != nullptr && stage_->input_count() == 1);
22 return Input(stage_, 0);
23 }
24
25 uint32_t Engine::Part::output_count() {
26 DCHECK(stage_ != nullptr);
27 return stage_->output_count();
28 }
29
30 Engine::Output Engine::Part::output(uint32_t index) {
31 DCHECK(stage_ != nullptr && index < stage_->output_count());
32 return Output(stage_, index);
33 }
34
35 Engine::Output Engine::Part::output() {
36 DCHECK(stage_ != nullptr && stage_->output_count() == 1);
37 return Output(stage_, 0);
38 }
39
40 Engine::Part Engine::Part::upstream_part(uint32_t index) {
41 DCHECK(stage_ != nullptr && index < stage_->input_count());
42 return Part(stage_->input(index).upstream_stage());
43 }
44
45 Engine::Part Engine::Part::upstream_part() {
46 DCHECK(stage_ != nullptr && stage_->input_count() == 1);
47 return Part(stage_->input(0).upstream_stage());
48 }
49
50 Engine::Part Engine::Part::downstream_part(uint32_t index) {
51 DCHECK(stage_ != nullptr && index < stage_->output_count());
52 return Part(stage_->output(index).downstream_stage());
53 }
54
55 Engine::Part Engine::Part::downstream_part() {
56 DCHECK(stage_ != nullptr && stage_->output_count() == 1);
57 return Part(stage_->output(0).downstream_stage());
58 }
59
60 Engine::Engine() {
61 update_function_ = [this](Stage* stage) {
62 DCHECK(stage);
63 base::AutoLock lock(lock_);
64 UpdateUnsafe(stage);
65 UpdateUnsafe();
66 };
67 }
68
69 Engine::~Engine() {
70 Reset();
71 }
72
73 void Engine::RemovePart(Part part) {
74 DCHECK(part);
75 base::AutoLock lock(lock_);
76 RemoveUnsafe(part.stage_);
77 }
78
79 Engine::Part Engine::Connect(Output output, Input input) {
80 DCHECK(output);
81 DCHECK(input);
82
83 base::AutoLock lock(lock_); 17 base::AutoLock lock(lock_);
84 18
85 if (output.connected()) { 19 std::queue<InputRef> backlog;
86 DisconnectOutputUnsafe(output.stage_, output.index_); 20 backlog.push(input_ref);
87 }
88 if (input.connected()) {
89 DisconnectInputUnsafe(input.stage_, input.index_);
90 }
91 21
92 output.stage_output().connect(input.stage_, input.index_); 22 while (!backlog.empty()) {
93 input.stage_input().connect(output.stage_, output.index_); 23 InputRef input = backlog.front();
24 backlog.pop();
25 DCHECK(input.valid());
26 DCHECK(input.connected());
27 DCHECK(!input.actual().prepared());
94 28
95 return input.part(); 29 PayloadAllocator* allocator = input.stage_->PrepareInput(input.index_);
96 } 30 input.actual().set_prepared(true);
97 31
98 Engine::Part Engine::ConnectParts(Part upstream_part, Part downstream_part) { 32 const OutputRef& output = input.mate();
99 DCHECK(upstream_part);
100 DCHECK(downstream_part);
101 Connect(upstream_part.output(), downstream_part.input());
102 return downstream_part;
103 }
104 33
105 Engine::Part Engine::ConnectOutputToPart( 34 output.stage_->PrepareOutput(
106 Output output, 35 output.index_,
107 Part downstream_part) { 36 allocator,
108 DCHECK(output); 37 [this, &output, &backlog](size_t input_index) {
johngro 2016/02/08 22:33:36 I don't think that you are using your captured 'th
dalesat 2016/02/09 00:34:11 I've changed this a bit to use a standard visitor.
109 DCHECK(downstream_part); 38 backlog.push(InputRef(output.stage_, input_index));
110 Connect(output, downstream_part.input()); 39 });
111 return downstream_part;
112 }
113
114 Engine::Part Engine::ConnectPartToInput(Part upstream_part, Input input) {
115 DCHECK(upstream_part);
116 DCHECK(input);
117 Connect(upstream_part.output(), input);
118 return input.part();
119 }
120
121 void Engine::DisconnectOutput(Output output) {
122 DCHECK(output);
123
124 base::AutoLock lock(lock_);
125 DisconnectOutputUnsafe(output.stage_, output.index_);
126 }
127
128 void Engine::DisconnectInput(Input input) {
129 DCHECK(input);
130
131 base::AutoLock lock(lock_);
132 DisconnectInputUnsafe(input.stage_, input.index_);
133 }
134
135 void Engine::RemovePartsConnectedToPart(Part part) {
136 DCHECK(part);
137
138 base::AutoLock lock(lock_);
139
140 std::deque<Part> to_remove { part };
141
142 while (!to_remove.empty()) {
143 Part part = to_remove.front();
144 to_remove.pop_front();
145
146 for (uint32_t i = 0; i < part.input_count(); ++i) {
147 to_remove.push_back(part.upstream_part(i));
148 }
149
150 for (uint32_t i = 0; i < part.output_count(); ++i) {
151 to_remove.push_back(part.downstream_part(i));
152 }
153
154 RemoveUnsafe(part.stage_);
155 } 40 }
156 } 41 }
157 42
158 void Engine::RemovePartsConnectedToOutput(Output output) { 43 void Engine::UnprepareInput(const InputRef& input_ref) {
159 DCHECK(output); 44 base::AutoLock lock(lock_);
160 45
161 if (!output.connected()) { 46 std::queue<InputRef> backlog;
johngro 2016/02/08 22:33:36 backlog seems to be essentially unused here. In P
dalesat 2016/02/09 00:34:11 Done.
162 return; 47 backlog.push(input_ref);
163 }
164 48
165 Part downstream_part = output.downstream_part(); 49 while (!backlog.empty()) {
166 DisconnectOutput(output); 50 InputRef input = backlog.front();
167 RemovePartsConnectedToPart(downstream_part); 51 backlog.pop();
168 } 52 DCHECK(input.valid());
53 DCHECK(input.connected());
54 DCHECK(input.actual().prepared());
169 55
170 void Engine::RemovePartsConnectedToInput(Input input) { 56 input.stage_->UnprepareInput(input.index_);
171 DCHECK(input); 57 input.actual().set_prepared(false);
172 58
173 if (!input.connected()) { 59 input.mate().stage_->UnprepareOutput(input.mate().index_);
174 return;
175 }
176
177 Part upstream_part = input.upstream_part();
178 DisconnectInput(input);
179 RemovePartsConnectedToPart(upstream_part);
180 }
181
182 void Engine::Prepare() {
183 base::AutoLock lock(lock_);
184 for (Stage* sink : sinks_) {
185 sink->Prepare(update_function_);
186 sink->prepared_ = true;
187 uint32_t input_count = sink->input_count();
188 for (uint32_t input_index = 0; input_index < input_count; input_index++) {
189 MaybePrepareUnsafe(sink->input(input_index).upstream_stage());
190 }
191 } 60 }
192 } 61 }
193 62
194 void Engine::Prepare(Part part) { 63 void Engine::FlushOutput(const OutputRef& output_ref) {
195 DCHECK(part);
196 base::AutoLock lock(lock_); 64 base::AutoLock lock(lock_);
197 MaybePrepareUnsafe(part.stage_);
198 }
199 65
200 void Engine::PrimeSinks() { 66 std::queue<OutputRef> backlog;
201 lock_.Acquire(); 67 backlog.push(output_ref);
202 std::list<Stage*> sinks(sinks_);
203 lock_.Release();
204 68
205 // TODO(dalesat): Threading issue: these sinks may go away during priming. 69 while (!backlog.empty()) {
206 for (Stage* sink : sinks) { 70 OutputRef output = backlog.front();
207 sink->Prime(); 71 backlog.pop();
72 DCHECK(output.valid());
73 DCHECK(output.connected());
74 DCHECK(output.mate().actual().prepared());
75
76 output.stage_->FlushOutput(output.index_);
77
78 const InputRef& input = output.mate();
79
80 input.stage_->FlushInput(
81 input.index_,
82 [this, &input, &backlog](size_t output_index) {
johngro 2016/02/08 22:33:36 same comment as before. 'this' is unused and it i
83 backlog.push(OutputRef(input.stage_, output_index));
84 });
208 } 85 }
209 } 86 }
210 87
211 void Engine::Reset() { 88 void Engine::RequestUpdate(Stage* stage) {
89 DCHECK(stage);
212 base::AutoLock lock(lock_); 90 base::AutoLock lock(lock_);
213 while (!supply_backlog_.empty()) { 91 Update(stage);
214 supply_backlog_.pop(); 92 Update();
215 }
216 while (!demand_backlog_.empty()) {
217 demand_backlog_.pop();
218 }
219 sources_.clear();
220 sinks_.clear();
221 while (!stages_.empty()) {
222 Stage* stage = stages_.front();
223 stages_.pop_front();
224 delete stage;
225 }
226 } 93 }
227 94
228 void Engine::PushToSupplyBacklogUnsafe(Stage* stage) { 95 void Engine::PushToSupplyBacklog(Stage* stage) {
229 lock_.AssertAcquired(); 96 lock_.AssertAcquired();
97 DCHECK(stage);
230 98
231 DCHECK(stage);
232 packets_produced_ = true; 99 packets_produced_ = true;
233 if (!stage->in_supply_backlog_) { 100 if (!stage->in_supply_backlog_) {
234 supply_backlog_.push(stage); 101 supply_backlog_.push(stage);
235 stage->in_supply_backlog_ = true; 102 stage->in_supply_backlog_ = true;
236 } 103 }
237 } 104 }
238 105
239 void Engine::PushToDemandBacklogUnsafe(Stage* stage) { 106 void Engine::PushToDemandBacklog(Stage* stage) {
240 lock_.AssertAcquired(); 107 lock_.AssertAcquired();
108 DCHECK(stage);
241 109
242 DCHECK(stage);
243 if (!stage->in_demand_backlog_) { 110 if (!stage->in_demand_backlog_) {
244 demand_backlog_.push(stage); 111 demand_backlog_.push(stage);
245 stage->in_demand_backlog_ = true; 112 stage->in_demand_backlog_ = true;
246 } 113 }
247 } 114 }
248 115
249 Engine::Part Engine::Add(Stage* stage) { 116 void Engine::Update() {
250 base::AutoLock lock(lock_);
251
252 stages_.push_back(stage);
253 if (stage->input_count() == 0) {
254 sources_.push_back(stage);
255 }
256 if (stage->output_count() == 0) {
257 sinks_.push_back(stage);
258 }
259 return Part(stage);
260 }
261
262 void Engine::DisconnectOutputUnsafe(Stage* stage, uint32_t index) {
263 DCHECK(stage);
264 DCHECK(index < stage->output_count());
265
266 lock_.AssertAcquired();
267
268 StageOutput& stage_output = stage->output(index);
269
270 if (stage_output.downstream_stage() == nullptr) {
271 return;
272 }
273
274 stage_output.mate().disconnect();
275 stage_output.disconnect();
276 }
277
278 void Engine::DisconnectInputUnsafe(Stage* stage, uint32_t index) {
279 DCHECK(stage);
280 DCHECK(index < stage->input_count());
281
282 lock_.AssertAcquired();
283
284 StageInput& stage_input = stage->input(index);
285
286 if (stage_input.upstream_stage() == nullptr) {
287 return;
288 }
289
290 stage_input.mate().disconnect();
291 stage_input.disconnect();
292 }
293
294 void Engine::RemoveUnsafe(Stage* stage) {
295 DCHECK(stage);
296
297 lock_.AssertAcquired();
298
299 uint32_t input_count = stage->input_count();
300 for (uint32_t input_index = 0; input_index < input_count; input_index++) {
301 if (stage->input(input_index).connected()) {
302 DisconnectInputUnsafe(stage, input_index);
303 }
304 }
305
306 uint32_t output_count = stage->output_count();
307 for (uint32_t output_index = 0; output_index < output_count; output_index++) {
308 if (stage->output(output_index).connected()) {
309 DisconnectOutputUnsafe(stage, output_index);
310 }
311 }
312
313 sources_.remove(stage);
314 sinks_.remove(stage);
315 stages_.remove(stage);
316 delete stage;
317 }
318
319 // static
320 Stage* Engine::CreateStage(MultiStreamPacketSourcePtr source) {
321 return new DistributorStage(source);
322 }
323
324 // static
325 Stage* Engine::CreateStage(PacketTransformPtr transform) {
326 return new PacketTransformStage(transform);
327 }
328
329 // static
330 Stage* Engine::CreateStage(ActiveSourcePtr source) {
331 return new ActiveSourceStage(source);
332 }
333
334 // static
335 Stage* Engine::CreateStage(ActiveSinkPtr sink) {
336 return new ActiveSinkStage(sink);
337 }
338
339 // static
340 Stage* Engine::CreateStage(LpcmTransformPtr transform) {
341 return new LpcmTransformStage(transform);
342 }
343
344 void Engine::MaybePrepareUnsafe(Stage* stage) {
345 lock_.AssertAcquired();
346
347 if (stage == nullptr || stage->prepared_) {
348 return;
349 }
350
351 // Make sure all downstream stages have been prepared.
352 uint32_t output_count = stage->output_count();
353 for (uint32_t output_index = 0; output_index < output_count; output_index++) {
354 StageOutput& output = stage->output(output_index);
355 if (output.connected() && !output.downstream_stage()->prepared()) {
356 return;
357 }
358 }
359
360 stage->Prepare(update_function_);
361 stage->prepared_ = true;
362
363 // Prepare all upstream stages.
364 uint32_t input_count = stage->input_count();
365 for (uint32_t input_index = 0; input_index < input_count; input_index++) {
366 MaybePrepareUnsafe(stage->input(input_index).upstream_stage());
367 }
368 }
369
370 void Engine::UpdateUnsafe() {
371 lock_.AssertAcquired(); 117 lock_.AssertAcquired();
372 118
373 while (true) { 119 while (true) {
374 Stage* stage = PopFromSupplyBacklogUnsafe(); 120 Stage* stage = PopFromSupplyBacklog();
375 if (stage != nullptr) { 121 if (stage != nullptr) {
376 UpdateUnsafe(stage); 122 Update(stage);
377 continue; 123 continue;
378 } 124 }
379 125
380 stage = PopFromDemandBacklogUnsafe(); 126 stage = PopFromDemandBacklog();
381 if (stage != nullptr) { 127 if (stage != nullptr) {
382 UpdateUnsafe(stage); 128 Update(stage);
383 continue; 129 continue;
384 } 130 }
385 131
386 break; 132 break;
387 } 133 }
388 } 134 }
389 135
390 void Engine::UpdateUnsafe(Stage *stage) { 136 void Engine::Update(Stage *stage) {
391 lock_.AssertAcquired(); 137 lock_.AssertAcquired();
392 138
393 DCHECK(stage); 139 DCHECK(stage);
394 140
395 packets_produced_ = false; 141 packets_produced_ = false;
johngro 2016/02/08 22:33:36 the stateful nature of the packets_produced_ flag
dalesat 2016/02/09 00:34:11 The supply backlog doesn't necessarily grow, becau
johngro 2016/02/09 21:00:36 Acknowledged.
396 142
397 stage->Update(this); 143 stage->Update(this);
398 144
399 // If the stage produced packets, it may need to reevaluate demand later. 145 // If the stage produced packets, it may need to reevaluate demand later.
400 if (packets_produced_) { 146 if (packets_produced_) {
401 PushToDemandBacklogUnsafe(stage); 147 PushToDemandBacklog(stage);
402 } 148 }
403 } 149 }
404 150
405 Stage* Engine::PopFromSupplyBacklogUnsafe() { 151 Stage* Engine::PopFromSupplyBacklog() {
406 lock_.AssertAcquired(); 152 lock_.AssertAcquired();
407 153
408 if (supply_backlog_.empty()) { 154 if (supply_backlog_.empty()) {
409 return nullptr; 155 return nullptr;
410 } 156 }
411 157
412 Stage* stage = supply_backlog_.front(); 158 Stage* stage = supply_backlog_.front();
413 supply_backlog_.pop(); 159 supply_backlog_.pop();
414 DCHECK(stage->in_supply_backlog_); 160 DCHECK(stage->in_supply_backlog_);
415 stage->in_supply_backlog_ = false; 161 stage->in_supply_backlog_ = false;
416 return stage; 162 return stage;
417 } 163 }
418 164
419 Stage* Engine::PopFromDemandBacklogUnsafe() { 165 Stage* Engine::PopFromDemandBacklog() {
420 lock_.AssertAcquired(); 166 lock_.AssertAcquired();
421 167
422 if (demand_backlog_.empty()) { 168 if (demand_backlog_.empty()) {
423 return nullptr; 169 return nullptr;
424 } 170 }
425 171
426 Stage* stage = demand_backlog_.top(); 172 Stage* stage = demand_backlog_.top();
427 demand_backlog_.pop(); 173 demand_backlog_.pop();
428 DCHECK(stage->in_demand_backlog_); 174 DCHECK(stage->in_demand_backlog_);
429 stage->in_demand_backlog_ = false; 175 stage->in_demand_backlog_ = false;
430 return stage; 176 return stage;
431 } 177 }
432 178
433 } // namespace media 179 } // namespace media
434 } // namespace mojo 180 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698