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.
@@ -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
- 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 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
- 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
- }
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
- 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
- // 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
- 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');
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
- 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
- }
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
- 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
- }
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
- 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];
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
- return state;
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
- 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
- }
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
- 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
- }
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
- 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);
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
  }