refactor single video track to an array.

This commit is contained in:
Miroslav Šedivý 2021-02-05 17:40:29 +01:00
parent ebd7e7c065
commit aa7a131da2
3 changed files with 76 additions and 47 deletions

View File

@ -19,7 +19,8 @@ type CaptureManagerCtx struct {
broadcast *BroacastManagerCtx
screencast *ScreencastManagerCtx
audio *StreamManagerCtx
video *StreamManagerCtx
videos map[string]*StreamManagerCtx
videoIDs []string
}
func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx {
@ -87,7 +88,10 @@ func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCt
broadcast: broadcastNew(broadcastPipeline),
screencast: screencastNew(config.Screencast, screencastPipeline),
audio: streamNew(config.AudioCodec, audioPipeline),
video: streamNew(config.VideoCodec, videoPipeline),
videos: map[string]*StreamManagerCtx{
"hq": streamNew(config.VideoCodec, videoPipeline),
},
videoIDs: []string{ "hq" },
}
}
@ -99,8 +103,10 @@ func (manager *CaptureManagerCtx) Start() {
}
manager.desktop.OnBeforeScreenSizeChange(func() {
if manager.video.Started() {
manager.video.destroyPipeline()
for _, video := range manager.videos {
if video.Started() {
video.destroyPipeline()
}
}
if manager.broadcast.Started() {
@ -113,11 +119,13 @@ func (manager *CaptureManagerCtx) Start() {
})
manager.desktop.OnAfterScreenSizeChange(func() {
if manager.video.Started() {
if err := manager.video.createPipeline(); err != nil {
for _, video := range manager.videos {
if video.Started() {
if err := video.createPipeline(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to recreate video pipeline")
}
}
}
if manager.broadcast.Started() {
if err := manager.broadcast.createPipeline(); err != nil {
@ -140,7 +148,11 @@ func (manager *CaptureManagerCtx) Shutdown() error {
manager.screencast.shutdown()
manager.audio.shutdown()
manager.video.shutdown()
for _, video := range manager.videos {
video.shutdown()
}
return nil
}
@ -156,8 +168,12 @@ func (manager *CaptureManagerCtx) Audio() types.StreamManager {
return manager.audio
}
func (manager *CaptureManagerCtx) Video() types.StreamManager {
return manager.video
func (manager *CaptureManagerCtx) Video(videoID string) types.StreamManager {
return manager.videos[videoID]
}
func (manager *CaptureManagerCtx) VideoIDs() []string {
return manager.videoIDs
}
func (manager *CaptureManagerCtx) StartStream() {
@ -166,14 +182,13 @@ func (manager *CaptureManagerCtx) StartStream() {
manager.logger.Info().Msgf("starting stream pipelines")
var err error
err = manager.Video().Start()
if err != nil {
for _, video := range manager.videos {
if err := video.Start(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to start video pipeline")
}
}
err = manager.Audio().Start()
if err != nil {
if err := manager.audio.Start(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to start audio pipeline")
}
@ -186,8 +201,11 @@ func (manager *CaptureManagerCtx) StopStream() {
manager.logger.Info().Msgf("stopping stream pipelines")
manager.Video().Stop()
manager.Audio().Stop()
for _, video := range manager.videos {
video.Stop()
}
manager.audio.Stop()
manager.streaming = false
}

View File

@ -37,7 +37,8 @@ type CaptureManager interface {
Broadcast() BroadcastManager
Screencast() ScreencastManager
Audio() StreamManager
Video() StreamManager
Video(videoID string) StreamManager
VideoIDs() []string
StartStream()
StopStream()

View File

@ -20,8 +20,7 @@ import (
func New(desktop types.DesktopManager, capture types.CaptureManager, config *config.WebRTC) *WebRTCManagerCtx {
return &WebRTCManagerCtx{
logger: log.With().Str("module", "webrtc").Logger(),
videoCodec: capture.Video().Codec(),
audioCodec: capture.Audio().Codec(),
defaultVideoID: capture.VideoIDs()[0],
desktop: desktop,
capture: capture,
config: config,
@ -30,9 +29,9 @@ func New(desktop types.DesktopManager, capture types.CaptureManager, config *con
type WebRTCManagerCtx struct {
logger zerolog.Logger
videoTrack *webrtc.TrackLocalStaticSample
videoTracks map[string]*webrtc.TrackLocalStaticSample
audioTrack *webrtc.TrackLocalStaticSample
videoCodec codec.RTPCodec
defaultVideoID string
audioCodec codec.RTPCodec
desktop types.DesktopManager
capture types.CaptureManager
@ -43,29 +42,37 @@ func (manager *WebRTCManagerCtx) Start() {
var err error
// create audio track
manager.audioTrack, err = webrtc.NewTrackLocalStaticSample(manager.audioCodec.Capability, "audio", "stream")
audio := manager.capture.Audio()
manager.audioTrack, err = webrtc.NewTrackLocalStaticSample(audio.Codec().Capability, "audio", "stream")
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create audio track")
}
manager.capture.Audio().OnSample(func(sample types.Sample) {
audio.OnSample(func(sample types.Sample) {
if err := manager.audioTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
manager.logger.Warn().Err(err).Msg("audio pipeline failed to write")
}
})
// create video track
manager.videoTrack, err = webrtc.NewTrackLocalStaticSample(manager.videoCodec.Capability, "video", "stream")
videoIDs := manager.capture.VideoIDs()
manager.videoTracks = map[string]*webrtc.TrackLocalStaticSample{}
for _, videoID := range videoIDs {
video := manager.capture.Video(videoID)
track, err := webrtc.NewTrackLocalStaticSample(video.Codec().Capability, "video", "stream")
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create video track")
manager.logger.Panic().Err(err).Msgf("unable to create video (%s) track", videoID)
}
manager.capture.Video().OnSample(func(sample types.Sample) {
if err := manager.videoTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
manager.logger.Warn().Err(err).Msg("video pipeline failed to write")
video.OnSample(func(sample types.Sample) {
if err := track.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
manager.logger.Warn().Err(err).Msgf("video (%s) pipeline failed to write", videoID)
}
})
manager.videoTracks[videoID] = track
}
manager.logger.Info().
Str("ice_lite", fmt.Sprintf("%t", manager.config.ICELite)).
Str("ice_trickle", fmt.Sprintf("%t", manager.config.ICETrickle)).
@ -137,7 +144,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess
return nil, err
}
videoTransceiver, err := connection.AddTransceiverFromTrack(manager.videoTrack, webrtc.RtpTransceiverInit{
videoTransceiver, err := connection.AddTransceiverFromTrack(manager.videoTracks[manager.defaultVideoID], webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
})
if err != nil {
@ -209,11 +216,14 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess
func (manager *WebRTCManagerCtx) mediaEngine() (*webrtc.MediaEngine, error) {
engine := &webrtc.MediaEngine{}
if err := manager.videoCodec.Register(engine); err != nil {
// all videos must have the same codec
videoCodec := manager.capture.Video(manager.defaultVideoID).Codec()
if err := videoCodec.Register(engine); err != nil {
return nil, err
}
if err := manager.audioCodec.Register(engine); err != nil {
audioCodec := manager.capture.Audio().Codec()
if err := audioCodec.Register(engine); err != nil {
return nil, err
}