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 4adaa28
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)

// 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 failure on line 81 in c-data-channels/webrtc.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use *peerConnection.LocalDescription() (variable of type webrtc.SessionDescription) as *webrtc.SessionDescription value in argument to encode (typecheck)

Check failure on line 81 in c-data-channels/webrtc.go

View workflow job for this annotation

GitHub Actions / test (1.23) / Go 1.23

cannot use *peerConnection.LocalDescription() (variable of type webrtc.SessionDescription) as *webrtc.SessionDescription value in argument to encode

Check failure on line 81 in c-data-channels/webrtc.go

View workflow job for this annotation

GitHub Actions / test (1.22) / Go 1.22

cannot use *peerConnection.LocalDescription() (variable of type webrtc.SessionDescription) as *webrtc.SessionDescription value in argument to encode

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

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

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use *peerConnection.LocalDescription() (variable of type webrtc.SessionDescription) as *webrtc.SessionDescription value in argument to encode (typecheck)

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

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

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use *peerConnection.LocalDescription() (variable of type webrtc.SessionDescription) as *webrtc.SessionDescription value in argument to encode (typecheck)

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

Please sign in to comment.