-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathps-slices_test.go
157 lines (145 loc) · 3.55 KB
/
ps-slices_test.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ps
import (
"github.com/MeloQi/interfaces"
"github.com/MeloQi/rtp"
"io"
"net"
"os"
"testing"
"time"
)
func TestH264OnlyPsMuxSlices(t *testing.T) {
/* var fileName string = `D:\workspace\test.h264`
var saveFileName string = `D:\workspace\test.mpeg`*/
mux := NewH264OnlyPsMuxSlices()
fReadbuf := make([]byte, BUF_LEN)
pStaticBuf := make([]byte, 2*BUF_LEN)
StaticBufSize := 0
pts := int64(0)
dts := int64(0)
f, err := os.Open(fileName)
if err != nil {
t.Error(err)
t.Fail()
return
}
saveFile, err := os.Create(saveFileName)
if err != nil {
t.Error(err)
t.Fail()
return
}
socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 1234,
})
if err != nil {
t.Error("连接失败!", err)
t.Fail()
return
}
defer socket.Close()
rtpInst := rtp.NewRRtpTransfer()
for {
n, err := f.Read(fReadbuf)
//t.Log("read ", n, " bytes")
if err != nil {
if err == io.EOF {
return
}
t.Error(err)
t.Fail()
return
}
dst := pStaticBuf[StaticBufSize:]
src := fReadbuf[:n]
copy(dst, src)
LastBlockLen := StaticBufSize + n
LeaveLen := LastBlockLen
pCurPos := pStaticBuf
NaluStartPos := pStaticBuf
FrameLen := 0
isFirstFrame := true
for LastBlockLen > 4 {
if (isH264Or265Frame(pCurPos, nil)) {
if isFirstFrame {
isFirstFrame = false
} else {
frame := NaluStartPos[4:FrameLen]
frameLen := len(frame)
pFrame := frame
pkg := &interfaces.Packet{IsMetadata: false, IsVideo: true, TimeStamp: uint32(pts / 90), RtpTimeStamp: uint32(pts), Data: []interfaces.FrameSlice{}, DataLen: frameLen}
Type := getH264NALtype(frame[0])
if Type == NAL_PPS || Type == NAL_SPS {
rtpData := make([]byte, frameLen)
copy(rtpData, pFrame)
pkg.Data = append(pkg.Data, interfaces.FrameSlice{Data: rtpData, RtpData: nil})
} else {
rtpData := make([]byte, 1)
copy(rtpData, []byte{frame[0]})
frameLen -= 1
pFrame = pFrame[1:]
pkg.Data = append(pkg.Data, interfaces.FrameSlice{Data: rtpData, RtpData: nil})
for frameLen > 0 {
var rtpData []byte
if frameLen > 1400 {
rtpData = make([]byte, 1412)
copy(rtpData[12:], pFrame[:1400])
pFrame = pFrame[1400:]
frameLen -= 1400
} else {
rtpData = make([]byte, len(pFrame)+12)
copy(rtpData[12:], pFrame)
frameLen -= len(pFrame)
}
pkg.Data = append(pkg.Data, interfaces.FrameSlice{Data: rtpData[12:], RtpData: rtpData})
}
}
if out, err := mux.Mux(pkg, pts); err != nil {
t.Error(err)
t.Log("mux error!")
t.Fail()
return
} else {
if out != nil && len(out) > 0 {
for i, v := range out {
saveFile.Write(v.Data)
d := v.RtpData
if d == nil || len(d) == 0 {
d = v.Data
}
rtpInst.PkgRtpOut(d, rtp.RTP_TYPE_VIDEO, v.RtpData != nil, 96, i == len(out)-1, uint32(dts), 11111, func(pack *rtp.RTPPack) {
socket.Write(pack.Buffer.Bytes())
})
}
}
}
if (Type == NAL_IDR) || (Type == NAL_PFRAME) {
pts += 3600
dts += 3600
time.Sleep(time.Millisecond * 38)
}
}
LeaveLen = LeaveLen - FrameLen
NaluStartPos = pCurPos
FrameLen = 0
pCurPos = pCurPos[2:]
LastBlockLen -= 2
FrameLen += 2
}
FrameLen++
pCurPos = pCurPos[1:]
LastBlockLen--
}
dst = pStaticBuf[:LeaveLen]
src = NaluStartPos[:LeaveLen]
copy(dst, src)
StaticBufSize = LeaveLen;
}
if f != nil {
f.Close()
}
if saveFile != nil {
saveFile.Close()
}
}