| 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 // Creates an audio output stream based on the PulseAudio asynchronous API; | 5 // Creates an audio output stream based on the PulseAudio asynchronous API; |
| 6 // specifically using the pa_threaded_mainloop model. | 6 // specifically using the pa_threaded_mainloop model. |
| 7 // | 7 // |
| 8 // If the stream is successfully opened, Close() must be called before the | 8 // If the stream is successfully opened, Close() must be called before the |
| 9 // stream is deleted as Close() is responsible for ensuring resource cleanup | 9 // stream is deleted as Close() is responsible for ensuring resource cleanup |
| 10 // occurs. | 10 // occurs. |
| 11 // | 11 // |
| 12 // This object is designed so that all AudioOutputStream methods will be called | 12 // This object is designed so that all AudioOutputStream methods will be called |
| 13 // on the same thread that created the object. | 13 // on the same thread that created the object. |
| 14 // | 14 // |
| 15 // WARNING: This object blocks on internal PulseAudio calls in Open() while | 15 // WARNING: This object blocks on internal PulseAudio calls in Open() while |
| 16 // waiting for PulseAudio's context structure to be ready. It also blocks in | 16 // waiting for PulseAudio's context structure to be ready. It also blocks in |
| 17 // inside PulseAudio in Start() and repeated during playback, waiting for | 17 // inside PulseAudio in Start() and repeated during playback, waiting for |
| 18 // PulseAudio write callbacks to occur. | 18 // PulseAudio write callbacks to occur. |
| 19 | 19 |
| 20 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 20 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
| 21 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 21 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
| 22 | 22 |
| 23 #include <pulse/pulseaudio.h> |
| 23 #include <stddef.h> | 24 #include <stddef.h> |
| 24 | 25 |
| 25 #include <string> | 26 #include <string> |
| 26 | 27 |
| 27 #include "base/macros.h" | 28 #include "base/macros.h" |
| 28 #include "base/memory/scoped_ptr.h" | 29 #include "base/memory/scoped_ptr.h" |
| 29 #include "base/threading/thread_checker.h" | 30 #include "base/threading/thread_checker.h" |
| 30 #include "media/audio/audio_io.h" | 31 #include "media/audio/audio_io.h" |
| 31 #include "media/audio/audio_parameters.h" | 32 #include "media/audio/audio_parameters.h" |
| 32 | 33 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 47 ~PulseAudioOutputStream() override; | 48 ~PulseAudioOutputStream() override; |
| 48 | 49 |
| 49 // Implementation of AudioOutputStream. | 50 // Implementation of AudioOutputStream. |
| 50 bool Open() override; | 51 bool Open() override; |
| 51 void Close() override; | 52 void Close() override; |
| 52 void Start(AudioSourceCallback* callback) override; | 53 void Start(AudioSourceCallback* callback) override; |
| 53 void Stop() override; | 54 void Stop() override; |
| 54 void SetVolume(double volume) override; | 55 void SetVolume(double volume) override; |
| 55 void GetVolume(double* volume) override; | 56 void GetVolume(double* volume) override; |
| 56 | 57 |
| 58 pa_threaded_mainloop* GetPAMainloop(); |
| 59 void SetDefaultSystemDeviceName(const std::string& name); |
| 60 |
| 57 private: | 61 private: |
| 58 // Called by PulseAudio when |pa_stream_| change state. If an unexpected | 62 // Called by PulseAudio when |pa_stream_| change state. If an unexpected |
| 59 // failure state change happens and |source_callback_| is set | 63 // failure state change happens and |source_callback_| is set |
| 60 // this method will forward the error via OnError(). | 64 // this method will forward the error via OnError(). |
| 61 static void StreamNotifyCallback(pa_stream* s, void* p_this); | 65 static void StreamNotifyCallback(pa_stream* s, void* p_this); |
| 62 | 66 |
| 63 // Called by PulseAudio when it needs more audio data. | 67 // Called by PulseAudio when it needs more audio data. |
| 64 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this); | 68 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this); |
| 65 | 69 |
| 70 // Initialize |pa_mainloop_| and |pa_context_| and prepare them for creating |
| 71 // an output stream. |
| 72 bool InitializeMainloopAndContext(); |
| 73 |
| 74 // Get default system output device for the output stream. |
| 75 void GetSystemDefaultOutputDevice(); |
| 76 |
| 66 // Fulfill a write request from the write request callback. Outputs silence | 77 // Fulfill a write request from the write request callback. Outputs silence |
| 67 // if the request could not be fulfilled. | 78 // if the request could not be fulfilled. |
| 68 void FulfillWriteRequest(size_t requested_bytes); | 79 void FulfillWriteRequest(size_t requested_bytes); |
| 69 | 80 |
| 70 // Close() helper function to free internal structs. | 81 // Close() helper function to free internal structs. |
| 71 void Reset(); | 82 void Reset(); |
| 72 | 83 |
| 73 // AudioParameters from the constructor. | 84 // AudioParameters from the constructor. |
| 74 const AudioParameters params_; | 85 const AudioParameters params_; |
| 75 | 86 |
| 76 // The device ID for the device to open. | 87 // The device ID for the device to open. |
| 77 const std::string device_id_; | 88 const std::string device_id_; |
| 89 // The name of the system default device. Set by |
| 90 // GetSystemDefaultOutputDeviceCallback if |device_id_| is set to be the |
| 91 // default device. |
| 92 std::string default_system_device_name_; |
| 78 | 93 |
| 79 // Audio manager that created us. Used to report that we've closed. | 94 // Audio manager that created us. Used to report that we've closed. |
| 80 AudioManagerBase* manager_; | 95 AudioManagerBase* manager_; |
| 81 | 96 |
| 82 // PulseAudio API structs. | 97 // PulseAudio API structs. |
| 83 pa_context* pa_context_; | 98 pa_context* pa_context_; |
| 84 pa_threaded_mainloop* pa_mainloop_; | 99 pa_threaded_mainloop* pa_mainloop_; |
| 85 pa_stream* pa_stream_; | 100 pa_stream* pa_stream_; |
| 86 | 101 |
| 87 // Float representation of volume from 0.0 to 1.0. | 102 // Float representation of volume from 0.0 to 1.0. |
| 88 float volume_; | 103 float volume_; |
| 89 | 104 |
| 90 // Callback to audio data source. Must only be modified while holding a lock | 105 // Callback to audio data source. Must only be modified while holding a lock |
| 91 // on |pa_mainloop_| via pa_threaded_mainloop_lock(). | 106 // on |pa_mainloop_| via pa_threaded_mainloop_lock(). |
| 92 AudioSourceCallback* source_callback_; | 107 AudioSourceCallback* source_callback_; |
| 93 | 108 |
| 94 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 109 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
| 95 scoped_ptr<AudioBus> audio_bus_; | 110 scoped_ptr<AudioBus> audio_bus_; |
| 96 | 111 |
| 97 base::ThreadChecker thread_checker_; | 112 base::ThreadChecker thread_checker_; |
| 98 | 113 |
| 99 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | 114 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
| 100 }; | 115 }; |
| 101 | 116 |
| 102 } // namespace media | 117 } // namespace media |
| 103 | 118 |
| 104 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 119 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
| OLD | NEW |