Skip to content

Commit 691d54e

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 pion/webrtc#1981
1 parent 96595fe commit 691d54e

File tree

14 files changed

+596
-184
lines changed

14 files changed

+596
-184
lines changed

c-data-channels/webrtc.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
package main
55

66
import (
7+
"bufio"
8+
"encoding/base64"
9+
"encoding/json"
10+
"errors"
711
"fmt"
12+
"io"
13+
"os"
14+
"strings"
815

9-
"github.com/pion/example-webrtc-applications/v3/internal/signal"
1016
"github.com/pion/webrtc/v3"
1117
)
1218

@@ -43,7 +49,7 @@ func Run(f func(*webrtc.DataChannel)) {
4349

4450
// Wait for the offer to be pasted
4551
offer := webrtc.SessionDescription{}
46-
signal.Decode(signal.MustReadStdin(), &offer)
52+
decode(readUntilNewline(), &offer)
4753

4854
// Set the remote SessionDescription
4955
err = peerConnection.SetRemoteDescription(offer)
@@ -72,10 +78,52 @@ func Run(f func(*webrtc.DataChannel)) {
7278
<-gatherComplete
7379

7480
// Output the answer in base64 so we can paste it in browser
75-
fmt.Println(signal.Encode(*peerConnection.LocalDescription())) //nolint
81+
fmt.Println(encode(peerConnection.LocalDescription())) //nolint
7682

7783
// Block forever
7884
select {}
7985
}
8086

8187
func main() {}
88+
89+
// Read from stdin until we get a newline
90+
func readUntilNewline() (in string) {
91+
var err error
92+
93+
r := bufio.NewReader(os.Stdin)
94+
for {
95+
in, err = r.ReadString('\n')
96+
if err != nil && !errors.Is(err, io.EOF) {
97+
panic(err)
98+
}
99+
100+
if in = strings.TrimSpace(in); len(in) > 0 {
101+
break
102+
}
103+
}
104+
105+
fmt.Println("")
106+
return
107+
}
108+
109+
// JSON encode + base64 a SessionDescription
110+
func encode(obj *webrtc.SessionDescription) string {
111+
b, err := json.Marshal(obj)
112+
if err != nil {
113+
panic(err)
114+
}
115+
116+
return base64.StdEncoding.EncodeToString(b)
117+
}
118+
119+
// Decode a base64 and unmarshal JSON into a SessionDescription
120+
func decode(in string, obj *webrtc.SessionDescription) {
121+
b, err := base64.StdEncoding.DecodeString(in)
122+
if err != nil {
123+
panic(err)
124+
}
125+
126+
if err = json.Unmarshal(b, obj); err != nil {
127+
panic(err)
128+
}
129+
}

ffmpeg-send/main.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
package main
99

1010
import (
11+
"bufio"
12+
"encoding/base64"
13+
"encoding/json"
1114
"errors"
1215
"fmt"
16+
"io"
17+
"os"
18+
"strings"
1319
"time"
1420

1521
"github.com/asticode/go-astiav"
16-
"github.com/pion/example-webrtc-applications/v3/internal/signal"
1722
"github.com/pion/webrtc/v3"
1823
"github.com/pion/webrtc/v3/pkg/media"
1924
)
@@ -48,7 +53,7 @@ func main() {
4853

4954
// Wait for the offer to be pasted
5055
offer := webrtc.SessionDescription{}
51-
signal.Decode(signal.MustReadStdin(), &offer)
56+
decode(readUntilNewline(), &offer)
5257

5358
// Set the remote SessionDescription
5459
err = peerConnection.SetRemoteDescription(offer)
@@ -74,7 +79,7 @@ func main() {
7479
<-gatherComplete
7580

7681
// Output the answer in base64 so we can paste it in browser
77-
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
82+
fmt.Println(encode(peerConnection.LocalDescription()))
7883

7984
// Start pushing buffers on these tracks
8085
writeH264ToTrack(videoTrack)
@@ -264,3 +269,45 @@ func freeVideoCoding() {
264269
encodeCodecContext.Free()
265270
encodePacket.Free()
266271
}
272+
273+
// Read from stdin until we get a newline
274+
func readUntilNewline() (in string) {
275+
var err error
276+
277+
r := bufio.NewReader(os.Stdin)
278+
for {
279+
in, err = r.ReadString('\n')
280+
if err != nil && !errors.Is(err, io.EOF) {
281+
panic(err)
282+
}
283+
284+
if in = strings.TrimSpace(in); len(in) > 0 {
285+
break
286+
}
287+
}
288+
289+
fmt.Println("")
290+
return
291+
}
292+
293+
// JSON encode + base64 a SessionDescription
294+
func encode(obj *webrtc.SessionDescription) string {
295+
b, err := json.Marshal(obj)
296+
if err != nil {
297+
panic(err)
298+
}
299+
300+
return base64.StdEncoding.EncodeToString(b)
301+
}
302+
303+
// Decode a base64 and unmarshal JSON into a SessionDescription
304+
func decode(in string, obj *webrtc.SessionDescription) {
305+
b, err := base64.StdEncoding.DecodeString(in)
306+
if err != nil {
307+
panic(err)
308+
}
309+
310+
if err = json.Unmarshal(b, obj); err != nil {
311+
panic(err)
312+
}
313+
}

gocv-receive/main.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ package main
88

99
import (
1010
"bufio"
11+
"encoding/base64"
12+
"encoding/json"
13+
"errors"
1114
"fmt"
1215
"image"
1316
"image/color"
1417
"io"
18+
"os"
1519
"os/exec"
1620
"runtime"
1721
"strconv"
22+
"strings"
1823
"time"
1924

20-
"github.com/pion/example-webrtc-applications/v3/internal/signal"
2125
"github.com/pion/rtcp"
2226
"github.com/pion/webrtc/v3"
2327
"github.com/pion/webrtc/v3/pkg/media/ivfwriter"
@@ -187,7 +191,7 @@ func createWebRTCConn(ffmpegIn io.Writer) {
187191

188192
// Wait for the offer to be pasted
189193
offer := webrtc.SessionDescription{}
190-
signal.Decode(signal.MustReadStdin(), &offer)
194+
decode(readUntilNewline(), &offer)
191195

192196
// Set the remote SessionDescription
193197
err = peerConnection.SetRemoteDescription(offer)
@@ -216,5 +220,47 @@ func createWebRTCConn(ffmpegIn io.Writer) {
216220
<-gatherComplete
217221

218222
// Output the answer in base64 so we can paste it in browser
219-
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
223+
fmt.Println(encode(peerConnection.LocalDescription()))
224+
}
225+
226+
// Read from stdin until we get a newline
227+
func readUntilNewline() (in string) {
228+
var err error
229+
230+
r := bufio.NewReader(os.Stdin)
231+
for {
232+
in, err = r.ReadString('\n')
233+
if err != nil && !errors.Is(err, io.EOF) {
234+
panic(err)
235+
}
236+
237+
if in = strings.TrimSpace(in); len(in) > 0 {
238+
break
239+
}
240+
}
241+
242+
fmt.Println("")
243+
return
244+
}
245+
246+
// JSON encode + base64 a SessionDescription
247+
func encode(obj *webrtc.SessionDescription) string {
248+
b, err := json.Marshal(obj)
249+
if err != nil {
250+
panic(err)
251+
}
252+
253+
return base64.StdEncoding.EncodeToString(b)
254+
}
255+
256+
// Decode a base64 and unmarshal JSON into a SessionDescription
257+
func decode(in string, obj *webrtc.SessionDescription) {
258+
b, err := base64.StdEncoding.DecodeString(in)
259+
if err != nil {
260+
panic(err)
261+
}
262+
263+
if err = json.Unmarshal(b, obj); err != nil {
264+
panic(err)
265+
}
220266
}

gstreamer-receive/main.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
package main
99

1010
import (
11+
"bufio"
12+
"encoding/base64"
13+
"encoding/json"
14+
"errors"
1115
"fmt"
16+
"io"
17+
"os"
1218
"strings"
1319
"time"
1420

1521
"github.com/go-gst/go-gst/gst"
1622
"github.com/go-gst/go-gst/gst/app"
17-
"github.com/pion/example-webrtc-applications/v3/internal/signal"
1823
"github.com/pion/rtcp"
1924
"github.com/pion/webrtc/v3"
2025
)
@@ -79,7 +84,7 @@ func main() {
7984

8085
// Wait for the offer to be pasted
8186
offer := webrtc.SessionDescription{}
82-
signal.Decode(signal.MustReadStdin(), &offer)
87+
decode(readUntilNewline(), &offer)
8388

8489
// Set the remote SessionDescription
8590
err = peerConnection.SetRemoteDescription(offer)
@@ -108,7 +113,7 @@ func main() {
108113
<-gatherComplete
109114

110115
// Output the answer in base64 so we can paste it in browser
111-
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
116+
fmt.Println(encode(peerConnection.LocalDescription()))
112117

113118
// Block forever
114119
select {}
@@ -148,3 +153,45 @@ func pipelineForCodec(track *webrtc.TrackRemote, codecName string) *app.Source {
148153

149154
return app.SrcFromElement(appSrc)
150155
}
156+
157+
// Read from stdin until we get a newline
158+
func readUntilNewline() (in string) {
159+
var err error
160+
161+
r := bufio.NewReader(os.Stdin)
162+
for {
163+
in, err = r.ReadString('\n')
164+
if err != nil && !errors.Is(err, io.EOF) {
165+
panic(err)
166+
}
167+
168+
if in = strings.TrimSpace(in); len(in) > 0 {
169+
break
170+
}
171+
}
172+
173+
fmt.Println("")
174+
return
175+
}
176+
177+
// JSON encode + base64 a SessionDescription
178+
func encode(obj *webrtc.SessionDescription) string {
179+
b, err := json.Marshal(obj)
180+
if err != nil {
181+
panic(err)
182+
}
183+
184+
return base64.StdEncoding.EncodeToString(b)
185+
}
186+
187+
// Decode a base64 and unmarshal JSON into a SessionDescription
188+
func decode(in string, obj *webrtc.SessionDescription) {
189+
b, err := base64.StdEncoding.DecodeString(in)
190+
if err != nil {
191+
panic(err)
192+
}
193+
194+
if err = json.Unmarshal(b, obj); err != nil {
195+
panic(err)
196+
}
197+
}

0 commit comments

Comments
 (0)