peerpigeon 0.0.5 → 0.0.6
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 +144 -4
- 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/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 +51 -8
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +381 -381
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +214 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1082 -723
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +105 -105
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +951 -951
- package/websocket-server/server.js +360 -362
package/src/MediaManager.js
CHANGED
|
@@ -2,261 +2,261 @@ 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 environments with media device support
|
|
57
|
+
if (!environmentDetector.isBrowser || typeof navigator.mediaDevices === 'undefined') {
|
|
58
|
+
console.warn('Media device enumeration not available in this environment');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
62
|
+
try {
|
|
63
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
64
|
+
|
|
65
|
+
this.devices.cameras = devices.filter(device => device.kind === 'videoinput');
|
|
66
|
+
this.devices.microphones = devices.filter(device => device.kind === 'audioinput');
|
|
67
|
+
this.devices.speakers = devices.filter(device => device.kind === 'audiooutput');
|
|
68
|
+
|
|
69
|
+
this.emit('devicesUpdated', this.devices);
|
|
70
|
+
return this.devices;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error('Failed to enumerate devices:', error);
|
|
73
|
+
this.emit('error', { type: 'enumerate', error });
|
|
74
|
+
throw error;
|
|
76
75
|
}
|
|
76
|
+
}
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
/**
|
|
79
79
|
* Start local media stream with specified constraints
|
|
80
80
|
*/
|
|
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;
|
|
81
|
+
async startLocalStream(options = {}) {
|
|
82
|
+
const { video = false, audio = false, deviceIds = {} } = options;
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
// Stop existing stream first
|
|
86
|
+
if (this.localStream) {
|
|
87
|
+
this.stopLocalStream();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const constraints = {};
|
|
91
|
+
|
|
92
|
+
if (video) {
|
|
93
|
+
constraints.video = { ...this.constraints.video };
|
|
94
|
+
if (deviceIds.camera) {
|
|
95
|
+
constraints.video.deviceId = { exact: deviceIds.camera };
|
|
138
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;
|
|
139
138
|
}
|
|
139
|
+
}
|
|
140
140
|
|
|
141
|
-
|
|
141
|
+
/**
|
|
142
142
|
* Stop local media stream
|
|
143
143
|
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
144
|
+
stopLocalStream() {
|
|
145
|
+
if (this.localStream) {
|
|
146
|
+
console.log('Stopping local media stream');
|
|
147
|
+
this.localStream.getTracks().forEach(track => {
|
|
148
|
+
track.stop();
|
|
149
|
+
});
|
|
150
|
+
this.localStream = null;
|
|
151
|
+
this.isVideoEnabled = false;
|
|
152
|
+
this.isAudioEnabled = false;
|
|
153
|
+
|
|
154
|
+
this.emit('localStreamStopped');
|
|
156
155
|
}
|
|
156
|
+
}
|
|
157
157
|
|
|
158
|
-
|
|
158
|
+
/**
|
|
159
159
|
* Toggle video track on/off
|
|
160
160
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
return false;
|
|
161
|
+
toggleVideo() {
|
|
162
|
+
if (this.localStream) {
|
|
163
|
+
const videoTrack = this.localStream.getVideoTracks()[0];
|
|
164
|
+
if (videoTrack) {
|
|
165
|
+
videoTrack.enabled = !videoTrack.enabled;
|
|
166
|
+
this.emit('videoToggled', { enabled: videoTrack.enabled });
|
|
167
|
+
return videoTrack.enabled;
|
|
168
|
+
}
|
|
171
169
|
}
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
172
|
|
|
173
|
-
|
|
173
|
+
/**
|
|
174
174
|
* Toggle audio track on/off
|
|
175
175
|
*/
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
return false;
|
|
176
|
+
toggleAudio() {
|
|
177
|
+
if (this.localStream) {
|
|
178
|
+
const audioTrack = this.localStream.getAudioTracks()[0];
|
|
179
|
+
if (audioTrack) {
|
|
180
|
+
audioTrack.enabled = !audioTrack.enabled;
|
|
181
|
+
this.emit('audioToggled', { enabled: audioTrack.enabled });
|
|
182
|
+
return audioTrack.enabled;
|
|
183
|
+
}
|
|
186
184
|
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
/**
|
|
189
189
|
* Get current media state
|
|
190
190
|
*/
|
|
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
|
-
}
|
|
191
|
+
getMediaState() {
|
|
192
|
+
const state = {
|
|
193
|
+
hasLocalStream: !!this.localStream,
|
|
194
|
+
videoEnabled: false,
|
|
195
|
+
audioEnabled: false,
|
|
196
|
+
devices: this.devices
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
if (this.localStream) {
|
|
200
|
+
const videoTrack = this.localStream.getVideoTracks()[0];
|
|
201
|
+
const audioTrack = this.localStream.getAudioTracks()[0];
|
|
206
202
|
|
|
207
|
-
|
|
203
|
+
state.videoEnabled = videoTrack ? videoTrack.enabled : false;
|
|
204
|
+
state.audioEnabled = audioTrack ? audioTrack.enabled : false;
|
|
208
205
|
}
|
|
209
206
|
|
|
210
|
-
|
|
207
|
+
return state;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
211
|
* Check if browser supports required APIs
|
|
212
212
|
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
213
|
+
static checkSupport() {
|
|
214
|
+
const support = {
|
|
215
|
+
getUserMedia: !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia),
|
|
216
|
+
enumerateDevices: !!(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices),
|
|
217
|
+
webRTC: !!(window.RTCPeerConnection)
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
support.fullSupport = support.getUserMedia && support.enumerateDevices && support.webRTC;
|
|
221
|
+
return support;
|
|
222
|
+
}
|
|
223
223
|
|
|
224
|
-
|
|
224
|
+
/**
|
|
225
225
|
* Get media permissions status
|
|
226
226
|
*/
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
227
|
+
async getPermissions() {
|
|
228
|
+
try {
|
|
229
|
+
const permissions = {};
|
|
230
|
+
|
|
231
|
+
if (navigator.permissions) {
|
|
232
|
+
permissions.camera = await navigator.permissions.query({ name: 'camera' });
|
|
233
|
+
permissions.microphone = await navigator.permissions.query({ name: 'microphone' });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return permissions;
|
|
237
|
+
} catch (error) {
|
|
238
|
+
console.warn('Could not check media permissions:', error);
|
|
239
|
+
return {};
|
|
241
240
|
}
|
|
241
|
+
}
|
|
242
242
|
|
|
243
|
-
|
|
243
|
+
/**
|
|
244
244
|
* Mark stream as local origin to prevent confusion with remote streams
|
|
245
245
|
*/
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
246
|
+
markStreamAsLocal(stream) {
|
|
247
|
+
if (!stream) return;
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
Object.defineProperty(stream, '_peerPigeonOrigin', {
|
|
251
|
+
value: 'local',
|
|
252
|
+
writable: false,
|
|
253
|
+
enumerable: false,
|
|
254
|
+
configurable: false
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
console.log(`🔒 Stream ${stream.id} marked as local origin in MediaManager`);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
console.warn('Could not mark stream as local origin:', error);
|
|
261
260
|
}
|
|
261
|
+
}
|
|
262
262
|
}
|