From bc38e036da8ce388527da687891e788991e6356e Mon Sep 17 00:00:00 2001 From: Craig Date: Fri, 14 Feb 2020 03:51:41 +0000 Subject: [PATCH] clean up track creation --- server/internal/webrtc/tracks.go | 59 ++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/server/internal/webrtc/tracks.go b/server/internal/webrtc/tracks.go index 2ee3d014..7da358e1 100644 --- a/server/internal/webrtc/tracks.go +++ b/server/internal/webrtc/tracks.go @@ -5,36 +5,45 @@ import ( "math/rand" "github.com/pion/webrtc/v2" + "n.eko.moe/neko/internal/gst" ) -func (m *WebRTCManager) createVideoTrack() (*webrtc.Track, error) { +func (m *WebRTCManager) createTrack(codecName string, pipelineDevice string, pipelineSrc string) (*gst.Pipeline, *webrtc.Track, error) { + pipeline, err := gst.CreatePipeline( + codecName, + pipelineDevice, + pipelineSrc, + ) + + if err != nil { + return nil, nil, err + } + var codec *webrtc.RTPCodec - for _, videoCodec := range m.engine.GetCodecsByKind(webrtc.RTPCodecTypeVideo) { - if videoCodec.Name == m.videoPipeline.CodecName { - codec = videoCodec - break - } + switch codecName { + case webrtc.VP8: + codec = webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000) + case webrtc.VP9: + codec = webrtc.NewRTPVP9Codec(webrtc.DefaultPayloadTypeVP9, 90000) + case webrtc.H264: + codec = webrtc.NewRTPH264Codec(webrtc.DefaultPayloadTypeH264, 90000) + case webrtc.Opus: + codec = webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000) + case webrtc.G722: + codec = webrtc.NewRTPG722Codec(webrtc.DefaultPayloadTypeG722, 8000) + case webrtc.PCMU: + codec = webrtc.NewRTPPCMUCodec(webrtc.DefaultPayloadTypePCMU, 8000) + case webrtc.PCMA: + codec = webrtc.NewRTPPCMACodec(webrtc.DefaultPayloadTypePCMA, 8000) + default: + return nil, nil, fmt.Errorf("unknown codec %s", codecName) } - if codec == nil || codec.PayloadType == 0 { - return nil, fmt.Errorf("remote peer does not support video codec %s", m.videoPipeline.CodecName) + m.engine.RegisterCodec(codec) + track, err := webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec) + if err != nil { + return nil, nil, err } - return webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec) -} - -func (m *WebRTCManager) createAudioTrack() (*webrtc.Track, error) { - var codec *webrtc.RTPCodec - for _, videoCodec := range m.engine.GetCodecsByKind(webrtc.RTPCodecTypeAudio) { - if videoCodec.Name == m.audioPipeline.CodecName { - codec = videoCodec - break - } - } - - if codec == nil || codec.PayloadType == 0 { - return nil, fmt.Errorf("remote peer does not support audio codec %s", m.audioPipeline.CodecName) - } - - return webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec) + return pipeline, track, nil }