You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.5 KiB

4 years ago
package webrtc
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
. "github.com/Monibuca/engine/v2"
4 years ago
. "github.com/Monibuca/plugin-rtp"
4 years ago
"github.com/pion/rtcp"
. "github.com/pion/webrtc/v2"
)
var config = &struct {
ICEServers []string
}{[]string{
"stun:stun.ekiga.net",
"stun:stun.ideasip.com",
"stun:stun.schlund.de",
"stun:stun.stunprotocol.org:3478",
"stun:stun.voiparound.com",
"stun:stun.voipbuster.com",
"stun:stun.voipstunt.com",
"stun:stun.voxgratia.org",
"stun:stun.services.mozilla.com",
4 years ago
"stun:stun.xten.com",
"stun:stun.softjoys.com",
"stun:stunserver.org",
"stun:stun.schlund.de",
"stun:stun.rixtelecom.se",
"stun:stun.iptel.org",
"stun:stun.ideasip.com",
"stun:stun.fwdnet.net",
"stun:stun.ekiga.net",
"stun:stun01.sipphone.com",
}}
// type udpConn struct {
// conn *net.UDPConn
// port int
// }
4 years ago
var m MediaEngine
var api *API
4 years ago
func init() {
m.RegisterCodec(NewRTPH264Codec(DefaultPayloadTypeH264, 90000))
//m.RegisterCodec(NewRTPPCMUCodec(DefaultPayloadTypePCMU, 8000))
api = NewAPI(WithMediaEngine(m))
4 years ago
InstallPlugin(&PluginConfig{
Config: config,
Name: "WebRTC",
Type: PLUGIN_PUBLISHER | PLUGIN_SUBSCRIBER,
Run: run,
})
}
type WebRTC struct {
4 years ago
RTP
*PeerConnection
}
func (rtc *WebRTC) Publish(streamPath string) bool {
4 years ago
peerConnection, err := api.NewPeerConnection(Configuration{
ICEServers: []ICEServer{
{
URLs: config.ICEServers,
},
},
})
if _, err = peerConnection.AddTransceiverFromKind(RTPCodecTypeVideo); err != nil {
4 years ago
if err != nil {
Println(err)
return false
4 years ago
}
}
if err != nil {
return false
4 years ago
}
peerConnection.OnICEConnectionStateChange(func(connectionState ICEConnectionState) {
4 years ago
Printf("%s Connection State has changed %s ", streamPath, connectionState.String())
switch connectionState {
case ICEConnectionStateDisconnected:
rtc.Stream.Close()
4 years ago
}
})
rtc.PeerConnection = peerConnection
4 years ago
if rtc.RTP.Publish(streamPath) {
rtc.Stream.Type = "WebRTC"
peerConnection.OnTrack(func(track *Track, receiver *RTPReceiver) {
4 years ago
defer rtc.Stream.Close()
go func() {
ticker := time.NewTicker(time.Second * 2)
select {
case <-ticker.C:
if rtcpErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: track.SSRC()}}); rtcpErr != nil {
fmt.Println(rtcpErr)
}
case <-rtc.Done():
return
}
}()
4 years ago
pack := &RTPPack{
Type: RTPType(track.Kind() - 1),
}
4 years ago
for b := make([]byte, 1460); ; rtc.PushPack(pack) {
i, err := track.Read(b)
if err != nil {
return
}
if err = pack.Unmarshal(b[:i]); err != nil {
return
}
}
})
} else {
return false
}
return true
}
func run() {
4 years ago
http.HandleFunc("/webrtc/answer", func(w http.ResponseWriter, r *http.Request) {
streamPath := r.URL.Query().Get("streamPath")
4 years ago
offer := SessionDescription{}
bytes, err := ioutil.ReadAll(r.Body)
err = json.Unmarshal(bytes, &offer)
if err != nil {
Println(err)
return
4 years ago
}
rtc := new(WebRTC)
if rtc.Publish(streamPath) {
// Set the remote SessionDescription
if err = rtc.SetRemoteDescription(offer); err != nil {
Println(err)
return
}
4 years ago
// Create answer
answer, err := rtc.CreateAnswer(nil)
if err != nil {
Println(err)
return
}
4 years ago
// Sets the LocalDescription, and starts our UDP listeners
if err = rtc.SetLocalDescription(answer); err != nil {
Println(err)
return
}
if bytes, err = json.Marshal(answer); err != nil {
Println(err)
return
}
w.Write(bytes)
4 years ago
} else {
w.Write([]byte(`{"errmsg":"bad name"}`))
4 years ago
}
})
}