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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (c) 2012 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.
#include "content/browser/renderer_host/media/audio_input_device_manager.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/media_stream_request.h"
#include "media/audio/audio_device_name.h"
#include "media/audio/audio_input_ipc.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/audio_parameters.h"
#include "media/base/channel_layout.h"
namespace content {
const int AudioInputDeviceManager::kFakeOpenSessionId = 1;
namespace {
// Starting id for the first capture session.
const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1;
}
AudioInputDeviceManager::AudioInputDeviceManager(
media::AudioManager* audio_manager)
: listener_(NULL),
next_capture_session_id_(kFirstSessionId),
use_fake_device_(false),
audio_manager_(audio_manager) {
}
AudioInputDeviceManager::~AudioInputDeviceManager() {
}
const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById(
int session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
StreamDeviceList::iterator device = GetDevice(session_id);
if (device == devices_.end())
return NULL;
return &(*device);
}
void AudioInputDeviceManager::Register(
MediaStreamProviderListener* listener,
base::MessageLoopProxy* device_thread_loop) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(!listener_);
DCHECK(!device_loop_);
listener_ = listener;
device_loop_ = device_thread_loop;
}
void AudioInputDeviceManager::Unregister() {
DCHECK(listener_);
listener_ = NULL;
}
void AudioInputDeviceManager::EnumerateDevices(MediaStreamType stream_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(listener_);
device_loop_->PostTask(
FROM_HERE,
base::Bind(&AudioInputDeviceManager::EnumerateOnDeviceThread,
this, stream_type));
}
int AudioInputDeviceManager::Open(const StreamDeviceInfo& device) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Generate a new id for this device.
int session_id = next_capture_session_id_++;
device_loop_->PostTask(
FROM_HERE,
base::Bind(&AudioInputDeviceManager::OpenOnDeviceThread,
this, session_id, device));
return session_id;
}
void AudioInputDeviceManager::Close(int session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(listener_);
StreamDeviceList::iterator device = GetDevice(session_id);
if (device == devices_.end())
return;
const MediaStreamType stream_type = device->device.type;
devices_.erase(device);
// Post a callback through the listener on IO thread since
// MediaStreamManager is expecting the callback asynchronously.
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&AudioInputDeviceManager::ClosedOnIOThread,
this, stream_type, session_id));
}
void AudioInputDeviceManager::UseFakeDevice() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
use_fake_device_ = true;
}
bool AudioInputDeviceManager::ShouldUseFakeDevice() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
return use_fake_device_;
}
void AudioInputDeviceManager::EnumerateOnDeviceThread(
MediaStreamType stream_type) {
DCHECK(IsOnDeviceThread());
media::AudioDeviceNames device_names;
switch (stream_type) {
case MEDIA_DEVICE_AUDIO_CAPTURE:
// AudioManager is guaranteed to outlive MediaStreamManager in
// BrowserMainloop.
audio_manager_->GetAudioInputDeviceNames(&device_names);
break;
default:
NOTREACHED();
break;
}
scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray());
for (media::AudioDeviceNames::iterator it = device_names.begin();
it != device_names.end(); ++it) {
// Add device information to device vector.
devices->push_back(StreamDeviceInfo(
stream_type, it->device_name, it->unique_id, false));
}
// Return the device list through the listener by posting a task on
// IO thread since MediaStreamManager handles the callback asynchronously.
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread,
this, stream_type, base::Passed(&devices)));
}
void AudioInputDeviceManager::OpenOnDeviceThread(
int session_id, const StreamDeviceInfo& info) {
DCHECK(IsOnDeviceThread());
// Get the preferred sample rate and channel configuration for the
// audio device.
media::AudioParameters params =
audio_manager_->GetInputStreamParameters(info.device.id);
StreamDeviceInfo out(info.device.type, info.device.name, info.device.id,
params.sample_rate(), params.channel_layout(), false);
out.session_id = session_id;
// Return the |session_id| through the listener by posting a task on
// IO thread since MediaStreamManager handles the callback asynchronously.
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&AudioInputDeviceManager::OpenedOnIOThread,
this, session_id, out));
}
void AudioInputDeviceManager::DevicesEnumeratedOnIOThread(
MediaStreamType stream_type,
scoped_ptr<StreamDeviceInfoArray> devices) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Ensure that |devices| gets deleted on exit.
if (listener_)
listener_->DevicesEnumerated(stream_type, *devices);
}
void AudioInputDeviceManager::OpenedOnIOThread(int session_id,
const StreamDeviceInfo& info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK_EQ(session_id, info.session_id);
DCHECK(GetDevice(session_id) == devices_.end());
devices_.push_back(info);
if (listener_)
listener_->Opened(info.device.type, session_id);
}
void AudioInputDeviceManager::ClosedOnIOThread(MediaStreamType stream_type,
int session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (listener_)
listener_->Closed(stream_type, session_id);
}
bool AudioInputDeviceManager::IsOnDeviceThread() const {
return device_loop_->BelongsToCurrentThread();
}
AudioInputDeviceManager::StreamDeviceList::iterator
AudioInputDeviceManager::GetDevice(int session_id) {
for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end();
++i) {
if (i->session_id == session_id)
return i;
}
return devices_.end();
}
} // namespace content
|