mirror of
https://github.com/Monibuca/plugin-webrtc.git
synced 2025-10-05 06:46:57 +08:00
多路音视频格式的选择机制
This commit is contained in:
149
subscriber.go
149
subscriber.go
@@ -21,9 +21,11 @@ type trackSender struct {
|
|||||||
type WebRTCSubscriber struct {
|
type WebRTCSubscriber struct {
|
||||||
Subscriber
|
Subscriber
|
||||||
WebRTCIO
|
WebRTCIO
|
||||||
audio trackSender
|
audio trackSender
|
||||||
video trackSender
|
video trackSender
|
||||||
DC *DataChannel
|
DC *DataChannel
|
||||||
|
videoTracks []*track.Video
|
||||||
|
audioTracks []*track.Audio
|
||||||
// flvHeadCache []byte
|
// flvHeadCache []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,37 +64,39 @@ func (suber *WebRTCSubscriber) createDataChannel() {
|
|||||||
func (suber *WebRTCSubscriber) OnEvent(event any) {
|
func (suber *WebRTCSubscriber) OnEvent(event any) {
|
||||||
switch v := event.(type) {
|
switch v := event.(type) {
|
||||||
case *track.Video:
|
case *track.Video:
|
||||||
switch v.CodecID {
|
suber.videoTracks = append(suber.videoTracks, v)
|
||||||
case codec.CodecID_H264:
|
// switch v.CodecID {
|
||||||
pli := fmt.Sprintf("%x", v.SPS[1:4])
|
// case codec.CodecID_H264:
|
||||||
// pli := "42001f"
|
// pli := fmt.Sprintf("%x", v.SPS[1:4])
|
||||||
if !strings.Contains(suber.SDP, pli) {
|
// // pli := "42001f"
|
||||||
list := reg_level.FindAllStringSubmatch(suber.SDP, -1)
|
// if !strings.Contains(suber.SDP, pli) {
|
||||||
if len(list) > 0 {
|
// list := reg_level.FindAllStringSubmatch(suber.SDP, -1)
|
||||||
pli = list[0][1]
|
// if len(list) > 0 {
|
||||||
}
|
// pli = list[0][1]
|
||||||
}
|
// }
|
||||||
suber.video.TrackLocalStaticRTP, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeH264, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=" + pli}, v.Name, suber.Subscriber.Stream.Path)
|
// }
|
||||||
case codec.CodecID_H265:
|
// suber.video.TrackLocalStaticRTP, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeH264, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=" + pli}, v.Name, suber.Subscriber.Stream.Path)
|
||||||
suber.createDataChannel()
|
// case codec.CodecID_H265:
|
||||||
// suber.videoTrack, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeH265, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=" + pli}, "video", suber.Subscriber.Stream.Path)
|
// suber.createDataChannel()
|
||||||
default:
|
// // suber.videoTrack, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeH265, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=" + pli}, "video", suber.Subscriber.Stream.Path)
|
||||||
return
|
// default:
|
||||||
}
|
// return
|
||||||
suber.Subscriber.AddTrack(v) //接受这个track
|
// }
|
||||||
|
// suber.Subscriber.AddTrack(v) //接受这个track
|
||||||
case *track.Audio:
|
case *track.Audio:
|
||||||
audioMimeType := MimeTypePCMA
|
// audioMimeType := MimeTypePCMA
|
||||||
if v.CodecID == codec.CodecID_PCMU {
|
// if v.CodecID == codec.CodecID_PCMU {
|
||||||
audioMimeType = MimeTypePCMU
|
// audioMimeType = MimeTypePCMU
|
||||||
}
|
// }
|
||||||
switch v.CodecID {
|
// switch v.CodecID {
|
||||||
case codec.CodecID_AAC:
|
// case codec.CodecID_AAC:
|
||||||
suber.createDataChannel()
|
// suber.createDataChannel()
|
||||||
case codec.CodecID_PCMA, codec.CodecID_PCMU:
|
// case codec.CodecID_PCMA, codec.CodecID_PCMU:
|
||||||
suber.audio.TrackLocalStaticRTP, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: audioMimeType}, v.Name, suber.Subscriber.Stream.Path)
|
// suber.audio.TrackLocalStaticRTP, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: audioMimeType}, v.Name, suber.Subscriber.Stream.Path)
|
||||||
//suber.audio.RTPSender, _ = suber.PeerConnection.AddTrack(suber.audio.TrackLocalStaticRTP)
|
// //suber.audio.RTPSender, _ = suber.PeerConnection.AddTrack(suber.audio.TrackLocalStaticRTP)
|
||||||
}
|
// }
|
||||||
suber.Subscriber.AddTrack(v) //接受这个track
|
// suber.Subscriber.AddTrack(v) //接受这个track
|
||||||
|
suber.audioTracks = append(suber.audioTracks, v)
|
||||||
// case VideoDeConf:
|
// case VideoDeConf:
|
||||||
// if suber.DC != nil {
|
// if suber.DC != nil {
|
||||||
// suber.queueDCData(codec.VideoAVCC2FLV(0, v)...)
|
// suber.queueDCData(codec.VideoAVCC2FLV(0, v)...)
|
||||||
@@ -120,32 +124,73 @@ func (suber *WebRTCSubscriber) OnEvent(event any) {
|
|||||||
suber.queueDCData(data...)
|
suber.queueDCData(data...)
|
||||||
}
|
}
|
||||||
case ISubscriber:
|
case ISubscriber:
|
||||||
if suber.DC == nil {
|
vm := make(map[codec.VideoCodecID]*track.Video)
|
||||||
if suber.audio.TrackLocalStaticRTP != nil {
|
am := make(map[codec.AudioCodecID]*track.Audio)
|
||||||
suber.audio.RTPSender, _ = suber.PeerConnection.AddTrack(suber.audio.TrackLocalStaticRTP)
|
for _, track := range suber.videoTracks {
|
||||||
}
|
vm[track.CodecID] = track
|
||||||
if suber.video.TrackLocalStaticRTP != nil {
|
}
|
||||||
suber.video.RTPSender, _ = suber.PeerConnection.AddTrack(suber.video.TrackLocalStaticRTP)
|
for _, track := range suber.audioTracks {
|
||||||
go func() {
|
am[track.CodecID] = track
|
||||||
rtcpBuf := make([]byte, 1500)
|
}
|
||||||
for {
|
if (vm[codec.CodecID_H264] != nil || vm[codec.CodecID_H265] == nil) && (am[codec.CodecID_PCMA] != nil || am[codec.CodecID_PCMU] != nil || am[codec.CodecID_AAC] == nil) {
|
||||||
if n, _, rtcpErr := suber.video.Read(rtcpBuf); rtcpErr != nil {
|
video := vm[codec.CodecID_H264]
|
||||||
|
if video != nil {
|
||||||
|
suber.Subscriber.AddTrack(video)
|
||||||
|
pli := fmt.Sprintf("%x", video.SPS[1:4])
|
||||||
|
// pli := "42001f"
|
||||||
|
if !strings.Contains(suber.SDP, pli) {
|
||||||
|
list := reg_level.FindAllStringSubmatch(suber.SDP, -1)
|
||||||
|
if len(list) > 0 {
|
||||||
|
pli = list[0][1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
suber.video.TrackLocalStaticRTP, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeH264, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=" + pli}, video.Name, suber.Subscriber.Stream.Path)
|
||||||
|
if suber.video.TrackLocalStaticRTP != nil {
|
||||||
|
suber.video.RTPSender, _ = suber.PeerConnection.AddTrack(suber.video.TrackLocalStaticRTP)
|
||||||
|
go func() {
|
||||||
|
rtcpBuf := make([]byte, 1500)
|
||||||
|
for {
|
||||||
|
if n, _, rtcpErr := suber.video.Read(rtcpBuf); rtcpErr != nil {
|
||||||
|
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if p, err := rtcp.Unmarshal(rtcpBuf[:n]); err == nil {
|
if p, err := rtcp.Unmarshal(rtcpBuf[:n]); err == nil {
|
||||||
for _, pp := range p {
|
for _, pp := range p {
|
||||||
switch pp.(type) {
|
switch pp.(type) {
|
||||||
case *rtcp.PictureLossIndication:
|
case *rtcp.PictureLossIndication:
|
||||||
// fmt.Println("PictureLossIndication")
|
// fmt.Println("PictureLossIndication")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
}()
|
}
|
||||||
|
}
|
||||||
|
var audio *track.Audio
|
||||||
|
audioMimeType := MimeTypePCMA
|
||||||
|
if am[codec.CodecID_PCMA] != nil {
|
||||||
|
audio = am[codec.CodecID_PCMA]
|
||||||
|
} else if am[codec.CodecID_PCMU] != nil {
|
||||||
|
audioMimeType = MimeTypePCMU
|
||||||
|
audio = am[codec.CodecID_PCMU]
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
if audio != nil {
|
||||||
|
suber.Subscriber.AddTrack(audio)
|
||||||
|
suber.audio.TrackLocalStaticRTP, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: audioMimeType}, audio.Name, suber.Subscriber.Stream.Path)
|
||||||
|
if suber.audio.TrackLocalStaticRTP != nil {
|
||||||
|
suber.audio.RTPSender, _ = suber.PeerConnection.AddTrack(suber.audio.TrackLocalStaticRTP)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if vm[codec.CodecID_H265] != nil {
|
||||||
|
suber.Subscriber.AddTrack(vm[codec.CodecID_H265])
|
||||||
|
}
|
||||||
|
if am[codec.CodecID_AAC] != nil {
|
||||||
|
suber.Subscriber.AddTrack(am[codec.CodecID_AAC])
|
||||||
|
}
|
||||||
suber.DC.OnOpen(func() {
|
suber.DC.OnOpen(func() {
|
||||||
suber.DC.Send(codec.FLVHeader)
|
suber.DC.Send(codec.FLVHeader)
|
||||||
go suber.PlayFLV()
|
go suber.PlayFLV()
|
||||||
|
Reference in New Issue
Block a user