forked from leonlinc/mpts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.go
92 lines (84 loc) · 2.08 KB
/
record.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
package mpts
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
type AdaptOut struct {
Pos int64
Content AdaptFieldPrivData
}
type Record interface {
Process(pkt *TsPkt)
NotifyTime(pcr int64, pos int64)
Flush()
Report(root string)
}
type BaseRecord struct {
Root string
Pid int
PcrTime int64
PcrPos int64
IFrameLog *os.File
AdaptFieldPrivDataLog *os.File
}
func (b *BaseRecord) NotifyTime(pcr int64, pos int64) {
b.PcrTime = pcr
b.PcrPos = pos
}
func (r *BaseRecord) LogIFrame(i IFrameInfo) {
if r.IFrameLog == nil {
var pid string = strconv.Itoa(r.Pid)
var err error
fname := filepath.Join(r.Root, pid+"-iframe"+".csv")
r.IFrameLog, err = os.Create(fname)
if err != nil {
panic(err)
}
header := "Pos, PTS, Key"
fmt.Fprintln(r.IFrameLog, header)
}
cols := []string{
strconv.FormatInt(i.Pos, 10),
strconv.FormatInt(i.Pts, 10),
strconv.FormatBool(i.Key),
}
fmt.Fprintln(r.IFrameLog, strings.Join(cols, ", "))
}
func (logger *BaseRecord) LogAdaptFieldPrivData(pkt *TsPkt) {
if pkt.AdaptField == nil || pkt.AdaptField.PrivateData == nil {
return
}
data := pkt.AdaptField.PrivateData
if logger.AdaptFieldPrivDataLog == nil {
fname := filepath.Join(logger.Root, strconv.Itoa(logger.Pid)+"-tspriv.csv")
var err error
logger.AdaptFieldPrivDataLog, err = os.Create(fname)
if err != nil {
panic(err)
}
}
privList := ParseAdaptFieldPrivData(data)
for _, p := range privList {
adaptOut := AdaptOut{pkt.Pos, p}
c, _ := json.Marshal(adaptOut)
fmt.Fprintln(logger.AdaptFieldPrivDataLog, string(c))
}
}
func CreateRecord(pid int, t string, root string) Record {
var record Record
switch t {
case "SCTE-35":
record = &Scte35Record{BaseRecord: BaseRecord{Pid: pid}}
case "MPEG-4 AVC Video":
record = &H264Record{BaseRecord: BaseRecord{Pid: pid, Root: root}}
case "MPEG-2 Video":
record = &Mp2vRecord{BaseRecord: BaseRecord{Pid: pid, Root: root}}
default:
record = &PesRecord{BaseRecord: BaseRecord{Pid: pid, Root: root}}
}
return record
}