@@ -53,52 +53,6 @@ func watchHandle(handle *janus.Handle) {
53
53
func main () {
54
54
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
55
55
56
- // Prepare the configuration
57
- config := webrtc.Configuration {
58
- ICEServers : []webrtc.ICEServer {
59
- {
60
- URLs : []string {"stun:stun.l.google.com:19302" },
61
- },
62
- },
63
- SDPSemantics : webrtc .SDPSemanticsUnifiedPlanWithFallback ,
64
- }
65
-
66
- // Create a new RTCPeerConnection
67
- peerConnection , err := webrtc .NewPeerConnection (config )
68
- if err != nil {
69
- panic (err )
70
- }
71
-
72
- // Allow us to receive 1 audio track, and 1 video track
73
- if _ , err = peerConnection .AddTransceiver (webrtc .RTPCodecTypeAudio ); err != nil {
74
- panic (err )
75
- } else if _ , err = peerConnection .AddTransceiver (webrtc .RTPCodecTypeVideo ); err != nil {
76
- panic (err )
77
- }
78
-
79
- peerConnection .OnICEConnectionStateChange (func (connectionState webrtc.ICEConnectionState ) {
80
- fmt .Printf ("Connection State has changed %s \n " , connectionState .String ())
81
- })
82
-
83
- peerConnection .OnTrack (func (track * webrtc.Track , receiver * webrtc.RTPReceiver ) {
84
- codec := track .Codec ()
85
- if codec .Name == webrtc .Opus {
86
- fmt .Println ("Got Opus track, saving to disk as output.ogg" )
87
- i , oggNewErr := oggwriter .New ("output.ogg" , codec .ClockRate , codec .Channels )
88
- if oggNewErr != nil {
89
- panic (oggNewErr )
90
- }
91
- saveToDisk (i , track )
92
- } else if codec .Name == webrtc .VP8 {
93
- fmt .Println ("Got VP8 track, saving to disk as output.ivf" )
94
- i , ivfNewErr := ivfwriter .New ("output.ivf" )
95
- if ivfNewErr != nil {
96
- panic (ivfNewErr )
97
- }
98
- saveToDisk (i , track )
99
- }
100
- })
101
-
102
56
// Janus
103
57
gateway , err := janus .Connect ("ws://localhost:8188/" )
104
58
if err != nil {
@@ -137,14 +91,64 @@ func main() {
137
91
}
138
92
139
93
if msg .Jsep != nil {
140
- err = peerConnection . SetRemoteDescription ( webrtc.SessionDescription {
94
+ offer := webrtc.SessionDescription {
141
95
Type : webrtc .SDPTypeOffer ,
142
96
SDP : msg .Jsep ["sdp" ].(string ),
97
+ }
98
+
99
+ mediaEngine := webrtc.MediaEngine {}
100
+ if err = mediaEngine .PopulateFromSDP (offer ); err != nil {
101
+ panic (err )
102
+ }
103
+
104
+ // Create a new RTCPeerConnection
105
+ var peerConnection * webrtc.PeerConnection
106
+ peerConnection , err = webrtc .NewAPI (webrtc .WithMediaEngine (mediaEngine )).NewPeerConnection (webrtc.Configuration {
107
+ ICEServers : []webrtc.ICEServer {
108
+ {
109
+ URLs : []string {"stun:stun.l.google.com:19302" },
110
+ },
111
+ },
112
+ SDPSemantics : webrtc .SDPSemanticsUnifiedPlanWithFallback ,
143
113
})
144
114
if err != nil {
145
115
panic (err )
146
116
}
147
117
118
+ // Allow us to receive 1 audio track, and 1 video track
119
+ if _ , err = peerConnection .AddTransceiver (webrtc .RTPCodecTypeAudio ); err != nil {
120
+ panic (err )
121
+ } else if _ , err = peerConnection .AddTransceiver (webrtc .RTPCodecTypeVideo ); err != nil {
122
+ panic (err )
123
+ }
124
+
125
+ peerConnection .OnICEConnectionStateChange (func (connectionState webrtc.ICEConnectionState ) {
126
+ fmt .Printf ("Connection State has changed %s \n " , connectionState .String ())
127
+ })
128
+
129
+ peerConnection .OnTrack (func (track * webrtc.Track , receiver * webrtc.RTPReceiver ) {
130
+ codec := track .Codec ()
131
+ if codec .Name == webrtc .Opus {
132
+ fmt .Println ("Got Opus track, saving to disk as output.ogg" )
133
+ i , oggNewErr := oggwriter .New ("output.ogg" , codec .ClockRate , codec .Channels )
134
+ if oggNewErr != nil {
135
+ panic (oggNewErr )
136
+ }
137
+ saveToDisk (i , track )
138
+ } else if codec .Name == webrtc .VP8 {
139
+ fmt .Println ("Got VP8 track, saving to disk as output.ivf" )
140
+ i , ivfNewErr := ivfwriter .New ("output.ivf" )
141
+ if ivfNewErr != nil {
142
+ panic (ivfNewErr )
143
+ }
144
+ saveToDisk (i , track )
145
+ }
146
+ })
147
+
148
+ if err = peerConnection .SetRemoteDescription (offer ); err != nil {
149
+ panic (err )
150
+ }
151
+
148
152
answer , answerErr := peerConnection .CreateAnswer (nil )
149
153
if answerErr != nil {
150
154
panic (answerErr )
0 commit comments