@stream-io/video-react-sdk 0.0.1-alpha.46 → 0.0.1-alpha.47

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.
Files changed (31) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/css/styles.css +1 -3
  3. package/dist/css/styles.css.map +1 -1
  4. package/dist/src/components/CallControls/ToggleAudioButton.js +5 -3
  5. package/dist/src/components/CallControls/ToggleAudioButton.js.map +1 -1
  6. package/dist/src/components/CallControls/ToggleVideoButton.js +6 -4
  7. package/dist/src/components/CallControls/ToggleVideoButton.js.map +1 -1
  8. package/dist/src/components/DeviceSettings/DeviceSelectorAudio.js +5 -3
  9. package/dist/src/components/DeviceSettings/DeviceSelectorAudio.js.map +1 -1
  10. package/dist/src/components/DeviceSettings/DeviceSelectorVideo.js +3 -2
  11. package/dist/src/components/DeviceSettings/DeviceSelectorVideo.js.map +1 -1
  12. package/dist/src/components/Video/VideoPreview.js +9 -4
  13. package/dist/src/components/Video/VideoPreview.js.map +1 -1
  14. package/dist/src/core/contexts/MediaDevicesContext.d.ts +4 -6
  15. package/dist/src/core/contexts/MediaDevicesContext.js +22 -85
  16. package/dist/src/core/contexts/MediaDevicesContext.js.map +1 -1
  17. package/dist/src/core/hooks/index.d.ts +1 -0
  18. package/dist/src/core/hooks/index.js +1 -0
  19. package/dist/src/core/hooks/index.js.map +1 -1
  20. package/dist/src/core/hooks/useDevices.d.ts +68 -0
  21. package/dist/src/core/hooks/useDevices.js +101 -0
  22. package/dist/src/core/hooks/useDevices.js.map +1 -0
  23. package/package.json +5 -5
  24. package/src/components/CallControls/ToggleAudioButton.tsx +7 -3
  25. package/src/components/CallControls/ToggleVideoButton.tsx +7 -4
  26. package/src/components/DeviceSettings/DeviceSelectorAudio.tsx +9 -4
  27. package/src/components/DeviceSettings/DeviceSelectorVideo.tsx +3 -3
  28. package/src/components/Video/VideoPreview.tsx +15 -5
  29. package/src/core/contexts/MediaDevicesContext.tsx +46 -121
  30. package/src/core/hooks/index.ts +1 -0
  31. package/src/core/hooks/useDevices.ts +145 -0
@@ -1,3 +1,4 @@
1
1
  export * from './useAudioPublisher';
2
+ export * from './useDevices';
2
3
  export * from './useVideoPublisher';
3
4
  export * from './useTrackElementVisibility';
