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

Side by Side Diff: media/audio/cras/cras_unified.cc

Issue 509893002: Revert of Remove the last piece of deprecated synchronous IO code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « media/audio/cras/cras_unified.h ('k') | media/audio/fake_audio_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "media/audio/cras/cras_unified.h" 5 #include "media/audio/cras/cras_unified.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "media/audio/cras/audio_manager_cras.h" 8 #include "media/audio/cras/audio_manager_cras.h"
9 9
10 namespace media { 10 namespace media {
11 11
12 // Overview of operation: 12 // Overview of operation:
13 // 1) An object of CrasUnifiedStream is created by the AudioManager 13 // 1) An object of CrasUnifiedStream is created by the AudioManager
14 // factory: audio_man->MakeAudioStream(). 14 // factory: audio_man->MakeAudioStream().
15 // 2) Next some thread will call Open(), at that point a client is created and 15 // 2) Next some thread will call Open(), at that point a client is created and
16 // configured for the correct format and sample rate. 16 // configured for the correct format and sample rate.
17 // 3) Then Start(source) is called and a stream is added to the CRAS client 17 // 3) Then Start(source) is called and a stream is added to the CRAS client
18 // which will create its own thread that periodically calls the source for more 18 // which will create its own thread that periodically calls the source for more
19 // data as buffers are being consumed. 19 // data as buffers are being consumed.
20 // 4) When finished Stop() is called, which is handled by stopping the stream. 20 // 4) When finished Stop() is called, which is handled by stopping the stream.
21 // 5) Finally Close() is called. It cleans up and notifies the audio manager, 21 // 5) Finally Close() is called. It cleans up and notifies the audio manager,
22 // which likely will destroy this object. 22 // which likely will destroy this object.
23 // 23 //
24 // For output-only streams, a unified stream is created with 0 input channels.
25 //
26 // Simplified data flow for unified streams:
27 //
28 // +-------------+ +------------------+
29 // | CRAS Server | | Chrome Client |
30 // +------+------+ Add Stream +---------+--------+
31 // |<----------------------------------|
32 // | |
33 // | buffer_frames captured to shm |
34 // |---------------------------------->|
35 // | | UnifiedCallback()
36 // | | ReadWriteAudio()
37 // | |
38 // | buffer_frames written to shm |
39 // |<----------------------------------|
40 // | |
41 // ... Repeats for each block. ...
42 // | |
43 // | |
44 // | Remove stream |
45 // |<----------------------------------|
46 // | |
47 //
24 // Simplified data flow for output only streams: 48 // Simplified data flow for output only streams:
25 // 49 //
26 // +-------------+ +------------------+ 50 // +-------------+ +------------------+
27 // | CRAS Server | | Chrome Client | 51 // | CRAS Server | | Chrome Client |
28 // +------+------+ Add Stream +---------+--------+ 52 // +------+------+ Add Stream +---------+--------+
29 // |<----------------------------------| 53 // |<----------------------------------|
30 // | | 54 // | |
31 // | Near out of samples, request more | 55 // | Near out of samples, request more |
32 // |---------------------------------->| 56 // |---------------------------------->|
33 // | | UnifiedCallback() 57 // | | UnifiedCallback()
(...skipping 21 matching lines...) Expand all
55 params_(params), 79 params_(params),
56 bytes_per_frame_(0), 80 bytes_per_frame_(0),
57 is_playing_(false), 81 is_playing_(false),
58 volume_(1.0), 82 volume_(1.0),
59 manager_(manager), 83 manager_(manager),
60 source_callback_(NULL), 84 source_callback_(NULL),
61 stream_direction_(CRAS_STREAM_OUTPUT) { 85 stream_direction_(CRAS_STREAM_OUTPUT) {
62 DCHECK(manager_); 86 DCHECK(manager_);
63 DCHECK(params_.channels() > 0); 87 DCHECK(params_.channels() > 0);
64 88
89 // Must have at least one input or output. If there are both they must be the
90 // same.
91 int input_channels = params_.input_channels();
92
93 if (input_channels) {
94 // A unified stream for input and output.
95 DCHECK(params_.channels() == input_channels);
96 stream_direction_ = CRAS_STREAM_UNIFIED;
97 input_bus_ = AudioBus::Create(input_channels,
98 params_.frames_per_buffer());
99 }
100
65 output_bus_ = AudioBus::Create(params); 101 output_bus_ = AudioBus::Create(params);
66 } 102 }
67 103
68 CrasUnifiedStream::~CrasUnifiedStream() { 104 CrasUnifiedStream::~CrasUnifiedStream() {
69 DCHECK(!is_playing_); 105 DCHECK(!is_playing_);
70 } 106 }
71 107
72 bool CrasUnifiedStream::Open() { 108 bool CrasUnifiedStream::Open() {
73 // Sanity check input values. 109 // Sanity check input values.
74 if (params_.sample_rate() <= 0) { 110 if (params_.sample_rate() <= 0) {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 uint8* input_samples, 318 uint8* input_samples,
283 uint8* output_samples, 319 uint8* output_samples,
284 const timespec* input_ts, 320 const timespec* input_ts,
285 const timespec* output_ts) { 321 const timespec* output_ts) {
286 switch (stream_direction_) { 322 switch (stream_direction_) {
287 case CRAS_STREAM_OUTPUT: 323 case CRAS_STREAM_OUTPUT:
288 return WriteAudio(frames, output_samples, output_ts); 324 return WriteAudio(frames, output_samples, output_ts);
289 case CRAS_STREAM_INPUT: 325 case CRAS_STREAM_INPUT:
290 NOTREACHED() << "CrasUnifiedStream doesn't support input streams."; 326 NOTREACHED() << "CrasUnifiedStream doesn't support input streams.";
291 return 0; 327 return 0;
328 case CRAS_STREAM_UNIFIED:
329 return ReadWriteAudio(frames, input_samples, output_samples,
330 input_ts, output_ts);
292 default: 331 default:
293 break; 332 break;
294 } 333 }
295 334
296 return 0; 335 return 0;
297 } 336 }
298 337
338 // Note these are run from a real time thread, so don't waste cycles here.
339 uint32 CrasUnifiedStream::ReadWriteAudio(size_t frames,
340 uint8* input_samples,
341 uint8* output_samples,
342 const timespec* input_ts,
343 const timespec* output_ts) {
344 DCHECK_EQ(frames, static_cast<size_t>(output_bus_->frames()));
345 DCHECK(source_callback_);
346
347 uint32 bytes_per_sample = bytes_per_frame_ / params_.channels();
348 input_bus_->FromInterleaved(input_samples, frames, bytes_per_sample);
349
350 // Determine latency and pass that on to the source. We have the capture time
351 // of the first input sample and the playback time of the next audio sample
352 // passed from the audio server, add them together for total latency.
353 uint32 total_delay_bytes;
354 timespec latency_ts = {0, 0};
355 cras_client_calc_capture_latency(input_ts, &latency_ts);
356 total_delay_bytes = GetBytesLatency(latency_ts);
357 cras_client_calc_playback_latency(output_ts, &latency_ts);
358 total_delay_bytes += GetBytesLatency(latency_ts);
359
360 int frames_filled = source_callback_->OnMoreData(
361 output_bus_.get(),
362 AudioBuffersState(0, total_delay_bytes));
363
364 output_bus_->ToInterleaved(frames_filled, bytes_per_sample, output_samples);
365
366 return frames_filled;
367 }
368
299 uint32 CrasUnifiedStream::WriteAudio(size_t frames, 369 uint32 CrasUnifiedStream::WriteAudio(size_t frames,
300 uint8* buffer, 370 uint8* buffer,
301 const timespec* sample_ts) { 371 const timespec* sample_ts) {
302 DCHECK_EQ(frames, static_cast<size_t>(output_bus_->frames())); 372 DCHECK_EQ(frames, static_cast<size_t>(output_bus_->frames()));
303 373
304 // Determine latency and pass that on to the source. 374 // Determine latency and pass that on to the source.
305 timespec latency_ts = {0, 0}; 375 timespec latency_ts = {0, 0};
306 cras_client_calc_playback_latency(sample_ts, &latency_ts); 376 cras_client_calc_playback_latency(sample_ts, &latency_ts);
307 377
308 int frames_filled = source_callback_->OnMoreData( 378 int frames_filled = source_callback_->OnMoreData(
309 output_bus_.get(), AudioBuffersState(0, GetBytesLatency(latency_ts))); 379 output_bus_.get(), AudioBuffersState(0, GetBytesLatency(latency_ts)));
310 380
311 // Note: If this ever changes to output raw float the data must be clipped and 381 // Note: If this ever changes to output raw float the data must be clipped and
312 // sanitized since it may come from an untrusted source such as NaCl. 382 // sanitized since it may come from an untrusted source such as NaCl.
313 output_bus_->ToInterleaved( 383 output_bus_->ToInterleaved(
314 frames_filled, bytes_per_frame_ / params_.channels(), buffer); 384 frames_filled, bytes_per_frame_ / params_.channels(), buffer);
315 385
316 return frames_filled; 386 return frames_filled;
317 } 387 }
318 388
319 void CrasUnifiedStream::NotifyStreamError(int err) { 389 void CrasUnifiedStream::NotifyStreamError(int err) {
320 // This will remove the stream from the client. 390 // This will remove the stream from the client.
321 if (source_callback_) 391 if (source_callback_)
322 source_callback_->OnError(this); 392 source_callback_->OnError(this);
323 } 393 }
324 394
325 } // namespace media 395 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/cras/cras_unified.h ('k') | media/audio/fake_audio_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698