1 // Copyright (c) 2012 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.
5 // Creates an audio output stream based on the PulseAudio asynchronous API;
6 // specifically using the pa_threaded_mainloop model.
8 // If the stream is successfully opened, Close() must be called before the
9 // stream is deleted as Close() is responsible for ensuring resource cleanup
12 // This object is designed so that all AudioOutputStream methods will be called
13 // on the same thread that created the object.
15 // WARNING: This object blocks on internal PulseAudio calls in Open() while
16 // waiting for PulseAudio's context structure to be ready. It also blocks in
17 // inside PulseAudio in Start() and repeated during playback, waiting for
18 // PulseAudio write callbacks to occur.
20 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_
21 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_
25 #include "base/memory/scoped_ptr.h"
26 #include "base/threading/thread_checker.h"
27 #include "media/audio/audio_io.h"
28 #include "media/audio/audio_parameters.h"
33 struct pa_threaded_mainloop;
36 class AudioManagerBase;
38 class PulseAudioOutputStream : public AudioOutputStream {
40 PulseAudioOutputStream(const AudioParameters& params,
41 const std::string& device_id,
42 AudioManagerBase* manager);
44 virtual ~PulseAudioOutputStream();
46 // Implementation of AudioOutputStream.
47 virtual bool Open() OVERRIDE;
48 virtual void Close() OVERRIDE;
49 virtual void Start(AudioSourceCallback* callback) OVERRIDE;
50 virtual void Stop() OVERRIDE;
51 virtual void SetVolume(double volume) OVERRIDE;
52 virtual void GetVolume(double* volume) OVERRIDE;
55 void SetMediaStreamProperties(const std::string& app_id,
56 const std::string& app_class) override;
57 const std::string& app_id() const {return app_id_;}
58 const std::string& app_class() const {return app_class_;}
62 // Called by PulseAudio when |pa_stream_| change state. If an unexpected
63 // failure state change happens and |source_callback_| is set
64 // this method will forward the error via OnError().
65 static void StreamNotifyCallback(pa_stream* s, void* p_this);
67 // Called by PulseAudio when it needs more audio data.
68 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this);
70 // Fulfill a write request from the write request callback. Outputs silence
71 // if the request could not be fulfilled.
72 void FulfillWriteRequest(size_t requested_bytes);
74 // Close() helper function to free internal structs.
77 // AudioParameters from the constructor.
78 const AudioParameters params_;
80 // The device ID for the device to open.
81 const std::string device_id_;
83 // Audio manager that created us. Used to report that we've closed.
84 AudioManagerBase* manager_;
86 // PulseAudio API structs.
87 pa_context* pa_context_;
88 pa_threaded_mainloop* pa_mainloop_;
89 pa_stream* pa_stream_;
91 // Float representation of volume from 0.0 to 1.0.
94 // Callback to audio data source. Must only be modified while holding a lock
95 // on |pa_mainloop_| via pa_threaded_mainloop_lock().
96 AudioSourceCallback* source_callback_;
98 // Container for retrieving data from AudioSourceCallback::OnMoreData().
99 scoped_ptr<AudioBus> audio_bus_;
101 #if defined(OS_TIZEN)
102 // Application ID and class for the pulseaudio streams.
104 std::string app_class_;
107 base::ThreadChecker thread_checker_;
109 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream);
114 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_