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 #1981
  • Loading branch information
Sean-Der committed May 20, 2024
1 parent 480be18 commit f65d434
Show file tree
Hide file tree
Showing 12 changed files with 479 additions and 200 deletions.
65 changes: 57 additions & 8 deletions examples/data-channels-detach/jsfiddle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
package main

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

"github.com/pion/randutil"
"github.com/pion/webrtc/v4"

"github.com/pion/webrtc/v4/examples/internal/signal"
)

const messageSize = 15
Expand Down Expand Up @@ -95,7 +100,7 @@ func main() {
})
peerConnection.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate != nil {
encodedDescr := signal.Encode(peerConnection.LocalDescription())
encodedDescr := encode(peerConnection.LocalDescription())
el := getElementByID("localSessionDescription")
el.Set("value", encodedDescr)
}
Expand Down Expand Up @@ -126,7 +131,7 @@ func main() {
}

descr := webrtc.SessionDescription{}
signal.Decode(sd, &descr)
decode(sd, &descr)
if err := peerConnection.SetRemoteDescription(descr); err != nil {
handleError(err)
}
Expand Down Expand Up @@ -155,13 +160,15 @@ func ReadLoop(d io.Reader) {
// WriteLoop shows how to write to the datachannel directly
func WriteLoop(d io.Writer) {
for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(messageSize)
log(fmt.Sprintf("Sending %s \n", message))

_, err := d.Write([]byte(message))
message, err := randutil.GenerateCryptoRandomString(messageSize, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
if err != nil {
handleError(err)
}

log(fmt.Sprintf("Sending %s \n", message))
if _, err := d.Write([]byte(message)); err != nil {
handleError(err)
}
}
}

Expand All @@ -178,3 +185,45 @@ func handleError(err error) {
func getElementByID(id string) js.Value {
return js.Global().Get("document").Call("getElementById", id)
}

// 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)
}
}
61 changes: 55 additions & 6 deletions examples/data-channels-detach/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
package main

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

"github.com/pion/randutil"
"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/examples/internal/signal"
)

const messageSize = 15
Expand Down Expand Up @@ -94,7 +99,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 @@ -123,7 +128,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 All @@ -146,12 +151,56 @@ func ReadLoop(d io.Reader) {
// WriteLoop shows how to write to the datachannel directly
func WriteLoop(d io.Writer) {
for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(messageSize)
message, err := randutil.GenerateCryptoRandomString(messageSize, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
if err != nil {
panic(err)
}

fmt.Printf("Sending %s \n", message)
if _, err := d.Write([]byte(message)); err != nil {
panic(err)
}
}
}

_, err := d.Write([]byte(message))
if err != nil {
// 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)
}
}
54 changes: 51 additions & 3 deletions examples/data-channels/jsfiddle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
package main

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

"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/examples/internal/signal"
)

func main() {
Expand Down Expand Up @@ -63,7 +69,7 @@ func main() {
})
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate != nil {
encodedDescr := signal.Encode(pc.LocalDescription())
encodedDescr := encode(pc.LocalDescription())
el := getElementByID("localSessionDescription")
el.Set("value", encodedDescr)
}
Expand Down Expand Up @@ -94,7 +100,7 @@ func main() {
}

descr := webrtc.SessionDescription{}
signal.Decode(sd, &descr)
decode(sd, &descr)
if err := pc.SetRemoteDescription(descr); err != nil {
handleError(err)
}
Expand Down Expand Up @@ -146,3 +152,45 @@ func handleError(err error) {
func getElementByID(id string) js.Value {
return js.Global().Get("document").Call("getElementById", id)
}

// 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)
}
}
31 changes: 0 additions & 31 deletions examples/internal/signal/http.go

This file was deleted.

19 changes: 0 additions & 19 deletions examples/internal/signal/rand.go

This file was deleted.

Loading

0 comments on commit f65d434

Please sign in to comment.