/
input-device-manager.js
200 lines (166 loc) · 6.78 KB
/
input-device-manager.js
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
/*
* Flocking Web Audio Input Manager
* http://github.com/colinbdclark/flocking
*
* Copyright 2013-2014, Colin Clark
* Dual licensed under the MIT and GPL Version 2 licenses.
*/
/*global require, MediaStreamTrack, jQuery*/
/*jshint white: false, newcap: true, regexp: true, browser: true,
forin: false, nomen: true, bitwise: false, maxerr: 100,
indent: 4, plusplus: false, curly: true, eqeqeq: true,
freeze: true, latedef: true, noarg: true, nonew: true, quotmark: double, undef: true,
unused: true, strict: true, asi: false, boss: false, evil: false, expr: false,
funcscope: false*/
var fluid = fluid || require("infusion"),
flock = fluid.registerNamespace("flock");
(function () {
"use strict";
// TODO: Remove this when Chrome implements navigator.getMediaDevices().
fluid.registerNamespace("flock.webAudio.chrome");
flock.webAudio.chrome.getSources = function (callback) {
return MediaStreamTrack.getSources(function (infoSpecs) {
var normalized = fluid.transform(infoSpecs, function (infoSpec) {
infoSpec.deviceId = infoSpec.id;
return infoSpec;
});
callback(normalized);
});
};
flock.webAudio.mediaStreamFailure = function () {
flock.fail("Media Capture and Streams are not supported on this browser.");
};
var webAudioShims = {
AudioContext: window.AudioContext || window.webkitAudioContext,
getUserMediaImpl: navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia || flock.webAudio.mediaStreamFailure,
getUserMedia: function () {
flock.shim.getUserMediaImpl.apply(navigator, arguments);
},
getMediaDevicesImpl: navigator.getMediaDevices ? navigator.getMediaDevices :
typeof window.MediaStreamTrack !== "undefined" ?
flock.webAudio.chrome.getSources : flock.webAudio.mediaStreamFailure,
getMediaDevice: function () {
flock.shim.getMediaDevicesImpl.apply(navigator, arguments);
}
};
jQuery.extend(flock.shim, webAudioShims);
/**
* Manages audio input devices using the Web Audio API.
*/
// Add a means for disconnecting audio input nodes.
fluid.defaults("flock.webAudio.inputDeviceManager", {
gradeNames: ["fluid.component"],
invokers: {
/**
* Opens the specified audio device.
* If no device is specified, the default device is opened.
*
* @param {Object} deviceSpec a device spec containing, optionally, an 'id' or 'label' parameter
*/
openAudioDevice: {
funcName: "flock.webAudio.inputDeviceManager.openAudioDevice",
args: [
"{arguments}.0",
"{that}.openAudioDeviceWithId",
"{that}.openFirstAudioDeviceWithLabel",
"{that}.openAudioDeviceWithConstraints"
]
},
/**
* Opens an audio device with the specified WebRTC constraints.
* If no constraints are specified, the default audio device is opened.
*
* @param {Object} constraints a WebRTC-compatible constraints object
*/
openAudioDeviceWithConstraints: {
funcName: "flock.webAudio.inputDeviceManager.openAudioDeviceWithConstraints",
args: [
"{audioSystem}.context",
"{enviro}",
"{nativeNodeManager}.createMediaStreamInput",
"{arguments}.0"
]
},
/**
* Opens an audio device with the specified WebRTC device id.
*
* @param {string} id a device identifier
*/
openAudioDeviceWithId: {
funcName: "flock.webAudio.inputDeviceManager.openAudioDeviceWithId",
args: ["{arguments}.0", "{that}.openAudioDeviceWithConstraints"]
},
/**
* Opens the first audio device found with the specified label.
* The label must be an exact, case-insensitive match.
*
* @param {string} label a device label
*/
openFirstAudioDeviceWithLabel: {
funcName: "flock.webAudio.inputDeviceManager.openFirstAudioDeviceWithLabel",
args: ["{arguments}.0", "{that}.openAudioDeviceWithId"]
}
}
});
flock.webAudio.inputDeviceManager.openAudioDevice = function (sourceSpec, idOpener, labelOpener, specOpener) {
if (sourceSpec) {
if (sourceSpec.id) {
return idOpener(sourceSpec.id);
} else if (sourceSpec.label) {
return labelOpener(sourceSpec.label);
}
}
return specOpener();
};
flock.webAudio.inputDeviceManager.openAudioDeviceWithId = function (id, deviceOpener) {
var options = {
audio: {
optional: [
{
sourceId: id
}
]
}
};
deviceOpener(options);
};
flock.webAudio.inputDeviceManager.openFirstAudioDeviceWithLabel = function (label, deviceOpener) {
if (!label) {
return;
}
// TODO: Can't access device labels until the user agrees
// to allow access to the current device.
flock.shim.getMediaDevices(function (deviceInfoSpecs) {
var matches = deviceInfoSpecs.filter(function (device) {
if (device.label.toLowerCase() === label.toLowerCase()) {
return true;
}
});
if (matches.length > 0) {
deviceOpener(matches[0].deviceId);
} else {
fluid.log(fluid.logLevel.IMPORTANT,
"An audio device named '" + label + "' could not be found.");
}
});
};
flock.webAudio.inputDeviceManager.openAudioDeviceWithConstraints = function (context, enviro, openMediaStream, options) {
options = options || {
audio: true
};
// Acquire an input bus ahead of time so we can synchronously
// notify the client where its output will be.
var busNum = enviro.busManager.acquireNextBus("input");
function error (err) {
fluid.log(fluid.logLevel.IMPORTANT,
"An error occurred while trying to access the user's microphone. " +
err);
}
function success (mediaStream) {
openMediaStream(mediaStream, busNum);
}
flock.shim.getUserMedia(options, success, error);
return busNum;
};
}());