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