From acf5db094c7bc9f5e379ae2651b0353fe8617839 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Sun, 19 May 2024 00:08:36 -0400 Subject: [PATCH] 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 --- examples/internal/signal/http.go | 31 -------- examples/internal/signal/rand.go | 19 ----- examples/internal/signal/signal.go | 113 ----------------------------- examples/play-from-disk/main.go | 53 +++++++++++++- 4 files changed, 50 insertions(+), 166 deletions(-) delete mode 100644 examples/internal/signal/http.go delete mode 100644 examples/internal/signal/rand.go delete mode 100644 examples/internal/signal/signal.go diff --git a/examples/internal/signal/http.go b/examples/internal/signal/http.go deleted file mode 100644 index d705e6428f2..00000000000 --- a/examples/internal/signal/http.go +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The Pion community -// SPDX-License-Identifier: MIT - -package signal - -import ( - "fmt" - "io" - "net/http" - "strconv" -) - -// HTTPSDPServer starts a HTTP Server that consumes SDPs -func HTTPSDPServer(port int) chan string { - sdpChan := make(chan string) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - body, _ := io.ReadAll(r.Body) - fmt.Fprintf(w, "done") - sdpChan <- string(body) - }) - - go func() { - // nolint: gosec - err := http.ListenAndServe(":"+strconv.Itoa(port), nil) - if err != nil { - panic(err) - } - }() - - return sdpChan -} diff --git a/examples/internal/signal/rand.go b/examples/internal/signal/rand.go deleted file mode 100644 index c55fc6cf54f..00000000000 --- a/examples/internal/signal/rand.go +++ /dev/null @@ -1,19 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The Pion community -// SPDX-License-Identifier: MIT - -package signal - -import "github.com/pion/randutil" - -// RandSeq generates a random string to serve as dummy data -// -// It returns a deterministic sequence of values each time a program is run. -// Use rand.Seed() function in your real applications. -func RandSeq(n int) string { - val, err := randutil.GenerateCryptoRandomString(n, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - if err != nil { - panic(err) - } - - return val -} diff --git a/examples/internal/signal/signal.go b/examples/internal/signal/signal.go deleted file mode 100644 index 14547a33c0d..00000000000 --- a/examples/internal/signal/signal.go +++ /dev/null @@ -1,113 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The Pion community -// SPDX-License-Identifier: MIT - -// Package signal contains helpers to exchange the SDP session -// description between examples. -package signal - -import ( - "bufio" - "bytes" - "compress/gzip" - "encoding/base64" - "encoding/json" - "fmt" - "io" - "os" - "strings" -) - -// Allows compressing offer/answer to bypass terminal input limits. -const compress = false - -// MustReadStdin blocks until input is received from stdin -func MustReadStdin() string { - r := bufio.NewReader(os.Stdin) - - var in string - for { - var err error - in, err = r.ReadString('\n') - if err != io.EOF { - if err != nil { - panic(err) - } - } - in = strings.TrimSpace(in) - if len(in) > 0 { - break - } - } - - fmt.Println("") - - return in -} - -// Encode encodes the input in base64 -// It can optionally zip the input before encoding -func Encode(obj interface{}) string { - b, err := json.Marshal(obj) - if err != nil { - panic(err) - } - - if compress { - b = zip(b) - } - - return base64.StdEncoding.EncodeToString(b) -} - -// Decode decodes the input from base64 -// It can optionally unzip the input after decoding -func Decode(in string, obj interface{}) { - b, err := base64.StdEncoding.DecodeString(in) - if err != nil { - panic(err) - } - - if compress { - b = unzip(b) - } - - err = json.Unmarshal(b, obj) - if err != nil { - panic(err) - } -} - -func zip(in []byte) []byte { - var b bytes.Buffer - gz := gzip.NewWriter(&b) - _, err := gz.Write(in) - if err != nil { - panic(err) - } - err = gz.Flush() - if err != nil { - panic(err) - } - err = gz.Close() - if err != nil { - panic(err) - } - return b.Bytes() -} - -func unzip(in []byte) []byte { - var b bytes.Buffer - _, err := b.Write(in) - if err != nil { - panic(err) - } - r, err := gzip.NewReader(&b) - if err != nil { - panic(err) - } - res, err := io.ReadAll(r) - if err != nil { - panic(err) - } - return res -} diff --git a/examples/play-from-disk/main.go b/examples/play-from-disk/main.go index 13b7f707cfa..e0837e73603 100644 --- a/examples/play-from-disk/main.go +++ b/examples/play-from-disk/main.go @@ -8,15 +8,18 @@ package main import ( + "bufio" "context" + "encoding/base64" + "encoding/json" "errors" "fmt" "io" "os" + "strings" "time" "github.com/pion/webrtc/v4" - "github.com/pion/webrtc/v4/examples/internal/signal" "github.com/pion/webrtc/v4/pkg/media" "github.com/pion/webrtc/v4/pkg/media/ivfreader" "github.com/pion/webrtc/v4/pkg/media/oggreader" @@ -248,7 +251,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 if err = peerConnection.SetRemoteDescription(offer); err != nil { @@ -275,8 +278,52 @@ 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 {} } + +// Read from stdin until we get a newline +func readUntilNewline() string { + var err error + var in string + + 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 in +} + +// 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) + } +}