OLD | NEW |
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/sounds/audio_stream_handler.h" | 5 #include "media/audio/sounds/audio_stream_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/cancelable_callback.h" | 9 #include "base/cancelable_callback.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 class AudioStreamHandler::AudioStreamContainer | 36 class AudioStreamHandler::AudioStreamContainer |
37 : public AudioOutputStream::AudioSourceCallback { | 37 : public AudioOutputStream::AudioSourceCallback { |
38 public: | 38 public: |
39 AudioStreamContainer(const WavAudioHandler& wav_audio) | 39 AudioStreamContainer(const WavAudioHandler& wav_audio) |
40 : started_(false), | 40 : started_(false), |
41 stream_(NULL), | 41 stream_(NULL), |
42 cursor_(0), | 42 cursor_(0), |
43 delayed_stop_posted_(false), | 43 delayed_stop_posted_(false), |
44 wav_audio_(wav_audio) {} | 44 wav_audio_(wav_audio) {} |
45 | 45 |
46 virtual ~AudioStreamContainer() { | 46 ~AudioStreamContainer() override { |
47 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 47 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
48 } | 48 } |
49 | 49 |
50 void Play() { | 50 void Play() { |
51 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 51 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
52 | 52 |
53 if (!stream_) { | 53 if (!stream_) { |
54 const AudioParameters& p = wav_audio_.params(); | 54 const AudioParameters& p = wav_audio_.params(); |
55 const AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 55 const AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
56 p.channel_layout(), | 56 p.channel_layout(), |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 StopStream(); | 97 StopStream(); |
98 if (stream_) | 98 if (stream_) |
99 stream_->Close(); | 99 stream_->Close(); |
100 stream_ = NULL; | 100 stream_ = NULL; |
101 stop_closure_.Cancel(); | 101 stop_closure_.Cancel(); |
102 } | 102 } |
103 | 103 |
104 private: | 104 private: |
105 // AudioOutputStream::AudioSourceCallback overrides: | 105 // AudioOutputStream::AudioSourceCallback overrides: |
106 // Following methods could be called from *ANY* thread. | 106 // Following methods could be called from *ANY* thread. |
107 virtual int OnMoreData(AudioBus* dest, | 107 int OnMoreData(AudioBus* dest, uint32 /* total_bytes_delay */) override { |
108 uint32 /* total_bytes_delay */) override { | |
109 base::AutoLock al(state_lock_); | 108 base::AutoLock al(state_lock_); |
110 size_t bytes_written = 0; | 109 size_t bytes_written = 0; |
111 | 110 |
112 if (wav_audio_.AtEnd(cursor_) || | 111 if (wav_audio_.AtEnd(cursor_) || |
113 !wav_audio_.CopyTo(dest, cursor_, &bytes_written)) { | 112 !wav_audio_.CopyTo(dest, cursor_, &bytes_written)) { |
114 if (delayed_stop_posted_) | 113 if (delayed_stop_posted_) |
115 return 0; | 114 return 0; |
116 delayed_stop_posted_ = true; | 115 delayed_stop_posted_ = true; |
117 AudioManager::Get()->GetTaskRunner()->PostDelayedTask( | 116 AudioManager::Get()->GetTaskRunner()->PostDelayedTask( |
118 FROM_HERE, | 117 FROM_HERE, |
119 stop_closure_.callback(), | 118 stop_closure_.callback(), |
120 base::TimeDelta::FromMilliseconds(kKeepAliveMs)); | 119 base::TimeDelta::FromMilliseconds(kKeepAliveMs)); |
121 return 0; | 120 return 0; |
122 } | 121 } |
123 cursor_ += bytes_written; | 122 cursor_ += bytes_written; |
124 return dest->frames(); | 123 return dest->frames(); |
125 } | 124 } |
126 | 125 |
127 virtual void OnError(AudioOutputStream* /* stream */) override { | 126 void OnError(AudioOutputStream* /* stream */) override { |
128 LOG(ERROR) << "Error during system sound reproduction."; | 127 LOG(ERROR) << "Error during system sound reproduction."; |
129 } | 128 } |
130 | 129 |
131 void StopStream() { | 130 void StopStream() { |
132 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 131 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
133 | 132 |
134 if (stream_ && started_) { | 133 if (stream_ && started_) { |
135 // Do not hold the |state_lock_| while stopping the output stream. | 134 // Do not hold the |state_lock_| while stopping the output stream. |
136 stream_->Stop(); | 135 stream_->Stop(); |
137 if (g_observer_for_testing) | 136 if (g_observer_for_testing) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 g_observer_for_testing = observer; | 209 g_observer_for_testing = observer; |
211 } | 210 } |
212 | 211 |
213 // static | 212 // static |
214 void AudioStreamHandler::SetAudioSourceForTesting( | 213 void AudioStreamHandler::SetAudioSourceForTesting( |
215 AudioOutputStream::AudioSourceCallback* source) { | 214 AudioOutputStream::AudioSourceCallback* source) { |
216 g_audio_source_for_testing = source; | 215 g_audio_source_for_testing = source; |
217 } | 216 } |
218 | 217 |
219 } // namespace media | 218 } // namespace media |
OLD | NEW |