langhuihui 4 years ago
parent
commit
e6df0a20c0
  1. 11
      go.mod
  2. 24
      go.sum
  3. 215
      main.go

11
go.mod

@ -3,14 +3,9 @@ module github.com/Monibuca/plugin-webrtc/v3
go 1.13
require (
github.com/Monibuca/engine/v3 v3.0.0
github.com/Monibuca/plugin-rtp v1.0.0
github.com/Monibuca/utils/v3 v3.0.0-alpha2
github.com/Monibuca/engine/v3 v3.0.0-alpha1
github.com/Monibuca/utils/v3 v3.0.0-alpha3
github.com/pion/rtcp v1.2.6
github.com/pion/rtp v1.6.2
github.com/pion/webrtc/v3 v3.0.4
github.com/shirou/gopsutil v2.20.8+incompatible // indirect
)
replace github.com/Monibuca/engine/v3 v3.0.0 => ../engine
replace github.com/Monibuca/utils/v3 => ../utils

24
go.sum

@ -1,11 +1,9 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Monibuca/engine/v2 v2.2.0 h1:A4SyWwzVLegd8Oa6LfSW3LpNfBmWq+MHJJLO55gvaYI=
github.com/Monibuca/engine/v2 v2.2.0/go.mod h1:34EYjjV15G6myuHOKaJkO7y5tJ1Arq/NfC9Weacr2mc=
github.com/Monibuca/plugin-rtp v1.0.0 h1:yksNsIIGxoKX8UZirkAUK+mGZ/XoEeS2vqbIqtqXyCg=
github.com/Monibuca/plugin-rtp v1.0.0/go.mod h1:0xkNm23a/BjVnEMz1zXyOqfEjoVmGe3PJqPNF1KyFGc=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/Monibuca/engine/v3 v3.0.0-alpha1 h1:WnUDkvEJUO7DIdt3EOZ3KbG0pOX9WeJKN0b3GwjpVfs=
github.com/Monibuca/engine/v3 v3.0.0-alpha1/go.mod h1:K5FB3wk+iS/nPc+NS4XwObYQV4gtF6klEwDxaVM0BsQ=
github.com/Monibuca/utils/v3 v3.0.0-alpha3 h1:n4Sq7mS1Iz8oBj2BcV4sXgKbZgix0fFLvjAfXYoiXl0=
github.com/Monibuca/utils/v3 v3.0.0-alpha3/go.mod h1:3xYmhQbgAZBHLyIMteUCd1va+1z/xnd72B585mCaT3c=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -17,8 +15,6 @@ github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 h1:Db9StoJ6RZN3YttC0Pm0
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478/go.mod h1:0j1+svBH8ABEIPdUP0AIg4qedsybnXGJBakCEw8cfoo=
github.com/funny/utest v0.0.0-20161029064919-43870a374500 h1:Z0r1CZnoIWFB/Uiwh1BU5FYmuFe6L5NPi6XWQEmsTRg=
github.com/funny/utest v0.0.0-20161029064919-43870a374500/go.mod h1:mUn39tBov9jKnTWV1RlOYoNzxdBFHiSzXWdY1FoNGGg=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
@ -41,14 +37,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 h1:bqDmpDG49ZRnB5PcgP0RXtQvnMSgIF14M7CBd2shtXs=
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/mask-pp/rtp-ps v1.0.0 h1:JFxuJL9N+gD1ldgJlAy3b7rYfY8wAVHi9ODNmdP4+EE=
github.com/mask-pp/rtp-ps v1.0.0/go.mod h1:jCxsZ2G7z/jX+aqFypEWMePnhNrfnUiXUEKm6Xp0vgU=
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
@ -79,8 +69,6 @@ github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA=
github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8=
github.com/pion/rtcp v1.2.6 h1:1zvwBbyd0TeEuuWftrd/4d++m+/kZSeiguxU61LFWpo=
github.com/pion/rtcp v1.2.6/go.mod h1:52rMNPWFsjr39z9B9MhnkqhPLoeHTv1aN63o/42bWE0=
github.com/pion/rtp v1.5.4 h1:PuNg6xqV3brIUihatcKZj1YDUs+M45L0ZbrZWYtkDxY=
github.com/pion/rtp v1.5.4/go.mod h1:bg60AL5GotNOlYZsqycbhDtEV3TkfbpXG0KBiUq29Mg=
github.com/pion/rtp v1.6.2 h1:iGBerLX6JiDjB9NXuaPzHyxHFG9JsIEdgwTC0lp5n/U=
github.com/pion/rtp v1.6.2/go.mod h1:bDb5n+BFZxXx0Ea7E5qe+klMuqiBrP+w8XSjiWtCUko=
github.com/pion/sctp v1.7.10 h1:o3p3/hZB5Cx12RMGyWmItevJtZ6o2cpuxaw6GOS4x+8=
@ -112,10 +100,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw=
github.com/shirou/gopsutil v2.20.1+incompatible h1:oIq9Cq4i84Hk8uQAUOG3eNdI/29hBawGrD5YRl6JRDY=
github.com/shirou/gopsutil v2.20.1+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v2.20.8+incompatible h1:8c7Atn0FAUZJo+f4wYbN0iVpdWniCQk7IYwGtgdh1mY=
github.com/shirou/gopsutil v2.20.8+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=

