Skip to content

Commit

Permalink
channel direct from the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
mbattista authored and m1k1o committed Jan 29, 2023
1 parent 5690a84 commit 161d121
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
28 changes: 5 additions & 23 deletions server/internal/capture/streamsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
type StreamSinkManagerCtx struct {
logger zerolog.Logger
mu sync.Mutex
wg sync.WaitGroup

codec codec.RTPCodec
pipeline *gst.Pipeline
Expand All @@ -29,7 +28,6 @@ type StreamSinkManagerCtx struct {
listenersMu sync.Mutex

changeFramerate int16
sampleChannel chan types.Sample
}

func streamSinkNew(codec codec.RTPCodec, pipelineFn func() (string, error), video_id string) *StreamSinkManagerCtx {
Expand All @@ -44,7 +42,6 @@ func streamSinkNew(codec codec.RTPCodec, pipelineFn func() (string, error), vide
pipelineFn: pipelineFn,
changeFramerate: 0,
adaptiveFramerate: false,
sampleChannel: make(chan types.Sample, 100),
}

return manager
Expand All @@ -54,7 +51,6 @@ func (manager *StreamSinkManagerCtx) shutdown() {
manager.logger.Info().Msgf("shutdown")

manager.destroyPipeline()
manager.wg.Wait()
}

func (manager *StreamSinkManagerCtx) Codec() codec.RTPCodec {
Expand Down Expand Up @@ -168,24 +164,6 @@ func (manager *StreamSinkManagerCtx) createPipeline() error {
manager.pipeline.AttachAppsink("appsink" + appsinkSubfix)
manager.pipeline.Play()

manager.wg.Add(1)
pipeline := manager.pipeline

go func() {
manager.logger.Debug().Msg("started emitting samples")
defer manager.wg.Done()

for {
sample, ok := <-pipeline.Sample
if !ok {
manager.logger.Debug().Msg("stopped emitting samples")
return
}

manager.sampleChannel <- sample
}
}()

return nil
}

Expand All @@ -203,7 +181,11 @@ func (manager *StreamSinkManagerCtx) destroyPipeline() {
}

func (manager *StreamSinkManagerCtx) GetSampleChannel() (chan types.Sample) {
return manager.sampleChannel
if manager.pipeline != nil {
return manager.pipeline.Sample
}

return nil
}

func (manager *StreamSinkManagerCtx) SetChangeFramerate(rate int16) {
Expand Down
10 changes: 10 additions & 0 deletions server/internal/webrtc/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (manager *WebRTCManager) Start() {

go func() {
for {
if manager.capture.Audio().GetSampleChannel() == nil {
// Pipeline not yet initialized
time.Sleep(50 * time.Millisecond)
continue
}
newSample := <- manager.capture.Audio().GetSampleChannel()
err := manager.audioTrack.WriteSample(media.Sample(newSample))
if err != nil && errors.Is(err, io.ErrClosedPipe) {
Expand All @@ -77,6 +82,11 @@ func (manager *WebRTCManager) Start() {

go func() {
for {
if manager.capture.Video().GetSampleChannel() == nil {
// Pipeline not yet initialized
time.Sleep(50 * time.Millisecond)
continue
}
newSample := <- manager.capture.Video().GetSampleChannel()
err := manager.videoTrack.WriteSample(media.Sample(newSample))
if err != nil && errors.Is(err, io.ErrClosedPipe) {
Expand Down

0 comments on commit 161d121

Please sign in to comment.