-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPublisher.tsx
199 lines (173 loc) · 5.68 KB
/
Publisher.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import React, { useEffect, useRef, useState } from 'react';
import {
Button,
Dimensions,
StyleSheet,
View,
} from 'react-native';
import { SignalingChannel } from './SignalingChannel';
import { mediaDevices, MediaStream, RTCPeerConnection, RTCView } from "react-native-webrtc";
import { config } from './config';
const STREAM_ID = "170714163152216487974907";
export const Publisher = () => {
const [started, setStarted] = useState(false);
const [isFrontCamera, setIsFrontCamera] = useState(true);
const [audiMuted, setAudioMuted] = useState(false)
const [videoMuted, setVideoMuted] = useState(false)
const [localStream, setLocalStream] = useState<MediaStream>();
const localStreamRef = useRef<MediaStream>();
const peerConnection = useRef<RTCPeerConnection>()
const startStreaming = async () => {
if (!localStreamRef?.current) {
return;
}
peerConnection.current = new RTCPeerConnection({
iceServers: []
})
peerConnection.current?.addStream(localStreamRef.current);
peerConnection.current.onsignalingstatechange = () => console.log(peerConnection.current?.signalingState)
peerConnection.current.onicecandidateerror = console.log
peerConnection.current.onicecandidate = (event) => {
const candidate = event.candidate;
if (candidate && signalingChannel.current?.isChannelOpen()) {
signalingChannel.current?.sendJSON({
command: "takeCandidate",
streamId: STREAM_ID,
label: candidate.sdpMLineIndex.toString(),
id: candidate.sdpMid,
candidate: candidate.candidate,
})
}
}
const offer = await peerConnection.current.createOffer();
await peerConnection.current.setLocalDescription(offer);
}
const signalingChannel = useRef<SignalingChannel>(new SignalingChannel(config.SIGNALING_URL, {
onopen: () => {
signalingChannel.current?.sendJSON({
command: "publish",
streamId: STREAM_ID,
})
},
start: async () => {
signalingChannel.current?.sendJSON({
command: "takeConfiguration",
streamId: STREAM_ID,
type: "offer",
sdp: peerConnection?.current?.localDescription?.sdp,
})
},
stop: () => {
console.log("stop called")
},
takeCandidate: (data) => {
console.log("onIceCandidate remote");
peerConnection.current?.addIceCandidate({
candidate: data?.candidate || "",
sdpMLineIndex: Number(data?.label) || 0,
sdpMid: data?.id || "",
})
},
takeConfiguration: (data) => {
console.log("got answer")
const answer = data?.sdp || "";
peerConnection.current?.setRemoteDescription({
sdp: answer,
type: "answer"
})
}
}));
useEffect(() => {
const getStream = async () => {
let sourceId;
const sourceInfos = await mediaDevices.enumerateDevices()
for (const info of sourceInfos) {
if (info.kind == "videoinput" && info.facing == isFrontCamera ? "user" : "environment") {
sourceId = info.deviceId;
}
}
const media = await mediaDevices.getUserMedia({
audio: true,
video: {
facingMode: isFrontCamera ? "user" : "environment",
mandatory: {
minFrameRate: 30,
minHeight: Dimensions.get("window").height,
minWidth: Dimensions.get("window").width,
},
optional: sourceId
}
})
if (media) {
localStreamRef.current = media as MediaStream;
setLocalStream(media as MediaStream)
}
}
getStream();
}, [])
useEffect(() => {
return () => {
signalingChannel.current.close();
}
}, [])
return (
<View style={StyleSheet.absoluteFill}>
{!!localStream &&
<RTCView streamURL={localStream?.toURL()} style={{ flex: 1 }} mirror={isFrontCamera} objectFit="cover" />
}
<View style={styles.bottom}>
<Button title={started ? "Stop" : "Start"} color="white" onPress={async () => {
if (!started) {
setStarted(true);
await startStreaming();
signalingChannel.current?.open();
return;
}
setStarted(false);
peerConnection.current?.close();
signalingChannel.current?.close();
}} />
<Button title={audiMuted? "UMA": "MA"} color="white" onPress={() => {
const localStreams = peerConnection.current?.getLocalStreams() || [];
for(const stream of localStreams){
stream.getAudioTracks().forEach(each => {
each.enabled = audiMuted;
})
}
setAudioMuted(m => !m);
}} />
<Button title={videoMuted? "UMV": "MV"} color="white" onPress={() => {
const localStreams = peerConnection.current?.getLocalStreams() || [];
for(const stream of localStreams){
stream.getVideoTracks().forEach(each => {
each.enabled = videoMuted;
})
}
setVideoMuted(m => !m);
}} />
<Button title="SC" color="white" onPress={() => {
const localStreams = peerConnection.current?.getLocalStreams() || [];
for(const stream of localStreams){
stream.getVideoTracks().forEach(each => {
// @ts-ignore
// easiest way is to switch camera this way
each._switchCamera();
})
}
setIsFrontCamera(c => !c);
}} />
</View>
</View>
);
};
const styles = StyleSheet.create({
bottom: {
position: "absolute",
bottom: 0,
flexDirection: "row",
alignItems: "center",
width: "100%",
justifyContent: "space-evenly",
marginBottom: 30
}
})