-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
121 lines (103 loc) · 3.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"github.com/pion/mediadevices"
"github.com/pion/mediadevices/pkg/codec/opus"
"github.com/pion/mediadevices/pkg/codec/vpx"
"github.com/pion/mediadevices/pkg/codec/x264"
_ "github.com/pion/mediadevices/pkg/driver/screen" // This is required to register screen adapter
"github.com/pion/mediadevices/pkg/prop"
//_ "github.com/pion/mediadevices/pkg/driver/camera"
//_ "github.com/pion/mediadevices/pkg/driver/microphone"
_ "github.com/pion/mediadevices/pkg/driver/audiotest"
_ "github.com/pion/mediadevices/pkg/driver/videotest"
"github.com/pion/webrtc/v3"
)
func main() {
video := flag.String("v", "screen", "input video device, can be \"screen\" or a named pipe")
audio := flag.String("a", "", "input audio device, can be a named pipe")
videoBitrate := flag.Int("b", 1_000_000, "video bitrate in bits per second")
iceServer := flag.String("i", "stun:stun.l.google.com:19302", "ice server")
token := flag.String("t", "", "publishing token")
videoCodec := flag.String("vc", "vp8", "video codec vp8|h264")
flag.Parse()
if len(flag.Args()) != 1 {
log.Fatal("Invalid number of arguments, pass the publishing url as the first argument")
}
mediaEngine := webrtc.MediaEngine{}
whip := NewWHIPClient(flag.Args()[0], *token)
// configure codec specific parameters
vpxParams, err := vpx.NewVP8Params()
if err != nil {
panic(err)
}
vpxParams.BitRate = *videoBitrate
opusParams, err := opus.NewParams()
if err != nil {
panic(err)
}
x264Params, err := x264.NewParams()
if err != nil {
panic(err)
}
x264Params.BitRate = *videoBitrate
x264Params.Preset = x264.PresetUltrafast
var videoCodecSelector mediadevices.CodecSelectorOption
if *videoCodec == "vp8" {
videoCodecSelector = mediadevices.WithVideoEncoders(&vpxParams)
} else {
videoCodecSelector = mediadevices.WithVideoEncoders(&x264Params)
}
var stream mediadevices.MediaStream
if *video == "screen" {
codecSelector := mediadevices.NewCodecSelector(videoCodecSelector)
codecSelector.Populate(&mediaEngine)
stream, err = mediadevices.GetDisplayMedia(mediadevices.MediaStreamConstraints{
Video: func(constraint *mediadevices.MediaTrackConstraints) {},
Codec: codecSelector,
})
if err != nil {
log.Fatal("Unexpected error capturing screen. ", err)
}
} else if *video == "test" {
codecSelector := mediadevices.NewCodecSelector(
videoCodecSelector,
mediadevices.WithAudioEncoders(&opusParams),
)
codecSelector.Populate(&mediaEngine)
stream, err = mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{
Video: func(constraint *mediadevices.MediaTrackConstraints) {
constraint.Width = prop.Int(640)
constraint.Height = prop.Int(480)
},
Audio: func(constraint *mediadevices.MediaTrackConstraints) {},
Codec: codecSelector,
})
if err != nil {
log.Fatal("Unexpected error capturing test source. ", err)
}
} else {
codecSelector := NewCodecSelector(
WithVideoEncoders(&vpxParams),
WithAudioEncoders(&opusParams),
)
codecSelector.Populate(&mediaEngine)
stream, err = GetInputMediaStream(*audio, *video, codecSelector)
if err != nil {
log.Fatal("Unexpected error capturing input pipe. ", err)
}
}
iceServers := []webrtc.ICEServer{
{
URLs: []string{*iceServer},
},
}
whip.Publish(stream, mediaEngine, iceServers, true)
fmt.Println("Press 'Enter' to finish...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
whip.Close(true)
}