mirror of https://bitbucket.org/ausocean/av.git
rtmp: remove unsafe use from rtmp.go
This commit is contained in:
parent
9c72a93763
commit
fcfdd69b8e
87
rtmp/rtmp.go
87
rtmp/rtmp.go
|
@ -43,48 +43,8 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const _Gi = 1 << 30
|
||||
|
||||
func pp2b(b, e *byte) []byte {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
base := unsafe.Pointer(b)
|
||||
len := uintptr(unsafe.Pointer(e)) - uintptr(base)
|
||||
return (*[_Gi]byte)(base)[:len]
|
||||
}
|
||||
|
||||
func b2pp(buf []byte) (b, e *byte) {
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if len(buf) == 0 {
|
||||
b = *(**byte)(unsafe.Pointer(&buf))
|
||||
return b, b
|
||||
}
|
||||
b = &buf[0]
|
||||
e = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + uintptr(len(buf))))
|
||||
return b, e
|
||||
}
|
||||
|
||||
func pl2b(b *byte, l int) []byte {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
base := unsafe.Pointer(b)
|
||||
return (*[_Gi]byte)(base)[:l]
|
||||
}
|
||||
|
||||
func bAddr(buf []byte) *byte {
|
||||
if len(buf) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &buf[0]
|
||||
}
|
||||
|
||||
const (
|
||||
minDataSize = 11
|
||||
debugMode = false
|
||||
|
@ -1355,7 +1315,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
|
|||
var origIdx int
|
||||
if packet.m_body != nil {
|
||||
// Span from -packetsize for the type to the start of the body.
|
||||
headBytes = packet.m_header[:RTMP_MAX_HEADER_SIZE]
|
||||
headBytes = packet.m_header
|
||||
origIdx = RTMP_MAX_HEADER_SIZE - packetSize[packet.m_headerType]
|
||||
} else {
|
||||
// Allocate a new header and allow 6 bytes of movement backward.
|
||||
|
@ -1437,54 +1397,49 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
|
|||
}
|
||||
|
||||
nSize := int(packet.m_nBodySize)
|
||||
buffer := &packet.m_body[0]
|
||||
nChunkSize := int(r.m_outChunkSize)
|
||||
|
||||
if debugMode {
|
||||
log.Printf("C_RTMP_SendPacket: fd=%v, size=%v", r.m_sb.sb_socket, nSize)
|
||||
}
|
||||
|
||||
header := bAddr(headBytes[origIdx:])
|
||||
for (nSize + hSize) != 0 {
|
||||
if nSize < nChunkSize {
|
||||
// TODO(kortschak): Rewrite this horrific peice of premature optimisation.
|
||||
for nSize+hSize != 0 {
|
||||
if nChunkSize > nSize {
|
||||
nChunkSize = nSize
|
||||
}
|
||||
|
||||
if !C_WriteN(r, pl2b(header, nChunkSize+hSize)) {
|
||||
if !C_WriteN(r, headBytes[origIdx:][:nChunkSize+hSize]) {
|
||||
return false
|
||||
}
|
||||
n := nChunkSize + hSize // Since C_WriteN doesn't return number of bytes written.
|
||||
|
||||
nSize -= nChunkSize
|
||||
buffer = incBytePtr(buffer, nChunkSize)
|
||||
origIdx += n
|
||||
hSize = 0
|
||||
|
||||
if nSize > 0 {
|
||||
header = decBytePtr(buffer, 1)
|
||||
hSize = 1
|
||||
|
||||
if cSize != 0 {
|
||||
header = decBytePtr(header, cSize)
|
||||
hSize += cSize
|
||||
}
|
||||
origIdx -= 1 + cSize
|
||||
hSize = 1 + cSize
|
||||
|
||||
if t >= 0xffffff {
|
||||
header = decBytePtr(header, 4)
|
||||
origIdx -= 4
|
||||
hSize += 4
|
||||
}
|
||||
|
||||
*header = 0xc0 | c
|
||||
headBytes[origIdx] = 0xc0 | c
|
||||
|
||||
if cSize != 0 {
|
||||
tmp := int(packet.m_nChannel) - 64
|
||||
pl2b(incBytePtr(header, 1), 2)[1] = byte(tmp)
|
||||
headBytes[origIdx+1] = byte(tmp)
|
||||
|
||||
if cSize == 2 {
|
||||
pl2b(incBytePtr(header, 1), 3)[2] = byte(tmp >> 8)
|
||||
headBytes[origIdx+2] = byte(tmp >> 8)
|
||||
}
|
||||
}
|
||||
if t >= 0xffffff {
|
||||
extendedTimestamp := incBytePtr(header, 1+cSize)
|
||||
C_AMF_EncodeInt32(pl2b(extendedTimestamp, 4), int32(t))
|
||||
extendedTimestamp := headBytes[origIdx+1+cSize:]
|
||||
C_AMF_EncodeInt32(extendedTimestamp[:4], int32(t))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1662,18 +1617,6 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int {
|
|||
return size
|
||||
}
|
||||
|
||||
// incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions
|
||||
// from the passed ptr
|
||||
func incBytePtr(ptr *byte, inc int) *byte {
|
||||
return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) + uintptr(inc)))
|
||||
}
|
||||
|
||||
// decBytePtr returns an unsafe.Pointer to a byte that is dec negative positions
|
||||
// from ptr
|
||||
func decBytePtr(ptr *byte, dec int) *byte {
|
||||
return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) - uintptr(dec)))
|
||||
}
|
||||
|
||||
var rtmpErrs = [...]string{
|
||||
1: "rtmp: not connected",
|
||||
2: "rtmp: write error",
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
NAME
|
||||
rtmp_test.go
|
||||
|
||||
DESCRIPTION
|
||||
See Readme.md
|
||||
|
||||
AUTHOR
|
||||
Saxon Nelson-Milton <saxon@ausocean.org>
|
||||
|
||||
LICENSE
|
||||
rtmp_test.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
||||
|
||||
It is free software: you can redistribute it and/or modify them
|
||||
under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation, either version 3 of the License, or (at your
|
||||
option) any later version.
|
||||
|
||||
It is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
||||
*/
|
||||
|
||||
package rtmp
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
arrStart = 0
|
||||
arrEnd = 5
|
||||
arrSize = 6
|
||||
inc = 3
|
||||
dec = 3
|
||||
)
|
||||
|
||||
const (
|
||||
byteSize = 1
|
||||
int32Size = 4
|
||||
int64Size = 8
|
||||
)
|
||||
|
||||
var (
|
||||
byteArr = [arrSize]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
|
||||
int32Arr = [arrSize]int32{1, 2, 3, 4, 5, 6}
|
||||
int64Arr = [arrSize]int64{1, 2, 3, 4, 5, 6}
|
||||
errMsg = "Obtained: %v, but wanted: %v"
|
||||
)
|
||||
|
||||
func TestIncPtr(t *testing.T) {
|
||||
// Test how it deals with bytes
|
||||
bytePtr := unsafe.Pointer(&byteArr[arrStart])
|
||||
valueByte := *(*byte)(incPtr(bytePtr, inc, byteSize))
|
||||
if valueByte != byteArr[inc] {
|
||||
t.Errorf(errMsg, valueByte, byteArr[inc])
|
||||
}
|
||||
|
||||
// Test how it deals with int32s
|
||||
int32Ptr := unsafe.Pointer(&int32Arr[arrStart])
|
||||
valueInt32 := *(*int32)(incPtr(int32Ptr, inc, int32Size))
|
||||
if valueInt32 != int32Arr[inc] {
|
||||
t.Errorf(errMsg, valueInt32, int32Arr[inc])
|
||||
}
|
||||
|
||||
// Test how it deals with int64
|
||||
int64Ptr := unsafe.Pointer(&int64Arr[arrStart])
|
||||
valueInt64 := *(*int64)(incPtr(int64Ptr, inc, int64Size))
|
||||
if valueInt64 != int64Arr[inc] {
|
||||
t.Errorf(errMsg, valueInt64, int64Arr[inc])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecPtr(t *testing.T) {
|
||||
// Test how it deals with bytes
|
||||
bytePtr := unsafe.Pointer(&byteArr[arrEnd])
|
||||
valueByte := *(*byte)(decPtr(bytePtr, dec, byteSize))
|
||||
if valueByte != byteArr[arrEnd-dec] {
|
||||
t.Errorf(errMsg, valueByte, byteArr[inc])
|
||||
}
|
||||
|
||||
// Test how it deals with ints
|
||||
int32Ptr := unsafe.Pointer(&int32Arr[arrEnd])
|
||||
valueInt32 := *(*int32)(decPtr(int32Ptr, dec, int32Size))
|
||||
if valueInt32 != int32Arr[arrEnd-inc] {
|
||||
t.Errorf(errMsg, valueInt32, int32Arr[inc])
|
||||
}
|
||||
|
||||
// Test how it deals with int64
|
||||
int64Ptr := unsafe.Pointer(&int64Arr[arrEnd])
|
||||
valueInt64 := *(*int64)(decPtr(int64Ptr, dec, int64Size))
|
||||
if valueInt64 != int64Arr[arrEnd-dec] {
|
||||
t.Errorf(errMsg, valueInt64, int64Arr[inc])
|
||||
}
|
||||
}
|
|
@ -53,6 +53,13 @@ import (
|
|||
"github.com/chamaken/cgolmnl/inet"
|
||||
)
|
||||
|
||||
func bAddr(buf []byte) *byte {
|
||||
if len(buf) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &buf[0]
|
||||
}
|
||||
|
||||
// int add_addr_info(struct sockaddr_in *service, AVal *host, int port)
|
||||
// rtmp.c +869
|
||||
func C_add_addr_info(service *C.sockaddr_in, hostname string, port uint16) (ok bool) {
|
||||
|
|
Loading…
Reference in New Issue