| 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_PULSE_PULSE_OUTPUT_H_ | |
| 20 #define MEDIA_AUDIO_PULSE_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 #if defined(OS_LINUX) | |
| 35 class AudioManagerLinux; | |
| 36 typedef AudioManagerLinux AudioManagerPulse; | |
| 37 #elif defined(OS_OPENBSD) | |
| 38 class AudioManagerOpenBSD; | |
| 39 typedef AudioManagerOpenBSD AudioManagerPulse; | |
| 40 #else | |
| 41 #error Unsupported platform | |
| 42 #endif | |
| 43 | |
| 44 struct AudioParameters; | |
| 45 class MessageLoop; | |
| 46 | |
| 47 class PulseAudioOutputStream : public AudioOutputStream { | |
| 48 public: | |
| 49 PulseAudioOutputStream(const AudioParameters& params, | |
| 50 AudioManagerPulse* manager, | |
| 51 MessageLoop* message_loop); | |
| 52 | |
| 53 virtual ~PulseAudioOutputStream(); | |
| 54 | |
| 55 // Implementation of AudioOutputStream. | |
| 56 virtual bool Open(); | |
| 57 virtual void Close(); | |
| 58 virtual void Start(AudioSourceCallback* callback); | |
| 59 virtual void Stop(); | |
| 60 virtual void SetVolume(double volume); | |
| 61 virtual void GetVolume(double* volume); | |
| 62 | |
| 63 private: | |
| 64 // PulseAudio Callbacks. | |
| 65 static void ContextStateCallback(pa_context* context, void* state_addr); | |
| 66 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, | |
| 67 void* stream_addr); | |
| 68 | |
| 69 // Iterate the PulseAudio mainloop to get write requests. | |
| 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. | |
| 77 void FulfillWriteRequest(size_t requested_bytes); | |
| 78 | |
| 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. | |
| 87 void Reset(); | |
| 88 | |
| 89 // Configuration constants from the constructor. Referencable by all threads | |
| 90 // since they are constants. | |
| 91 const ChannelLayout channel_layout_; | |
| 92 const uint32 channel_count_; | |
| 93 const pa_sample_format_t sample_format_; | |
| 94 const uint32 sample_rate_; | |
| 95 const uint32 bytes_per_frame_; | |
| 96 | |
| 97 // Audio manager that created us. Used to report that we've closed. | |
| 98 AudioManagerPulse* manager_; | |
| 99 | |
| 100 // PulseAudio API structs. | |
| 101 pa_context* pa_context_; | |
| 102 pa_mainloop* pa_mainloop_; | |
| 103 | |
| 104 // Handle to the actual PulseAudio playback stream. | |
| 105 pa_stream* playback_handle_; | |
| 106 | |
| 107 // Device configuration data. Populated after Open() completes. | |
| 108 uint32 packet_size_; | |
| 109 uint32 frames_per_packet_; | |
| 110 | |
| 111 // Client side audio buffer feeding pulse audio's server side buffer. | |
| 112 scoped_ptr<media::SeekableBuffer> client_buffer_; | |
| 113 | |
| 114 // Float representation of volume from 0.0 to 1.0. | |
| 115 float volume_; | |
| 116 | |
| 117 // 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 | |
| 119 // entered an unrecoverable error state, or the Close() has executed. | |
| 120 bool stream_stopped_; | |
| 121 | |
| 122 // Whether or not PulseAudio has called the WriteCallback for the most recent | |
| 123 // set of pa_mainloop iterations. | |
| 124 bool write_callback_handled_; | |
| 125 | |
| 126 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on | |
| 127 // the audio thread while waiting for PulseAudio write callbacks. | |
| 128 MessageLoop* message_loop_; | |
| 129 | |
| 130 // Allows us to run tasks on the PulseAudioOutputStream instance which are | |
| 131 // bound by its lifetime. | |
| 132 base::WeakPtrFactory<PulseAudioOutputStream> weak_factory_; | |
| 133 | |
| 134 // Callback to audio data source. | |
| 135 AudioSourceCallback* source_callback_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | |
| 138 }; | |
| 139 | |
| 140 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | |
| OLD | NEW |