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 #ifndef MEDIA_AUDIO_CRAS_CRAS_INPUT_H_
6 #define MEDIA_AUDIO_CRAS_CRAS_INPUT_H_
8 #include <cras_client.h>
12 #include "base/compiler_specific.h"
13 #include "media/audio/agc_audio_stream.h"
14 #include "media/audio/audio_io.h"
15 #include "media/audio/audio_parameters.h"
19 class AudioManagerCras
;
21 // Provides an input stream for audio capture based on CRAS, the ChromeOS Audio
22 // Server. This object is not thread safe and all methods should be invoked in
23 // the thread that created the object.
24 class CrasInputStream
: public AgcAudioStream
<AudioInputStream
> {
26 // The ctor takes all the usual parameters, plus |manager| which is the
27 // audio manager who is creating this object.
28 CrasInputStream(const AudioParameters
& params
,
29 AudioManagerCras
* manager
,
30 const std::string
& device_id
);
32 // The dtor is typically called by the AudioManager only and it is usually
33 // triggered by calling AudioOutputStream::Close().
34 ~CrasInputStream() override
;
36 // Implementation of AudioInputStream.
38 void Start(AudioInputCallback
* callback
) override
;
40 void Close() override
;
41 double GetMaxVolume() override
;
42 void SetVolume(double volume
) override
;
43 double GetVolume() override
;
44 bool IsMuted() override
;
47 // Handles requests to get samples from the provided buffer. This will be
48 // called by the audio server when it has samples ready.
49 static int SamplesReady(cras_client
* client
,
50 cras_stream_id_t stream_id
,
53 const timespec
* sample_ts
,
56 // Handles notification that there was an error with the playback stream.
57 static int StreamError(cras_client
* client
,
58 cras_stream_id_t stream_id
,
62 // Reads one or more buffers of audio from the device, passes on to the
63 // registered callback. Called from SamplesReady().
64 void ReadAudio(size_t frames
, uint8
* buffer
, const timespec
* sample_ts
);
66 // Deals with an error that occured in the stream. Called from StreamError().
67 void NotifyStreamError(int err
);
69 // Convert from dB * 100 to a volume ratio.
70 double GetVolumeRatioFromDecibels(double dB
) const;
72 // Convert from a volume ratio to dB.
73 double GetDecibelsFromVolumeRatio(double volume_ratio
) const;
75 // Non-refcounted pointer back to the audio manager.
76 // The AudioManager indirectly holds on to stream objects, so we don't
77 // want circular references. Additionally, stream objects live on the audio
78 // thread, which is owned by the audio manager and we don't want to addref
79 // the manager from that thread.
80 AudioManagerCras
* const audio_manager_
;
82 // Size of frame in bytes.
83 uint32 bytes_per_frame_
;
85 // Callback to pass audio samples too, valid while recording.
86 AudioInputCallback
* callback_
;
88 // The client used to communicate with the audio server.
91 // PCM parameters for the stream.
92 const AudioParameters params_
;
94 // True if the stream has been started.
97 // ID of the playing stream.
98 cras_stream_id_t stream_id_
;
100 // Direction of the stream.
101 const CRAS_STREAM_DIRECTION stream_direction_
;
103 // Index of the CRAS device to stream input from.
106 // True if the stream is a system-wide loopback stream.
109 scoped_ptr
<AudioBus
> audio_bus_
;
111 DISALLOW_COPY_AND_ASSIGN(CrasInputStream
);
116 #endif // MEDIA_AUDIO_CRAS_CRAS_INPUT_H_