| 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/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "media/audio/android/audio_manager_android.h" | 9 #include "media/audio/android/audio_manager_android.h" |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 OpenSLESOutputStream::OpenSLESOutputStream(AudioManagerAndroid* manager, | 22 OpenSLESOutputStream::OpenSLESOutputStream(AudioManagerAndroid* manager, |
| 23 const AudioParameters& params) | 23 const AudioParameters& params) |
| 24 : audio_manager_(manager), | 24 : audio_manager_(manager), |
| 25 callback_(NULL), | 25 callback_(NULL), |
| 26 player_(NULL), | 26 player_(NULL), |
| 27 simple_buffer_queue_(NULL), | 27 simple_buffer_queue_(NULL), |
| 28 active_queue_(0), | 28 active_queue_(0), |
| 29 buffer_size_bytes_(0), | 29 buffer_size_bytes_(0), |
| 30 started_(false), | 30 started_(false), |
| 31 volume_(1.0) { | 31 volume_(1.0) { |
| 32 DVLOG(2) << "OpenSLESOutputStream::OpenSLESOutputStream()"; |
| 32 format_.formatType = SL_DATAFORMAT_PCM; | 33 format_.formatType = SL_DATAFORMAT_PCM; |
| 33 format_.numChannels = static_cast<SLuint32>(params.channels()); | 34 format_.numChannels = static_cast<SLuint32>(params.channels()); |
| 34 // Provides sampling rate in milliHertz to OpenSLES. | 35 // Provides sampling rate in milliHertz to OpenSLES. |
| 35 format_.samplesPerSec = static_cast<SLuint32>(params.sample_rate() * 1000); | 36 format_.samplesPerSec = static_cast<SLuint32>(params.sample_rate() * 1000); |
| 36 format_.bitsPerSample = params.bits_per_sample(); | 37 format_.bitsPerSample = params.bits_per_sample(); |
| 37 format_.containerSize = params.bits_per_sample(); | 38 format_.containerSize = params.bits_per_sample(); |
| 38 format_.endianness = SL_BYTEORDER_LITTLEENDIAN; | 39 format_.endianness = SL_BYTEORDER_LITTLEENDIAN; |
| 39 if (format_.numChannels == 1) | 40 if (format_.numChannels == 1) |
| 40 format_.channelMask = SL_SPEAKER_FRONT_CENTER; | 41 format_.channelMask = SL_SPEAKER_FRONT_CENTER; |
| 41 else if (format_.numChannels == 2) | 42 else if (format_.numChannels == 2) |
| 42 format_.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; | 43 format_.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; |
| 43 else | 44 else |
| 44 NOTREACHED() << "Unsupported number of channels: " << format_.numChannels; | 45 NOTREACHED() << "Unsupported number of channels: " << format_.numChannels; |
| 45 | 46 |
| 46 buffer_size_bytes_ = params.GetBytesPerBuffer(); | 47 buffer_size_bytes_ = params.GetBytesPerBuffer(); |
| 47 audio_bus_ = AudioBus::Create(params); | 48 audio_bus_ = AudioBus::Create(params); |
| 48 | 49 |
| 49 memset(&audio_data_, 0, sizeof(audio_data_)); | 50 memset(&audio_data_, 0, sizeof(audio_data_)); |
| 50 } | 51 } |
| 51 | 52 |
| 52 OpenSLESOutputStream::~OpenSLESOutputStream() { | 53 OpenSLESOutputStream::~OpenSLESOutputStream() { |
| 54 DVLOG(2) << "OpenSLESOutputStream::~OpenSLESOutputStream()"; |
| 53 DCHECK(!engine_object_.Get()); | 55 DCHECK(!engine_object_.Get()); |
| 54 DCHECK(!player_object_.Get()); | 56 DCHECK(!player_object_.Get()); |
| 55 DCHECK(!output_mixer_.Get()); | 57 DCHECK(!output_mixer_.Get()); |
| 56 DCHECK(!player_); | 58 DCHECK(!player_); |
| 57 DCHECK(!simple_buffer_queue_); | 59 DCHECK(!simple_buffer_queue_); |
| 58 DCHECK(!audio_data_[0]); | 60 DCHECK(!audio_data_[0]); |
| 59 } | 61 } |
| 60 | 62 |
| 61 bool OpenSLESOutputStream::Open() { | 63 bool OpenSLESOutputStream::Open() { |
| 64 DVLOG(2) << "OpenSLESOutputStream::Open()"; |
| 62 if (engine_object_.Get()) | 65 if (engine_object_.Get()) |
| 63 return false; | 66 return false; |
| 64 | 67 |
| 65 if (!CreatePlayer()) | 68 if (!CreatePlayer()) |
| 66 return false; | 69 return false; |
| 67 | 70 |
| 68 SetupAudioBuffer(); | 71 SetupAudioBuffer(); |
| 69 | 72 |
| 70 return true; | 73 return true; |
| 71 } | 74 } |
| 72 | 75 |
| 73 void OpenSLESOutputStream::Start(AudioSourceCallback* callback) { | 76 void OpenSLESOutputStream::Start(AudioSourceCallback* callback) { |
| 77 DVLOG(2) << "OpenSLESOutputStream::Start()"; |
| 74 DCHECK(callback); | 78 DCHECK(callback); |
| 75 DCHECK(player_); | 79 DCHECK(player_); |
| 76 DCHECK(simple_buffer_queue_); | 80 DCHECK(simple_buffer_queue_); |
| 77 if (started_) | 81 if (started_) |
| 78 return; | 82 return; |
| 79 | 83 |
| 80 // Enable the flags before streaming. | 84 // Enable the flags before streaming. |
| 81 callback_ = callback; | 85 callback_ = callback; |
| 82 active_queue_ = 0; | 86 active_queue_ = 0; |
| 83 started_ = true; | 87 started_ = true; |
| 84 | 88 |
| 85 // Avoid start-up glitches by filling up one buffer queue before starting | 89 // Avoid start-up glitches by filling up one buffer queue before starting |
| 86 // the stream. | 90 // the stream. |
| 87 FillBufferQueue(); | 91 FillBufferQueue(); |
| 88 | 92 |
| 89 // Start streaming data by setting the play state to |SL_PLAYSTATE_PLAYING|. | 93 // Start streaming data by setting the play state to |SL_PLAYSTATE_PLAYING|. |
| 90 LOG_ON_FAILURE_AND_RETURN( | 94 LOG_ON_FAILURE_AND_RETURN( |
| 91 (*player_)->SetPlayState(player_, SL_PLAYSTATE_PLAYING)); | 95 (*player_)->SetPlayState(player_, SL_PLAYSTATE_PLAYING)); |
| 92 } | 96 } |
| 93 | 97 |
| 94 void OpenSLESOutputStream::Stop() { | 98 void OpenSLESOutputStream::Stop() { |
| 99 DVLOG(2) << "OpenSLESOutputStream::Stop()"; |
| 95 if (!started_) | 100 if (!started_) |
| 96 return; | 101 return; |
| 97 | 102 |
| 98 started_ = false; | 103 started_ = false; |
| 99 // Stop playing by setting the play state to |SL_PLAYSTATE_STOPPED|. | 104 // Stop playing by setting the play state to |SL_PLAYSTATE_STOPPED|. |
| 100 LOG_ON_FAILURE_AND_RETURN( | 105 LOG_ON_FAILURE_AND_RETURN( |
| 101 (*player_)->SetPlayState(player_, SL_PLAYSTATE_STOPPED)); | 106 (*player_)->SetPlayState(player_, SL_PLAYSTATE_STOPPED)); |
| 102 | 107 |
| 103 // Clear the buffer queue so that the old data won't be played when | 108 // Clear the buffer queue so that the old data won't be played when |
| 104 // resuming playing. | 109 // resuming playing. |
| 105 LOG_ON_FAILURE_AND_RETURN( | 110 LOG_ON_FAILURE_AND_RETURN( |
| 106 (*simple_buffer_queue_)->Clear(simple_buffer_queue_)); | 111 (*simple_buffer_queue_)->Clear(simple_buffer_queue_)); |
| 107 } | 112 } |
| 108 | 113 |
| 109 void OpenSLESOutputStream::Close() { | 114 void OpenSLESOutputStream::Close() { |
| 115 DVLOG(2) << "OpenSLESOutputStream::Close()"; |
| 110 // Stop the stream if it is still playing. | 116 // Stop the stream if it is still playing. |
| 111 Stop(); | 117 Stop(); |
| 112 | 118 |
| 113 // Explicitly free the player objects and invalidate their associated | 119 // Explicitly free the player objects and invalidate their associated |
| 114 // interfaces. They have to be done in the correct order. | 120 // interfaces. They have to be done in the correct order. |
| 115 player_object_.Reset(); | 121 player_object_.Reset(); |
| 116 output_mixer_.Reset(); | 122 output_mixer_.Reset(); |
| 117 engine_object_.Reset(); | 123 engine_object_.Reset(); |
| 118 simple_buffer_queue_ = NULL; | 124 simple_buffer_queue_ = NULL; |
| 119 player_ = NULL; | 125 player_ = NULL; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 } | 310 } |
| 305 } | 311 } |
| 306 | 312 |
| 307 void OpenSLESOutputStream::HandleError(SLresult error) { | 313 void OpenSLESOutputStream::HandleError(SLresult error) { |
| 308 DLOG(ERROR) << "OpenSLES Output error " << error; | 314 DLOG(ERROR) << "OpenSLES Output error " << error; |
| 309 if (callback_) | 315 if (callback_) |
| 310 callback_->OnError(this); | 316 callback_->OnError(this); |
| 311 } | 317 } |
| 312 | 318 |
| 313 } // namespace media | 319 } // namespace media |
| OLD | NEW |