Finished dealing with bugs, but now I have a really ugly error that gives no indication to where abouts

This commit is contained in:
saxon 2018-07-16 18:50:17 +09:30
parent 376a694cd9
commit d73dea687a
1 changed files with 68 additions and 48 deletions

View File

@ -41,6 +41,7 @@ int write_frame(RTMP* rtmp, char* data, uint data_length);
int end_session(RTMP* rtmp); int end_session(RTMP* rtmp);
void AV_queue(RTMP_METHOD **vals, int *num, AVal *av, int txn); void AV_queue(RTMP_METHOD **vals, int *num, AVal *av, int txn);
int WriteN(RTMP *r, const char *buffer, int n); int WriteN(RTMP *r, const char *buffer, int n);
int EncodeInt32LE(char *output, int nVal);
*/ */
import "C" import "C"
@ -175,12 +176,12 @@ func rtmpWrite(r *C.RTMP, data []byte) int {
return 0 return 0
} }
if indxBytePtr(buf,0) == 'F' && indxBytePtr(buf,1) == 'L' && indxBytePtr(buf,2) == 'V' { if *indxBytePtr(buf,0) == 'F' && *indxBytePtr(buf,1) == 'L' && *indxBytePtr(buf,2) == 'V' {
buf = unsafe.Pointer(uintptr(buf) + uintptr(13)) buf = unsafe.Pointer(uintptr(buf) + uintptr(13))
s2 -= 13 s2 -= 13
} }
pkt.m_packetType = C.uchar(indxBytePtr(buf,0)) pkt.m_packetType = C.uchar(*indxBytePtr(buf,0))
buf = incBytePtr(buf, 1) buf = incBytePtr(buf, 1)
// TODO: port this // TODO: port this
pkt.m_nBodySize = C.AMF_DecodeInt24((*C.char)(buf)) pkt.m_nBodySize = C.AMF_DecodeInt24((*C.char)(buf))
@ -188,7 +189,7 @@ func rtmpWrite(r *C.RTMP, data []byte) int {
// TODO: replace with ported version // TODO: replace with ported version
pkt.m_nTimeStamp = C.AMF_DecodeInt24((*C.char)(buf)) pkt.m_nTimeStamp = C.AMF_DecodeInt24((*C.char)(buf))
buf = incBytePtr(buf, 3) buf = incBytePtr(buf, 3)
pkt.m_nTimeStamp |= C.uint(indxBytePtr(buf,0)) << 24 pkt.m_nTimeStamp |= C.uint(*indxBytePtr(buf,0)) << 24
buf = incBytePtr(buf, 4) buf = incBytePtr(buf, 4)
s2 -= 11 s2 -= 11
@ -314,13 +315,13 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
t = int32(int(packet.m_nTimeStamp) - last) t = int32(int(packet.m_nTimeStamp) - last)
if packet.m_body != nil { if packet.m_body != nil {
header = decPtr(unsafe.Pointer(packet.m_body), nSize, header = decBytePtr(unsafe.Pointer(packet.m_body), nSize)
hend = packet.m_body hend = unsafe.Pointer(packet.m_body)
} else { } else {
header = incPtr(hbuf,6) header = incBytePtr(hbuf,6)
// TODO: be cautious about this sizeof - make sure it works how you think it // TODO: be cautious about this sizeof - make sure it works how you think it
// does. C code used sizeof(hbuf) where hbuf is a *char // does. C code used sizeof(hbuf) where hbuf is a *char
hend = incPtr(hbuf,unsafe.Sizeof((*byte)(hbuf))) hend = incBytePtr(hbuf,C.RTMP_MAX_HEADER_SIZE)
} }
switch { switch {
@ -331,9 +332,14 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
} }
if cSize != 0 { if cSize != 0 {
header = decPtr(header,4) header = decBytePtr(header,4)
hSize = incPtr(hSize,4) hSize += cSize
log.Printf("Larger timsetamp than 24-bit: 0x%x", t) }
if t >= 0xffffff {
header = decBytePtr(header,4)
hSize += 4
log.Printf("Larger timestamp than 24-bit: 0x%v", t)
} }
hptr = header hptr = header
@ -347,33 +353,44 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
c |= byte(1) c |= byte(1)
} }
*(*byte)(hptr) = c
hptr = incBytePtr(hptr,1)
if cSize != 0 {
tmp := packet.m_nChannel - 64
*(*byte)(hptr) = byte(tmp & 0xff)
hptr = incBytePtr(hptr,1)
if cSize == 2 {
*(*byte)(hptr) = byte(tmp >> 8) *(*byte)(hptr) = byte(tmp >> 8)
hptr = incPtr(hptr,1) hptr = incBytePtr(hptr,1)
}
}
if nSize > 1 { if nSize > 1 {
res := t res := t
if t > 0xffffff { if t > 0xffffff {
res = 0xffffff res = 0xffffff
} }
hptr = unsafe.Pointer(AMF_EncodeInt24((*C.char)(hptr),(*C.char)(hend), res)) hptr = unsafe.Pointer(C.AMF_EncodeInt24((*C.char)(hptr),(*C.char)(hend), C.int(res)))
} }
if nSize > 4 { if nSize > 4 {
*(*byte)(hptr) = byte(packet.m_packetType) *(*byte)(hptr) = byte(packet.m_packetType)
hptr = unsafe.Pointer(AMF_EncodeInt24((*C.char)(hptr), (*C.char)(hend), hptr = unsafe.Pointer(C.AMF_EncodeInt24((*C.char)(hptr), (*C.char)(hend),
packet.m_nbodySize)) C.int(packet.m_nBodySize)))
} }
if nSize > 8 { if nSize > 8 {
hptr = incPtr(hptr, int(EncodeInt32LE((*C.char)(hptr), packet.m_nInfoField2))) hptr = incBytePtr(hptr, int(C.EncodeInt32LE((*C.char)(hptr), packet.m_nInfoField2)))
} }
if t >= 0xffffff { if t >= 0xffffff {
hptr = unsafe.Pointer(AMF_EncodeInt32((*C.char)(hptr), (*C.char)(hend), t)) hptr = unsafe.Pointer(C.AMF_EncodeInt32((*C.char)(hptr), (*C.char)(hend), C.int(t)))
} }
nSize = int(packet.m_nBodySize) nSize = int(packet.m_nBodySize)
buffer = unsafe.Pointer(m_body) buffer = unsafe.Pointer(packet.m_body)
nChunkSize = int(r.m_outChunkSize) nChunkSize = int(r.m_outChunkSize)
if debugMode { if debugMode {
@ -382,14 +399,14 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
// send all chunks in one HTTP request // send all chunks in one HTTP request
// TODO: port RTMP_FEATURE_HTTP // TODO: port RTMP_FEATURE_HTTP
if r.Link.protocol & C.RTMP_FEATURE_HTTP { if int(r.Link.protocol & C.RTMP_FEATURE_HTTP) != 0 {
chunks := (nSize+nChunkSize-1)/nChunkSize chunks := (nSize+nChunkSize-1)/nChunkSize
if chunks > 1 { if chunks > 1 {
tlen = chunks *(cSize+1) +nSize +hSize tlen = chunks *(cSize+1) +nSize +hSize
// TODO: figure out how to do this in go // TODO: figure out how to do this in go
tbuf = C.malloc(tlen) tbuf = C.malloc(C.ulong(tlen))
if tbuf == 0 { if tbuf == nil {
return 0 return 0
} }
toff = tbuf toff = tbuf
@ -407,11 +424,11 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
// RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)buffer, nChunkSize); // RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)buffer, nChunkSize);
if tbuf != nil { if tbuf != nil {
memmove(toff, header, nChunksize + hSize) memmove(toff, header, uintptr(nChunkSize + hSize))
toff = incPtr(toff, nChunkSize + hSize) toff = incBytePtr(toff, nChunkSize + hSize)
} else { } else {
// TODO: port this // TODO: port this
wrote = C.WriteN(r, (*C.char)(header), C.int(nChunkSize+hSize)) wrote = int(C.WriteN(r, (*C.char)(header), C.int(nChunkSize+hSize)))
if wrote == 0 { if wrote == 0 {
return 0 return 0
@ -419,20 +436,20 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
} }
nSize -= nChunkSize nSize -= nChunkSize
buffer = incPtr(buffer,nChunkSize) buffer = incBytePtr(buffer,nChunkSize)
hSize = 0 hSize = 0
if nSize > 0 { if nSize > 0 {
header = decPtr(buffer, 1) header = decBytePtr(buffer, 1)
hSize = 1 hSize = 1
if cSize != 0 { if cSize != 0 {
header = decPtr(header,1) header = decBytePtr(header,1)
hSize += cSize hSize += cSize
} }
if t >= 0xffffff { if t >= 0xffffff {
header = decPtr(header,4) header = decBytePtr(header,4)
hSize += 4 hSize += 4
} }
@ -440,25 +457,26 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
if cSize != 0 { if cSize != 0 {
tmp := int(packet.m_nChannel) - 64 tmp := int(packet.m_nChannel) - 64
indxBytePtr(header,1) = byte(tmp & 0xff) *indxBytePtr(header,1) = byte(tmp & 0xff)
if cSize == 2 { if cSize == 2 {
indxBytePtr(header,2) = byte(tmp >> 8) *indxBytePtr(header,2) = byte(tmp >> 8)
} }
} }
if t >= 0xffffff { if t >= 0xffffff {
extendedTimestamp := incPtr(header,1+cSize) extendedTimestamp := incBytePtr(header,1+cSize)
// TODO: port this // TODO: port this
C.AMF_EncodeInt32((*C.char)(extendedTimestamp), C.AMF_EncodeInt32((*C.char)(extendedTimestamp),
(*C.char)(incPtr(extendedTimestamp,4)), t) (*C.char)(incBytePtr(extendedTimestamp,4)), C.int(t))
} }
} }
} }
if tbuf != 0 { if tbuf != nil {
// TODO: port C.writeN // TODO: port C.writeN
wrote := int(C.WriteN(r, (*C.char)(tbuf), (*C.char)(decPtr(toff,tbuf)))) wrote := int(C.WriteN(r, (*C.char)(tbuf), C.int(uintptr(decBytePtr(toff,
C.free((*C.char)(tbuf)) int(uintptr(unsafe.Pointer(tbuf))))))))
C.free(tbuf)
tbuf = nil tbuf = nil
if wrote == 0 { if wrote == 0 {
@ -472,7 +490,7 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
// TODO: port C.AVal // TODO: port C.AVal
var method C.AVal var method C.AVal
var ptr unsafe.Pointer var ptr unsafe.Pointer
ptr = incPtr(unsafe.Pointer(packet.m_body),1) ptr = incBytePtr(unsafe.Pointer(packet.m_body),1)
// TODO: port this // TODO: port this
C.AMF_DecodeString((*C.char)(ptr), &method) C.AMF_DecodeString((*C.char)(ptr), &method)
@ -482,7 +500,7 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
// keep it in call queue till result arrives // keep it in call queue till result arrives
if queue != 0 { if queue != 0 {
var txn int var txn int
ptr = incPtr(ptr, 3 + int(method.av_len)) ptr = incBytePtr(ptr, 3 + int(method.av_len))
// TODO: port this // TODO: port this
txn = int(C.AMF_DecodeNumber((*C.char)(ptr))) txn = int(C.AMF_DecodeNumber((*C.char)(ptr)))
// TODO: port this // TODO: port this
@ -490,12 +508,14 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
} }
} }
if indxPtr(unsafe.Pointer(r.m_vecChannelsOut),packet.m_nChannel) == 0 { if unsafe.Pointer((*C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut),
(*C.char)(indxPtr(r.m_vecChannelsOut)) = C.malloc(unsafe.Sizof(C.RTMPPacket)) int(packet.m_nChannel), int(unsafe.Sizeof(packet))))) == nil {
r.m_vecChannelsOut = (**C.RTMPPacket)(C.malloc(C.ulong(unsafe.Sizeof(packet))))
} }
memmove(indxPtr(unsafe.Pointer(r.m_vecChannelsOut),packet.m_nChannel), memmove(incPtr(unsafe.Pointer(r.m_vecChannelsOut),int(packet.m_nChannel),
unsafe.Pointer(packet), unsafe.Sizeof(C.RTMPPacket)) int(unsafe.Sizeof(packet))),unsafe.Pointer(packet), unsafe.Sizeof(packet))
return 1 return 1
} }
@ -563,18 +583,18 @@ func avQueue(vals **C.RTMP_METHOD, num *int, av *AVal, txn int ) {
func memmove(to, from unsafe.Pointer, n uintptr) func memmove(to, from unsafe.Pointer, n uintptr)
// indxBytePtr returns a byte at the indx inc give a ptr // indxBytePtr returns a byte at the indx inc give a ptr
func indxBytePtr(ptr unsafe.Pointer, inc int) byte { func indxBytePtr(ptr unsafe.Pointer, inc int) *byte {
return *(*byte)(incPtr(ptr, inc, byteSize)) return (*byte)(incPtr(ptr, inc, byteSize))
} }
// indxInt32Ptr returns an int32 at the indx inc given a ptr // indxInt32Ptr returns an int32 at the indx inc given a ptr
func indxInt32Ptr(ptr unsafe.Pointer, inc int) int32 { func indxInt32Ptr(ptr unsafe.Pointer, inc int) *int32 {
return *(*int32)(incPtr(ptr, inc, int32Size)) return (*int32)(incPtr(ptr, inc, int32Size))
} }
// indxInt64Ptr returns an int64 at the indx inc given a ptr // indxInt64Ptr returns an int64 at the indx inc given a ptr
func indxInt64Ptr(ptr unsafe.Pointer, inc int) int64 { func indxInt64Ptr(ptr unsafe.Pointer, inc int) *int64 {
return *(*int64)(incPtr(ptr, inc, int64Size)) return (*int64)(incPtr(ptr, inc, int64Size))
} }
// incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions // incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions
@ -620,7 +640,7 @@ func decInt32Ptr(ptr unsafe.Pointer, dec int) unsafe.Pointer {
// decBytePtr returns an unsafe.Pointer to a int64 that is dec negative positions // decBytePtr returns an unsafe.Pointer to a int64 that is dec negative positions
// from ptr // from ptr
func decInt64Ptr(ptr unsafe.Pointer, dec int) unsafe.Pointer { func decInt64Ptr(ptr unsafe.Pointer, dec int) unsafe.Pointer {
return decPTr(ptr,dec,int64Size) return decPtr(ptr,dec,int64Size)
} }
// sliceToPtr get's the address of the first data element and returns as unsafe // sliceToPtr get's the address of the first data element and returns as unsafe