Code refactoring
Code refactoring
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
## Userful links
|
||||||
|
|
||||||
|
- https://www.ietf.org/archive/id/draft-ietf-wish-whip-01.html
|
||||||
|
- https://www.ietf.org/id/draft-murillo-whep-01.html
|
||||||
|
- https://github.com/Glimesh/broadcast-box/
|
||||||
|
- https://github.com/obsproject/obs-studio/pull/7926
|
||||||
@@ -61,7 +61,7 @@ func asyncCandidates(tr *api.Transport, cons *webrtc.Conn) {
|
|||||||
if candidates, ok := ctx["candidate"].([]string); ok {
|
if candidates, ok := ctx["candidate"].([]string); ok {
|
||||||
// process candidates that receive before this moment
|
// process candidates that receive before this moment
|
||||||
for _, candidate := range candidates {
|
for _, candidate := range candidates {
|
||||||
cons.AddCandidate(candidate)
|
_ = cons.AddCandidate(candidate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove already processed candidates
|
// remove already processed candidates
|
||||||
|
|||||||
+1
-2
@@ -84,8 +84,7 @@ func Do(req *http.Request) (*http.Response, error) {
|
|||||||
|
|
||||||
req.Header.Set("Authorization", header)
|
req.Header.Set("Authorization", header)
|
||||||
|
|
||||||
res, err = client.Do(req)
|
if res, err = client.Do(req); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
## StateChange
|
||||||
|
|
||||||
|
1. offer = pc.CreateOffer()
|
||||||
|
2. pc.SetLocalDescription(offer)
|
||||||
|
3. OnICEGatheringStateChange: gathering
|
||||||
|
4. OnSignalingStateChange: have-local-offer
|
||||||
|
*. OnICEGatheringStateChange: complete
|
||||||
|
5. pc.SetRemoteDescription(answer)
|
||||||
|
6. OnSignalingStateChange: stable
|
||||||
|
7. OnICEConnectionStateChange: checking
|
||||||
|
8. OnICEConnectionStateChange: connected
|
||||||
@@ -57,7 +57,7 @@ func (c *Conn) GetAnswer() (answer string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return desc.SDP, nil
|
return c.pc.LocalDescription().SDP, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) GetCompleteAnswer() (answer string, err error) {
|
func (c *Conn) GetCompleteAnswer() (answer string, err error) {
|
||||||
|
|||||||
+13
-12
@@ -27,14 +27,17 @@
|
|||||||
<video id="video" autoplay controls playsinline muted></video>
|
<video id="video" autoplay controls playsinline muted></video>
|
||||||
<script>
|
<script>
|
||||||
// support api_path
|
// support api_path
|
||||||
let baseUrl = location.origin + location.pathname.substr(
|
const baseUrl = location.origin + location.pathname.substr(
|
||||||
0, location.pathname.lastIndexOf("/")
|
0, location.pathname.lastIndexOf("/")
|
||||||
);
|
);
|
||||||
|
|
||||||
let pc = new RTCPeerConnection({
|
const pc = new RTCPeerConnection({
|
||||||
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
iceServers: [
|
||||||
|
{urls: 'stun:stun.l.google.com:19302'},
|
||||||
|
]
|
||||||
});
|
});
|
||||||
pc.onicegatheringstatechange = async () => {
|
|
||||||
|
pc.addEventListener('icegatheringstatechange', async () => {
|
||||||
if (pc.iceGatheringState !== 'complete') return;
|
if (pc.iceGatheringState !== 'complete') return;
|
||||||
|
|
||||||
let r = await fetch(`${baseUrl}/api/webrtc${location.search}`, {
|
let r = await fetch(`${baseUrl}/api/webrtc${location.search}`, {
|
||||||
@@ -43,20 +46,18 @@
|
|||||||
await pc.setRemoteDescription({
|
await pc.setRemoteDescription({
|
||||||
type: 'answer', sdp: await r.text()
|
type: 'answer', sdp: await r.text()
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
pc.ontrack = ev => {
|
pc.addEventListener('track', ev => {
|
||||||
let video = document.getElementById('video');
|
let video = document.getElementById('video');
|
||||||
if (video.srcObject === null) {
|
if (video.srcObject === null) {
|
||||||
video.srcObject = ev.streams[0];
|
video.srcObject = ev.streams[0];
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
pc.addTransceiver('video');
|
pc.addTransceiver('video', {direction: 'recvonly'});
|
||||||
pc.addTransceiver('audio');
|
pc.addTransceiver('audio', {direction: 'recvonly'});
|
||||||
|
|
||||||
pc.createOffer({
|
pc.createOffer().then(offer => {
|
||||||
offerToReceiveVideo: true, offerToReceiveAudio: true
|
|
||||||
}).then(offer => {
|
|
||||||
pc.setLocalDescription(offer);
|
pc.setLocalDescription(offer);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
+1
-1
@@ -61,7 +61,7 @@
|
|||||||
|
|
||||||
if (ev.candidate !== null) {
|
if (ev.candidate !== null) {
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
type: 'webrtc/candidate', value: ev.candidate.toJSON().candidate
|
type: 'webrtc/candidate', value: ev.candidate.candidate
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user