-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental support for ICE TCP
See pion/ice issue and PR: - https://github.com/pion/ice/tree/issue-196 - pion/ice#226
- Loading branch information
Showing
9 changed files
with
272 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# ice-tcp | ||
ice-tcp demonstrates Pion WebRTC's ICE TCP abilities. | ||
|
||
## Instructions | ||
|
||
### Download ice-tcp | ||
This example requires you to clone the repo since it is serving static HTML. | ||
|
||
``` | ||
mkdir -p $GOPATH/src/github.com/pion | ||
cd $GOPATH/src/github.com/pion | ||
git clone https://github.com/pion/webrtc.git | ||
cd webrtc/examples/ice-tcp | ||
``` | ||
|
||
### Run ice-tcp | ||
Execute `go run *.go` | ||
|
||
### Open the Web UI | ||
Open [http://localhost:8080](http://localhost:8080). This will automatically start a PeerConnection. This page will now prints stats about the PeerConnection. The UDP candidates will be filtered out from the SDP. | ||
|
||
Congrats, you have used Pion WebRTC! Now start building something cool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<html> | ||
<head> | ||
<title>ice-tcp</title> | ||
</head> | ||
|
||
<body> | ||
<button onclick="window.doSignaling(true)"> ICE TCP </button><br /> | ||
|
||
|
||
<h3> ICE Connection States </h3> | ||
<div id="iceConnectionStates"></div> <br /> | ||
|
||
<h3> Inbound DataChannel Messages </h3> | ||
<div id="inboundDataChannelMessages"></div> | ||
</body> | ||
|
||
<script> | ||
let pc = new RTCPeerConnection() | ||
let dc = pc.createDataChannel('data') | ||
|
||
dc.onmessage = event => { | ||
let el = document.createElement('p') | ||
el.appendChild(document.createTextNode(event.data)) | ||
|
||
document.getElementById('inboundDataChannelMessages').appendChild(el); | ||
} | ||
|
||
pc.oniceconnectionstatechange = () => { | ||
let el = document.createElement('p') | ||
el.appendChild(document.createTextNode(pc.iceConnectionState)) | ||
|
||
document.getElementById('iceConnectionStates').appendChild(el); | ||
} | ||
|
||
pc.createOffer() | ||
.then(offer => { | ||
pc.setLocalDescription(offer) | ||
|
||
return fetch(`/doSignaling`, { | ||
method: 'post', | ||
headers: { | ||
'Accept': 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(offer) | ||
}) | ||
}) | ||
.then(res => res.json()) | ||
.then(res => { | ||
pc.setRemoteDescription(res) | ||
}) | ||
.catch(alert) | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pion/webrtc/v3" | ||
) | ||
|
||
var peerConnection *webrtc.PeerConnection //nolint | ||
|
||
func doSignaling(w http.ResponseWriter, r *http.Request) { | ||
var err error | ||
|
||
if peerConnection == nil { | ||
m := webrtc.MediaEngine{} | ||
m.RegisterDefaultCodecs() | ||
|
||
settingEngine := webrtc.SettingEngine{} | ||
|
||
// Enable support only for TCP ICE candidates. | ||
settingEngine.SetNetworkTypes([]webrtc.NetworkType{ | ||
webrtc.NetworkTypeTCP4, | ||
webrtc.NetworkTypeTCP6, | ||
}) | ||
settingEngine.SetICETCPPort(8443) | ||
|
||
api := webrtc.NewAPI( | ||
webrtc.WithMediaEngine(m), | ||
webrtc.WithSettingEngine(settingEngine), | ||
) | ||
if peerConnection, err = api.NewPeerConnection(webrtc.Configuration{}); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Set the handler for ICE connection state | ||
// This will notify you when the peer has connected/disconnected | ||
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { | ||
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) | ||
}) | ||
|
||
// Send the current time via a DataChannel to the remote peer every 3 seconds | ||
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) { | ||
d.OnOpen(func() { | ||
for range time.Tick(time.Second * 3) { | ||
if err = d.SendText(time.Now().String()); err != nil { | ||
panic(err) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
var offer webrtc.SessionDescription | ||
if err = json.NewDecoder(r.Body).Decode(&offer); err != nil { | ||
panic(err) | ||
} | ||
|
||
if err = peerConnection.SetRemoteDescription(offer); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Create channel that is blocked until ICE Gathering is complete | ||
gatherComplete := webrtc.GatheringCompletePromise(peerConnection) | ||
|
||
answer, err := peerConnection.CreateAnswer(nil) | ||
if err != nil { | ||
panic(err) | ||
} else if err = peerConnection.SetLocalDescription(answer); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Block until ICE Gathering is complete, disabling trickle ICE | ||
// we do this because we only can exchange one signaling message | ||
// in a production application you should exchange ICE Candidates via OnICECandidate | ||
<-gatherComplete | ||
|
||
response, err := json.Marshal(*peerConnection.LocalDescription()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
if _, err := w.Write(response); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func main() { | ||
http.Handle("/", http.FileServer(http.Dir("."))) | ||
http.HandleFunc("/doSignaling", doSignaling) | ||
|
||
fmt.Println("Open http://localhost:8080 to access this demo") | ||
panic(http.ListenAndServe(":8080", nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.