-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (148 loc) · 4.33 KB
/
index.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
// See chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=987548
import { EventEmitter } from 'eventemitter3';
import window from 'global/window';
import document from 'global/document';
let CHANNEL_LOCAL_STORAGE_KEY = 'rtc_live_storage_broadcast_channel';
const ACTIVE_TRACK_EVENT = 'active_track';
const INACTIVE_TRACK_EVENT = 'inactive_track';
const emitter = new EventEmitter();
let hiddenProperty = '';
if ('hidden' in document) {
hiddenProperty = 'hidden';
} else {
hiddenProperty = 'webkitHidden';
}
const visibilityChange = hiddenProperty.replace(/hidden/i, 'visibilitychange');
const uaMatched = /(chrome)[ /]([\w.]+)/.exec(window.navigator.userAgent.toLowerCase()) || [];
const isChrome = uaMatched[1] || '';
function isSupport() {
return isChrome && window.localStorage && (typeof StorageEvent !== 'undefined');
}
function emitActiveTrackEvent() {
// delay run
setTimeout(() => {
emitter.emit(ACTIVE_TRACK_EVENT);
}, 1000);
}
function emitInactiveTrackEvent() {
emitter.emit(INACTIVE_TRACK_EVENT);
}
let channelPeersNumber = 0; // tab peers
function setNumberStorage(num) {
if (typeof num !== 'number') return;
try {
window.localStorage.setItem(CHANNEL_LOCAL_STORAGE_KEY, JSON.stringify({
num,
ts: Date.now(),
}));
} catch (err) {
console.log(err);
}
}
function getNumberStorage() {
let storageNum = 0;
try {
const storageData = JSON.parse(window.localStorage.getItem(CHANNEL_LOCAL_STORAGE_KEY));
storageNum = Number(storageData.num);
} catch (err) {
// ignore
}
return storageNum;
}
function broadcastActive() {
const storageNum = getNumberStorage();
channelPeersNumber = storageNum + 1;
setNumberStorage(channelPeersNumber);
}
function broadcastInactive() {
if (channelPeersNumber < 1) return;
channelPeersNumber -= 1;
setNumberStorage(channelPeersNumber);
}
function handleStorageEvent(data) {
let storageData = {};
try {
storageData = JSON.parse(data);
} catch (err) {
// ignore
}
const storageNumber = Number(storageData.num);
if (typeof storageNumber === 'number') {
channelPeersNumber = storageNumber;
}
if (channelPeersNumber === 1) {
// enable self track
emitActiveTrackEvent();
}
}
if (isSupport()) {
document.addEventListener(visibilityChange, () => {
// important: has other tab
if (channelPeersNumber > 1) {
if (document[hiddenProperty]) {
// disable self track
emitInactiveTrackEvent();
} else {
// enable self track
emitActiveTrackEvent();
}
}
});
window.addEventListener('storage', (evt) => {
if (evt.key !== CHANNEL_LOCAL_STORAGE_KEY) return;
if (!evt.newValue) return;
handleStorageEvent(evt.newValue);
});
window.addEventListener('unload', () => {
broadcastInactive();
});
// Notify other peers of new connections
broadcastActive();
}
class Fix {
constructor(mediaStream, videoElement) {
this._mediaStream = mediaStream;
this._videoElement = videoElement;
const audioTracks = mediaStream.getAudioTracks();
this._audioTrack = audioTracks[0] || null;
if (this._audioTrack) {
this._handleActive = this._handleActive.bind(this);
this._handleInactive = this._handleInactive.bind(this);
emitter.on(ACTIVE_TRACK_EVENT, this._handleActive);
emitter.on(INACTIVE_TRACK_EVENT, this._handleInactive);
}
}
_handleActive() {
if (this._videoElement.paused) return;
const cpAudioTrack = this._audioTrack.clone();
this._mediaStream.addTrack(cpAudioTrack);
this._mediaStream.removeTrack(this._audioTrack);
this._audioTrack = cpAudioTrack;
}
_handleInactive() { // eslint-disable-line
// this._mediaStream.removeTrack(this._audioTrack);
}
destroy() {
if (this._audioTrack) {
emitter.off(ACTIVE_TRACK_EVENT, this._handleActive);
emitter.off(INACTIVE_TRACK_EVENT, this._handleInactive);
}
this._mediaStream = null;
this._videoElement = null;
this._audioTrack = null;
}
}
export function createFix(mediaStream, videoElement) {
if (!mediaStream) {
throw new Error('You must provide media stream object!');
}
if (!videoElement) {
throw new Error('You must provide video element');
}
return new Fix(mediaStream, videoElement);
}
export function setChannelLocalStorageKey(key) {
if (key) {
CHANNEL_LOCAL_STORAGE_KEY = key;
}
}