peerpigeon 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +190 -12
- package/examples/browser/app.js +40 -40
- package/examples/browser/crypto-test.html +427 -0
- package/examples/browser/index.html +161 -0
- package/examples/browser/styles.css +216 -0
- package/examples/browser/ui-old.js +1704 -1708
- package/examples/browser/ui.js +2226 -1692
- package/examples/nativescript/README.md +73 -0
- package/examples/nativescript/app.js +152 -0
- package/examples/nativescript/package.json +27 -0
- package/examples/node/test-crypto-media.js +137 -0
- package/examples/node/test-crypto.js +146 -0
- package/examples/node/test-node.js +114 -115
- package/package.json +16 -13
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +457 -384
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +216 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1100 -721
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +118 -104
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +1123 -948
- package/websocket-server/server.js +360 -362
package/src/MediaManager.js
CHANGED
|
@@ -2,261 +2,263 @@ import { EventEmitter } from './EventEmitter.js';
|
|
|
2
2
|
import { environmentDetector } from './EnvironmentDetector.js';
|
|
3
3
|
|
|
4
4
|
export class MediaManager extends EventEmitter {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
this.localStream = null;
|
|
8
|
+
this.isVideoEnabled = false;
|
|
9
|
+
this.isAudioEnabled = false;
|
|
10
|
+
this.devices = {
|
|
11
|
+
cameras: [],
|
|
12
|
+
microphones: [],
|
|
13
|
+
speakers: []
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
this.constraints = {
|
|
17
|
+
video: {
|
|
18
|
+
width: { ideal: 640 },
|
|
19
|
+
height: { ideal: 480 },
|
|
20
|
+
frameRate: { ideal: 30 }
|
|
21
|
+
},
|
|
22
|
+
audio: {
|
|
23
|
+
echoCancellation: true,
|
|
24
|
+
noiseSuppression: true,
|
|
25
|
+
autoGainControl: true
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
31
|
* Initialize media devices and permissions
|
|
32
32
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
async init() {
|
|
34
|
+
// Check if media APIs are available
|
|
35
|
+
if (!environmentDetector.hasGetUserMedia) {
|
|
36
|
+
console.warn('getUserMedia not available in this environment');
|
|
37
|
+
this.emit('error', { type: 'init', error: new Error('getUserMedia not supported') });
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
41
|
+
try {
|
|
42
|
+
// Enumerate available devices
|
|
43
|
+
await this.enumerateDevices();
|
|
44
|
+
return true;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error('Failed to initialize media manager:', error);
|
|
47
|
+
this.emit('error', { type: 'init', error });
|
|
48
|
+
return false;
|
|
50
49
|
}
|
|
50
|
+
}
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
/**
|
|
53
53
|
* Get available media devices
|
|
54
54
|
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
async enumerateDevices() {
|
|
56
|
+
// Only available in browser and NativeScript environments with media device support
|
|
57
|
+
if ((!environmentDetector.isBrowser && !environmentDetector.isNativeScript) ||
|
|
58
|
+
typeof navigator === 'undefined' ||
|
|
59
|
+
typeof navigator.mediaDevices === 'undefined') {
|
|
60
|
+
console.warn('Media device enumeration not available in this environment');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
64
|
+
try {
|
|
65
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
66
|
+
|
|
67
|
+
this.devices.cameras = devices.filter(device => device.kind === 'videoinput');
|
|
68
|
+
this.devices.microphones = devices.filter(device => device.kind === 'audioinput');
|
|
69
|
+
this.devices.speakers = devices.filter(device => device.kind === 'audiooutput');
|
|
70
|
+
|
|
71
|
+
this.emit('devicesUpdated', this.devices);
|
|
72
|
+
return this.devices;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error('Failed to enumerate devices:', error);
|
|
75
|
+
this.emit('error', { type: 'enumerate', error });
|
|
76
|
+
throw error;
|
|
76
77
|
}
|
|
78
|
+
}
|
|
77
79
|
|
|
78
|
-
|
|
80
|
+
/**
|
|
79
81
|
* Start local media stream with specified constraints
|
|
80
82
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (audio) {
|
|
100
|
-
constraints.audio = { ...this.constraints.audio };
|
|
101
|
-
if (deviceIds.microphone) {
|
|
102
|
-
constraints.audio.deviceId = { exact: deviceIds.microphone };
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (!video && !audio) {
|
|
107
|
-
throw new Error('At least one of video or audio must be enabled');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
this.localStream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
111
|
-
this.isVideoEnabled = video;
|
|
112
|
-
this.isAudioEnabled = audio;
|
|
113
|
-
|
|
114
|
-
// Mark stream as local origin to prevent confusion with remote streams
|
|
115
|
-
this.markStreamAsLocal(this.localStream);
|
|
116
|
-
|
|
117
|
-
console.log('Local media stream started:', {
|
|
118
|
-
video: this.isVideoEnabled,
|
|
119
|
-
audio: this.isAudioEnabled,
|
|
120
|
-
tracks: this.localStream.getTracks().map(track => ({
|
|
121
|
-
kind: track.kind,
|
|
122
|
-
enabled: track.enabled,
|
|
123
|
-
label: track.label
|
|
124
|
-
}))
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
this.emit('localStreamStarted', {
|
|
128
|
-
stream: this.localStream,
|
|
129
|
-
video: this.isVideoEnabled,
|
|
130
|
-
audio: this.isAudioEnabled
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
return this.localStream;
|
|
134
|
-
} catch (error) {
|
|
135
|
-
console.error('Failed to start local media stream:', error);
|
|
136
|
-
this.emit('error', { type: 'getUserMedia', error });
|
|
137
|
-
throw error;
|
|
83
|
+
async startLocalStream(options = {}) {
|
|
84
|
+
const { video = false, audio = false, deviceIds = {} } = options;
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
// Stop existing stream first
|
|
88
|
+
if (this.localStream) {
|
|
89
|
+
this.stopLocalStream();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const constraints = {};
|
|
93
|
+
|
|
94
|
+
if (video) {
|
|
95
|
+
constraints.video = { ...this.constraints.video };
|
|
96
|
+
if (deviceIds.camera) {
|
|
97
|
+
constraints.video.deviceId = { exact: deviceIds.camera };
|
|
138
98
|
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (audio) {
|
|
102
|
+
constraints.audio = { ...this.constraints.audio };
|
|
103
|
+
if (deviceIds.microphone) {
|
|
104
|
+
constraints.audio.deviceId = { exact: deviceIds.microphone };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!video && !audio) {
|
|
109
|
+
throw new Error('At least one of video or audio must be enabled');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
this.localStream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
113
|
+
this.isVideoEnabled = video;
|
|
114
|
+
this.isAudioEnabled = audio;
|
|
115
|
+
|
|
116
|
+
// Mark stream as local origin to prevent confusion with remote streams
|
|
117
|
+
this.markStreamAsLocal(this.localStream);
|
|
118
|
+
|
|
119
|
+
console.log('Local media stream started:', {
|
|
120
|
+
video: this.isVideoEnabled,
|
|
121
|
+
audio: this.isAudioEnabled,
|
|
122
|
+
tracks: this.localStream.getTracks().map(track => ({
|
|
123
|
+
kind: track.kind,
|
|
124
|
+
enabled: track.enabled,
|
|
125
|
+
label: track.label
|
|
126
|
+
}))
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
this.emit('localStreamStarted', {
|
|
130
|
+
stream: this.localStream,
|
|
131
|
+
video: this.isVideoEnabled,
|
|
132
|
+
audio: this.isAudioEnabled
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return this.localStream;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('Failed to start local media stream:', error);
|
|
138
|
+
this.emit('error', { type: 'getUserMedia', error });
|
|
139
|
+
throw error;
|
|
139
140
|
}
|
|
141
|
+
}
|
|
140
142
|
|
|
141
|
-
|
|
143
|
+
/**
|
|
142
144
|
* Stop local media stream
|
|
143
145
|
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
146
|
+
stopLocalStream() {
|
|
147
|
+
if (this.localStream) {
|
|
148
|
+
console.log('Stopping local media stream');
|
|
149
|
+
this.localStream.getTracks().forEach(track => {
|
|
150
|
+
track.stop();
|
|
151
|
+
});
|
|
152
|
+
this.localStream = null;
|
|
153
|
+
this.isVideoEnabled = false;
|
|
154
|
+
this.isAudioEnabled = false;
|
|
155
|
+
|
|
156
|
+
this.emit('localStreamStopped');
|
|
156
157
|
}
|
|
158
|
+
}
|
|
157
159
|
|
|
158
|
-
|
|
160
|
+
/**
|
|
159
161
|
* Toggle video track on/off
|
|
160
162
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
return false;
|
|
163
|
+
toggleVideo() {
|
|
164
|
+
if (this.localStream) {
|
|
165
|
+
const videoTrack = this.localStream.getVideoTracks()[0];
|
|
166
|
+
if (videoTrack) {
|
|
167
|
+
videoTrack.enabled = !videoTrack.enabled;
|
|
168
|
+
this.emit('videoToggled', { enabled: videoTrack.enabled });
|
|
169
|
+
return videoTrack.enabled;
|
|
170
|
+
}
|
|
171
171
|
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
172
174
|
|
|
173
|
-
|
|
175
|
+
/**
|
|
174
176
|
* Toggle audio track on/off
|
|
175
177
|
*/
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
return false;
|
|
178
|
+
toggleAudio() {
|
|
179
|
+
if (this.localStream) {
|
|
180
|
+
const audioTrack = this.localStream.getAudioTracks()[0];
|
|
181
|
+
if (audioTrack) {
|
|
182
|
+
audioTrack.enabled = !audioTrack.enabled;
|
|
183
|
+
this.emit('audioToggled', { enabled: audioTrack.enabled });
|
|
184
|
+
return audioTrack.enabled;
|
|
185
|
+
}
|
|
186
186
|
}
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
187
189
|
|
|
188
|
-
|
|
190
|
+
/**
|
|
189
191
|
* Get current media state
|
|
190
192
|
*/
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
state.videoEnabled = videoTrack ? videoTrack.enabled : false;
|
|
204
|
-
state.audioEnabled = audioTrack ? audioTrack.enabled : false;
|
|
205
|
-
}
|
|
193
|
+
getMediaState() {
|
|
194
|
+
const state = {
|
|
195
|
+
hasLocalStream: !!this.localStream,
|
|
196
|
+
videoEnabled: false,
|
|
197
|
+
audioEnabled: false,
|
|
198
|
+
devices: this.devices
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
if (this.localStream) {
|
|
202
|
+
const videoTrack = this.localStream.getVideoTracks()[0];
|
|
203
|
+
const audioTrack = this.localStream.getAudioTracks()[0];
|
|
206
204
|
|
|
207
|
-
|
|
205
|
+
state.videoEnabled = videoTrack ? videoTrack.enabled : false;
|
|
206
|
+
state.audioEnabled = audioTrack ? audioTrack.enabled : false;
|
|
208
207
|
}
|
|
209
208
|
|
|
210
|
-
|
|
209
|
+
return state;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
211
213
|
* Check if browser supports required APIs
|
|
212
214
|
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
215
|
+
static checkSupport() {
|
|
216
|
+
const support = {
|
|
217
|
+
getUserMedia: !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia),
|
|
218
|
+
enumerateDevices: !!(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices),
|
|
219
|
+
webRTC: !!(window.RTCPeerConnection)
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
support.fullSupport = support.getUserMedia && support.enumerateDevices && support.webRTC;
|
|
223
|
+
return support;
|
|
224
|
+
}
|
|
223
225
|
|
|
224
|
-
|
|
226
|
+
/**
|
|
225
227
|
* Get media permissions status
|
|
226
228
|
*/
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
229
|
+
async getPermissions() {
|
|
230
|
+
try {
|
|
231
|
+
const permissions = {};
|
|
232
|
+
|
|
233
|
+
if (navigator.permissions) {
|
|
234
|
+
permissions.camera = await navigator.permissions.query({ name: 'camera' });
|
|
235
|
+
permissions.microphone = await navigator.permissions.query({ name: 'microphone' });
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return permissions;
|
|
239
|
+
} catch (error) {
|
|
240
|
+
console.warn('Could not check media permissions:', error);
|
|
241
|
+
return {};
|
|
241
242
|
}
|
|
243
|
+
}
|
|
242
244
|
|
|
243
|
-
|
|
245
|
+
/**
|
|
244
246
|
* Mark stream as local origin to prevent confusion with remote streams
|
|
245
247
|
*/
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
248
|
+
markStreamAsLocal(stream) {
|
|
249
|
+
if (!stream) return;
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
Object.defineProperty(stream, '_peerPigeonOrigin', {
|
|
253
|
+
value: 'local',
|
|
254
|
+
writable: false,
|
|
255
|
+
enumerable: false,
|
|
256
|
+
configurable: false
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
console.log(`🔒 Stream ${stream.id} marked as local origin in MediaManager`);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
console.warn('Could not mark stream as local origin:', error);
|
|
261
262
|
}
|
|
263
|
+
}
|
|
262
264
|
}
|