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 10, 2024
1 parent 96595fe commit 121fa2d
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 184 deletions.
54 changes: 51 additions & 3 deletions c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

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

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

fmt.Println("")
return

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

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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/webrtc.go

View check run for this annotation

Codecov / codecov/patch

c-data-channels/webrtc.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)

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

Please sign in to comment.