-
Notifications
You must be signed in to change notification settings - Fork 119
/
recorder.go
80 lines (61 loc) · 1.61 KB
/
recorder.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
package mediaserver
import (
"strconv"
"time"
native "github.com/notedit/media-server-go/wrapper"
)
// Recorder represent a file recorder
type Recorder struct {
tracks map[string]*RecorderTrack
recorder native.MP4RecorderFacade
ticker *time.Ticker
refresher *Refresher
maxTrackId int
}
// NewRecorder create a new recorder
func NewRecorder(filename string, waitForIntra bool, refresh int) *Recorder {
recorder := &Recorder{}
recorder.recorder = native.NewMP4RecorderFacade()
recorder.recorder.Create(filename)
recorder.recorder.Record(waitForIntra)
recorder.tracks = map[string]*RecorderTrack{}
recorder.maxTrackId = 1
if refresh > 0 {
recorder.refresher = NewRefresher(refresh)
}
return recorder
}
// Record start record an incoming track
func (r *Recorder) Record(incoming *IncomingStreamTrack) {
for _, encoding := range incoming.GetEncodings() {
encoding.GetDepacketizer().AddMediaListener(r.recorder)
r.maxTrackId += 1
recorderTrack := NewRecorderTrack(strconv.Itoa(r.maxTrackId), incoming, encoding)
r.tracks[recorderTrack.GetID()] = recorderTrack
}
if r.refresher != nil {
r.refresher.Add(incoming)
}
}
// RecordStream start record an incoming stream
func (r *Recorder) RecordStream(incoming *IncomingStream) {
for _, track := range incoming.GetTracks() {
r.Record(track)
}
}
// Stop stop the recorder
func (r *Recorder) Stop() {
if r.recorder == nil {
return
}
for _, track := range r.tracks {
track.Stop()
}
if r.refresher != nil {
r.refresher.Stop()
}
r.recorder.Close()
native.DeleteMP4RecorderFacade(r.recorder)
r.refresher = nil
r.recorder = nil
}