UNPKG

6.98 kBJavaScriptView Raw
1import {
2 getLocalIsSubscribedToTrack,
3 getLocalTrack,
4 getRemoteTrack,
5 getLocalCustomTrack,
6 getRemoteCustomTrack,
7} from './shared-with-pluot-core/selectors';
8
9// Adds tracks to daily-js Participant object.
10export function addTracks(p) {
11 const state = store.getState();
12 for (const type of ['cam', 'screen']) {
13 for (const kind of ['video', 'audio']) {
14 const key =
15 type === 'cam'
16 ? kind
17 : `screen${kind.charAt(0).toUpperCase() + kind.slice(1)}`;
18 const trackInfo = p.tracks[key];
19 if (trackInfo) {
20 const track = p.local
21 ? getLocalTrack(state, type, kind)
22 : getRemoteTrack(state, p.session_id, type, kind);
23 if (trackInfo.state === 'playable') {
24 trackInfo.track = track;
25 }
26 // Set "persistent" track field where track is present even if not "playable"
27 trackInfo.persistentTrack = track;
28 }
29 }
30 }
31}
32
33// todo: refactor so that his logic is part of addTracks and friends()
34export function addCustomTracks(p) {
35 try {
36 const state = store.getState();
37 for (const trackEntryKey in p.tracks) {
38 if (isPredefinedTrack(trackEntryKey)) {
39 continue;
40 }
41 const kind = p.tracks[trackEntryKey].kind;
42 if (!kind) {
43 console.error('unknown type for custom track');
44 continue;
45 }
46 const track = p.local
47 ? getLocalCustomTrack(state, trackEntryKey, kind)
48 : getRemoteCustomTrack(state, p.session_id, trackEntryKey, kind);
49 const trackInfo = p.tracks[trackEntryKey];
50 if (track && trackInfo && trackInfo.state === 'playable') {
51 p.tracks[trackEntryKey].track = track;
52 }
53 }
54 } catch (e) {
55 console.error(e);
56 }
57}
58
59export function isPredefinedTrack(trackEntryKey) {
60 return ['video', 'audio', 'screenVideo', 'screenAudio'].includes(
61 trackEntryKey
62 );
63}
64
65// Adds tracks to daily-js Participant object.
66export function addLegacyTracks(p, prevP) {
67 let state = store.getState();
68
69 if (p.local) {
70 if (p.audio) {
71 try {
72 p.audioTrack = state.local.streams.cam.stream.getAudioTracks()[0];
73 if (!p.audioTrack) {
74 p.audio = false;
75 }
76 } catch (e) {}
77 }
78 if (p.video) {
79 try {
80 p.videoTrack = state.local.streams.cam.stream.getVideoTracks()[0];
81 if (!p.videoTrack) {
82 p.video = false;
83 }
84 } catch (e) {}
85 }
86 if (p.screen) {
87 try {
88 p.screenVideoTrack = state.local.streams.screen.stream.getVideoTracks()[0];
89 p.screenAudioTrack = state.local.streams.screen.stream.getAudioTracks()[0];
90 if (!(p.screenVideoTrack || p.screenAudioTrack)) {
91 p.screen = false;
92 }
93 } catch (e) {}
94 }
95 return;
96 }
97
98 let connected = true; // default to true to minimize impact of new bugs
99 // as of 11/20/2019 when this block of code was
100 // first written
101 try {
102 let sp = state.participants[p.session_id];
103 if (
104 sp &&
105 sp.public &&
106 sp.public.rtcType &&
107 sp.public.rtcType.impl === 'peer-to-peer'
108 ) {
109 if (
110 sp.private &&
111 !['connected', 'completed'].includes(sp.private.peeringState)
112 ) {
113 connected = false;
114 }
115 }
116 } catch (e) {
117 console.error(e);
118 }
119 if (!connected) {
120 p.audio = false;
121 p.audioTrack = false;
122 p.video = false;
123 p.videoTrack = false;
124 p.screen = false;
125 p.screenTrack = false;
126 return;
127 }
128
129 try {
130 const allStreams = state.streams;
131
132 // find audio track
133 if (
134 p.audio &&
135 getLocalIsSubscribedToTrack(state, p.session_id, 'cam-audio')
136 ) {
137 const audioTrack = getRemoteTrack(state, p.session_id, 'cam', 'audio');
138 if (audioTrack) {
139 if (
140 prevP &&
141 prevP.audioTrack &&
142 prevP.audioTrack.id === audioTrack.id
143 ) {
144 // if we have an apparently identical audio track already in
145 // our participant struct leave it in place to avoid flicker
146 // during quick muted/unmuted PeerConnection cycles. we'll update
147 // audio/video muted at the app level via signaling
148 p.audioTrack = audioTrack;
149 } else if (!audioTrack.muted) {
150 // otherwise, add the found track if it's not muted
151 p.audioTrack = audioTrack;
152 }
153 }
154 if (!p.audioTrack) {
155 p.audio = false;
156 }
157 }
158 // find video track
159 if (
160 p.video &&
161 getLocalIsSubscribedToTrack(state, p.session_id, 'cam-video')
162 ) {
163 const videoTrack = getRemoteTrack(state, p.session_id, 'cam', 'video');
164 if (videoTrack) {
165 if (
166 prevP &&
167 prevP.videoTrack &&
168 prevP.videoTrack.id === videoTrack.id
169 ) {
170 p.videoTrack = videoTrack;
171 } else if (!videoTrack.muted) {
172 // otherwise, add the found track if it's not muted
173 p.videoTrack = videoTrack;
174 }
175 }
176 if (!p.videoTrack) {
177 p.video = false;
178 }
179 }
180
181 // find screen-share audio track
182 if (
183 p.screen &&
184 getLocalIsSubscribedToTrack(state, p.session_id, 'screen-audio')
185 ) {
186 const screenAudioTrack = getRemoteTrack(
187 state,
188 p.session_id,
189 'screen',
190 'audio'
191 );
192 if (screenAudioTrack) {
193 if (
194 prevP &&
195 prevP.screenAudioTrack &&
196 prevP.screenAudioTrack.id === screenAudioTrack.id
197 ) {
198 p.screenAudioTrack = screenAudioTrack;
199 } else if (!screenAudioTrack.muted) {
200 // otherwise, add the found track if it's not muted
201 p.screenAudioTrack = screenAudioTrack;
202 }
203 }
204 }
205 // find screen-share video track
206 if (
207 p.screen &&
208 getLocalIsSubscribedToTrack(state, p.session_id, 'screen-video')
209 ) {
210 const screenVideoTrack = getRemoteTrack(
211 state,
212 p.session_id,
213 'screen',
214 'video'
215 );
216 if (screenVideoTrack) {
217 if (
218 prevP &&
219 prevP.screenVideoTrack &&
220 prevP.screenVideoTrack.id === screenVideoTrack.id
221 ) {
222 p.screenVideoTrack = screenVideoTrack;
223 } else if (!screenVideoTrack.muted) {
224 // otherwise, add the found track if it's not muted
225 // note: there is an issue here with timing ... Chrome (and
226 // possibly other browsers), gets a video track that's initially
227 // not muted, for an audio-only screenshare. The track
228 // switches to muted fairly quickly, but we don't have any
229 // logic in place to respond to that. todo: fix this so that,
230 // at the very least we get a track-stopped event when the
231 // "empty" track switches to muted.
232 p.screenVideoTrack = screenVideoTrack;
233 }
234 }
235 }
236 if (!(p.screenVideoTrack || p.screenAudioTrack)) {
237 p.screen = false;
238 }
239 } catch (e) {
240 console.error('unexpected error matching up tracks', e);
241 }
242}