Skip to content

Commit 166a330

Browse files
committed
fix crash on fixHeader and remove duplicated code getStartCodeSize method
1 parent 927abd7 commit 166a330

5 files changed

Lines changed: 30 additions & 53 deletions

File tree

common/src/main/java/com/pedro/common/Extensions.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ fun ByteBuffer.toByteArray(): ByteArray {
6868
}
6969
}
7070

71+
fun ByteBuffer.getStartCodeSize(): Int {
72+
var startCodeSize = 0
73+
if (this.get(0).toInt() == 0x00 && this.get(1).toInt() == 0x00
74+
&& this.get(2).toInt() == 0x00 && this.get(3).toInt() == 0x01) {
75+
//match 00 00 00 01
76+
startCodeSize = 4
77+
} else if (this.get(0).toInt() == 0x00 && this.get(1).toInt() == 0x00
78+
&& this.get(2).toInt() == 0x01) {
79+
//match 00 00 01
80+
startCodeSize = 3
81+
}
82+
return startCodeSize
83+
}
84+
7185
fun ByteBuffer.removeInfo(info: MediaFrame.Info): ByteBuffer {
7286
try {
7387
position(info.offset)

rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H264Packet.kt

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.pedro.rtmp.flv.video.packet
1818

1919
import android.util.Log
2020
import com.pedro.common.frame.MediaFrame
21+
import com.pedro.common.getStartCodeSize
2122
import com.pedro.common.removeInfo
2223
import com.pedro.rtmp.flv.BasePacket
2324
import com.pedro.rtmp.flv.FlvPacket
@@ -131,7 +132,7 @@ class H264Packet: BasePacket() {
131132
}
132133

133134
private fun removeHeader(byteBuffer: ByteBuffer, size: Int = -1): ByteBuffer {
134-
val position = if (size == -1) getStartCodeSize(byteBuffer) else size
135+
val position = if (size == -1) byteBuffer.getStartCodeSize() else size
135136
byteBuffer.position(position)
136137
return byteBuffer.slice()
137138
}
@@ -142,9 +143,9 @@ class H264Packet: BasePacket() {
142143
val sps = this.sps
143144
val pps = this.pps
144145
if (sps != null && pps != null) {
145-
val startCodeSize = getStartCodeSize(byteBuffer)
146+
val startCodeSize = byteBuffer.getStartCodeSize()
146147
if (startCodeSize == 0) return 0
147-
val startCode = ByteArray(startCodeSize) { 0x00 }
148+
val startCode = ByteArray(startCodeSize)
148149
startCode[startCodeSize - 1] = 0x01
149150
val avcHeader = startCode.plus(sps).plus(startCode).plus(pps).plus(startCode)
150151
if (byteBuffer.remaining() < avcHeader.size) return startCodeSize
@@ -160,20 +161,6 @@ class H264Packet: BasePacket() {
160161
return 0
161162
}
162163

163-
private fun getStartCodeSize(byteBuffer: ByteBuffer): Int {
164-
var startCodeSize = 0
165-
if (byteBuffer.get(0).toInt() == 0x00 && byteBuffer.get(1).toInt() == 0x00
166-
&& byteBuffer.get(2).toInt() == 0x00 && byteBuffer.get(3).toInt() == 0x01) {
167-
//match 00 00 00 01
168-
startCodeSize = 4
169-
} else if (byteBuffer.get(0).toInt() == 0x00 && byteBuffer.get(1).toInt() == 0x00
170-
&& byteBuffer.get(2).toInt() == 0x01) {
171-
//match 00 00 01
172-
startCodeSize = 3
173-
}
174-
return startCodeSize
175-
}
176-
177164
override fun reset(resetInfo: Boolean) {
178165
if (resetInfo) {
179166
sps = null

rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H265Packet.kt

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.pedro.rtmp.flv.video.packet
1818

1919
import android.util.Log
2020
import com.pedro.common.frame.MediaFrame
21+
import com.pedro.common.getStartCodeSize
2122
import com.pedro.common.removeInfo
2223
import com.pedro.rtmp.flv.BasePacket
2324
import com.pedro.rtmp.flv.FlvPacket
@@ -137,7 +138,7 @@ class H265Packet: BasePacket() {
137138
}
138139

139140
private fun removeHeader(byteBuffer: ByteBuffer, size: Int = -1): ByteBuffer {
140-
val position = if (size == -1) getStartCodeSize(byteBuffer) else size
141+
val position = if (size == -1) byteBuffer.getStartCodeSize() else size
141142
byteBuffer.position(position)
142143
return byteBuffer.slice()
143144
}
@@ -149,9 +150,9 @@ class H265Packet: BasePacket() {
149150
val pps = this.pps
150151
val vps = this.vps
151152
if (sps != null && pps != null && vps != null) {
152-
val startCodeSize = getStartCodeSize(byteBuffer)
153+
val startCodeSize = byteBuffer.getStartCodeSize()
153154
if (startCodeSize == 0) return 0
154-
val startCode = ByteArray(startCodeSize) { 0x00 }
155+
val startCode = ByteArray(startCodeSize)
155156
startCode[startCodeSize - 1] = 0x01
156157
val avcHeader = startCode.plus(vps).plus(startCode).plus(sps).plus(startCode).plus(pps).plus(startCode)
157158
if (byteBuffer.remaining() < avcHeader.size) return startCodeSize
@@ -167,20 +168,6 @@ class H265Packet: BasePacket() {
167168
return 0
168169
}
169170

170-
private fun getStartCodeSize(byteBuffer: ByteBuffer): Int {
171-
var startCodeSize = 0
172-
if (byteBuffer.get(0).toInt() == 0x00 && byteBuffer.get(1).toInt() == 0x00
173-
&& byteBuffer.get(2).toInt() == 0x00 && byteBuffer.get(3).toInt() == 0x01) {
174-
//match 00 00 00 01
175-
startCodeSize = 4
176-
} else if (byteBuffer.get(0).toInt() == 0x00 && byteBuffer.get(1).toInt() == 0x00
177-
&& byteBuffer.get(2).toInt() == 0x01) {
178-
//match 00 00 01
179-
startCodeSize = 3
180-
}
181-
return startCodeSize
182-
}
183-
184171
override fun reset(resetInfo: Boolean) {
185172
if (resetInfo) {
186173
sps = null

rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H264Packet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class H264Packet: BasePacket(RtpConstants.clockVideoFrequency,
157157
if (sps != null && pps != null) {
158158
val startCodeSize = byteBuffer.getVideoStartCodeSize()
159159
if (startCodeSize == 0) return 0
160-
val startCode = ByteArray(startCodeSize) { 0x00 }
160+
val startCode = ByteArray(startCodeSize)
161161
startCode[startCodeSize - 1] = 0x01
162162
val avcHeader = startCode.plus(sps).plus(startCode).plus(pps).plus(startCode)
163163
if (byteBuffer.remaining() < avcHeader.size) return startCodeSize

srt/src/main/java/com/pedro/srt/mpeg2ts/packets/H26XPacket.kt

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.pedro.srt.mpeg2ts.packets
1818

1919
import android.util.Log
2020
import com.pedro.common.frame.MediaFrame
21+
import com.pedro.common.getStartCodeSize
2122
import com.pedro.common.removeInfo
2223
import com.pedro.common.toByteArray
2324
import com.pedro.srt.mpeg2ts.Codec
@@ -111,7 +112,7 @@ class H26XPacket(
111112
*/
112113
private fun fixHeader(byteBuffer: ByteBuffer, isKeyFrame: Boolean): ByteBuffer {
113114
var noHeaderBuffer = removeHeader(byteBuffer, isKeyFrame) //remove video info header
114-
val startCodeSize = getStartCodeSize(noHeaderBuffer)
115+
val startCodeSize = noHeaderBuffer.getStartCodeSize()
115116
if (startCodeSize == 0) { //make sure buffer start with prefix
116117
val bufferWithPrefix = ByteBuffer.allocate(noHeaderBuffer.remaining() + 4)
117118
bufferWithPrefix.putInt(0x00000001)
@@ -122,9 +123,11 @@ class H26XPacket(
122123
val vps = this.vps ?: byteArrayOf()
123124
val sps = this.sps ?: byteArrayOf()
124125
val pps = this.pps ?: byteArrayOf()
126+
val codec = this.codec
125127
val audSize = if (codec == Codec.AVC) 6 else 7
126128
val videoHeader = vps.plus(sps).plus(pps)
127-
val validBuffer = ByteBuffer.allocate(audSize + videoHeader.size + noHeaderBuffer.remaining())
129+
val noHeaderBytes = noHeaderBuffer.toByteArray()
130+
val validBuffer = ByteBuffer.allocate(audSize + videoHeader.size + noHeaderBytes.size)
128131
validBuffer.putInt(0x00000001)
129132
if (codec == Codec.AVC) {
130133
validBuffer.put(0x09.toByte())
@@ -135,7 +138,7 @@ class H26XPacket(
135138
validBuffer.put(0x50.toByte())
136139
}
137140
validBuffer.put(videoHeader)
138-
validBuffer.put(noHeaderBuffer.toByteArray())
141+
validBuffer.put(noHeaderBytes)
139142
validBuffer.rewind()
140143
configSend = true
141144
validBuffer
@@ -147,7 +150,7 @@ class H26XPacket(
147150

148151
private fun getVideoInfoData(byteBuffer: ByteBuffer): ByteArray {
149152
byteBuffer.rewind()
150-
val startCodeSize = getStartCodeSize(byteBuffer)
153+
val startCodeSize = byteBuffer.getStartCodeSize()
151154
return if (startCodeSize == 0) { //make sure video info start with prefix
152155
val validBuffer = ByteBuffer.allocate(byteBuffer.remaining() + 4)
153156
validBuffer.putInt(0x00000001)
@@ -189,18 +192,4 @@ class H26XPacket(
189192
return byteBuffer
190193
}
191194
}
192-
193-
private fun getStartCodeSize(byteBuffer: ByteBuffer): Int {
194-
var startCodeSize = 0
195-
if (byteBuffer.get(0).toInt() == 0x00 && byteBuffer.get(1).toInt() == 0x00
196-
&& byteBuffer.get(2).toInt() == 0x00 && byteBuffer.get(3).toInt() == 0x01) {
197-
//match 00 00 00 01
198-
startCodeSize = 4
199-
} else if (byteBuffer.get(0).toInt() == 0x00 && byteBuffer.get(1).toInt() == 0x00
200-
&& byteBuffer.get(2).toInt() == 0x01) {
201-
//match 00 00 01
202-
startCodeSize = 3
203-
}
204-
return startCodeSize
205-
}
206195
}

0 commit comments

Comments
 (0)