Skip to content

Commit

Permalink
refactor mediaframe listener
Browse files Browse the repository at this point in the history
Former-commit-id: 02b8b52b3a774d50cb682d72f811af8221a9c7cc [formerly 703891639a4a379cc2e1a9ac626df9926f747b77] [formerly d4ce2ce69a1f7c0ee80a288d59a379779d1f54a7 [formerly eed4f75]]
Former-commit-id: 37d87f97f7b5bb7f32987f83c6fd00fc5f430d44 [formerly a97a6bfd101a040aca2f9e1b137fca940ff542da]
Former-commit-id: 06ae356347d0a403df12393ba2765ec0235a016c
  • Loading branch information
notedit committed Feb 26, 2019
1 parent cc08454 commit 666d5b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
35 changes: 25 additions & 10 deletions incomingstreamtrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ type IncomingTrackStopListener func()

// IncomingStreamTrack Audio or Video track of a remote media stream
type IncomingStreamTrack struct {
id string
media string
receiver native.RTPReceiverFacade
counter int
encodings []*Encoding
trackInfo *sdp.TrackInfo
stats map[string]*IncomingAllStats
onStopListeners []func()
onAttachedListeners []func()
onDetachedListeners []func()
id string
media string
receiver native.RTPReceiverFacade
counter int
encodings []*Encoding
trackInfo *sdp.TrackInfo
stats map[string]*IncomingAllStats
mediaStreamDuplicater *MediaStreamDuplicater
onStopListeners []func()
onAttachedListeners []func()
onDetachedListeners []func()
}

// IncomingStats info
Expand Down Expand Up @@ -482,6 +483,15 @@ func (i *IncomingStreamTrack) OnStop(stop func()) {
i.onStopListeners = append(i.onStopListeners, stop)
}

func (i *IncomingStreamTrack) OnMediaFrame(listener func([]byte, uint)) {

if i.mediaStreamDuplicater == nil {
i.mediaStreamDuplicater = NewMediaStreamDuplicater(i)
}

i.mediaStreamDuplicater.SetMediaFrameListener(listener)
}

// Stop Removes the track from the incoming stream and also detaches any attached outgoing track or recorder
func (i *IncomingStreamTrack) Stop() {

Expand All @@ -499,6 +509,11 @@ func (i *IncomingStreamTrack) Stop() {
}
}

if i.mediaStreamDuplicater != nil {
i.mediaStreamDuplicater.Stop()
i.mediaStreamDuplicater = nil
}

for _, stopFunc := range i.onStopListeners {
stopFunc()
}
Expand Down
28 changes: 16 additions & 12 deletions mediastreamduplicater.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package mediaserver

import "C"
import (
"fmt"
"unsafe"

native "github.com/notedit/media-server-go/wrapper"
)

// MediaStreamDuplicater we can make a copy of the incoming stream and callback the mediaframe data
type MediaStreamDuplicater struct {
MediaFrames chan []byte
track *IncomingStreamTrack
duplicater native.MediaStreamDuplicaterFacade
listener mediaframeListener
track *IncomingStreamTrack
duplicater native.MediaStreamDuplicaterFacade
listener mediaframeListener // used for native wrapper, see swig's doc

mediaframeListener func([]byte, uint) // used for outside
}

type mediaframeListener interface {
Expand All @@ -35,15 +37,17 @@ type overwrittenMediaFrameListener struct {

func (p *overwrittenMediaFrameListener) OnMediaFrame(frame native.MediaFrame) {

if p.duplicater != nil {
if p.duplicater != nil && p.duplicater.mediaframeListener != nil {
buffer := C.GoBytes(unsafe.Pointer(frame.GetData()), C.int(frame.GetLength()))
if frame.GetType() == native.MediaFrameVideo {
data, err := annexbConvert(buffer)
if err == nil {
p.duplicater.MediaFrames <- data
p.duplicater.mediaframeListener(data, frame.GetTimeStamp())
} else {
fmt.Println(err)
}
} else {
p.duplicater.MediaFrames <- buffer
p.duplicater.mediaframeListener(buffer, frame.GetTimeStamp())
}

}
Expand All @@ -59,10 +63,6 @@ func NewMediaStreamDuplicater(track *IncomingStreamTrack) *MediaStreamDuplicater
source := track.GetFirstEncoding().GetSource()
duplicater.duplicater = native.NewMediaStreamDuplicaterFacade(source)

track.OnStop(func() {
duplicater.Stop()
})

listener := &overwrittenMediaFrameListener{
duplicater: duplicater,
}
Expand All @@ -73,10 +73,14 @@ func NewMediaStreamDuplicater(track *IncomingStreamTrack) *MediaStreamDuplicater

duplicater.duplicater.AddMediaListener(duplicater.listener)

duplicater.MediaFrames = make(chan []byte, 5)
return duplicater
}

// SetMediaFrameListener set outside mediaframe listener
func (d *MediaStreamDuplicater) SetMediaFrameListener(listener func([]byte, uint)) {
d.mediaframeListener = listener
}

// Stop stop this
func (d *MediaStreamDuplicater) Stop() {

Expand Down

0 comments on commit 666d5b1

Please sign in to comment.