Remove examples/internal

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
This commit is contained in:
Sean DuBois
2024-05-19 00:08:36 -04:00
committed by Sean DuBois
parent aa9c623ae1
commit 09461d55a6
24 changed files with 1065 additions and 252 deletions

View File

@@ -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() {
@@ -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)
}
@@ -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)
}
@@ -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)
}
}