Skip to content

Commit 7acbbed

Browse files
Merge pull request #24 from ant-media/fix/issue-6466
Fix cannot re use adaptor multiple times
2 parents 174fc97 + 7848ca4 commit 7acbbed

File tree

2 files changed

+83
-49
lines changed

2 files changed

+83
-49
lines changed

example/src/Conference.tsx

+82-48
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Icon from 'react-native-vector-icons/Ionicons';
1414
import { DeviceEventEmitter } from 'react-native';
1515
import InCallManager from 'react-native-incall-manager';
1616

17-
var publishStreamId:string;
17+
var publishStreamId: string;
1818

1919
export default function Conference() {
2020
var defaultRoomName = 'room1';
@@ -28,7 +28,7 @@ export default function Conference() {
2828
const [remoteTracks, setremoteTracks] = useState<any>([]);
2929
const [isMuted, setIsMuted] = useState(false);
3030
const [isCameraOpen, setIsCameraOpen] = useState(true);
31-
31+
const [isWaitingWebsocketInit, setIsWaitingWebsocketInit] = useState(false);
3232

3333
const adaptor = useAntMedia({
3434
url: webSocketUrl,
@@ -43,6 +43,9 @@ export default function Conference() {
4343
},
4444
callback(command: any, data: any) {
4545
switch (command) {
46+
case 'initiated':
47+
console.log('initiated');
48+
break;
4649
case 'pong':
4750
break;
4851
case 'publish_started':
@@ -52,6 +55,19 @@ export default function Conference() {
5255
break;
5356
case 'publish_finished':
5457
setIsPublishing(false);
58+
adaptor.closeWebSocket();
59+
break;
60+
case 'local_stream_updated':
61+
console.log('local_stream_updated');
62+
verify();
63+
break;
64+
case 'websocket_not_initialized':
65+
setIsWaitingWebsocketInit(true);
66+
adaptor.initialiseWebSocket();
67+
break;
68+
case 'websocket_closed':
69+
console.log('websocket_closed');
70+
adaptor.stopLocalStream();
5571
break;
5672
case 'play_finished':
5773
console.log('play_finished');
@@ -77,6 +93,10 @@ export default function Conference() {
7793
}
7894
}
7995
break;
96+
case "data_received":
97+
console.log('data_received', data);
98+
handleNotificationEvent(data);
99+
break;
80100
case "available_devices":
81101
console.log('available_devices', data);
82102
break;
@@ -92,16 +112,42 @@ export default function Conference() {
92112
console.error('callbackError', err, data);
93113
}
94114
},
95-
peer_connection_config: {
96-
iceServers: [
97-
{
98-
url: 'stun:stun.l.google.com:19302',
99-
},
100-
],
101-
},
102115
debug: true,
103116
});
104117

118+
const verify = () => {
119+
console.log('in verify');
120+
if (adaptor.localStream.current && adaptor.localStream.current.toURL()) {
121+
console.log('in verify if adaptor local stream', adaptor.localStream);
122+
if (isWaitingWebsocketInit) {
123+
setIsWaitingWebsocketInit(false);
124+
publishStreamId = generateRandomString(12);
125+
adaptor.publish(publishStreamId, undefined, undefined, undefined, undefined, roomId, "");
126+
}
127+
return setLocalMedia(adaptor.localStream.current.toURL());
128+
}
129+
setTimeout(verify, 5000);
130+
};
131+
132+
const handleNotificationEvent = (notificationEvent: any) => {
133+
//var notificationEvent = JSON.parse(data);
134+
if (notificationEvent != null && typeof notificationEvent == "object") {
135+
var eventStreamId = notificationEvent.streamId;
136+
var eventType = notificationEvent.eventType;
137+
138+
if (eventType == "VIDEO_TRACK_ASSIGNMENT_LIST") {
139+
var videoTrackAssignmentList = notificationEvent.payload;
140+
console.log("VIDEO_TRACK_ASSIGNMENT_LIST", videoTrackAssignmentList);
141+
} else if (eventType == "AUDIO_TRACK_ASSIGNMENT") {
142+
console.log("AUDIO_TRACK_ASSIGNMENT", notificationEvent.payload);
143+
} else if (eventType == "TRACK_LIST_UPDATED") {
144+
console.log("TRACK_LIST_UPDATED", notificationEvent.payload);
145+
adaptor.requestVideoTrackAssignments(roomId);
146+
}
147+
148+
}
149+
};
150+
105151
const handleMic = useCallback(() => {
106152
if (adaptor) {
107153
(isMuted) ? adaptor.unmuteLocalMic() : adaptor.muteLocalMic();
@@ -133,14 +179,14 @@ export default function Conference() {
133179
}
134180
}, [adaptor, roomId]);
135181

136-
/*
137-
const handleRemoteAudio = useCallback((streamId: string) => {
138-
if (adaptor) {
139-
adaptor?.muteRemoteAudio(streamId, roomId);
140-
//adaptor?.unmuteRemoteAudio(streamId, roomId);
141-
}
142-
}, [adaptor]);
143-
*/
182+
/*
183+
const handleRemoteAudio = useCallback((streamId: string) => {
184+
if (adaptor) {
185+
adaptor?.muteRemoteAudio(streamId, roomId);
186+
//adaptor?.unmuteRemoteAudio(streamId, roomId);
187+
}
188+
}, [adaptor]);
189+
*/
144190

145191
const removeRemoteVideo = (streamId?: string) => {
146192
if (streamId != null || streamId != undefined) {
@@ -160,18 +206,6 @@ export default function Conference() {
160206
setremoteTracks([]);
161207
};
162208

163-
useEffect(() => {
164-
const verify = () => {
165-
if (adaptor.localStream.current && adaptor.localStream.current.toURL()) {
166-
let videoTrack = adaptor.localStream.current.getVideoTracks()[0];
167-
// @ts-ignore
168-
return setLocalMedia(videoTrack);
169-
}
170-
setTimeout(verify, 5000);
171-
};
172-
verify();
173-
}, [adaptor.localStream]);
174-
175209
useEffect(() => {
176210
if (localMedia && remoteTracks) {
177211
InCallManager.start({ media: 'video' });
@@ -207,14 +241,14 @@ export default function Conference() {
207241
</>
208242
) : (
209243
<>
210-
<View style={{ flexDirection: 'row', justifyContent: 'center' }}>
211-
<TouchableOpacity onPress={handleMic} style={styles.roundButton}>
212-
<Icon name={isMuted ? 'mic-off-outline' : 'mic-outline'} size={15} color="#000" />
213-
</TouchableOpacity>
214-
<TouchableOpacity onPress={handleCamera} style={styles.roundButton}>
215-
<Icon name={isCameraOpen ? 'videocam-outline' : 'videocam-off-outline'} size={15} color="#000" />
216-
</TouchableOpacity>
217-
</View>
244+
<View style={{ flexDirection: 'row', justifyContent: 'center' }}>
245+
<TouchableOpacity onPress={handleMic} style={styles.roundButton}>
246+
<Icon name={isMuted ? 'mic-off-outline' : 'mic-outline'} size={15} color="#000" />
247+
</TouchableOpacity>
248+
<TouchableOpacity onPress={handleCamera} style={styles.roundButton}>
249+
<Icon name={isCameraOpen ? 'videocam-outline' : 'videocam-off-outline'} size={15} color="#000" />
250+
</TouchableOpacity>
251+
</View>
218252
<Text style={styles.heading1}>Remote Streams</Text>
219253
{
220254
<ScrollView
@@ -236,7 +270,7 @@ export default function Conference() {
236270
<View key={index} style={trackObj.track.kind === 'audio' ? { display: 'none' } : {}}>
237271
<>{
238272
// @ts-ignore
239-
rtc_view(trackObj.track, styles.players)
273+
rtc_view(trackObj.track, styles.players)
240274
}</>
241275
{/*
242276
<View style={{ flexDirection: 'row', justifyContent: 'center' }}>
@@ -313,14 +347,14 @@ const styles = StyleSheet.create({
313347
marginTop: 20,
314348
},
315349
roundButton: {
316-
alignItems: 'center',
317-
justifyContent: 'center',
318-
backgroundColor: '#DDDDDD',
319-
padding: 5,
320-
borderRadius: 25, // This will make the button round
321-
width: 30, // Diameter of the button
322-
height: 30, // Diameter of the button
323-
marginTop: 10,
324-
marginHorizontal: 10,
325-
},
350+
alignItems: 'center',
351+
justifyContent: 'center',
352+
backgroundColor: '#DDDDDD',
353+
padding: 5,
354+
borderRadius: 25, // This will make the button round
355+
width: 30, // Diameter of the button
356+
height: 30, // Diameter of the button
357+
marginTop: 10,
358+
marginHorizontal: 10,
359+
},
326360
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antmedia/react-native-ant-media",
3-
"version": "1.0.9",
3+
"version": "1.10.0",
44
"description": "Ant Media Server WebRTC React Native SDK and Reference Project",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)