Skip to content

Commit

Permalink
Remove examples/internal
Browse files Browse the repository at this point in the history
Users find it frustrating that example code doesn't work out of tree.
This makes copying the examples out of the repo easier.

Relates to pion/webrtc#1981
  • Loading branch information
Sean-Der committed Sep 11, 2024
1 parent 96595fe commit fad9431
Show file tree
Hide file tree
Showing 16 changed files with 600 additions and 188 deletions.
2 changes: 1 addition & 1 deletion c-data-channels/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
c-data-channels: webrtc.so data-channels.c
gcc -o $@ data-channels.c ./webrtc.so

webrtc.so: webrtc.go bridge.go
webrtc.so: main.go bridge.go
go build -o $@ -buildmode=c-shared $^

clean:
Expand Down
6 changes: 3 additions & 3 deletions c-data-channels/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# c-data-channels
c-data-channels is a Pion WebRTC application that shows how you can send/recv DataChannel messages from a web browser that's
c-data-channels is a Pion WebRTC application that shows how you can send/recv DataChannel messages from a web browser that's
mostly identical to the pure Go implementation, https://github.com/pion/webrtc/tree/master/examples/data-channels.
The main difference is that the OnDataChannel is fully implemented in C.

Expand Down Expand Up @@ -47,10 +47,10 @@ Congrats, you have used Pion WebRTC! Now start building something cool
### bridge.go
This file contains all of the bridging between Go and C. This is the only file that contains cgo stuff.

### webrtc.go
### main.go
This file is pure Go. It is mostly identical to the original data-channel example.

### Reference
* https://github.com/golang/go/issues/20639
* https://github.com/golang/go/issues/25832
* https://github.com/pion/webrtc/tree/master/examples/data-channels/jsfiddle - jsfiddle source codes
* https://github.com/pion/webrtc/tree/master/examples/data-channels/jsfiddle - jsfiddle source codes
54 changes: 51 additions & 3 deletions c-data-channels/webrtc.go → c-data-channels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
package main

import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/pion/example-webrtc-applications/v3/internal/signal"
"github.com/pion/webrtc/v3"
)

Expand Down Expand Up @@ -43,7 +49,7 @@ func Run(f func(*webrtc.DataChannel)) {

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer)
decode(readUntilNewline(), &offer)

// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
Expand Down Expand Up @@ -72,10 +78,52 @@ func Run(f func(*webrtc.DataChannel)) {
<-gatherComplete

// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription())) //nolint
fmt.Println(encode(peerConnection.LocalDescription())) //nolint

// Block forever
select {}
}

func main() {}

// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error

r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}

if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}

fmt.Println("")
return
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(b)
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}
53 changes: 50 additions & 3 deletions ffmpeg-send/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
package main

import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"

"github.com/asticode/go-astiav"
"github.com/pion/example-webrtc-applications/v3/internal/signal"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
)
Expand Down Expand Up @@ -48,7 +53,7 @@ func main() {

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer)
decode(readUntilNewline(), &offer)

// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
Expand All @@ -74,7 +79,7 @@ func main() {
<-gatherComplete

// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
fmt.Println(encode(peerConnection.LocalDescription()))

// Start pushing buffers on these tracks
writeH264ToTrack(videoTrack)
Expand Down Expand Up @@ -264,3 +269,45 @@ func freeVideoCoding() {
encodeCodecContext.Free()
encodePacket.Free()
}

// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error

r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}

if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}

fmt.Println("")
return
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(b)
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}
52 changes: 49 additions & 3 deletions gocv-receive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ package main

import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"image"
"image/color"
"io"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"

"github.com/pion/example-webrtc-applications/v3/internal/signal"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media/ivfwriter"
Expand Down Expand Up @@ -187,7 +191,7 @@ func createWebRTCConn(ffmpegIn io.Writer) {

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer)
decode(readUntilNewline(), &offer)

// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
Expand Down Expand Up @@ -216,5 +220,47 @@ func createWebRTCConn(ffmpegIn io.Writer) {
<-gatherComplete

// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
fmt.Println(encode(peerConnection.LocalDescription()))
}

// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error

r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}

if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}

fmt.Println("")
return
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(b)
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}
53 changes: 50 additions & 3 deletions gstreamer-receive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
package main

import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"

"github.com/go-gst/go-gst/gst"
"github.com/go-gst/go-gst/gst/app"
"github.com/pion/example-webrtc-applications/v3/internal/signal"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
)
Expand Down Expand Up @@ -79,7 +84,7 @@ func main() {

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer)
decode(readUntilNewline(), &offer)

// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
Expand Down Expand Up @@ -108,7 +113,7 @@ func main() {
<-gatherComplete

// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
fmt.Println(encode(peerConnection.LocalDescription()))

// Block forever
select {}
Expand Down Expand Up @@ -148,3 +153,45 @@ func pipelineForCodec(track *webrtc.TrackRemote, codecName string) *app.Source {

return app.SrcFromElement(appSrc)
}

// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error

r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}

if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}

fmt.Println("")
return
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(b)
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}
Loading

0 comments on commit fad9431

Please sign in to comment.