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