forked from bluenviron/gortsplib
/
mpegts_muxer.go
123 lines (98 loc) · 2.37 KB
/
mpegts_muxer.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bufio"
"os"
"time"
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
)
func durationGoToMPEGTS(v time.Duration) int64 {
return int64(v.Seconds() * 90000)
}
// mpegtsMuxer allows to save a H265 stream into a MPEG-TS file.
type mpegtsMuxer struct {
vps []byte
sps []byte
pps []byte
f *os.File
b *bufio.Writer
w *mpegts.Writer
track *mpegts.Track
dtsExtractor *h265.DTSExtractor
}
// newMPEGTSMuxer allocates a mpegtsMuxer.
func newMPEGTSMuxer(vps []byte, sps []byte, pps []byte) (*mpegtsMuxer, error) {
f, err := os.Create("mystream.ts")
if err != nil {
return nil, err
}
b := bufio.NewWriter(f)
track := &mpegts.Track{
Codec: &mpegts.CodecH265{},
}
w := mpegts.NewWriter(b, []*mpegts.Track{track})
return &mpegtsMuxer{
vps: vps,
sps: sps,
pps: pps,
f: f,
b: b,
w: w,
track: track,
}, nil
}
// close closes all the mpegtsMuxer resources.
func (e *mpegtsMuxer) close() {
e.b.Flush()
e.f.Close()
}
// encode encodes a H265 access unit into MPEG-TS.
func (e *mpegtsMuxer) encode(au [][]byte, pts time.Duration) error {
// prepend an AUD. This is required by some players
filteredAU := [][]byte{
{byte(h265.NALUType_AUD_NUT) << 1, 1, 0x50},
}
isRandomAccess := false
for _, nalu := range au {
typ := h265.NALUType((nalu[0] >> 1) & 0b111111)
switch typ {
case h265.NALUType_VPS_NUT:
e.vps = nalu
continue
case h265.NALUType_SPS_NUT:
e.sps = nalu
continue
case h265.NALUType_PPS_NUT:
e.pps = nalu
continue
case h265.NALUType_AUD_NUT:
continue
case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT:
isRandomAccess = true
}
filteredAU = append(filteredAU, nalu)
}
au = filteredAU
if len(au) <= 1 {
return nil
}
// add VPS, SPS and PPS before random access access unit
if isRandomAccess {
au = append([][]byte{e.vps, e.sps, e.pps}, au...)
}
var dts time.Duration
if e.dtsExtractor == nil {
// skip samples silently until we find one with a IDR
if !isRandomAccess {
return nil
}
e.dtsExtractor = h265.NewDTSExtractor()
}
var err error
dts, err = e.dtsExtractor.Extract(au, pts)
if err != nil {
return err
}
// encode into MPEG-TS
return e.w.WriteH26x(e.track, durationGoToMPEGTS(pts), durationGoToMPEGTS(dts), isRandomAccess, au)
}