Added more specific decrement functions

This commit is contained in:
saxon 2018-07-16 14:37:26 +09:30
parent d1362d659f
commit 376a694cd9
1 changed files with 44 additions and 21 deletions

View File

@ -87,6 +87,8 @@ func AVC(str string) C.AVal {
// av_setDataFrame is a static const global in rtmp.c // av_setDataFrame is a static const global in rtmp.c
var setDataFrame = AVC("@setDataFrame") var setDataFrame = AVC("@setDataFrame")
var packetSize = [...]int32{ 12, 8, 4, 1}
// Session provides an interface for sending flv tags over rtmp. // Session provides an interface for sending flv tags over rtmp.
type Session interface { type Session interface {
Open() error Open() error
@ -263,21 +265,24 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
if packet.m_nChannel >= r.m_channelsAllocatedOut { if packet.m_nChannel >= r.m_channelsAllocatedOut {
n := int(packet.m_nChannel+10) n := int(packet.m_nChannel+10)
packets := C.realloc(r.m_vecChannelsOut, unsafe.Sizeof(*C.RTMPPacket) * n) var tmp *C.RTMPPacket
packets := C.realloc(unsafe.Pointer(r.m_vecChannelsOut),
C.ulong(unsafe.Sizeof(tmp) * uintptr(n)))
if packets == 0 { if uintptr(packets) == uintptr(0) {
C.free(r.m_vecChannelsOut) C.free(unsafe.Pointer(r.m_vecChannelsOut))
r.m_vecChannelsOut = nil r.m_vecChannelsOut = nil
r.m_channelsAllocatedOut = 0 r.m_channelsAllocatedOut = 0
return 0 return 0
} }
r.m_vecChannelsOut = packets r.m_vecChannelsOut = (**C.RTMPPacket)(packets)
C.memset(r.m_vecChannelsOut + r.m_channelsAllocatedOut, 0, C.memset(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int(r.m_channelsAllocatedOut),
unsafe.Sizeof(*RTMPPacket) * (n-r.m_channelsAllocatedOut)) int(unsafe.Sizeof(tmp))), 0, C.ulong(unsafe.Sizeof(tmp) *
r.m_channelsAllocatedOut = n uintptr(n-int(r.m_channelsAllocatedOut))))
r.m_channelsAllocatedOut = C.int(n)
} }
prevPackt = *(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), prevPacket = *(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut),
packet.m_nChannel)) int(packet.m_nChannel), int(unsafe.Sizeof(packet))))
if prevPacket != nil && packet.m_headerType != RTMP_PACKET_SIZE_LARGE { if prevPacket != nil && packet.m_headerType != RTMP_PACKET_SIZE_LARGE {
// compress a bit by using the prev packet's attributes // compress a bit by using the prev packet's attributes
@ -290,11 +295,11 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
if prevPacket.m_nTimeStamp == packet.m_nTimeStamp && if prevPacket.m_nTimeStamp == packet.m_nTimeStamp &&
packet.m_headerType == RTMP_PACKET_SIZE_SMALL { packet.m_headerType == RTMP_PACKET_SIZE_SMALL {
// TODO: port this constant
packet.m_headerType = RTMP_PACKET_SIZE_MINIMUM packet.m_headerType = C.RTMP_PACKET_SIZE_MINIMUM
} }
last = prevPacket.m_nTimeStamp last = int(prevPacket.m_nTimeStamp)
} }
if packet.m_headerType > 3 { if packet.m_headerType > 3 {
@ -303,13 +308,13 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
return 0 return 0
} }
nSize = int(*(*int)(incPtr(unsafe.Pointer(packetSize),int(packet.m_headerType)))) nSize = int(*(*int)(incInt32Ptr(unsafe.Pointer(&packetSize[0]),int(packet.m_headerType))))
hSize = nSize hSize = nSize
cSize = 0 cSize = 0
t = packet.m_nTimeStamp - last t = int32(int(packet.m_nTimeStamp) - last)
if packet.m_body { if packet.m_body != nil {
header = packet.m_body - nSize header = decPtr(unsafe.Pointer(packet.m_body), nSize,
hend = packet.m_body hend = packet.m_body
} else { } else {
header = incPtr(hbuf,6) header = incPtr(hbuf,6)
@ -572,20 +577,20 @@ 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 positions // incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions
// from the passed ptr // from the passed ptr
func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer { func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer {
return incPtr(ptr,inc,byteSize) return incPtr(ptr,inc,byteSize)
} }
// incInt32Ptr returns an unsafe.Pointer to an int32 that is inc positions from // incInt32Ptr returns an unsafe.Pointer to an int32 that is inc positive
// the passed ptr // positions from the passed ptr
func incInt32Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer { func incInt32Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer {
return incPtr(ptr,inc,int32Size) return incPtr(ptr,inc,int32Size)
} }
// incInt64Ptr returns an unsafe.Pointer to an int64 that is inc positions from // incInt64Ptr returns an unsafe.Pointer to an int64 that is inc positive
// the passed ptr // positions from the passed ptr
func incInt64Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer { func incInt64Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer {
return incPtr(ptr,inc,int64Size) return incPtr(ptr,inc,int64Size)
} }
@ -600,6 +605,24 @@ func decPtr(ptr unsafe.Pointer, dec, typeSize int) unsafe.Pointer {
return unsafe.Pointer(uintptr(ptr) - uintptr(dec*typeSize)) return unsafe.Pointer(uintptr(ptr) - uintptr(dec*typeSize))
} }
// decBytePtr returns an unsafe.Pointer to a byte that is dec negative positions
// from ptr
func decBytePtr(ptr unsafe.Pointer, dec int) unsafe.Pointer {
return decPtr(ptr,dec,byteSize)
}
// decBytePtr returns an unsafe.Pointer to a int32 that is dec negative positions
// from ptr
func decInt32Ptr(ptr unsafe.Pointer, dec int) unsafe.Pointer {
return decPtr(ptr,dec,int32Size)
}
// decBytePtr returns an unsafe.Pointer to a int64 that is dec negative positions
// from ptr
func decInt64Ptr(ptr unsafe.Pointer, dec int) unsafe.Pointer {
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
// pointer // pointer
func sliceToPtr(data []byte) unsafe.Pointer { func sliceToPtr(data []byte) unsafe.Pointer {