-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.go
102 lines (84 loc) · 2.17 KB
/
track.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
95
96
97
98
99
100
101
102
package rtc
import (
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/google/uuid"
webrtc "github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/pion/webrtc/v3/pkg/media/h264reader"
"github.com/sirupsen/logrus"
)
// loop
func NewH264LocalStaticSampleVideoTrack(videoFile string, fps int) (videoTrack *webrtc.TrackLocalStaticSample, err error) {
if videoFile == "" {
err = fmt.Errorf("videoFile is empty")
return
}
var videoFileAbs string
videoFileAbs, err = filepath.Abs(videoFile)
if err != nil {
return
}
_, err = os.Stat(videoFileAbs)
if err != nil {
return
}
videoTrack, err = webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeH264,
}, "video", fmt.Sprintf("rtckit/%s", uuid.New().String()))
if err != nil {
return
}
go h264VideoFileConsumer(videoTrack, videoFileAbs, time.Duration(1000/fps)*time.Millisecond)
return
}
func h264VideoFileConsumer(videoTrack *webrtc.TrackLocalStaticSample, videoFile string, videoFps time.Duration) {
for {
file, h264Err := os.Open(videoFile)
if h264Err != nil {
logrus.Errorf("open file failed, err: %v", h264Err.Error())
continue
}
h264, h264Err := h264reader.NewReader(file)
if h264Err != nil {
logrus.Errorf("new reader failed, err: %v", h264Err.Error())
continue
}
buf := make(chan []byte, 1024*1024) // 1MB?
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for data := range buf {
sample := media.Sample{Data: data, Duration: videoFps, Timestamp: time.Now(), PacketTimestamp: uint32(time.Now().Unix())}
if h264Err1 := videoTrack.WriteSample(sample); h264Err1 != nil {
logrus.Errorf("write sample failed, err: %v", h264Err1.Error())
continue
}
}
}()
go func() {
defer wg.Done()
ticker := time.NewTicker(videoFps)
for ; true; <-ticker.C {
nal, h264Err := h264.NextNAL()
if h264Err == io.EOF {
logrus.Warnf("all video frames parsed and sent, loop playback")
break
}
if h264Err != nil {
logrus.Errorf("next nal failed, err: %v", h264Err.Error())
break
}
buf <- nal.Data
}
close(buf)
}()
wg.Wait()
_ = file.Close()
}
}