mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 15:02:19 +00:00
Pass webrtc sdp chan in newPeerHandler
This commit is contained in:
parent
f21ad568cc
commit
7d8c16fbb7
@ -46,22 +46,24 @@ func GetNumberConnectedSessions(streamID string) int {
|
|||||||
|
|
||||||
// newPeerHandler is called when server receive a new session description
|
// newPeerHandler is called when server receive a new session description
|
||||||
// this initiates a WebRTC connection and return server description
|
// this initiates a WebRTC connection and return server description
|
||||||
func newPeerHandler(remoteSdp struct {
|
func newPeerHandler(localSdpChan chan webrtc.SessionDescription, remoteSdp struct {
|
||||||
StreamID string
|
StreamID string
|
||||||
RemoteDescription webrtc.SessionDescription
|
RemoteDescription webrtc.SessionDescription
|
||||||
}, cfg *Options) webrtc.SessionDescription {
|
}, cfg *Options) {
|
||||||
// Create media engine using client SDP
|
// Create media engine using client SDP
|
||||||
mediaEngine := webrtc.MediaEngine{}
|
mediaEngine := webrtc.MediaEngine{}
|
||||||
if err := mediaEngine.PopulateFromSDP(remoteSdp.RemoteDescription); err != nil {
|
if err := mediaEngine.PopulateFromSDP(remoteSdp.RemoteDescription); err != nil {
|
||||||
log.Println("Failed to create new media engine", err)
|
log.Println("Failed to create new media engine", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new PeerConnection
|
// Create a new PeerConnection
|
||||||
settingsEngine := webrtc.SettingEngine{}
|
settingsEngine := webrtc.SettingEngine{}
|
||||||
if err := settingsEngine.SetEphemeralUDPPortRange(cfg.MinPortUDP, cfg.MaxPortUDP); err != nil {
|
if err := settingsEngine.SetEphemeralUDPPortRange(cfg.MinPortUDP, cfg.MaxPortUDP); err != nil {
|
||||||
log.Println("Failed to set min/max UDP ports", err)
|
log.Println("Failed to set min/max UDP ports", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
api := webrtc.NewAPI(
|
api := webrtc.NewAPI(
|
||||||
webrtc.WithMediaEngine(mediaEngine),
|
webrtc.WithMediaEngine(mediaEngine),
|
||||||
@ -72,7 +74,8 @@ func newPeerHandler(remoteSdp struct {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to initiate peer connection", err)
|
log.Println("Failed to initiate peer connection", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create video track
|
// Create video track
|
||||||
@ -80,11 +83,13 @@ func newPeerHandler(remoteSdp struct {
|
|||||||
videoTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "video", "pion", codec)
|
videoTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "video", "pion", codec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create new video track", err)
|
log.Println("Failed to create new video track", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if _, err = peerConnection.AddTrack(videoTrack); err != nil {
|
if _, err = peerConnection.AddTrack(videoTrack); err != nil {
|
||||||
log.Println("Failed to add video track", err)
|
log.Println("Failed to add video track", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create audio track
|
// Create audio track
|
||||||
@ -92,33 +97,20 @@ func newPeerHandler(remoteSdp struct {
|
|||||||
audioTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "audio", "pion", codec)
|
audioTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "audio", "pion", codec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create new audio track", err)
|
log.Println("Failed to create new audio track", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if _, err = peerConnection.AddTrack(audioTrack); err != nil {
|
if _, err = peerConnection.AddTrack(audioTrack); err != nil {
|
||||||
log.Println("Failed to add audio track", err)
|
log.Println("Failed to add audio track", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the remote SessionDescription
|
// Set the remote SessionDescription
|
||||||
if err = peerConnection.SetRemoteDescription(remoteSdp.RemoteDescription); err != nil {
|
if err = peerConnection.SetRemoteDescription(remoteSdp.RemoteDescription); err != nil {
|
||||||
log.Println("Failed to set remote description", err)
|
log.Println("Failed to set remote description", err)
|
||||||
return webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
}
|
return
|
||||||
|
|
||||||
// Create answer
|
|
||||||
answer, err := peerConnection.CreateAnswer(nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to create answer", err)
|
|
||||||
return webrtc.SessionDescription{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create channel that is blocked until ICE Gathering is complete
|
|
||||||
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
|
|
||||||
|
|
||||||
// Sets the LocalDescription, and starts our UDP listeners
|
|
||||||
if err = peerConnection.SetLocalDescription(answer); err != nil {
|
|
||||||
log.Println("Failed to set local description", err)
|
|
||||||
return webrtc.SessionDescription{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
streamID := remoteSdp.StreamID
|
streamID := remoteSdp.StreamID
|
||||||
@ -154,13 +146,23 @@ func newPeerHandler(remoteSdp struct {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Block until ICE Gathering is complete, disabling trickle ICE
|
// Create answer
|
||||||
// we do this because we only can exchange one signaling message
|
answer, err := peerConnection.CreateAnswer(nil)
|
||||||
// in a production application you should exchange ICE Candidates via OnICECandidate
|
if err != nil {
|
||||||
<-gatherComplete
|
log.Println("Failed to create answer", err)
|
||||||
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Output the local description and send it to browser
|
// Sets the LocalDescription, and starts our UDP listeners
|
||||||
return *peerConnection.LocalDescription()
|
if err = peerConnection.SetLocalDescription(answer); err != nil {
|
||||||
|
log.Println("Failed to set local description", err)
|
||||||
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send answer to client
|
||||||
|
localSdpChan <- answer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for Codec PayloadType
|
// Search for Codec PayloadType
|
||||||
@ -198,6 +200,6 @@ func Serve(remoteSdpChan chan struct {
|
|||||||
for {
|
for {
|
||||||
// Wait for incoming session description
|
// Wait for incoming session description
|
||||||
// then send the local description to browser
|
// then send the local description to browser
|
||||||
localSdpChan <- newPeerHandler(<-remoteSdpChan, cfg)
|
newPeerHandler(localSdpChan, <-remoteSdpChan, cfg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user