|
@@ -0,0 +1,91 @@
|
|
1
|
+/* global
|
|
2
|
+ __filename
|
|
3
|
+*/
|
|
4
|
+
|
|
5
|
+import { getLogger } from 'jitsi-meet-logger';
|
|
6
|
+import { createAudioContext } from './WebAudioUtils';
|
|
7
|
+
|
|
8
|
+const logger = getLogger(__filename);
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * The AudioMixer, as the name implies, mixes a number of MediaStreams containing audio tracks into a single
|
|
12
|
+ * MediaStream.
|
|
13
|
+ */
|
|
14
|
+export default class AudioMixer {
|
|
15
|
+ /**
|
|
16
|
+ * Create AudioMixer instance.
|
|
17
|
+ */
|
|
18
|
+ constructor() {
|
|
19
|
+ this._started = false;
|
|
20
|
+ this._streamsToMix = [];
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ /**
|
|
24
|
+ * Add audio MediaStream to be mixed, if the stream doesn't contain any audio tracks it will be ignored.
|
|
25
|
+ *
|
|
26
|
+ * @param {MediaStream} stream - MediaStream to be mixed.
|
|
27
|
+ */
|
|
28
|
+ addMediaStream(stream) {
|
|
29
|
+ if (!stream.getAudioTracks()) {
|
|
30
|
+ logger.warn('Added MediaStream doesn\'t contain audio tracks.');
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ this._streamsToMix.push(stream);
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ /**
|
|
37
|
+ * At this point a WebAudio ChannelMergerNode is created and and the two associated MediaStreams are connected to
|
|
38
|
+ * it; the resulting mixed MediaStream is returned.
|
|
39
|
+ *
|
|
40
|
+ * @returns {MediaStream} - MediaStream containing added streams mixed together, or null if no MediaStream
|
|
41
|
+ * is added.
|
|
42
|
+ */
|
|
43
|
+ start() {
|
|
44
|
+ // If the mixer was already started just return the existing mixed stream.
|
|
45
|
+ if (this._started) {
|
|
46
|
+ return this._mixedMSD.stream;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ this._audioContext = createAudioContext();
|
|
50
|
+
|
|
51
|
+ if (!this._streamsToMix.length) {
|
|
52
|
+ logger.warn('No MediaStream\'s added to AudioMixer, nothing will happen.');
|
|
53
|
+
|
|
54
|
+ return null;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ this._started = true;
|
|
58
|
+
|
|
59
|
+ // Create ChannelMergerNode and connect all MediaStreams to it.
|
|
60
|
+ this._channelMerger = this._audioContext.createChannelMerger(this._streamsToMix.length);
|
|
61
|
+
|
|
62
|
+ for (const stream of this._streamsToMix) {
|
|
63
|
+ const streamMSS = this._audioContext.createMediaStreamSource(stream);
|
|
64
|
+
|
|
65
|
+ streamMSS.connect(this._channelMerger);
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ this._mixedMSD = this._audioContext.createMediaStreamDestination();
|
|
69
|
+ this._channelMerger.connect(this._mixedMSD);
|
|
70
|
+
|
|
71
|
+ return this._mixedMSD.stream;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ /**
|
|
75
|
+ * Disconnect the ChannelMergerNode stopping the audio mix process.References to MediaStreams are also cleared.
|
|
76
|
+ *
|
|
77
|
+ * @returns {void}
|
|
78
|
+ */
|
|
79
|
+ reset() {
|
|
80
|
+ this._started = false;
|
|
81
|
+ this._streamsToMix = [];
|
|
82
|
+
|
|
83
|
+ if (this._channelMerger) {
|
|
84
|
+ this._channelMerger.disconnect();
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ if (this._audioContext) {
|
|
88
|
+ this._audioContext = undefined;
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+}
|