-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
113 lines (90 loc) · 3.07 KB
/
lib.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
import { am } from './js/audio.js';
import * as dom from './js/dom.js';
import settings from './js/settings.js';
// Things that I'm generally not sure where to put yet...
/**************************************************
* Stream related functions *
***************************************************/
let localStreamNode = null;
function resetConnection(signaller) {
// AudioContext gets suspended if created before
// a user interaction https://goo.gl/7K7WLu
// This will be called by 'None' receiver
am.context.resume();
// Leave any old channels
if (signaller) {
signaller.leaveAllChannels();
}
// Reset constraints
settings.mediaStreamConstraints = settings.newMediaStreamConstraints();
// Disconnect the local stream if we set one up
if (localStreamNode) {
localStreamNode.disconnect();
localStreamNode = null;
}
}
/**
* Source audio from user media like desktop capture
* or microphone input.
* @param {string} device Input device name
*/
async function setupLocalMediaStreams(deviceId) {
// AudioContext gets suspended if created before
// a user interaction https://goo.gl/7K7WLu
am.context.resume();
// Remove the constraints that exclude microphone
delete settings.mediaStreamConstraints.audio.mandatory.chromeMediaSource;
// Remove the constraints that turn off mic processing
//delete settings.mediaStreamConstraints.audio.mandatory;
// Default to the simplest option
settings.mediaStreamConstraints.audio = true;
if (deviceId) {
settings.mediaStreamConstraints.audio = {}
settings.mediaStreamConstraints.audio.deviceId = deviceId;
}
return new Promise((resolve, reject) => {
navigator.mediaDevices.getUserMedia(settings.mediaStreamConstraints)
.then((stream) => {
gotLocalMediaStream(stream);
resolve();
})
.catch((e) => {
console.warn(`Failed to obtain local media stream: ${e}`);
reject(e);
});
});
}
/**
* Source audio from a file.
* @param {string} filepath Relative or absolute path to the file
*/
async function setupLocalMediaStreamsFromFile(filepath) {
// AudioContext gets suspended if created before
// a user interaction https://goo.gl/7K7WLu
am.context.resume();
return new Promise(async (resolve, reject) => {
if (settings.receiverOnly) {
resolve();
return;
}
// Attach file to audio element
dom.localAudio.src = filepath;
dom.localAudio.classList.remove('hidden');
resolve();
});
}
function gotLocalMediaStream(mediaStream) {
// Disconnect our old one if we get a new one
if (localStreamNode) {
localStreamNode.disconnect();
}
console.dir(mediaStream);
localStreamNode = am.context.createMediaStreamSource(mediaStream);
localStreamNode.connect(am.outgoingRemoteGainNode);
trace('Connected localStreamNode.');
}
export {
resetConnection,
setupLocalMediaStreams,
setupLocalMediaStreamsFromFile
}