| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/android/opensles_output.h" | 5 #include "media/audio/android/opensles_output.h" |
| 6 | 6 |
| 7 #include "base/android/build_info.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/macros.h" | 9 #include "base/macros.h" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 11 #include "media/audio/android/audio_manager_android.h" | 12 #include "media/audio/android/audio_manager_android.h" |
| 12 #include "media/base/audio_timestamp_helper.h" | 13 #include "media/base/audio_timestamp_helper.h" |
| 13 | 14 |
| 14 #define LOG_ON_FAILURE_AND_RETURN(op, ...) \ | 15 #define LOG_ON_FAILURE_AND_RETURN(op, ...) \ |
| 15 do { \ | 16 do { \ |
| 16 SLresult err = (op); \ | 17 SLresult err = (op); \ |
| 17 if (err != SL_RESULT_SUCCESS) { \ | 18 if (err != SL_RESULT_SUCCESS) { \ |
| 18 DLOG(ERROR) << #op << " failed: " << err; \ | 19 DLOG(ERROR) << #op << " failed: " << err; \ |
| 19 return __VA_ARGS__; \ | 20 return __VA_ARGS__; \ |
| 20 } \ | 21 } \ |
| 21 } while (0) | 22 } while (0) |
| 22 | 23 |
| 24 #if !defined(SL_ANDROID_KEY_PERFORMANCE_MODE) |
| 25 /** Audio Performance mode key */ |
| 26 #define SL_ANDROID_KEY_PERFORMANCE_MODE \ |
| 27 ((const SLchar*)"androidPerformanceMode") |
| 28 |
| 29 /** Audio performance values */ |
| 30 /* No specific performance requirement. Allows HW and SW pre/post |
| 31 * processing. */ |
| 32 #define SL_ANDROID_PERFORMANCE_NONE ((SLuint32)0x00000000) |
| 33 /* Priority given to latency. No HW or software pre/post processing. |
| 34 * This is the default if no performance mode is specified. */ |
| 35 #define SL_ANDROID_PERFORMANCE_LATENCY ((SLuint32)0x00000001) |
| 36 /* Priority given to latency while still allowing HW pre and post |
| 37 * processing. */ |
| 38 #define SL_ANDROID_PERFORMANCE_LATENCY_EFFECTS ((SLuint32)0x00000002) |
| 39 /* Priority given to power saving if latency is not a concern. |
| 40 * Allows HW and SW pre/post processing. */ |
| 41 #define SL_ANDROID_PERFORMANCE_POWER_SAVING ((SLuint32)0x00000003) |
| 42 #endif |
| 43 |
| 23 namespace media { | 44 namespace media { |
| 24 | 45 |
| 25 OpenSLESOutputStream::OpenSLESOutputStream(AudioManagerAndroid* manager, | 46 OpenSLESOutputStream::OpenSLESOutputStream(AudioManagerAndroid* manager, |
| 26 const AudioParameters& params, | 47 const AudioParameters& params, |
| 27 SLint32 stream_type) | 48 SLint32 stream_type) |
| 28 : audio_manager_(manager), | 49 : audio_manager_(manager), |
| 29 stream_type_(stream_type), | 50 stream_type_(stream_type), |
| 30 callback_(NULL), | 51 callback_(NULL), |
| 31 player_(NULL), | 52 player_(NULL), |
| 32 simple_buffer_queue_(NULL), | 53 simple_buffer_queue_(NULL), |
| 33 audio_data_(), | 54 audio_data_(), |
| 34 active_buffer_index_(0), | 55 active_buffer_index_(0), |
| 35 bytes_per_frame_(params.GetBytesPerFrame()), | 56 bytes_per_frame_(params.GetBytesPerFrame()), |
| 36 buffer_size_bytes_(params.GetBytesPerBuffer()), | 57 buffer_size_bytes_(params.GetBytesPerBuffer()), |
| 37 started_(false), | 58 started_(false), |
| 38 muted_(false), | 59 muted_(false), |
| 39 volume_(1.0), | 60 volume_(1.0), |
| 40 samples_per_second_(params.sample_rate()), | 61 samples_per_second_(params.sample_rate()), |
| 62 use_high_latency_(params.latency_tag() == AudioLatency::LATENCY_PLAYBACK), |
| 41 delay_calculator_(samples_per_second_) { | 63 delay_calculator_(samples_per_second_) { |
| 42 DVLOG(2) << "OpenSLESOutputStream::OpenSLESOutputStream(" | 64 DVLOG(2) << "OpenSLESOutputStream::OpenSLESOutputStream(" |
| 43 << "stream_type=" << stream_type << ")"; | 65 << "stream_type=" << stream_type << ")"; |
| 44 format_.formatType = SL_DATAFORMAT_PCM; | 66 format_.formatType = SL_DATAFORMAT_PCM; |
| 45 format_.numChannels = static_cast<SLuint32>(params.channels()); | 67 format_.numChannels = static_cast<SLuint32>(params.channels()); |
| 46 // Despite the name, this field is actually the sampling rate in millihertz :| | 68 // Despite the name, this field is actually the sampling rate in millihertz :| |
| 47 format_.samplesPerSec = static_cast<SLuint32>(samples_per_second_ * 1000); | 69 format_.samplesPerSec = static_cast<SLuint32>(samples_per_second_ * 1000); |
| 48 format_.bitsPerSample = params.bits_per_sample(); | 70 format_.bitsPerSample = params.bits_per_sample(); |
| 49 format_.containerSize = params.bits_per_sample(); | 71 format_.containerSize = params.bits_per_sample(); |
| 50 format_.endianness = SL_BYTEORDER_LITTLEENDIAN; | 72 format_.endianness = SL_BYTEORDER_LITTLEENDIAN; |
| 51 format_.channelMask = ChannelCountToSLESChannelMask(params.channels()); | 73 format_.channelMask = ChannelCountToSLESChannelMask(params.channels()); |
| 52 audio_bus_ = AudioBus::Create(params); | 74 audio_bus_ = AudioBus::Create(params); |
| 75 |
| 76 if (use_high_latency_) { |
| 77 LOG(ERROR) << "Enabling high latency audio with buffer size: " |
| 78 << params.frames_per_buffer(); |
| 79 } |
| 53 } | 80 } |
| 54 | 81 |
| 55 OpenSLESOutputStream::~OpenSLESOutputStream() { | 82 OpenSLESOutputStream::~OpenSLESOutputStream() { |
| 56 DVLOG(2) << "OpenSLESOutputStream::~OpenSLESOutputStream()"; | 83 DVLOG(2) << "OpenSLESOutputStream::~OpenSLESOutputStream()"; |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 DCHECK(!engine_object_.Get()); | 85 DCHECK(!engine_object_.Get()); |
| 59 DCHECK(!player_object_.Get()); | 86 DCHECK(!player_object_.Get()); |
| 60 DCHECK(!output_mixer_.Get()); | 87 DCHECK(!output_mixer_.Get()); |
| 61 DCHECK(!player_); | 88 DCHECK(!player_); |
| 62 DCHECK(!simple_buffer_queue_); | 89 DCHECK(!simple_buffer_queue_); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 292 |
| 266 // Create AudioPlayer and specify SL_IID_ANDROIDCONFIGURATION. | 293 // Create AudioPlayer and specify SL_IID_ANDROIDCONFIGURATION. |
| 267 SLAndroidConfigurationItf player_config; | 294 SLAndroidConfigurationItf player_config; |
| 268 LOG_ON_FAILURE_AND_RETURN( | 295 LOG_ON_FAILURE_AND_RETURN( |
| 269 player_object_->GetInterface( | 296 player_object_->GetInterface( |
| 270 player_object_.Get(), SL_IID_ANDROIDCONFIGURATION, &player_config), | 297 player_object_.Get(), SL_IID_ANDROIDCONFIGURATION, &player_config), |
| 271 false); | 298 false); |
| 272 | 299 |
| 273 // Set configuration using the stream type provided at construction. | 300 // Set configuration using the stream type provided at construction. |
| 274 LOG_ON_FAILURE_AND_RETURN( | 301 LOG_ON_FAILURE_AND_RETURN( |
| 275 (*player_config)->SetConfiguration(player_config, | 302 (*player_config) |
| 276 SL_ANDROID_KEY_STREAM_TYPE, | 303 ->SetConfiguration(player_config, SL_ANDROID_KEY_STREAM_TYPE, |
| 277 &stream_type_, | 304 &stream_type_, sizeof(SLint32)), |
| 278 sizeof(SLint32)), | |
| 279 false); | 305 false); |
| 280 | 306 |
| 307 // Set configuration using the stream type provided at construction. |
| 308 if (use_high_latency_ && base::android::BuildInfo::GetInstance()->sdk_int() >= |
| 309 base::android::SDK_VERSION_NOUGAT) { |
| 310 SLuint32 perf_mode = SL_ANDROID_PERFORMANCE_POWER_SAVING; |
| 311 LOG_ON_FAILURE_AND_RETURN( |
| 312 (*player_config) |
| 313 ->SetConfiguration(player_config, SL_ANDROID_KEY_PERFORMANCE_MODE, |
| 314 &perf_mode, sizeof(SLuint32)), |
| 315 false); |
| 316 } |
| 317 |
| 281 // Realize the player object in synchronous mode. | 318 // Realize the player object in synchronous mode. |
| 282 LOG_ON_FAILURE_AND_RETURN( | 319 LOG_ON_FAILURE_AND_RETURN( |
| 283 player_object_->Realize(player_object_.Get(), SL_BOOLEAN_FALSE), false); | 320 player_object_->Realize(player_object_.Get(), SL_BOOLEAN_FALSE), false); |
| 284 | 321 |
| 285 // Get an implicit player interface. | 322 // Get an implicit player interface. |
| 286 LOG_ON_FAILURE_AND_RETURN( | 323 LOG_ON_FAILURE_AND_RETURN( |
| 287 player_object_->GetInterface(player_object_.Get(), SL_IID_PLAY, &player_), | 324 player_object_->GetInterface(player_object_.Get(), SL_IID_PLAY, &player_), |
| 288 false); | 325 false); |
| 289 | 326 |
| 290 // Get the simple buffer queue interface. | 327 // Get the simple buffer queue interface. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 } | 445 } |
| 409 } | 446 } |
| 410 | 447 |
| 411 void OpenSLESOutputStream::HandleError(SLresult error) { | 448 void OpenSLESOutputStream::HandleError(SLresult error) { |
| 412 DLOG(ERROR) << "OpenSLES Output error " << error; | 449 DLOG(ERROR) << "OpenSLES Output error " << error; |
| 413 if (callback_) | 450 if (callback_) |
| 414 callback_->OnError(this); | 451 callback_->OnError(this); |
| 415 } | 452 } |
| 416 | 453 |
| 417 } // namespace media | 454 } // namespace media |
| OLD | NEW |