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 b5b268a
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)

Check warning on line 52 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L52

Added line #L52 was not covered by tests

// 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

Check warning on line 81 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L81

Added line #L81 was not covered by tests

// Block forever
select {}
}

func main() {}

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

Check warning on line 91 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L90-L91

Added lines #L90 - L91 were not covered by tests

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

Check warning on line 97 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L93-L97

Added lines #L93 - L97 were not covered by tests
}

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

Check warning on line 101 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}
}

fmt.Println("")
return

Check warning on line 106 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L105-L106

Added lines #L105 - L106 were not covered by tests
}

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

Check warning on line 113 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L110-L113

Added lines #L110 - L113 were not covered by tests
}

return base64.StdEncoding.EncodeToString(b)

Check warning on line 116 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L116

Added line #L116 was not covered by tests
}

// 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)

Check warning on line 123 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L120-L123

Added lines #L120 - L123 were not covered by tests
}

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

Check warning on line 127 in c-data-channels/main.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/main.go#L126-L127

Added lines #L126 - L127 were not covered by tests
}
}
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)

Check warning on line 56 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L56

Added line #L56 was not covered by tests

// 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()))

Check warning on line 82 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L82

Added line #L82 was not covered by tests

// 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

Check warning on line 275 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L274-L275

Added lines #L274 - L275 were not covered by tests

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

Check warning on line 281 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L277-L281

Added lines #L277 - L281 were not covered by tests
}

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

Check warning on line 285 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L284-L285

Added lines #L284 - L285 were not covered by tests
}
}

fmt.Println("")
return

Check warning on line 290 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L289-L290

Added lines #L289 - L290 were not covered by tests
}

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

Check warning on line 297 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L294-L297

Added lines #L294 - L297 were not covered by tests
}

return base64.StdEncoding.EncodeToString(b)

Check warning on line 300 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L300

Added line #L300 was not covered by tests
}

// 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)

Check warning on line 307 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L304-L307

Added lines #L304 - L307 were not covered by tests
}

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

Check warning on line 311 in ffmpeg-send/main.go

View check run for this annotation

Codecov / codecov/patch

ffmpeg-send/main.go#L310-L311

Added lines #L310 - L311 were not covered by tests
}
}
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)

Check warning on line 87 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L87

Added line #L87 was not covered by tests

// 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()))

Check warning on line 116 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L116

Added line #L116 was not covered by tests

// 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

Check warning on line 159 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L158-L159

Added lines #L158 - L159 were not covered by tests

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

Check warning on line 165 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L161-L165

Added lines #L161 - L165 were not covered by tests
}

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

Check warning on line 169 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L168-L169

Added lines #L168 - L169 were not covered by tests
}
}

fmt.Println("")
return

Check warning on line 174 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L173-L174

Added lines #L173 - L174 were not covered by tests
}

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

Check warning on line 181 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L178-L181

Added lines #L178 - L181 were not covered by tests
}

return base64.StdEncoding.EncodeToString(b)

Check warning on line 184 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L184

Added line #L184 was not covered by tests
}

// 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)

Check warning on line 191 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L188-L191

Added lines #L188 - L191 were not covered by tests
}

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

Check warning on line 195 in gstreamer-receive/main.go

View check run for this annotation

Codecov / codecov/patch

gstreamer-receive/main.go#L194-L195

Added lines #L194 - L195 were not covered by tests
}
}
Loading

0 comments on commit b5b268a

Please sign in to comment.