peerpigeon 0.0.4 → 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.
@@ -2,238 +2,261 @@ import { EventEmitter } from './EventEmitter.js';
2
2
  import { environmentDetector } from './EnvironmentDetector.js';
3
3
 
4
4
  export class MediaManager extends EventEmitter {
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
- }
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
- 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
- }
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
- 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;
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
- 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
- }
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
- 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;
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
- 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 };
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
- console.log('Local media stream started:', {
115
- video: this.isVideoEnabled,
116
- audio: this.isAudioEnabled,
117
- tracks: this.localStream.getTracks().map(track => ({
118
- kind: track.kind,
119
- enabled: track.enabled,
120
- label: track.label
121
- }))
122
- });
123
-
124
- this.emit('localStreamStarted', {
125
- stream: this.localStream,
126
- video: this.isVideoEnabled,
127
- audio: this.isAudioEnabled
128
- });
129
-
130
- return this.localStream;
131
- } catch (error) {
132
- console.error('Failed to start local media stream:', error);
133
- this.emit('error', { type: 'getUserMedia', error });
134
- 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 };
96
+ }
97
+ }
98
+
99
+ if (audio) {
100
+ constraints.audio = { ...this.constraints.audio };
101
+ if (deviceIds.microphone) {
102
+ constraints.audio.deviceId = { exact: deviceIds.microphone };
135
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;
136
138
  }
139
+ }
137
140
 
138
- /**
141
+ /**
139
142
  * Stop local media stream
140
143
  */
141
- stopLocalStream() {
142
- if (this.localStream) {
143
- console.log('Stopping local media stream');
144
- this.localStream.getTracks().forEach(track => {
145
- track.stop();
146
- });
147
- this.localStream = null;
148
- this.isVideoEnabled = false;
149
- this.isAudioEnabled = false;
150
-
151
- this.emit('localStreamStopped');
152
- }
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');
153
155
  }
156
+ }
154
157
 
155
- /**
158
+ /**
156
159
  * Toggle video track on/off
157
160
  */
158
- toggleVideo() {
159
- if (this.localStream) {
160
- const videoTrack = this.localStream.getVideoTracks()[0];
161
- if (videoTrack) {
162
- videoTrack.enabled = !videoTrack.enabled;
163
- this.emit('videoToggled', { enabled: videoTrack.enabled });
164
- return videoTrack.enabled;
165
- }
166
- }
167
- 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
+ }
168
169
  }
170
+ return false;
171
+ }
169
172
 
170
- /**
173
+ /**
171
174
  * Toggle audio track on/off
172
175
  */
173
- toggleAudio() {
174
- if (this.localStream) {
175
- const audioTrack = this.localStream.getAudioTracks()[0];
176
- if (audioTrack) {
177
- audioTrack.enabled = !audioTrack.enabled;
178
- this.emit('audioToggled', { enabled: audioTrack.enabled });
179
- return audioTrack.enabled;
180
- }
181
- }
182
- 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
+ }
183
184
  }
185
+ return false;
186
+ }
184
187
 
185
- /**
188
+ /**
186
189
  * Get current media state
187
190
  */
188
- getMediaState() {
189
- const state = {
190
- hasLocalStream: !!this.localStream,
191
- videoEnabled: false,
192
- audioEnabled: false,
193
- devices: this.devices
194
- };
195
-
196
- if (this.localStream) {
197
- const videoTrack = this.localStream.getVideoTracks()[0];
198
- const audioTrack = this.localStream.getAudioTracks()[0];
199
-
200
- state.videoEnabled = videoTrack ? videoTrack.enabled : false;
201
- state.audioEnabled = audioTrack ? audioTrack.enabled : false;
202
- }
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];
203
202
 
204
- return state;
203
+ state.videoEnabled = videoTrack ? videoTrack.enabled : false;
204
+ state.audioEnabled = audioTrack ? audioTrack.enabled : false;
205
205
  }
206
206
 
207
- /**
207
+ return state;
208
+ }
209
+
210
+ /**
208
211
  * Check if browser supports required APIs
209
212
  */
210
- static checkSupport() {
211
- const support = {
212
- getUserMedia: !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia),
213
- enumerateDevices: !!(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices),
214
- webRTC: !!(window.RTCPeerConnection)
215
- };
216
-
217
- support.fullSupport = support.getUserMedia && support.enumerateDevices && support.webRTC;
218
- return support;
219
- }
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
+ };
220
219
 
221
- /**
220
+ support.fullSupport = support.getUserMedia && support.enumerateDevices && support.webRTC;
221
+ return support;
222
+ }
223
+
224
+ /**
222
225
  * Get media permissions status
223
226
  */
224
- async getPermissions() {
225
- try {
226
- const permissions = {};
227
-
228
- if (navigator.permissions) {
229
- permissions.camera = await navigator.permissions.query({ name: 'camera' });
230
- permissions.microphone = await navigator.permissions.query({ name: 'microphone' });
231
- }
232
-
233
- return permissions;
234
- } catch (error) {
235
- console.warn('Could not check media permissions:', error);
236
- return {};
237
- }
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 {};
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Mark stream as local origin to prevent confusion with remote streams
245
+ */
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);
238
260
  }
261
+ }
239
262
  }