| 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> | |
| 24 #include <stddef.h> | 23 #include <stddef.h> |
| 25 | 24 |
| 26 #include <string> | 25 #include <string> |
| 27 | 26 |
| 28 #include "base/macros.h" | 27 #include "base/macros.h" |
| 29 #include "base/memory/scoped_ptr.h" | 28 #include "base/memory/scoped_ptr.h" |
| 30 #include "base/threading/thread_checker.h" | 29 #include "base/threading/thread_checker.h" |
| 31 #include "media/audio/audio_io.h" | 30 #include "media/audio/audio_io.h" |
| 32 #include "media/audio/audio_parameters.h" | 31 #include "media/audio/audio_parameters.h" |
| 33 | 32 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 57 | 56 |
| 58 private: | 57 private: |
| 59 // Called by PulseAudio when |pa_stream_| change state. If an unexpected | 58 // Called by PulseAudio when |pa_stream_| change state. If an unexpected |
| 60 // failure state change happens and |source_callback_| is set | 59 // failure state change happens and |source_callback_| is set |
| 61 // this method will forward the error via OnError(). | 60 // this method will forward the error via OnError(). |
| 62 static void StreamNotifyCallback(pa_stream* s, void* p_this); | 61 static void StreamNotifyCallback(pa_stream* s, void* p_this); |
| 63 | 62 |
| 64 // Called by PulseAudio when it needs more audio data. | 63 // Called by PulseAudio when it needs more audio data. |
| 65 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this); | 64 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this); |
| 66 | 65 |
| 67 // pa_context_get_server_info callback. It's used by | |
| 68 // GetSystemDefaultOutputDevice to set |default_system_device_name_| to the | |
| 69 // default system output device. | |
| 70 static void GetSystemDefaultOutputDeviceCallback(pa_context* context, | |
| 71 const pa_server_info* info, | |
| 72 void* user_data); | |
| 73 | |
| 74 // Initialize |pa_mainloop_| and |pa_context_| and prepare them for creating | |
| 75 // an output stream. | |
| 76 bool InitializeMainloopAndContext(); | |
| 77 | |
| 78 // Get default system output device for the output stream. | |
| 79 void GetSystemDefaultOutputDevice(); | |
| 80 | |
| 81 // Fulfill a write request from the write request callback. Outputs silence | 66 // Fulfill a write request from the write request callback. Outputs silence |
| 82 // if the request could not be fulfilled. | 67 // if the request could not be fulfilled. |
| 83 void FulfillWriteRequest(size_t requested_bytes); | 68 void FulfillWriteRequest(size_t requested_bytes); |
| 84 | 69 |
| 85 // Close() helper function to free internal structs. | 70 // Close() helper function to free internal structs. |
| 86 void Reset(); | 71 void Reset(); |
| 87 | 72 |
| 88 // AudioParameters from the constructor. | 73 // AudioParameters from the constructor. |
| 89 const AudioParameters params_; | 74 const AudioParameters params_; |
| 90 | 75 |
| 91 // The device ID for the device to open. | 76 // The device ID for the device to open. |
| 92 const std::string device_id_; | 77 const std::string device_id_; |
| 93 // The name of the system default device. Set by | |
| 94 // GetSystemDefaultOutputDeviceCallback if |device_id_| is set to be the | |
| 95 // default device. | |
| 96 std::string default_system_device_name_; | |
| 97 | 78 |
| 98 // Audio manager that created us. Used to report that we've closed. | 79 // Audio manager that created us. Used to report that we've closed. |
| 99 AudioManagerBase* manager_; | 80 AudioManagerBase* manager_; |
| 100 | 81 |
| 101 // PulseAudio API structs. | 82 // PulseAudio API structs. |
| 102 pa_context* pa_context_; | 83 pa_context* pa_context_; |
| 103 pa_threaded_mainloop* pa_mainloop_; | 84 pa_threaded_mainloop* pa_mainloop_; |
| 104 pa_stream* pa_stream_; | 85 pa_stream* pa_stream_; |
| 105 | 86 |
| 106 // Float representation of volume from 0.0 to 1.0. | 87 // Float representation of volume from 0.0 to 1.0. |
| 107 float volume_; | 88 float volume_; |
| 108 | 89 |
| 109 // Callback to audio data source. Must only be modified while holding a lock | 90 // Callback to audio data source. Must only be modified while holding a lock |
| 110 // on |pa_mainloop_| via pa_threaded_mainloop_lock(). | 91 // on |pa_mainloop_| via pa_threaded_mainloop_lock(). |
| 111 AudioSourceCallback* source_callback_; | 92 AudioSourceCallback* source_callback_; |
| 112 | 93 |
| 113 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 94 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
| 114 scoped_ptr<AudioBus> audio_bus_; | 95 scoped_ptr<AudioBus> audio_bus_; |
| 115 | 96 |
| 116 base::ThreadChecker thread_checker_; | 97 base::ThreadChecker thread_checker_; |
| 117 | 98 |
| 118 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | 99 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
| 119 }; | 100 }; |
| 120 | 101 |
| 121 } // namespace media | 102 } // namespace media |
| 122 | 103 |
| 123 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 104 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
| OLD | NEW |