Home | History | Annotate | Download | only in pulse
      1 // Copyright 2013 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 #ifndef MEDIA_AUDIO_PULSE_PULSE_UNIFIED_H_
      6 #define MEDIA_AUDIO_PULSE_PULSE_UNIFIED_H_
      7 
      8 #include <pulse/pulseaudio.h>
      9 #include <string>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "media/audio/audio_io.h"
     13 #include "media/audio/audio_parameters.h"
     14 #include "media/base/audio_fifo.h"
     15 
     16 namespace media {
     17 
     18 class AudioManagerBase;
     19 class SeekableBuffer;
     20 
     21 class PulseAudioUnifiedStream : public AudioOutputStream {
     22  public:
     23   PulseAudioUnifiedStream(const AudioParameters& params,
     24                           const std::string& input_device_id,
     25                           AudioManagerBase* manager);
     26 
     27   virtual ~PulseAudioUnifiedStream();
     28 
     29   // Implementation of PulseAudioUnifiedStream.
     30   virtual bool Open() OVERRIDE;
     31   virtual void Close() OVERRIDE;
     32   virtual void Start(AudioSourceCallback* callback) OVERRIDE;
     33   virtual void Stop() OVERRIDE;
     34   virtual void SetVolume(double volume) OVERRIDE;
     35   virtual void GetVolume(double* volume) OVERRIDE;
     36 
     37  private:
     38   // Called by PulseAudio when |pa_stream_| change state.  If an unexpected
     39   // failure state change happens and |source_callback_| is set
     40   // this method will forward the error via OnError().
     41   static void StreamNotifyCallback(pa_stream* s, void* user_data);
     42 
     43   // Called by PulseAudio recording stream when it has data.
     44   static void ReadCallback(pa_stream* s, size_t length, void* user_data);
     45 
     46   // Helpers for ReadCallback() to read and write data.
     47   void WriteData(size_t requested_bytes);
     48   void ReadData();
     49 
     50   // Close() helper function to free internal structs.
     51   void Reset();
     52 
     53   // AudioParameters from the constructor.
     54   const AudioParameters params_;
     55 
     56   // Device unique ID of the input device.
     57   const std::string input_device_id_;
     58 
     59   // Audio manager that created us.  Used to report that we've closed.
     60   AudioManagerBase* manager_;
     61 
     62   // PulseAudio API structs.
     63   pa_context* pa_context_;
     64   pa_threaded_mainloop* pa_mainloop_;
     65   pa_stream* input_stream_;
     66   pa_stream* output_stream_;
     67 
     68   // Float representation of volume from 0.0 to 1.0.
     69   float volume_;
     70 
     71   // Callback to audio data source.  Must only be modified while holding a lock
     72   // on |pa_mainloop_| via pa_threaded_mainloop_lock().
     73   AudioSourceCallback* source_callback_;
     74 
     75   scoped_ptr<AudioBus> input_bus_;
     76   scoped_ptr<AudioBus> output_bus_;
     77 
     78   // Used for input to output buffering.
     79   scoped_ptr<media::SeekableBuffer> fifo_;
     80 
     81   // Temporary storage for recorded data. It gets a packet of data from
     82   // |fifo_| and deliver the data to OnMoreIOData() callback.
     83   scoped_ptr<uint8[]> input_data_buffer_;
     84 
     85   DISALLOW_COPY_AND_ASSIGN(PulseAudioUnifiedStream);
     86 };
     87 
     88 }  // namespace media
     89 
     90 #endif  // MEDIA_AUDIO_PULSE_PULSE_UNIFIED_H_
     91