215
main.go

@ -11,10 +11,10 @@ import (
"time"
"github.com/Monibuca/engine/v3"
. "github.com/Monibuca/plugin-rtp"
"github.com/Monibuca/utils/v3"
"github.com/Monibuca/utils/v3/codec"
"github.com/pion/rtcp"
"github.com/pion/rtp"
. "github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
)
@ -52,9 +52,11 @@ var config struct {
// conn *net.UDPConn
// port int
// }
var playWaitList WaitList
var reg_level = regexp.MustCompile("profile-level-id=(4.+f)")
var (
playWaitList WaitList
reg_level = regexp.MustCompile("profile-level-id=(4.+f)")
api *API
)
type WaitList struct {
m map[string]*WebRTC
@ -84,17 +86,11 @@ func init() {
}
type WebRTC struct {
RTP
engine.Publisher
*PeerConnection
RemoteAddr string
audioTrack *TrackLocalStaticSample
videoTrack *TrackLocalStaticSample
m MediaEngine
s SettingEngine
api *API
payloader codec.H264
// codecs.H264Packet
// *os.File
}
func (rtc *WebRTC) Play(streamPath string) bool {
@ -102,83 +98,53 @@ func (rtc *WebRTC) Play(streamPath string) bool {
sub.ID = rtc.RemoteAddr
sub.Type = "WebRTC"
var lastTimeStampV, lastTiimeStampA uint32
onVideo := func(pack engine.VideoPack){
var s uint32
if lastTimeStampV > 0 {
s = pack.Timestamp - lastTimeStampV
}
lastTimeStampV = pack.Timestamp
if pack.NalType == codec.NALU_IDR_Picture {
rtc.videoTrack.WriteSample(media.Sample{
Data:sub.VideoTracks[0].SPS,
})
rtc.videoTrack.WriteSample(media.Sample{
Data:sub.VideoTracks[0].PPS,
})
}
onVideo := func(pack engine.VideoPack) {
var s uint32
if lastTimeStampV > 0 {
s = pack.Timestamp - lastTimeStampV
}
lastTimeStampV = pack.Timestamp
if pack.NalType == codec.NALU_IDR_Picture {
rtc.videoTrack.WriteSample(media.Sample{
Data: sub.VideoTracks[0].SPS,
})
rtc.videoTrack.WriteSample(media.Sample{
Data: sub.VideoTracks[0].PPS,
})
}
rtc.videoTrack.WriteSample(media.Sample{
Data:pack.Payload,
Duration:time.Millisecond*time.Duration(s),
Data: pack.Payload,
Duration: time.Millisecond * time.Duration(s),
})
}
onAudio := func(pack engine.AudioPack){
onAudio := func(pack engine.AudioPack) {
var s uint32
if lastTiimeStampA > 0 {
s = pack.Timestamp - lastTiimeStampA
}
lastTiimeStampA = pack.Timestamp
if lastTiimeStampA > 0 {
s = pack.Timestamp - lastTiimeStampA
}
lastTiimeStampA = pack.Timestamp
rtc.audioTrack.WriteSample(media.Sample{
Data:pack.Payload,Duration: time.Millisecond*time.Duration(s),
Data: pack.Payload, Duration: time.Millisecond * time.Duration(s),
})
}
// sub.OnData = func(packet *codec.SendPacket) error {
// if packet.Type == codec.FLV_TAG_TYPE_AUDIO {
// var s uint32
// if lastTiimeStampA > 0 {
// s = packet.Timestamp - lastTiimeStampA
// }
// lastTiimeStampA = packet.Timestamp
// rtc.audioTrack.WriteSample(media.Sample{
// Data: packet.Payload[1:],
// Samples: s * 8,
// })
// return nil
// }
// if packet.IsSequence {
// rtc.payloader.PPS = sub.PPS
// rtc.payloader.SPS = sub.SPS
// } else {
// var s uint32
// if lastTimeStampV > 0 {
// s = packet.Timestamp - lastTimeStampV
// }
// lastTimeStampV = packet.Timestamp
// rtc.videoTrack.WriteSample(media.Sample{
// Data: packet.Payload,
// Samples: s * 90,
// })
// }
// return nil
// }
// go sub.Subscribe(streamPath)
rtc.OnICEConnectionStateChange(func(connectionState ICEConnectionState) {
utils.Printf("%s Connection State has changed %s ", streamPath, connectionState.String())
switch connectionState {
case ICEConnectionStateDisconnected:
case ICEConnectionStateDisconnected, ICEConnectionStateFailed:
sub.Close()
rtc.Close()
rtc.PeerConnection.Close()
case ICEConnectionStateConnected:
//rtc.videoTrack = rtc.GetSenders()[0].Track()
if err := sub.Subscribe(streamPath);err== nil {
go sub.VideoTracks[0].Play(sub.Context,onVideo)
go sub.AudioTracks[0].Play(sub.Context,onAudio)
if err := sub.Subscribe(streamPath); err == nil {
go sub.VideoTracks[0].Play(sub.Context, onVideo)
if rtc.audioTrack != nil {
go sub.AudioTracks[0].Play(sub.Context, onAudio)
}
}
}
})
return true
}
func (rtc *WebRTC) Publish(streamPath string) bool {
rtc.m.RegisterDefaultCodecs()
// rtc.m.RegisterCodec(NewRTPCodec(RTPCodecTypeVideo,
// H264,
// 90000,
@ -193,19 +159,16 @@ func (rtc *WebRTC) Publish(streamPath string) bool {
// }, RTPCodecTypeVideo);
//m.RegisterCodec(NewRTPPCMUCodec(DefaultPayloadTypePCMU, 8000))
if !strings.HasPrefix(rtc.RemoteAddr, "127.0.0.1") && !strings.HasPrefix(rtc.RemoteAddr, "[::1]") {
rtc.s.SetNAT1To1IPs(config.PublicIP, ICECandidateTypeHost)
}
if config.PortMin > 0 && config.PortMax > 0 {
rtc.s.SetEphemeralUDPPortRange(config.PortMin, config.PortMax)
}
rtc.api = NewAPI(WithMediaEngine(&rtc.m), WithSettingEngine(rtc.s))
peerConnection, err := rtc.api.NewPeerConnection(Configuration{
ICEServers: []ICEServer{
{
URLs: config.ICEServers,
},
},
// if !strings.HasPrefix(rtc.RemoteAddr, "127.0.0.1") && !strings.HasPrefix(rtc.RemoteAddr, "[::1]") {
// rtc.s.SetNAT1To1IPs(config.PublicIP, ICECandidateTypeHost)
// }
peerConnection, err := api.NewPeerConnection(Configuration{
// ICEServers: []ICEServer{
// {
// URLs: config.ICEServers,
// },
// },
})
if err != nil {
utils.Println(err)
@ -230,7 +193,7 @@ func (rtc *WebRTC) Publish(streamPath string) bool {
}
})
rtc.PeerConnection = peerConnection
if rtc.RTP.Publish(streamPath) {
if rtc.Publish(streamPath) {
//f, _ := os.OpenFile("resource/live/rtc.h264", os.O_TRUNC|os.O_WRONLY, 0666)
rtc.Stream.Type = "WebRTC"
peerConnection.OnTrack(func(track *TrackRemote, receiver *RTPReceiver) {
@ -246,19 +209,23 @@ func (rtc *WebRTC) Publish(streamPath string) bool {
return
}
}()
pack := &RTPPack{
Type: RTPType(track.Kind() - 1),
var etrack engine.Track
if track.Kind() == RTPCodecTypeAudio {
etrack = rtc.AudioTracks[0]
} else {
etrack = rtc.VideoTracks[0]
}
for b := make([]byte, 1460); ; rtc.PushPack(pack) {
i,_, err := track.Read(b)
var pack rtp.Packet
b := make([]byte, 1460)
for {
i, _, err := track.Read(b)
if err != nil {
return
}
if err = pack.Unmarshal(b[:i]); err != nil {
return
}
// rtc.Unmarshal(pack.Payload)
// f.Write(bytes)
etrack.Push(pack.Timestamp, pack.Payload)
}
})
} else {
@ -272,12 +239,13 @@ func (rtc *WebRTC) GetAnswer() ([]byte, error) {
if err != nil {
return nil, err
}
//gatherComplete := webrtc.GatheringCompletePromise(rtc.PeerConnection)
gatherComplete := GatheringCompletePromise(rtc.PeerConnection)
if err := rtc.SetLocalDescription(answer); err != nil {
utils.Println(err)
return nil, err
}
if bytes, err := json.Marshal(answer); err != nil {
<-gatherComplete
if bytes, err := json.Marshal(rtc.LocalDescription()); err != nil {
utils.Println(err)
return bytes, err
} else {
@ -286,6 +254,13 @@ func (rtc *WebRTC) GetAnswer() ([]byte, error) {
}
func run() {
var m MediaEngine
var s SettingEngine
if config.PortMin > 0 && config.PortMax > 0 {
s.SetEphemeralUDPPortRange(config.PortMin, config.PortMax)
}
m.RegisterDefaultCodecs()
api = NewAPI(WithMediaEngine(&m), WithSettingEngine(s))
http.HandleFunc("/webrtc/play", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Credentials", "true")
origin := r.Header["Origin"]
@ -324,36 +299,8 @@ func run() {
if !strings.Contains(offer.SDP, pli) {
pli = reg_level.FindAllStringSubmatch(offer.SDP, -1)[0][1]
}
// rtc.m.RegisterCodec(NewRTPCodec(RTPCodecTypeVideo,
// H264,
// 90000,
// 0,
// "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id="+pli,
// DefaultPayloadTypeH264,
// &rtc.payloader))
rtc.m.RegisterDefaultCodecs()
// rtc.m.RegisterCodec(RTPCodecParameters{
// RTPCodecCapability: RTPCodecCapability{MimeType: "video/h264", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
// PayloadType: 102,
// }, RTPCodecTypeVideo);
// rtc.m.RegisterCodec(NewRTPPCMACodec(DefaultPayloadTypePCMA, 8000))
// if !strings.HasPrefix(r.RemoteAddr, "127.0.0.1") && !strings.HasPrefix(r.RemoteAddr, "[::1]") {
// rtc.s.SetNAT1To1IPs(config.PublicIP, ICECandidateTypeHost)
// }
if config.PortMin > 0 && config.PortMax > 0 {
rtc.s.SetEphemeralUDPPortRange(config.PortMin, config.PortMax)
}
rtc.api = NewAPI(WithMediaEngine(&rtc.m), WithSettingEngine(rtc.s))
if rtc.PeerConnection, err = rtc.api.NewPeerConnection(Configuration{
// ICEServers: []ICEServer{
// {
// URLs: config.ICEServers,
// },
// },
}); err != nil {
if rtc.PeerConnection, err = api.NewPeerConnection(Configuration{}); err != nil {
return
}
rtc.OnICECandidate(func(ice *ICECandidate) {
@ -361,35 +308,11 @@ func run() {
utils.Println(ice.ToJSON().Candidate)
}
})
// if r, err := peerConnection.AddTransceiverFromKind(RTPCodecTypeVideo); err == nil {
// rtc.videoTrack = r.Sender().Track()
// } else {
// Println(err)
// }
rtc.RemoteAddr = r.RemoteAddr
if err = rtc.SetRemoteDescription(offer); err != nil {
return
}
// rtc.m.PopulateFromSDP(offer)
// var vpayloadType uint8 = 0
// for _, videoCodec := range rtc.m.GetCodecsByKind(RTPCodecTypeVideo) {
// if videoCodec.Name == H264 {
// vpayloadType = videoCodec.PayloadType
// videoCodec.Payloader = &rtc.payloader
// Printf("H264 fmtp %v", videoCodec.SDPFmtpLine)
// }
// }
// println(vpayloadType)
// if rtc.videoTrack, err = rtc.Track(DefaultPayloadTypeH264, 8, "video", "monibuca"); err != nil {
// return
// }
// if rtc.audioTrack, err = rtc.Track(DefaultPayloadTypePCMA, 9, "audio", "monibuca"); err != nil {
// return
// }
if rtc.videoTrack,err = NewTrackLocalStaticSample(RTPCodecCapability{MimeType:"video/h264",SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id="+pli},"video","m7s");err!=nil{
if rtc.videoTrack, err = NewTrackLocalStaticSample(RTPCodecCapability{MimeType: "video/h264", SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=" + pli}, "video", "m7s"); err != nil {
return
}
if _, err = rtc.AddTrack(rtc.videoTrack); err != nil {

Loading…
Cancel
Save