blob: 33b2c8aee43f90be3db12c0b7b0af439da9a577e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_AUDIO_LINUX_ALSA_INPUT_H_
#define MEDIA_AUDIO_LINUX_ALSA_INPUT_H_
#include <alsa/asoundlib.h>
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/task.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_parameters.h"
class AlsaWrapper;
// Provides an input stream for audio capture based on the ALSA PCM interface.
// This object is not thread safe and all methods should be invoked in the
// thread that created the object.
class AlsaPcmInputStream : public AudioInputStream {
public:
// Pass this to the constructor if you want to attempt auto-selection
// of the audio recording device.
static const char* kAutoSelectDevice;
// Create a PCM Output stream for the ALSA device identified by
// |device_name|. If unsure of what to use for |device_name|, use
// |kAutoSelectDevice|.
AlsaPcmInputStream(const std::string& device_name,
const AudioParameters& params,
AlsaWrapper* wrapper);
virtual ~AlsaPcmInputStream();
// Implementation of AudioOutputStream.
virtual bool Open();
virtual void Start(AudioInputCallback* callback);
virtual void Stop();
virtual void Close();
private:
// Logs the error and invokes any registered callbacks.
void HandleError(const char* method, int error);
// Reads one or more packets of audio from the device, passes on to the
// registered callback and schedules the next read.
void ReadAudio();
// Recovers from any device errors if possible.
bool Recover(int error);
std::string device_name_;
AudioParameters params_;
int bytes_per_packet_;
AlsaWrapper* wrapper_;
int packet_duration_ms_; // Length of each recorded packet in milliseconds.
AudioInputCallback* callback_; // Valid during a recording session.
base::Time next_read_time_; // Scheduled time for the next read callback.
snd_pcm_t* device_handle_; // Handle to the ALSA PCM recording device.
ScopedRunnableMethodFactory<AlsaPcmInputStream> task_factory_;
scoped_array<uint8> audio_packet_; // Buffer used for reading audio data.
bool read_callback_behind_schedule_;
DISALLOW_COPY_AND_ASSIGN(AlsaPcmInputStream);
};
#endif // MEDIA_AUDIO_LINUX_ALSA_INPUT_H_
|