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"
16 #include "media/base/media_export.h"
20 class AudioManagerCras
;
22 // Provides an input stream for audio capture based on CRAS, the ChromeOS Audio
23 // Server. This object is not thread safe and all methods should be invoked in
24 // the thread that created the object.
25 class MEDIA_EXPORT CrasInputStream
: public AgcAudioStream
<AudioInputStream
> {
27 // The ctor takes all the usual parameters, plus |manager| which is the
28 // audio manager who is creating this object.
29 CrasInputStream(const AudioParameters
& params
,
30 AudioManagerCras
* manager
,
31 const std::string
& device_id
);
33 // The dtor is typically called by the AudioManager only and it is usually
34 // triggered by calling AudioOutputStream::Close().
35 ~CrasInputStream() override
;
37 // Implementation of AudioInputStream.
39 void Start(AudioInputCallback
* callback
) override
;
41 void Close() override
;
42 double GetMaxVolume() override
;
43 void SetVolume(double volume
) override
;
44 double GetVolume() override
;
45 bool IsMuted() override
;
48 // Handles requests to get samples from the provided buffer. This will be
49 // called by the audio server when it has samples ready.
50 static int SamplesReady(cras_client
* client
,
51 cras_stream_id_t stream_id
,
54 const timespec
* sample_ts
,
57 // Handles notification that there was an error with the playback stream.
58 static int StreamError(cras_client
* client
,
59 cras_stream_id_t stream_id
,
63 // Reads one or more buffers of audio from the device, passes on to the
64 // registered callback. Called from SamplesReady().
65 void ReadAudio(size_t frames
, uint8
* buffer
, const timespec
* sample_ts
);
67 // Deals with an error that occured in the stream. Called from StreamError().
68 void NotifyStreamError(int err
);
70 // Convert from dB * 100 to a volume ratio.
71 double GetVolumeRatioFromDecibels(double dB
) const;
73 // Convert from a volume ratio to dB.
74 double GetDecibelsFromVolumeRatio(double volume_ratio
) const;
76 // Non-refcounted pointer back to the audio manager.
77 // The AudioManager indirectly holds on to stream objects, so we don't
78 // want circular references. Additionally, stream objects live on the audio
79 // thread, which is owned by the audio manager and we don't want to addref
80 // the manager from that thread.
81 AudioManagerCras
* const audio_manager_
;
83 // Size of frame in bytes.
84 uint32 bytes_per_frame_
;
86 // Callback to pass audio samples too, valid while recording.
87 AudioInputCallback
* callback_
;
89 // The client used to communicate with the audio server.
92 // PCM parameters for the stream.
93 const AudioParameters params_
;
95 // True if the stream has been started.
98 // ID of the playing stream.
99 cras_stream_id_t stream_id_
;
101 // Direction of the stream.
102 const CRAS_STREAM_DIRECTION stream_direction_
;
104 // Index of the CRAS device to stream input from.
107 // True if the stream is a system-wide loopback stream.
110 scoped_ptr
<AudioBus
> audio_bus_
;
112 DISALLOW_COPY_AND_ASSIGN(CrasInputStream
);
117 #endif // MEDIA_AUDIO_CRAS_CRAS_INPUT_H_