| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // | 6 // |
| 7 // If the stream is successfully opened, Close() must be called before the | 7 // If the stream is successfully opened, Close() must be called before the |
| 8 // stream is deleted as Close() is responsible for ensuring resource cleanup | 8 // stream is deleted as Close() is responsible for ensuring resource cleanup |
| 9 // occurs. | 9 // occurs. |
| 10 // | 10 // |
| 11 // This object is designed so that all AudioOutputStream methods will be called | 11 // This object is designed so that all AudioOutputStream methods will be called |
| 12 // on the same thread that created the object. | 12 // on the same thread that created the object. |
| 13 // | 13 // |
| 14 // WARNING: This object blocks on internal PulseAudio calls in Open() while | 14 // WARNING: This object blocks on internal PulseAudio calls in Open() while |
| 15 // waiting for PulseAudio's context structure to be ready. It also blocks in | 15 // waiting for PulseAudio's context structure to be ready. |
| 16 // inside PulseAudio in Start() and repeated during playback, waiting for | |
| 17 // PulseAudio write callbacks to occur. | |
| 18 | 16 |
| 19 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 17 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
| 20 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 18 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
| 21 | 19 |
| 22 #include <pulse/pulseaudio.h> | 20 #include <pulse/pulseaudio.h> |
| 23 | 21 |
| 24 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/memory/weak_ptr.h" | 23 #include "base/memory/weak_ptr.h" |
| 26 #include "base/task.h" | 24 #include "base/task.h" |
| 27 #include "media/audio/audio_io.h" | 25 #include "media/audio/audio_io.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 55 // Implementation of AudioOutputStream. | 53 // Implementation of AudioOutputStream. |
| 56 virtual bool Open(); | 54 virtual bool Open(); |
| 57 virtual void Close(); | 55 virtual void Close(); |
| 58 virtual void Start(AudioSourceCallback* callback); | 56 virtual void Start(AudioSourceCallback* callback); |
| 59 virtual void Stop(); | 57 virtual void Stop(); |
| 60 virtual void SetVolume(double volume); | 58 virtual void SetVolume(double volume); |
| 61 virtual void GetVolume(double* volume); | 59 virtual void GetVolume(double* volume); |
| 62 | 60 |
| 63 private: | 61 private: |
| 64 // PulseAudio Callbacks. | 62 // PulseAudio Callbacks. |
| 65 static void ContextStateCallback(pa_context* context, void* state_addr); | 63 static void ContextStateCallback(pa_context* context, void* user_data); |
| 66 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, | 64 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, |
| 67 void* stream_addr); | 65 void* user_data); |
| 68 | 66 |
| 69 // Iterate the PulseAudio mainloop to get write requests. | 67 // Fulfills a write request from the write request callback. If the write |
| 70 void WaitForWriteRequest(); | |
| 71 | |
| 72 // Get another packet from the data source and write it to the client buffer. | |
| 73 bool BufferPacketFromSource(); | |
| 74 | |
| 75 // Fulfill a write request from the write request callback. If the write | |
| 76 // can't be finished a first, post a new attempt to the message loop. | 68 // can't be finished a first, post a new attempt to the message loop. |
| 77 void FulfillWriteRequest(size_t requested_bytes); | 69 void FulfillWriteRequest(size_t requested_bytes); |
| 78 | 70 |
| 79 // Write data from the client buffer to the PulseAudio stream. | |
| 80 void WriteToStream(size_t bytes_to_write, size_t* bytes_written); | |
| 81 | |
| 82 // API for Proxying calls to the AudioSourceCallback provided during Start(). | |
| 83 uint32 RunDataCallback(uint8* dest, uint32 max_size, | |
| 84 AudioBuffersState buffers_state); | |
| 85 | |
| 86 // Close() helper function to free internal structs. | 71 // Close() helper function to free internal structs. |
| 87 void Reset(); | 72 void Reset(); |
| 88 | 73 |
| 89 // Configuration constants from the constructor. Referencable by all threads | 74 // Configuration constants from the constructor. Referencable by all threads |
| 90 // since they are constants. | 75 // since they are constants. |
| 91 const ChannelLayout channel_layout_; | 76 const uint32 channels_; |
| 92 const uint32 channel_count_; | |
| 93 const pa_sample_format_t sample_format_; | 77 const pa_sample_format_t sample_format_; |
| 94 const uint32 sample_rate_; | 78 const uint32 sample_rate_; |
| 95 const uint32 bytes_per_frame_; | 79 const uint32 bytes_per_frame_; |
| 80 const uint32 packet_size_; |
| 81 const uint32 frames_per_packet_; |
| 96 | 82 |
| 97 // Audio manager that created us. Used to report that we've closed. | 83 // Audio manager that created us. Used to report that we've closed. |
| 98 AudioManagerPulse* manager_; | 84 AudioManagerPulse* manager_; |
| 99 | 85 |
| 100 // PulseAudio API structs. | 86 // PulseAudio API structs. |
| 101 pa_context* pa_context_; | 87 pa_context* pa_context_; |
| 102 pa_mainloop* pa_mainloop_; | 88 pa_threaded_mainloop* pa_mainloop_; |
| 103 | 89 |
| 104 // Handle to the actual PulseAudio playback stream. | 90 // Handle to the actual PulseAudio playback stream. |
| 105 pa_stream* playback_handle_; | 91 pa_stream* playback_handle_; |
| 106 | 92 |
| 107 // Device configuration data. Populated after Open() completes. | 93 // Device configuration data. Populated after Open() completes. |
| 108 uint32 packet_size_; | 94 uint32 pa_buffer_size_; |
| 109 uint32 frames_per_packet_; | |
| 110 | 95 |
| 111 // Client side audio buffer feeding pulse audio's server side buffer. | 96 // Client side audio buffer feeding pulse audio's server side buffer. |
| 112 scoped_ptr<media::SeekableBuffer> client_buffer_; | 97 scoped_ptr<media::SeekableBuffer> buffer_; |
| 113 | 98 |
| 114 // Float representation of volume from 0.0 to 1.0. | 99 // Float representation of volume from 0.0 to 1.0. |
| 115 float volume_; | 100 float volume_; |
| 116 | 101 |
| 117 // Flag indicating the code should stop reading from the data source or | 102 // Flag indicating the code should stop reading from the data source or |
| 118 // writing to the PulseAudio server. This is set because the device has | 103 // writing to the PulseAudio server. This is set because the device has |
| 119 // entered an unrecoverable error state, or the Close() has executed. | 104 // entered an unrecoverable error state, or the Close() has executed. |
| 120 bool stream_stopped_; | 105 bool stream_stopped_; |
| 121 | 106 |
| 122 // Whether or not PulseAudio has called the WriteCallback for the most recent | 107 // Flag indicating the state of the context has been changed. |
| 123 // set of pa_mainloop iterations. | 108 bool context_state_changed_; |
| 124 bool write_callback_handled_; | |
| 125 | 109 |
| 126 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on | 110 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on |
| 127 // the audio thread while waiting for PulseAudio write callbacks. | 111 // the audio thread while waiting for PulseAudio write callbacks. |
| 128 MessageLoop* message_loop_; | 112 MessageLoop* message_loop_; |
| 129 | 113 |
| 130 // Allows us to run tasks on the PulseAudioOutputStream instance which are | 114 // Allows us to run tasks on the PulseAudioOutputStream instance which are |
| 131 // bound by its lifetime. | 115 // bound by its lifetime. |
| 132 base::WeakPtrFactory<PulseAudioOutputStream> weak_factory_; | 116 base::WeakPtrFactory<PulseAudioOutputStream> weak_factory_; |
| 133 | 117 |
| 134 // Callback to audio data source. | 118 // Callback to audio data source. |
| 135 AudioSourceCallback* source_callback_; | 119 AudioSourceCallback* source_callback_; |
| 136 | 120 |
| 137 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | 121 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
| 138 }; | 122 }; |
| 139 | 123 |
| 140 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 124 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
| OLD | NEW |