|
@ -11,10 +11,10 @@ import ( |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
"github.com/Monibuca/engine/v3" |
|
|
"github.com/Monibuca/engine/v3" |
|
|
. "github.com/Monibuca/plugin-rtp" |
|
|
|
|
|
"github.com/Monibuca/utils/v3" |
|
|
"github.com/Monibuca/utils/v3" |
|
|
"github.com/Monibuca/utils/v3/codec" |
|
|
"github.com/Monibuca/utils/v3/codec" |
|
|
"github.com/pion/rtcp" |
|
|
"github.com/pion/rtcp" |
|
|
|
|
|
"github.com/pion/rtp" |
|
|
. "github.com/pion/webrtc/v3" |
|
|
. "github.com/pion/webrtc/v3" |
|
|
"github.com/pion/webrtc/v3/pkg/media" |
|
|
"github.com/pion/webrtc/v3/pkg/media" |
|
|
) |
|
|
) |
|
@ -52,9 +52,11 @@ var config struct { |
|
|
// conn *net.UDPConn
|
|
|
// conn *net.UDPConn
|
|
|
// port int
|
|
|
// port int
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
var ( |
|
|
var playWaitList WaitList |
|
|
playWaitList WaitList |
|
|
var reg_level = regexp.MustCompile("profile-level-id=(4.+f)") |
|
|
reg_level = regexp.MustCompile("profile-level-id=(4.+f)") |
|
|
|
|
|
api *API |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
type WaitList struct { |
|
|
type WaitList struct { |
|
|
m map[string]*WebRTC |
|
|
m map[string]*WebRTC |
|
@ -84,17 +86,11 @@ func init() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type WebRTC struct { |
|
|
type WebRTC struct { |
|
|
RTP |
|
|
engine.Publisher |
|
|
*PeerConnection |
|
|
*PeerConnection |
|
|
RemoteAddr string |
|
|
RemoteAddr string |
|
|
audioTrack *TrackLocalStaticSample |
|
|
audioTrack *TrackLocalStaticSample |
|
|
videoTrack *TrackLocalStaticSample |
|
|
videoTrack *TrackLocalStaticSample |
|
|
m MediaEngine |
|
|
|
|
|
s SettingEngine |
|
|
|
|
|
api *API |
|
|
|
|
|
payloader codec.H264 |
|
|
|
|
|
// codecs.H264Packet
|
|
|
|
|
|
// *os.File
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (rtc *WebRTC) Play(streamPath string) bool { |
|
|
func (rtc *WebRTC) Play(streamPath string) bool { |
|
@ -131,54 +127,24 @@ func (rtc *WebRTC) Play(streamPath string) bool { |
|
|
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) { |
|
|
rtc.OnICEConnectionStateChange(func(connectionState ICEConnectionState) { |
|
|
utils.Printf("%s Connection State has changed %s ", streamPath, connectionState.String()) |
|
|
utils.Printf("%s Connection State has changed %s ", streamPath, connectionState.String()) |
|
|
switch connectionState { |
|
|
switch connectionState { |
|
|
case ICEConnectionStateDisconnected: |
|
|
case ICEConnectionStateDisconnected, ICEConnectionStateFailed: |
|
|
sub.Close() |
|
|
sub.Close() |
|
|
rtc.Close() |
|
|
rtc.PeerConnection.Close() |
|
|
case ICEConnectionStateConnected: |
|
|
case ICEConnectionStateConnected: |
|
|
//rtc.videoTrack = rtc.GetSenders()[0].Track()
|
|
|
|
|
|
if err := sub.Subscribe(streamPath); err == nil { |
|
|
if err := sub.Subscribe(streamPath); err == nil { |
|
|
go sub.VideoTracks[0].Play(sub.Context, onVideo) |
|
|
go sub.VideoTracks[0].Play(sub.Context, onVideo) |
|
|
|
|
|
if rtc.audioTrack != nil { |
|
|
go sub.AudioTracks[0].Play(sub.Context, onAudio) |
|
|
go sub.AudioTracks[0].Play(sub.Context, onAudio) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
}) |
|
|
}) |
|
|
return true |
|
|
return true |
|
|
} |
|
|
} |
|
|
func (rtc *WebRTC) Publish(streamPath string) bool { |
|
|
func (rtc *WebRTC) Publish(streamPath string) bool { |
|
|
rtc.m.RegisterDefaultCodecs() |
|
|
|
|
|
// rtc.m.RegisterCodec(NewRTPCodec(RTPCodecTypeVideo,
|
|
|
// rtc.m.RegisterCodec(NewRTPCodec(RTPCodecTypeVideo,
|
|
|
// H264,
|
|
|
// H264,
|
|
|
// 90000,
|
|
|
// 90000,
|
|
@ -193,19 +159,16 @@ func (rtc *WebRTC) Publish(streamPath string) bool { |
|
|
// }, RTPCodecTypeVideo);
|
|
|
// }, RTPCodecTypeVideo);
|
|
|
|
|
|
|
|
|
//m.RegisterCodec(NewRTPPCMUCodec(DefaultPayloadTypePCMU, 8000))
|
|
|
//m.RegisterCodec(NewRTPPCMUCodec(DefaultPayloadTypePCMU, 8000))
|
|
|
if !strings.HasPrefix(rtc.RemoteAddr, "127.0.0.1") && !strings.HasPrefix(rtc.RemoteAddr, "[::1]") { |
|
|
// if !strings.HasPrefix(rtc.RemoteAddr, "127.0.0.1") && !strings.HasPrefix(rtc.RemoteAddr, "[::1]") {
|
|
|
rtc.s.SetNAT1To1IPs(config.PublicIP, ICECandidateTypeHost) |
|
|
// rtc.s.SetNAT1To1IPs(config.PublicIP, ICECandidateTypeHost)
|
|
|
} |
|
|
// }
|
|
|
if config.PortMin > 0 && config.PortMax > 0 { |
|
|
|
|
|
rtc.s.SetEphemeralUDPPortRange(config.PortMin, config.PortMax) |
|
|
peerConnection, err := api.NewPeerConnection(Configuration{ |
|
|
} |
|
|
// ICEServers: []ICEServer{
|
|
|
rtc.api = NewAPI(WithMediaEngine(&rtc.m), WithSettingEngine(rtc.s)) |
|
|
// {
|
|
|
peerConnection, err := rtc.api.NewPeerConnection(Configuration{ |
|
|
// URLs: config.ICEServers,
|
|
|
ICEServers: []ICEServer{ |
|
|
// },
|
|
|
{ |
|
|
// },
|
|
|
URLs: config.ICEServers, |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
}) |
|
|
}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
utils.Println(err) |
|
|
utils.Println(err) |
|
@ -230,7 +193,7 @@ func (rtc *WebRTC) Publish(streamPath string) bool { |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
rtc.PeerConnection = peerConnection |
|
|
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)
|
|
|
//f, _ := os.OpenFile("resource/live/rtc.h264", os.O_TRUNC|os.O_WRONLY, 0666)
|
|
|
rtc.Stream.Type = "WebRTC" |
|
|
rtc.Stream.Type = "WebRTC" |
|
|
peerConnection.OnTrack(func(track *TrackRemote, receiver *RTPReceiver) { |
|
|
peerConnection.OnTrack(func(track *TrackRemote, receiver *RTPReceiver) { |
|
@ -246,10 +209,15 @@ func (rtc *WebRTC) Publish(streamPath string) bool { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
}() |
|
|
}() |
|
|
pack := &RTPPack{ |
|
|
var etrack engine.Track |
|
|
Type: RTPType(track.Kind() - 1), |
|
|
if track.Kind() == RTPCodecTypeAudio { |
|
|
|
|
|
etrack = rtc.AudioTracks[0] |
|
|
|
|
|
} else { |
|
|
|
|
|
etrack = rtc.VideoTracks[0] |
|
|
} |
|
|
} |
|
|
for b := make([]byte, 1460); ; rtc.PushPack(pack) { |
|
|
var pack rtp.Packet |
|
|
|
|
|
b := make([]byte, 1460) |
|
|
|
|
|
for { |
|
|
i, _, err := track.Read(b) |
|
|
i, _, err := track.Read(b) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return |
|
|
return |
|
@ -257,8 +225,7 @@ func (rtc *WebRTC) Publish(streamPath string) bool { |
|
|
if err = pack.Unmarshal(b[:i]); err != nil { |
|
|
if err = pack.Unmarshal(b[:i]); err != nil { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
// rtc.Unmarshal(pack.Payload)
|
|
|
etrack.Push(pack.Timestamp, pack.Payload) |
|
|
// f.Write(bytes)
|
|
|
|
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
} else { |
|
|
} else { |
|
@ -272,12 +239,13 @@ func (rtc *WebRTC) GetAnswer() ([]byte, error) { |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
//gatherComplete := webrtc.GatheringCompletePromise(rtc.PeerConnection)
|
|
|
gatherComplete := GatheringCompletePromise(rtc.PeerConnection) |
|
|
if err := rtc.SetLocalDescription(answer); err != nil { |
|
|
if err := rtc.SetLocalDescription(answer); err != nil { |
|
|
utils.Println(err) |
|
|
utils.Println(err) |
|
|
return nil, 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) |
|
|
utils.Println(err) |
|
|
return bytes, err |
|
|
return bytes, err |
|
|
} else { |
|
|
} else { |
|
@ -286,6 +254,13 @@ func (rtc *WebRTC) GetAnswer() ([]byte, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func run() { |
|
|
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) { |
|
|
http.HandleFunc("/webrtc/play", func(w http.ResponseWriter, r *http.Request) { |
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true") |
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true") |
|
|
origin := r.Header["Origin"] |
|
|
origin := r.Header["Origin"] |
|
@ -324,36 +299,8 @@ func run() { |
|
|
if !strings.Contains(offer.SDP, pli) { |
|
|
if !strings.Contains(offer.SDP, pli) { |
|
|
pli = reg_level.FindAllStringSubmatch(offer.SDP, -1)[0][1] |
|
|
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{ |
|
|
if rtc.PeerConnection, err = api.NewPeerConnection(Configuration{}); err != nil { |
|
|
// ICEServers: []ICEServer{
|
|
|
|
|
|
// {
|
|
|
|
|
|
// URLs: config.ICEServers,
|
|
|
|
|
|
// },
|
|
|
|
|
|
// },
|
|
|
|
|
|
}); err != nil { |
|
|
|
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
rtc.OnICECandidate(func(ice *ICECandidate) { |
|
|
rtc.OnICECandidate(func(ice *ICECandidate) { |
|
@ -361,34 +308,10 @@ func run() { |
|
|
utils.Println(ice.ToJSON().Candidate) |
|
|
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 |
|
|
rtc.RemoteAddr = r.RemoteAddr |
|
|
if err = rtc.SetRemoteDescription(offer); err != nil { |
|
|
if err = rtc.SetRemoteDescription(offer); err != nil { |
|
|
return |
|
|
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 |
|
|
return |
|
|
} |
|
|
} |
|
|