OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // Creates an audio output stream based on the PulseAudio asynchronous API. |
| 6 // |
| 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 |
| 9 // occurs. |
| 10 // |
| 11 // This object is designed so that all AudioOutputStream methods will be called |
| 12 // on the same thread that created the object. |
| 13 // |
| 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 |
| 16 // inside PulseAudio in Start() and repeated during playback, waiting for |
| 17 // PulseAudio write callbacks to occur. |
| 18 |
| 19 #ifndef MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ |
| 20 #define MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ |
| 21 |
| 22 #include <pulse/pulseaudio.h> |
| 23 |
| 24 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/task.h" |
| 26 #include "media/audio/audio_io.h" |
| 27 #include "media/base/channel_layout.h" |
| 28 |
| 29 namespace media { |
| 30 class SeekableBuffer; |
| 31 } |
| 32 |
| 33 class AudioManagerLinux; |
| 34 struct AudioParameters; |
| 35 class MessageLoop; |
| 36 |
| 37 class PulseAudioOutputStream : public AudioOutputStream { |
| 38 public: |
| 39 PulseAudioOutputStream(const AudioParameters& params, |
| 40 AudioManagerLinux* manager, |
| 41 MessageLoop* message_loop); |
| 42 |
| 43 virtual ~PulseAudioOutputStream(); |
| 44 |
| 45 // Implementation of AudioOutputStream. |
| 46 virtual bool Open(); |
| 47 virtual void Close(); |
| 48 virtual void Start(AudioSourceCallback* callback); |
| 49 virtual void Stop(); |
| 50 virtual void SetVolume(double volume); |
| 51 virtual void GetVolume(double* volume); |
| 52 |
| 53 private: |
| 54 // PulseAudio Callbacks. |
| 55 static void ContextStateCallback(pa_context* context, void* state_addr); |
| 56 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, |
| 57 void* stream_addr); |
| 58 |
| 59 // Iterate the PulseAudio mainloop to get write requests. |
| 60 void WaitForWriteRequest(); |
| 61 |
| 62 // Get another packet from the data source and write it to the client buffer. |
| 63 bool BufferPacketFromSource(); |
| 64 |
| 65 // Fulfill a write request from the write request callback. If the write |
| 66 // can't be finished a first, post a new attempt to the message loop. |
| 67 void FulfillWriteRequest(size_t requested_bytes); |
| 68 |
| 69 // Write data from the client buffer to the PulseAudio stream. |
| 70 void WriteToStream(size_t bytes_to_write, size_t* bytes_written); |
| 71 |
| 72 // API for Proxying calls to the AudioSourceCallback provided during Start(). |
| 73 uint32 RunDataCallback(uint8* dest, uint32 max_size, |
| 74 AudioBuffersState buffers_state); |
| 75 |
| 76 // Close() helper function to free internal structs. |
| 77 void Reset(); |
| 78 |
| 79 // Configuration constants from the constructor. Referencable by all threads |
| 80 // since they are constants. |
| 81 const ChannelLayout channel_layout_; |
| 82 const uint32 channel_count_; |
| 83 const pa_sample_format_t sample_format_; |
| 84 const uint32 sample_rate_; |
| 85 const uint32 bytes_per_frame_; |
| 86 |
| 87 // Audio manager that created us. Used to report that we've closed. |
| 88 AudioManagerLinux* manager_; |
| 89 |
| 90 // PulseAudio API structs. |
| 91 pa_context* pa_context_; |
| 92 pa_mainloop* pa_mainloop_; |
| 93 |
| 94 // Handle to the actual PulseAudio playback stream. |
| 95 pa_stream* playback_handle_; |
| 96 |
| 97 // Device configuration data. Populated after Open() completes. |
| 98 uint32 packet_size_; |
| 99 uint32 frames_per_packet_; |
| 100 |
| 101 // Client side audio buffer feeding pulse audio's server side buffer. |
| 102 scoped_ptr<media::SeekableBuffer> client_buffer_; |
| 103 |
| 104 // Float representation of volume from 0.0 to 1.0. |
| 105 float volume_; |
| 106 |
| 107 // Flag indicating the code should stop reading from the data source or |
| 108 // writing to the PulseAudio server. This is set because the device has |
| 109 // entered an unrecoverable error state, or the Close() has executed. |
| 110 bool stream_stopped_; |
| 111 |
| 112 // Whether or not PulseAudio has called the WriteCallback for the most recent |
| 113 // set of pa_mainloop iterations. |
| 114 bool write_callback_handled_; |
| 115 |
| 116 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on |
| 117 // the audio thread while waiting for PulseAudio write callbacks. |
| 118 MessageLoop* message_loop_; |
| 119 |
| 120 // Allows us to run tasks on the PulseAudioOutputStream instance which are |
| 121 // bound by its lifetime. |
| 122 ScopedRunnableMethodFactory<PulseAudioOutputStream> method_factory_; |
| 123 |
| 124 // Callback to audio data source. |
| 125 AudioSourceCallback* source_callback_; |
| 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
| 128 }; |
| 129 |
| 130 #endif // MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ |
OLD | NEW |