Skip to content

Commit e0a4d9c

Browse files
committed
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
1 parent aa9c623 commit e0a4d9c

File tree

24 files changed

+1064
-251
lines changed

24 files changed

+1064
-251
lines changed

examples/bandwidth-estimation-from-disk/main.go

+48-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
package main
99

1010
import (
11+
"bufio"
12+
"encoding/base64"
13+
"encoding/json"
1114
"errors"
1215
"fmt"
1316
"io"
1417
"os"
18+
"strings"
1519
"time"
1620

1721
"github.com/pion/interceptor"
1822
"github.com/pion/interceptor/pkg/cc"
1923
"github.com/pion/interceptor/pkg/gcc"
2024
"github.com/pion/webrtc/v4"
21-
"github.com/pion/webrtc/v4/examples/internal/signal"
2225
"github.com/pion/webrtc/v4/pkg/media"
2326
"github.com/pion/webrtc/v4/pkg/media/ivfreader"
2427
)
@@ -144,7 +147,7 @@ func main() {
144147

145148
// Wait for the offer to be pasted
146149
offer := webrtc.SessionDescription{}
147-
signal.Decode(signal.MustReadStdin(), &offer)
150+
decode(readUntilNewline(), &offer)
148151

149152
// Set the remote SessionDescription
150153
if err = peerConnection.SetRemoteDescription(offer); err != nil {
@@ -171,7 +174,7 @@ func main() {
171174
<-gatherComplete
172175

173176
// Output the answer in base64 so we can paste it in browser
174-
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
177+
fmt.Println(encode(peerConnection.LocalDescription()))
175178

176179
// Open a IVF file and start reading using our IVFReader
177180
file, err := os.Open(qualityLevels[currentQuality].fileName)
@@ -254,3 +257,45 @@ func setReaderFile(filename string) func(_ int64) io.Reader {
254257
return file
255258
}
256259
}
260+
261+
// Read from stdin until we get a newline
262+
func readUntilNewline() (in string) {
263+
var err error
264+
265+
r := bufio.NewReader(os.Stdin)
266+
for {
267+
in, err = r.ReadString('\n')
268+
if err != nil && !errors.Is(err, io.EOF) {
269+
panic(err)
270+
}
271+
272+
if in = strings.TrimSpace(in); len(in) > 0 {
273+
break
274+
}
275+
}
276+
277+
fmt.Println("")
278+
return
279+
}
280+
281+
// JSON encode + base64 a SessionDescription
282+
func encode(obj *webrtc.SessionDescription) string {
283+
b, err := json.Marshal(obj)
284+
if err != nil {
285+
panic(err)
286+
}
287+
288+
return base64.StdEncoding.EncodeToString(b)
289+
}
290+
291+
// Decode a base64 and unmarshal JSON into a SessionDescription
292+
func decode(in string, obj *webrtc.SessionDescription) {
293+
b, err := base64.StdEncoding.DecodeString(in)
294+
if err != nil {
295+
panic(err)
296+
}
297+
298+
if err = json.Unmarshal(b, obj); err != nil {
299+
panic(err)
300+
}
301+
}

examples/broadcast/main.go

+48-6
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,29 @@
88
package main
99

1010
import (
11+
"encoding/base64"
12+
"encoding/json"
1113
"errors"
1214
"flag"
1315
"fmt"
1416
"io"
17+
"net/http"
18+
"strconv"
1519

1620
"github.com/pion/interceptor"
1721
"github.com/pion/interceptor/pkg/intervalpli"
1822
"github.com/pion/webrtc/v4"
19-
"github.com/pion/webrtc/v4/examples/internal/signal"
2023
)
2124

2225
func main() { // nolint:gocognit
2326
port := flag.Int("port", 8080, "http server port")
2427
flag.Parse()
2528

26-
sdpChan := signal.HTTPSDPServer(*port)
29+
sdpChan := httpSDPServer(*port)
2730

2831
// Everything below is the Pion WebRTC API, thanks for using it ❤️.
2932
offer := webrtc.SessionDescription{}
30-
signal.Decode(<-sdpChan, &offer)
33+
decode(<-sdpChan, &offer)
3134
fmt.Println("")
3235

3336
peerConnectionConfig := webrtc.Configuration{
@@ -132,15 +135,15 @@ func main() { // nolint:gocognit
132135
<-gatherComplete
133136

134137
// Get the LocalDescription and take it to base64 so we can paste in browser
135-
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
138+
fmt.Println(encode(peerConnection.LocalDescription()))
136139

137140
localTrack := <-localTrackChan
138141
for {
139142
fmt.Println("")
140143
fmt.Println("Curl an base64 SDP to start sendonly peer connection")
141144

142145
recvOnlyOffer := webrtc.SessionDescription{}
143-
signal.Decode(<-sdpChan, &recvOnlyOffer)
146+
decode(<-sdpChan, &recvOnlyOffer)
144147

145148
// Create a new PeerConnection
146149
peerConnection, err := webrtc.NewPeerConnection(peerConnectionConfig)
@@ -192,6 +195,45 @@ func main() { // nolint:gocognit
192195
<-gatherComplete
193196

194197
// Get the LocalDescription and take it to base64 so we can paste in browser
195-
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
198+
fmt.Println(encode(peerConnection.LocalDescription()))
196199
}
197200
}
201+
202+
// JSON encode + base64 a SessionDescription
203+
func encode(obj *webrtc.SessionDescription) string {
204+
b, err := json.Marshal(obj)
205+
if err != nil {
206+
panic(err)
207+
}
208+
209+
return base64.StdEncoding.EncodeToString(b)
210+
}
211+
212+
// Decode a base64 and unmarshal JSON into a SessionDescription
213+
func decode(in string, obj *webrtc.SessionDescription) {
214+
b, err := base64.StdEncoding.DecodeString(in)
215+
if err != nil {
216+
panic(err)
217+
}
218+
219+
if err = json.Unmarshal(b, obj); err != nil {
220+
panic(err)
221+
}
222+
}
223+
224+
// httpSDPServer starts a HTTP Server that consumes SDPs
225+
func httpSDPServer(port int) chan string {
226+
sdpChan := make(chan string)
227+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
228+
body, _ := io.ReadAll(r.Body)
229+
fmt.Fprintf(w, "done")
230+
sdpChan <- string(body)
231+
})
232+
233+
go func() {
234+
// nolint: gosec
235+
panic(http.ListenAndServe(":"+strconv.Itoa(port), nil))
236+
}()
237+
238+
return sdpChan
239+
}

examples/data-channels-detach/jsfiddle/main.go

+57-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
package main
88

99
import (
10+
"bufio"
11+
"encoding/base64"
12+
"encoding/json"
13+
"errors"
1014
"fmt"
1115
"io"
16+
"os"
17+
"strings"
1218
"syscall/js"
1319
"time"
1420

21+
"github.com/pion/randutil"
1522
"github.com/pion/webrtc/v4"
16-
17-
"github.com/pion/webrtc/v4/examples/internal/signal"
1823
)
1924

2025
const messageSize = 15
@@ -95,7 +100,7 @@ func main() {
95100
})
96101
peerConnection.OnICECandidate(func(candidate *webrtc.ICECandidate) {
97102
if candidate != nil {
98-
encodedDescr := signal.Encode(peerConnection.LocalDescription())
103+
encodedDescr := encode(peerConnection.LocalDescription())
99104
el := getElementByID("localSessionDescription")
100105
el.Set("value", encodedDescr)
101106
}
@@ -126,7 +131,7 @@ func main() {
126131
}
127132

128133
descr := webrtc.SessionDescription{}
129-
signal.Decode(sd, &descr)
134+
decode(sd, &descr)
130135
if err := peerConnection.SetRemoteDescription(descr); err != nil {
131136
handleError(err)
132137
}
@@ -155,13 +160,15 @@ func ReadLoop(d io.Reader) {
155160
// WriteLoop shows how to write to the datachannel directly
156161
func WriteLoop(d io.Writer) {
157162
for range time.NewTicker(5 * time.Second).C {
158-
message := signal.RandSeq(messageSize)
159-
log(fmt.Sprintf("Sending %s \n", message))
160-
161-
_, err := d.Write([]byte(message))
163+
message, err := randutil.GenerateCryptoRandomString(messageSize, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
162164
if err != nil {
163165
handleError(err)
164166
}
167+
168+
log(fmt.Sprintf("Sending %s \n", message))
169+
if _, err := d.Write([]byte(message)); err != nil {
170+
handleError(err)
171+
}
165172
}
166173
}
167174

@@ -178,3 +185,45 @@ func handleError(err error) {
178185
func getElementByID(id string) js.Value {
179186
return js.Global().Get("document").Call("getElementById", id)
180187
}
188+
189+
// Read from stdin until we get a newline
190+
func readUntilNewline() (in string) {
191+
var err error
192+
193+
r := bufio.NewReader(os.Stdin)
194+
for {
195+
in, err = r.ReadString('\n')
196+
if err != nil && !errors.Is(err, io.EOF) {
197+
panic(err)
198+
}
199+
200+
if in = strings.TrimSpace(in); len(in) > 0 {
201+
break
202+
}
203+
}
204+
205+
fmt.Println("")
206+
return
207+
}
208+
209+
// JSON encode + base64 a SessionDescription
210+
func encode(obj *webrtc.SessionDescription) string {
211+
b, err := json.Marshal(obj)
212+
if err != nil {
213+
panic(err)
214+
}
215+
216+
return base64.StdEncoding.EncodeToString(b)
217+
}
218+
219+
// Decode a base64 and unmarshal JSON into a SessionDescription
220+
func decode(in string, obj *webrtc.SessionDescription) {
221+
b, err := base64.StdEncoding.DecodeString(in)
222+
if err != nil {
223+
panic(err)
224+
}
225+
226+
if err = json.Unmarshal(b, obj); err != nil {
227+
panic(err)
228+
}
229+
}

examples/data-channels-detach/main.go

+55-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
package main
66

77
import (
8+
"bufio"
9+
"encoding/base64"
10+
"encoding/json"
11+
"errors"
812
"fmt"
913
"io"
1014
"os"
15+
"strings"
1116
"time"
1217

18+
"github.com/pion/randutil"
1319
"github.com/pion/webrtc/v4"
14-
"github.com/pion/webrtc/v4/examples/internal/signal"
1520
)
1621

1722
const messageSize = 15
@@ -94,7 +99,7 @@ func main() {
9499

95100
// Wait for the offer to be pasted
96101
offer := webrtc.SessionDescription{}
97-
signal.Decode(signal.MustReadStdin(), &offer)
102+
decode(readUntilNewline(), &offer)
98103

99104
// Set the remote SessionDescription
100105
err = peerConnection.SetRemoteDescription(offer)
@@ -123,7 +128,7 @@ func main() {
123128
<-gatherComplete
124129

125130
// Output the answer in base64 so we can paste it in browser
126-
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
131+
fmt.Println(encode(peerConnection.LocalDescription()))
127132

128133
// Block forever
129134
select {}
@@ -146,12 +151,56 @@ func ReadLoop(d io.Reader) {
146151
// WriteLoop shows how to write to the datachannel directly
147152
func WriteLoop(d io.Writer) {
148153
for range time.NewTicker(5 * time.Second).C {
149-
message := signal.RandSeq(messageSize)
154+
message, err := randutil.GenerateCryptoRandomString(messageSize, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
155+
if err != nil {
156+
panic(err)
157+
}
158+
150159
fmt.Printf("Sending %s \n", message)
160+
if _, err := d.Write([]byte(message)); err != nil {
161+
panic(err)
162+
}
163+
}
164+
}
151165

152-
_, err := d.Write([]byte(message))
153-
if err != nil {
166+
// Read from stdin until we get a newline
167+
func readUntilNewline() (in string) {
168+
var err error
169+
170+
r := bufio.NewReader(os.Stdin)
171+
for {
172+
in, err = r.ReadString('\n')
173+
if err != nil && !errors.Is(err, io.EOF) {
154174
panic(err)
155175
}
176+
177+
if in = strings.TrimSpace(in); len(in) > 0 {
178+
break
179+
}
180+
}
181+
182+
fmt.Println("")
183+
return
184+
}
185+
186+
// JSON encode + base64 a SessionDescription
187+
func encode(obj *webrtc.SessionDescription) string {
188+
b, err := json.Marshal(obj)
189+
if err != nil {
190+
panic(err)
191+
}
192+
193+
return base64.StdEncoding.EncodeToString(b)
194+
}
195+
196+
// Decode a base64 and unmarshal JSON into a SessionDescription
197+
func decode(in string, obj *webrtc.SessionDescription) {
198+
b, err := base64.StdEncoding.DecodeString(in)
199+
if err != nil {
200+
panic(err)
201+
}
202+
203+
if err = json.Unmarshal(b, obj); err != nil {
204+
panic(err)
156205
}
157206
}

0 commit comments

Comments
 (0)