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