-
Notifications
You must be signed in to change notification settings - Fork 119
/
mediaframemultiplexer.go
94 lines (73 loc) · 2.51 KB
/
mediaframemultiplexer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package mediaserver
import "C"
import (
native "github.com/notedit/media-server-go/wrapper"
)
// MediaStreamDuplicater we can make a copy of the incoming stream and callback the mediaframe data
type MediaFrameMultiplexer struct {
track *IncomingStreamTrack
multiplexer native.MediaFrameMultiplexer
listener mediaframeListener // used for native wrapper, see swig's doc
mediaframeListener func([]byte, uint64) // used for outside
}
type mediaframeListener interface {
native.MediaFrameListenerFacade
deleteMediaFrameListener()
}
type goMediaFrameListener struct {
native.MediaFrameListenerFacade
}
func (m *goMediaFrameListener) deleteMediaFrameListener() {
native.DeleteDirectorMediaFrameListenerFacade(m.MediaFrameListenerFacade)
}
type overwrittenMediaFrameListener struct {
p native.MediaFrameListenerFacade
multiplexer *MediaFrameMultiplexer
}
func (p *overwrittenMediaFrameListener) OnMediaFrame(frame native.MediaFrame) {
//if p.multiplexer != nil && p.multiplexer.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.multiplexer.mediaframeListener(data, frame.GetTimeStamp())
// } else {
// fmt.Println(err)
// }
// } else {
// p.multiplexer.mediaframeListener(buffer, frame.GetTimeStamp())
// }
//
//}
}
// NewMediaStreamDuplicater duplicate this IncomingStreamTrack and callback the mediaframe
func NewMediaFrameMultiplexer(track *IncomingStreamTrack) *MediaFrameMultiplexer {
duplicater := &MediaFrameMultiplexer{}
duplicater.track = track
// We should make sure this source is the main source
source := track.GetFirstEncoding().GetSource()
duplicater.multiplexer = native.NewMediaFrameMultiplexer(source)
listener := &overwrittenMediaFrameListener{
multiplexer: duplicater,
}
p := native.NewDirectorMediaFrameListenerFacade(listener)
listener.p = p
duplicater.listener = &goMediaFrameListener{MediaFrameListenerFacade: p}
duplicater.multiplexer.AddMediaListener(duplicater.listener)
return duplicater
}
// SetMediaFrameListener set outside mediaframe listener
func (d *MediaFrameMultiplexer) SetMediaFrameListener(listener func([]byte, uint64)) {
d.mediaframeListener = listener
}
// Stop stop this
func (d *MediaFrameMultiplexer) Stop() {
if d.track == nil {
return
}
if d.listener != nil {
d.multiplexer.RemoveMediaListener(d.listener)
d.listener.deleteMediaFrameListener()
}
d.track = nil
}