cras_input.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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. 4 5#ifndef MEDIA_AUDIO_CRAS_CRAS_INPUT_H_ 6#define MEDIA_AUDIO_CRAS_CRAS_INPUT_H_ 7 8#include <cras_client.h> 9 10#include <string> 11 12#include "base/compiler_specific.h" 13#include "base/memory/scoped_ptr.h" 14#include "base/memory/weak_ptr.h" 15#include "media/audio/audio_input_stream_impl.h" 16#include "media/audio/audio_io.h" 17#include "media/audio/audio_parameters.h" 18 19namespace media { 20 21class AudioManagerCras; 22 23// Provides an input stream for audio capture based on CRAS, the ChromeOS Audio 24// Server. This object is not thread safe and all methods should be invoked in 25// the thread that created the object. 26class CrasInputStream : public AudioInputStreamImpl { 27 public: 28 // The ctor takes all the usual parameters, plus |manager| which is the 29 // audio manager who is creating this object. 30 CrasInputStream(const AudioParameters& params, AudioManagerCras* manager); 31 32 // The dtor is typically called by the AudioManager only and it is usually 33 // triggered by calling AudioOutputStream::Close(). 34 virtual ~CrasInputStream(); 35 36 // Implementation of AudioInputStream. 37 virtual bool Open() OVERRIDE; 38 virtual void Start(AudioInputCallback* callback) OVERRIDE; 39 virtual void Stop() OVERRIDE; 40 virtual void Close() OVERRIDE; 41 virtual double GetMaxVolume() OVERRIDE; 42 virtual void SetVolume(double volume) OVERRIDE; 43 virtual double GetVolume() OVERRIDE; 44 45 private: 46 // Handles requests to get samples from the provided buffer. This will be 47 // called by the audio server when it has samples ready. 48 static int SamplesReady(cras_client* client, 49 cras_stream_id_t stream_id, 50 uint8* samples, 51 size_t frames, 52 const timespec* sample_ts, 53 void* arg); 54 55 // Handles notificaiton that there was an error with the playback stream. 56 static int StreamError(cras_client* client, 57 cras_stream_id_t stream_id, 58 int err, 59 void* arg); 60 61 // Reads one or more buffers of audio from the device, passes on to the 62 // registered callback. Called from SamplesReady(). 63 void ReadAudio(size_t frames, uint8* buffer, const timespec* sample_ts); 64 65 // Deals with an error that occured in the stream. Called from StreamError(). 66 void NotifyStreamError(int err); 67 68 // Convert from dB * 100 to a volume ratio. 69 double GetVolumeRatioFromDecibels(double dB) const; 70 71 // Convert from a volume ratio to dB. 72 double GetDecibelsFromVolumeRatio(double volume_ratio) const; 73 74 // Non-refcounted pointer back to the audio manager. 75 // The AudioManager indirectly holds on to stream objects, so we don't 76 // want circular references. Additionally, stream objects live on the audio 77 // thread, which is owned by the audio manager and we don't want to addref 78 // the manager from that thread. 79 AudioManagerCras* audio_manager_; 80 81 // Size of frame in bytes. 82 uint32 bytes_per_frame_; 83 84 // Callback to pass audio samples too, valid while recording. 85 AudioInputCallback* callback_; 86 87 // The client used to communicate with the audio server. 88 cras_client* client_; 89 90 // PCM parameters for the stream. 91 AudioParameters params_; 92 93 // True if the stream has been started. 94 bool started_; 95 96 // ID of the playing stream. 97 cras_stream_id_t stream_id_; 98 99 DISALLOW_COPY_AND_ASSIGN(CrasInputStream); 100}; 101 102} // namespace media 103 104#endif // MEDIA_AUDIO_CRAS_ALSA_INPUT_H_ 105