From c40e4a53ef1b4f7950a460e2efccf45fa3c1da2c Mon Sep 17 00:00:00 2001 From: jinleileiking Date: Wed, 13 Feb 2019 17:31:56 +0800 Subject: [PATCH] Add http endpoint to SFU example Resolves #400 --- README.md | 1 + examples/sfu/README.md | 9 +++++++-- examples/sfu/main.go | 30 +++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9ca42dfd..1d6b7ae6 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Check out the **[contributing wiki](https://github.com/pions/webrtc/wiki/Contrib * [Yutaka Takeda](https://github.com/enobufs) *Fix ICE connection timeout* * [Hugo Arregui](https://github.com/hugoArregui) *Fix connection timeout* * [Rob Deutsch](https://github.com/rob-deutsch) *RTCRtpReceiver graceful shutdown* +* [Jin Lei](https://github.com/jinleileiking) - *SFU example use http* ### License MIT License - see [LICENSE](LICENSE) for full text diff --git a/examples/sfu/README.md b/examples/sfu/README.md index 6cff1130..b00c6207 100644 --- a/examples/sfu/README.md +++ b/examples/sfu/README.md @@ -17,10 +17,15 @@ go get github.com/pions/webrtc/examples/sfu Run `sfu` OR run `main.go` in `github.com/pions/webrtc/examples/sfu` ### Start a publisher -Click `Publish a Broadcast` and paste the SDP into your terminal. The `sfu` application will respond with an offer, paste this into the second input field. Then press `Start Session` + +* Click `Publish a Broadcast` +* `curl localhost:8080/sdp -d "YOUR SDP"`. The `sfu` application will respond with an offer, paste this into the second input field. Then press `Start Session` ### Join the broadcast -Click `Join a Broadcast` and paste the SDP into your terminal. The `sfu` application will respond with an offer, paste this into the second input field. Then press `Start Session` +* Click `Join a Broadcast` +* `curl localhost:8080/sdp -d "YOUR SDP"`. The `sfu` application will respond with an offer, paste this into the second input field. Then press `Start Session` + +You can change the listening port using `-port 8011` You can `Join the broadcast` as many times as you want. The `sfu` Golang application is relaying all traffic, so your browser only has to upload once. diff --git a/examples/sfu/main.go b/examples/sfu/main.go index 3f425a43..3b567389 100644 --- a/examples/sfu/main.go +++ b/examples/sfu/main.go @@ -2,8 +2,11 @@ package main import ( "bufio" + "flag" "fmt" - "os" + "io/ioutil" + "net/http" + "strconv" "sync" "time" @@ -29,15 +32,32 @@ func mustReadStdin(reader *bufio.Reader) string { return rawSd } +func mustReadHttp(sdp chan string) string { + ret := <-sdp + return ret +} + const ( rtcpPLIInterval = time.Second * 3 ) func main() { - reader := bufio.NewReader(os.Stdin) + port := flag.Int("port", 8080, "http server port") + flag.Parse() + + sdp := make(chan string) + http.HandleFunc("/sdp", func(w http.ResponseWriter, r *http.Request) { + body, _ := ioutil.ReadAll(r.Body) + fmt.Fprintf(w, "done") + sdp <- string(body) + }) + + go func() { + http.ListenAndServe(":"+strconv.Itoa(*port), nil) + }() offer := webrtc.RTCSessionDescription{} - util.Decode(mustReadStdin(reader), &offer) + util.Decode(mustReadHttp(sdp), &offer) fmt.Println("") /* Everything below is the pion-WebRTC API, thanks for using it! */ @@ -101,10 +121,10 @@ func main() { outboundPayloadType := <-inboundPayloadType for { fmt.Println("") - fmt.Println("Paste an base64 SDP to start sendonly peer connection") + fmt.Println("Curl an base64 SDP to start sendonly peer connection") recvOnlyOffer := webrtc.RTCSessionDescription{} - util.Decode(mustReadStdin(reader), &recvOnlyOffer) + util.Decode(mustReadHttp(sdp), &recvOnlyOffer) // Create a new RTCPeerConnection peerConnection, err := webrtc.New(peerConnectionConfig)