|
|
@@ -9,6 +9,12 @@ const logger = getLogger('modules/webaudio/AudioMixer');
|
|
9
|
9
|
* MediaStream.
|
|
10
|
10
|
*/
|
|
11
|
11
|
export default class AudioMixer {
|
|
|
12
|
+ private _started: boolean;
|
|
|
13
|
+ private _streamsToMix: MediaStream[];
|
|
|
14
|
+ private _streamMSSArray: MediaStreamAudioSourceNode[];
|
|
|
15
|
+ private _audioContext?: AudioContext;
|
|
|
16
|
+ private _mixedMSD?: MediaStreamAudioDestinationNode;
|
|
|
17
|
+
|
|
12
|
18
|
/**
|
|
13
|
19
|
* Create AudioMixer instance.
|
|
14
|
20
|
*/
|
|
|
@@ -23,8 +29,8 @@ export default class AudioMixer {
|
|
23
|
29
|
*
|
|
24
|
30
|
* @param {MediaStream} stream - MediaStream to be mixed.
|
|
25
|
31
|
*/
|
|
26
|
|
- addMediaStream(stream) {
|
|
27
|
|
- if (!stream.getAudioTracks()) {
|
|
|
32
|
+ addMediaStream(stream: MediaStream): void {
|
|
|
33
|
+ if (!stream.getAudioTracks() || stream.getAudioTracks().length === 0) {
|
|
28
|
34
|
logger.warn('Added MediaStream doesn\'t contain audio tracks.');
|
|
29
|
35
|
}
|
|
30
|
36
|
|
|
|
@@ -35,12 +41,12 @@ export default class AudioMixer {
|
|
35
|
41
|
* At this point a WebAudio ChannelMergerNode is created and and the two associated MediaStreams are connected to
|
|
36
|
42
|
* it; the resulting mixed MediaStream is returned.
|
|
37
|
43
|
*
|
|
38
|
|
- * @returns {MediaStream} - MediaStream containing added streams mixed together, or null if no MediaStream
|
|
|
44
|
+ * @returns {MediaStream | null} - MediaStream containing added streams mixed together, or null if no MediaStream
|
|
39
|
45
|
* is added.
|
|
40
|
46
|
*/
|
|
41
|
|
- start() {
|
|
|
47
|
+ start(): MediaStream | null {
|
|
42
|
48
|
// If the mixer was already started just return the existing mixed stream.
|
|
43
|
|
- if (this._started) {
|
|
|
49
|
+ if (this._started && this._mixedMSD) {
|
|
44
|
50
|
return this._mixedMSD.stream;
|
|
45
|
51
|
}
|
|
46
|
52
|
|
|
|
@@ -54,10 +60,10 @@ export default class AudioMixer {
|
|
54
|
60
|
|
|
55
|
61
|
this._started = true;
|
|
56
|
62
|
|
|
57
|
|
- this._mixedMSD = this._audioContext.createMediaStreamDestination();
|
|
|
63
|
+ this._mixedMSD = this._audioContext!.createMediaStreamDestination();
|
|
58
|
64
|
|
|
59
|
65
|
for (const stream of this._streamsToMix) {
|
|
60
|
|
- const streamMSS = this._audioContext.createMediaStreamSource(stream);
|
|
|
66
|
+ const streamMSS = this._audioContext!.createMediaStreamSource(stream);
|
|
61
|
67
|
|
|
62
|
68
|
streamMSS.connect(this._mixedMSD);
|
|
63
|
69
|
|
|
|
@@ -73,7 +79,7 @@ export default class AudioMixer {
|
|
73
|
79
|
*
|
|
74
|
80
|
* @returns {void}
|
|
75
|
81
|
*/
|
|
76
|
|
- reset() {
|
|
|
82
|
+ reset(): void {
|
|
77
|
83
|
this._started = false;
|
|
78
|
84
|
this._streamsToMix = [];
|
|
79
|
85
|
|
|
|
@@ -87,5 +93,6 @@ export default class AudioMixer {
|
|
87
|
93
|
if (this._audioContext) {
|
|
88
|
94
|
this._audioContext = undefined;
|
|
89
|
95
|
}
|
|
|
96
|
+ this._mixedMSD = undefined;
|
|
90
|
97
|
}
|
|
91
|
98
|
}
|