mirror of https://bitbucket.org/ausocean/av.git
Making more C like to try and fix bugs - about to test
This commit is contained in:
parent
f4cb2700ca
commit
b1f470344d
66
rtmp/rtmp.go
66
rtmp/rtmp.go
|
@ -33,6 +33,7 @@ package rtmp
|
|||
#cgo LDFLAGS: -lrtmp -Wl,-rpath=/usr/local/lib
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <rtmp.h>
|
||||
|
||||
RTMP* start_session(RTMP* rtmp, char* url, uint connect_timeout);
|
||||
|
@ -110,17 +111,20 @@ func (s *session) Write(data []byte) (int, error) {
|
|||
if C.RTMP_IsConnected(s.rtmp) <= 0 {
|
||||
return 0, Err(1)
|
||||
}
|
||||
if s.rtmpWrite(s.rtmp, data) <= 0 {
|
||||
if rtmpWrite(s.rtmp, data) <= 0 {
|
||||
return 0, Err(2)
|
||||
}
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func rtmpWrite(r *C.RTMP, buf []byte) int {
|
||||
func rtmpWrite(r *C.RTMP, data []byte) int {
|
||||
buf := (*C.char)(unsafe.Pointer(&data[0]))
|
||||
var pkt *C.RTMPPacket = &r.m_write
|
||||
var enc unsafe.Pointer
|
||||
size := len(buf)
|
||||
var pend, enc *C.char
|
||||
size := len(data)
|
||||
s2 := size
|
||||
var ret, num int
|
||||
|
||||
pkt.m_nChannel = 0x04
|
||||
pkt.m_nInfoField2 = r.m_stream_id
|
||||
|
||||
|
@ -132,20 +136,23 @@ func rtmpWrite(r *C.RTMP, buf []byte) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
if buf[0] == 'F' && buf[1] == 'L' && buf[2] == 'V' {
|
||||
buf = buf[13:]
|
||||
buf0 := *(*byte)(unsafe.Pointer(buf))
|
||||
buf1 := *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(1)))
|
||||
buf2 := *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(2)))
|
||||
if buf0 == 'F' && buf1 == 'L' && buf2 == 'V' {
|
||||
buf = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(13)))
|
||||
s2 -= 13
|
||||
}
|
||||
|
||||
buf = buf[1:]
|
||||
pkt.m_packetType = C.uchar(buf[0])
|
||||
pkt.m_nBodySize = C.AMF_DecodeInt24(byteSliceToCArr(buf))
|
||||
buf = buf[3:]
|
||||
pkt.m_nTimeStamp = C.AMF_DecodeInt24(byteSliceToCArr(buf))
|
||||
buf = buf[3:]
|
||||
buf = buf[1:]
|
||||
pkt.m_nTimeStamp |= C.uint(buf[0] << 24)
|
||||
buf = buf[3:]
|
||||
buf = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(1)))
|
||||
pkt.m_packetType = C.uchar(*(*byte)(unsafe.Pointer(buf)))
|
||||
|
||||
pkt.m_nBodySize = C.AMF_DecodeInt24(buf)
|
||||
buf = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(3)))
|
||||
pkt.m_nTimeStamp = C.AMF_DecodeInt24(buf)
|
||||
buf = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(4)))
|
||||
pkt.m_nTimeStamp |= C.uint(*(*byte)(unsafe.Pointer(buf)) << 24)
|
||||
buf = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(3)))
|
||||
s2 -= 11
|
||||
|
||||
if ((pkt.m_packetType == C.RTMP_PACKET_TYPE_AUDIO ||
|
||||
|
@ -162,40 +169,39 @@ func rtmpWrite(r *C.RTMP, buf []byte) int {
|
|||
}
|
||||
|
||||
if int(C.RTMPPacket_Alloc(pkt, pkt.m_nBodySize)) == 0 {
|
||||
log.Println("Failed to allocated packet")
|
||||
log.Println("Failed to allocate packet")
|
||||
return 0
|
||||
}
|
||||
|
||||
enc = unsafe.Pointer(pkt.m_body)
|
||||
pend := unsafe.Pointer(uintptr(enc) + uintptr(pkt.m_nBodySize))
|
||||
enc = pkt.m_body
|
||||
pend = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(enc)) +
|
||||
uintptr(pkt.m_nBodySize)))
|
||||
|
||||
if pkt.m_packetType == C.RTMP_PACKET_TYPE_INFO {
|
||||
enc = unsafe.Pointer(C.AMF_EncodeString((*C.char)(enc), (*C.char)(pend),
|
||||
&av_setDataFrame))
|
||||
pkt.m_nBytesRead = (C.uint)(uintptr(enc) -
|
||||
enc = C.AMF_EncodeString(enc, pend, &av_setDataFrame)
|
||||
pkt.m_nBytesRead = (C.uint)(uintptr(unsafe.Pointer(enc)) -
|
||||
uintptr(unsafe.Pointer(pkt.m_body)))
|
||||
}
|
||||
} else {
|
||||
enc = unsafe.Pointer(uintptr(unsafe.Pointer(pkt.m_body)) +
|
||||
uintptr(pkt.m_nBytesRead))
|
||||
enc = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(pkt.m_body)) +
|
||||
uintptr(pkt.m_nBytesRead)))
|
||||
}
|
||||
num := int(pkt.m_nBodySize - pkt.m_nBytesRead)
|
||||
num = int(pkt.m_nBodySize - pkt.m_nBytesRead)
|
||||
if num > s2 {
|
||||
num = s2
|
||||
}
|
||||
encSlice := C.GoBytes(enc, C.int(num))
|
||||
copy(encSlice, buf)
|
||||
C.memcpy(unsafe.Pointer(enc),unsafe.Pointer(buf),C.ulong(num))
|
||||
pkt.m_nBytesRead += C.uint(num)
|
||||
s2 -= num
|
||||
buf = buf[num:]
|
||||
buf = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(num)))
|
||||
if pkt.m_nBytesRead == pkt.m_nBodySize {
|
||||
ret := int(C.RTMP_SendPacket(r, pkt, 0))
|
||||
ret = int(C.RTMP_SendPacket(r, pkt, 0))
|
||||
C.RTMPPacket_Free(pkt)
|
||||
pkt.m_nBytesRead = 0
|
||||
if ret == 0 {
|
||||
return -1
|
||||
}
|
||||
buf = buf[4:]
|
||||
buf = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(4)))
|
||||
s2 -= 4
|
||||
if s2 < 0 {
|
||||
break
|
||||
|
@ -205,12 +211,14 @@ func rtmpWrite(r *C.RTMP, buf []byte) int {
|
|||
return size + s2
|
||||
}
|
||||
|
||||
/*
|
||||
func sendPacket(r *C.RTMP, pkt *C.RTMPPacket, queue int) int {
|
||||
const prevPacket *C.RTMPPacket
|
||||
last := 0
|
||||
var nSize, hSize, cSize int
|
||||
var
|
||||
}
|
||||
*/
|
||||
|
||||
// Close terminates the rtmp connection
|
||||
func (s *session) Close() error {
|
||||
|
|
Loading…
Reference in New Issue