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 // Creates an output stream based on the ALSA PCM interface. | 5 // Creates an output stream based on the ALSA PCM interface. |
6 // | 6 // |
7 // On device write failure, the stream will move itself to an invalid state. | 7 // On device write failure, the stream will move itself to an invalid state. |
8 // No more data will be pulled from the data source, or written to the device. | 8 // No more data will be pulled from the data source, or written to the device. |
9 // All calls to public API functions will either no-op themselves, or return an | 9 // All calls to public API functions will either no-op themselves, or return an |
10 // error if possible. Specifically, If the stream is in an error state, Open() | 10 // error if possible. Specifically, If the stream is in an error state, Open() |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include <memory> | 27 #include <memory> |
28 #include <string> | 28 #include <string> |
29 | 29 |
30 #include "base/compiler_specific.h" | 30 #include "base/compiler_specific.h" |
31 #include "base/gtest_prod_util.h" | 31 #include "base/gtest_prod_util.h" |
32 #include "base/macros.h" | 32 #include "base/macros.h" |
33 #include "base/memory/ref_counted.h" | 33 #include "base/memory/ref_counted.h" |
34 #include "base/memory/weak_ptr.h" | 34 #include "base/memory/weak_ptr.h" |
35 #include "base/single_thread_task_runner.h" | 35 #include "base/single_thread_task_runner.h" |
36 #include "base/threading/non_thread_safe.h" | 36 #include "base/threading/non_thread_safe.h" |
| 37 #include "base/time/tick_clock.h" |
37 #include "base/time/time.h" | 38 #include "base/time/time.h" |
38 #include "media/audio/audio_io.h" | 39 #include "media/audio/audio_io.h" |
39 #include "media/base/audio_parameters.h" | 40 #include "media/base/audio_parameters.h" |
40 | 41 |
41 namespace media { | 42 namespace media { |
42 | 43 |
43 class AlsaWrapper; | 44 class AlsaWrapper; |
44 class AudioManagerBase; | 45 class AudioManagerBase; |
45 class ChannelMixer; | 46 class ChannelMixer; |
46 class SeekableBuffer; | 47 class SeekableBuffer; |
(...skipping 30 matching lines...) Expand all Loading... |
77 ~AlsaPcmOutputStream() override; | 78 ~AlsaPcmOutputStream() override; |
78 | 79 |
79 // Implementation of AudioOutputStream. | 80 // Implementation of AudioOutputStream. |
80 bool Open() override; | 81 bool Open() override; |
81 void Close() override; | 82 void Close() override; |
82 void Start(AudioSourceCallback* callback) override; | 83 void Start(AudioSourceCallback* callback) override; |
83 void Stop() override; | 84 void Stop() override; |
84 void SetVolume(double volume) override; | 85 void SetVolume(double volume) override; |
85 void GetVolume(double* volume) override; | 86 void GetVolume(double* volume) override; |
86 | 87 |
| 88 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock); |
| 89 |
87 private: | 90 private: |
88 friend class AlsaPcmOutputStreamTest; | 91 friend class AlsaPcmOutputStreamTest; |
89 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, | 92 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, |
90 AutoSelectDevice_DeviceSelect); | 93 AutoSelectDevice_DeviceSelect); |
91 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, | 94 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, |
92 AutoSelectDevice_FallbackDevices); | 95 AutoSelectDevice_FallbackDevices); |
93 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, AutoSelectDevice_HintFail); | 96 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, AutoSelectDevice_HintFail); |
94 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, BufferPacket); | 97 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, BufferPacket); |
95 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, BufferPacket_Negative); | 98 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, BufferPacket_Negative); |
96 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, BufferPacket_StopStream); | 99 FRIEND_TEST_ALL_PREFIXES(AlsaPcmOutputStreamTest, BufferPacket_StopStream); |
(...skipping 25 matching lines...) Expand all Loading... |
122 friend std::ostream& operator<<(std::ostream& os, InternalState); | 125 friend std::ostream& operator<<(std::ostream& os, InternalState); |
123 | 126 |
124 // Functions to get another packet from the data source and write it into the | 127 // Functions to get another packet from the data source and write it into the |
125 // ALSA device. | 128 // ALSA device. |
126 void BufferPacket(bool* source_exhausted); | 129 void BufferPacket(bool* source_exhausted); |
127 void WritePacket(); | 130 void WritePacket(); |
128 void WriteTask(); | 131 void WriteTask(); |
129 void ScheduleNextWrite(bool source_exhausted); | 132 void ScheduleNextWrite(bool source_exhausted); |
130 | 133 |
131 // Utility functions for talking with the ALSA API. | 134 // Utility functions for talking with the ALSA API. |
132 static base::TimeDelta FramesToTimeDelta(int frames, double sample_rate); | |
133 std::string FindDeviceForChannels(uint32_t channels); | 135 std::string FindDeviceForChannels(uint32_t channels); |
134 snd_pcm_sframes_t GetAvailableFrames(); | 136 snd_pcm_sframes_t GetAvailableFrames(); |
135 snd_pcm_sframes_t GetCurrentDelay(); | 137 snd_pcm_sframes_t GetCurrentDelay(); |
136 | 138 |
137 // Attempts to find the best matching linux audio device for the given number | 139 // Attempts to find the best matching linux audio device for the given number |
138 // of channels. This function will set |device_name_| and |channel_mixer_|. | 140 // of channels. This function will set |device_name_| and |channel_mixer_|. |
139 snd_pcm_t* AutoSelectDevice(uint32_t latency); | 141 snd_pcm_t* AutoSelectDevice(uint32_t latency); |
140 | 142 |
141 // Functions to safeguard state transitions. All changes to the object state | 143 // Functions to safeguard state transitions. All changes to the object state |
142 // should go through these functions. | 144 // should go through these functions. |
143 bool CanTransitionTo(InternalState to); | 145 bool CanTransitionTo(InternalState to); |
144 InternalState TransitionTo(InternalState to); | 146 InternalState TransitionTo(InternalState to); |
145 InternalState state(); | 147 InternalState state(); |
146 | 148 |
147 // API for Proxying calls to the AudioSourceCallback provided during | 149 // API for Proxying calls to the AudioSourceCallback provided during |
148 // Start(). | 150 // Start(). |
149 // | 151 // |
150 // TODO(ajwong): This is necessary because the ownership semantics for the | 152 // TODO(ajwong): This is necessary because the ownership semantics for the |
151 // |source_callback_| object are incorrect in AudioRenderHost. The callback | 153 // |source_callback_| object are incorrect in AudioRenderHost. The callback |
152 // is passed into the output stream, but ownership is not transfered which | 154 // is passed into the output stream, but ownership is not transfered which |
153 // requires a synchronization on access of the |source_callback_| to avoid | 155 // requires a synchronization on access of the |source_callback_| to avoid |
154 // using a deleted callback. | 156 // using a deleted callback. |
155 int RunDataCallback(AudioBus* audio_bus, uint32_t total_bytes_delay); | 157 int RunDataCallback(base::TimeDelta delay, |
| 158 base::TimeTicks delay_timestamp, |
| 159 AudioBus* audio_bus); |
156 void RunErrorCallback(int code); | 160 void RunErrorCallback(int code); |
157 | 161 |
158 // Changes the AudioSourceCallback to proxy calls to. Pass in NULL to | 162 // Changes the AudioSourceCallback to proxy calls to. Pass in NULL to |
159 // release ownership of the currently registered callback. | 163 // release ownership of the currently registered callback. |
160 void set_source_callback(AudioSourceCallback* callback); | 164 void set_source_callback(AudioSourceCallback* callback); |
161 | 165 |
162 // Configuration constants from the constructor. Referenceable by all threads | 166 // Configuration constants from the constructor. Referenceable by all threads |
163 // since they are constants. | 167 // since they are constants. |
164 const std::string requested_device_name_; | 168 const std::string requested_device_name_; |
165 const snd_pcm_format_t pcm_format_; | 169 const snd_pcm_format_t pcm_format_; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 | 205 |
202 AudioSourceCallback* source_callback_; | 206 AudioSourceCallback* source_callback_; |
203 | 207 |
204 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 208 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
205 std::unique_ptr<AudioBus> audio_bus_; | 209 std::unique_ptr<AudioBus> audio_bus_; |
206 | 210 |
207 // Channel mixer and temporary bus for the final mixed channel data. | 211 // Channel mixer and temporary bus for the final mixed channel data. |
208 std::unique_ptr<ChannelMixer> channel_mixer_; | 212 std::unique_ptr<ChannelMixer> channel_mixer_; |
209 std::unique_ptr<AudioBus> mixed_audio_bus_; | 213 std::unique_ptr<AudioBus> mixed_audio_bus_; |
210 | 214 |
| 215 std::unique_ptr<base::TickClock> tick_clock_; |
| 216 |
211 // Allows us to run tasks on the AlsaPcmOutputStream instance which are | 217 // Allows us to run tasks on the AlsaPcmOutputStream instance which are |
212 // bound by its lifetime. | 218 // bound by its lifetime. |
213 // NOTE: Weak pointers must be invalidated before all other member variables. | 219 // NOTE: Weak pointers must be invalidated before all other member variables. |
214 base::WeakPtrFactory<AlsaPcmOutputStream> weak_factory_; | 220 base::WeakPtrFactory<AlsaPcmOutputStream> weak_factory_; |
215 | 221 |
216 DISALLOW_COPY_AND_ASSIGN(AlsaPcmOutputStream); | 222 DISALLOW_COPY_AND_ASSIGN(AlsaPcmOutputStream); |
217 }; | 223 }; |
218 | 224 |
219 MEDIA_EXPORT std::ostream& operator<<(std::ostream& os, | 225 MEDIA_EXPORT std::ostream& operator<<(std::ostream& os, |
220 AlsaPcmOutputStream::InternalState); | 226 AlsaPcmOutputStream::InternalState); |
221 | 227 |
222 }; // namespace media | 228 }; // namespace media |
223 | 229 |
224 #endif // MEDIA_AUDIO_ALSA_ALSA_OUTPUT_H_ | 230 #endif // MEDIA_AUDIO_ALSA_ALSA_OUTPUT_H_ |
OLD | NEW |