@@ -0,0 +1,145 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { Observable, pairwise } from 'rxjs';
3
+ import {
4
+ getAudioDevices,
5
+ getAudioOutputDevices,
6
+ getVideoDevices,
7
+ } from '@stream-io/video-client';
8
+
9
+ /**
10
+ * Observes changes in connected devices and maintains an up-to-date array of connected MediaDeviceInfo objects.
11
+ * @param observeDevices
12
+ * @category Device Management
13
+ */
14
+ export const useDevices = (
15
+ observeDevices: () => Observable<MediaDeviceInfo[]>,
16
+ ) => {
17
+ const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
18
+
19
+ useEffect(() => {
20
+ const subscription = observeDevices().subscribe(setDevices);
21
+
22
+ return () => {
23
+ subscription.unsubscribe();
24
+ };
25
+ }, [observeDevices]);
26
+
27
+ return devices;
28
+ };
29
+
30
+ /**
31
+ * Observes changes and maintains an array of connected video input devices
32
+ * @category Device Management
33
+ */
34
+ export const useVideoDevices = () => useDevices(getVideoDevices);
35
+
36
+ /**
37
+ * Observes changes and maintains an array of connected audio input devices
38
+ * @category Device Management
39
+ */
40
+ export const useAudioInputDevices = () => useDevices(getAudioDevices);
41
+
42
+ /**
43
+ * Observes changes and maintains an array of connected audio output devices
44
+ * @category Device Management
45
+ */
46
+ export const useAudioOutputDevices = () => useDevices(getAudioOutputDevices);
47
+
48
+ /**
49
+ * Verifies that newly selected device id exists among the registered devices.
50
+ * If the selected device id is not found among existing devices, switches to the default device.
51
+ * @param devices$
52
+ * @param switchToDefaultDevice
53
+ * @param selectedDeviceId
54
+ * @category Device Management
55
+ */
56
+ export const useDeviceFallback = (
57
+ devices$: Observable<MediaDeviceInfo[]>,
58
+ switchToDefaultDevice: () => void,
59
+ selectedDeviceId?: string,
60
+ ) => {
61
+ useEffect(() => {
62
+ const validateDeviceId = devices$.pipe().subscribe((devices) => {
63
+ const deviceFound = devices.find(
64
+ (device) => device.deviceId === selectedDeviceId,
65
+ );
66
+ if (!deviceFound) switchToDefaultDevice();
67
+ });
68
+
69
+ return () => {
70
+ validateDeviceId.unsubscribe();
71
+ };
72
+ }, [devices$, selectedDeviceId, switchToDefaultDevice]);
73
+ };
74
+
75
+ /**
76
+ * Verifies that newly selected video device id exists among the registered devices.
77
+ * If the selected device id is not found among existing devices, switches to the default video device.
78
+ * @param switchToDefaultDevice
79
+ * @param selectedDeviceId
80
+ * @category Device Management
81
+ */
82
+ export const useVideoDeviceFallback = (
83
+ switchToDefaultDevice: () => void,
84
+ selectedDeviceId?: string,
85
+ ) =>
86
+ useDeviceFallback(getVideoDevices(), switchToDefaultDevice, selectedDeviceId);
87
+
88
+ /**
89
+ * Verifies that newly selected audio input device id exists among the registered devices.
90
+ * If the selected device id is not found among existing devices, switches to the default audio input device.
91
+ * @param switchToDefaultDevice
92
+ * @param selectedDeviceId
93
+ * @category Device Management
94
+ */
95
+ export const useAudioInputDeviceFallback = (
96
+ switchToDefaultDevice: () => void,
97
+ selectedDeviceId?: string,
98
+ ) =>
99
+ useDeviceFallback(getAudioDevices(), switchToDefaultDevice, selectedDeviceId);
100
+
101
+ /**
102
+ * Verifies that newly selected audio output device id exists among the registered devices.
103
+ * If the selected device id is not found among existing devices, switches to the default audio output device.
104
+ * @param switchToDefaultDevice
105
+ * @param selectedDeviceId
106
+ * @category Device Management
107
+ */
108
+ export const useAudioOutputDeviceFallback = (
109
+ switchToDefaultDevice: () => void,
110
+ selectedDeviceId?: string,
111
+ ) =>
112
+ useDeviceFallback(
113
+ getAudioOutputDevices(),
114
+ switchToDefaultDevice,
115
+ selectedDeviceId,
116
+ );
117
+
118
+ /**
119
+ * Observes devices of certain kind are made unavailable and executes onDisconnect callback
120
+ * @param observeDevices
121
+ * @param onDisconnect
122
+ * @category Device Management
123
+ */
124
+ export const useOnUnavailableDevices = (
125
+ observeDevices: () => Observable<MediaDeviceInfo[]>,
126
+ onDisconnect: () => void,
127
+ ) => {
128
+ useEffect(() => {
129
+ const subscription = observeDevices()
130
+ .pipe(pairwise())
131
+ .subscribe(([prev, current]) => {
132
+ if (prev.length > 0 && current.length === 0) onDisconnect();
133
+ });
134
+
135
+ return () => subscription.unsubscribe();
136
+ }, [observeDevices, onDisconnect]);
137
+ };
138
+
139
+ /**
140
+ * Observes disconnect of all video devices and executes onDisconnect callback
141
+ * @param onDisconnect
142
+ * @category Device Management
143
+ */
144
+ export const useOnUnavailableVideoDevices = (onDisconnect: () => void) =>
145
+ useOnUnavailableDevices(getVideoDevices, onDisconnect);