mirror of https://bitbucket.org/ausocean/av.git
About to make a big change, so want to commit now
This commit is contained in:
parent
072c065224
commit
9c67bc06a3
42
rtmp/rtmp.go
42
rtmp/rtmp.go
|
@ -50,6 +50,10 @@ import (
|
||||||
"bitbucket.org/ausocean/av/tools"
|
"bitbucket.org/ausocean/av/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
minDataSize = 11
|
||||||
|
)
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -79,11 +83,14 @@ func NewSession(url string, connectTimeout uint) Session {
|
||||||
// See #define AVC(str) {str, sizeof(str)-1} in amf.h
|
// See #define AVC(str) {str, sizeof(str)-1} in amf.h
|
||||||
func AVC(str string) C.AVal {
|
func AVC(str string) C.AVal {
|
||||||
var aval C.AVal
|
var aval C.AVal
|
||||||
aval.av_val = (*C.char)(unsafe.Pointer(&([]byte(str)[0])))
|
aval.av_val = C.CString(str)
|
||||||
aval.av_len = (C.int)(int(unsafe.Sizeof([]byte(str))-1))
|
aval.av_len = C.int(len(str))
|
||||||
return aval
|
return aval
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// av_setDataFrame is a static const global in rtmp.c
|
||||||
|
var av_setDataFrame = AVC("@setDataFrame")
|
||||||
|
|
||||||
// Open establishes an rtmp connection with the url passed into the
|
// Open establishes an rtmp connection with the url passed into the
|
||||||
// constructor
|
// constructor
|
||||||
func (s *session) Open() error {
|
func (s *session) Open() error {
|
||||||
|
@ -98,10 +105,13 @@ func (s *session) Open() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func byteSliceToCArr(buf []byte) *C.char {
|
||||||
|
return (*C.char)(unsafe.Pointer(&buf[0]))
|
||||||
|
}
|
||||||
|
|
||||||
func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int {
|
func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int {
|
||||||
var pkt *C.RTMPPacket = &r.m_write
|
var pkt *C.RTMPPacket = &r.m_write
|
||||||
var pend, enc *C.char
|
var pend, enc []byte
|
||||||
size := len(buf)
|
size := len(buf)
|
||||||
s2 := size
|
s2 := size
|
||||||
var ret, num int
|
var ret, num int
|
||||||
|
@ -111,7 +121,7 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int {
|
||||||
|
|
||||||
for s2 > 0 {
|
for s2 > 0 {
|
||||||
if pkt.m_nBytesRead == 0 {
|
if pkt.m_nBytesRead == 0 {
|
||||||
if size < 11 {
|
if size < minDataSize {
|
||||||
log.Printf("size: %d\n", size)
|
log.Printf("size: %d\n", size)
|
||||||
log.Printf("too small \n")
|
log.Printf("too small \n")
|
||||||
return 0
|
return 0
|
||||||
|
@ -123,29 +133,15 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int {
|
||||||
s2 -= 13
|
s2 -= 13
|
||||||
}
|
}
|
||||||
|
|
||||||
// pkt.m_packetType = *buf++
|
|
||||||
buf = buf[1:]
|
buf = buf[1:]
|
||||||
pkt.m_packetType = C.uchar(buf[0])
|
pkt.m_packetType = C.uchar(buf[0])
|
||||||
|
pkt.m_nBodySize = C.AMF_DecodeInt24(byteSliceToCArr(buf))
|
||||||
cChar := (*C.char)(unsafe.Pointer(&buf[0]))
|
|
||||||
pkt.m_nBodySize = C.AMF_DecodeInt24(cChar)
|
|
||||||
|
|
||||||
// C: buf+=3
|
|
||||||
buf = buf[3:]
|
buf = buf[3:]
|
||||||
|
pkt.m_nTimeStamp = C.AMF_DecodeInt24(byteSliceToCArr(buf))
|
||||||
cChar = (*C.char)(unsafe.Pointer(&buf[0]))
|
|
||||||
pkt.m_nTimeStamp = C.AMF_DecodeInt24(cChar)
|
|
||||||
|
|
||||||
// C: buf+=3
|
|
||||||
buf = buf[3:]
|
buf = buf[3:]
|
||||||
|
|
||||||
// C: pkt->m_nTimeStamp |= *buf++ << 24;
|
|
||||||
buf = buf[1:]
|
buf = buf[1:]
|
||||||
pkt.m_nTimeStamp |= C.uint( buf[0] << 24 )
|
pkt.m_nTimeStamp |= C.uint( buf[0] << 24 )
|
||||||
|
|
||||||
// C: buf+=3
|
|
||||||
buf = buf[3:]
|
buf = buf[3:]
|
||||||
|
|
||||||
s2 -= 11
|
s2 -= 11
|
||||||
|
|
||||||
if ((pkt.m_packetType == C.RTMP_PACKET_TYPE_AUDIO ||
|
if ((pkt.m_packetType == C.RTMP_PACKET_TYPE_AUDIO ||
|
||||||
|
@ -161,7 +157,7 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// C: if (!RTMPPacket_Alloc(pkt, pkt->m_nBodySize))
|
// C: if (!RTMPPacket_Alloc(pkt, pkt->m_nBodySize))
|
||||||
if !tools.IntToBool(int( C.RTMPPacket_Alloc(pkt, pkt.m_nBodySize))) {
|
if int( C.RTMPPacket_Alloc(pkt, pkt.m_nBodySize)) == 0 {
|
||||||
log.Println("Failed to allocated packet")
|
log.Println("Failed to allocated packet")
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -171,10 +167,8 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int {
|
||||||
// pend = enc + pkt.m_nBodySize
|
// pend = enc + pkt.m_nBodySize
|
||||||
pend = (*C.char)(unsafe.Pointer( uintptr(unsafe.Pointer(enc)) +
|
pend = (*C.char)(unsafe.Pointer( uintptr(unsafe.Pointer(enc)) +
|
||||||
( unsafe.Sizeof(C.char(0x00)) * (uintptr(pkt.m_nBodySize)) ) ))
|
( unsafe.Sizeof(C.char(0x00)) * (uintptr(pkt.m_nBodySize)) ) ))
|
||||||
if pkt.m_packetType == C.RTMP_PACKET_TYPE_INFO {
|
|
||||||
|
|
||||||
// av_setDataFrame is a static const global in rtmp.c
|
if pkt.m_packetType == C.RTMP_PACKET_TYPE_INFO {
|
||||||
av_setDataFrame := AVC("@setDataFrame")
|
|
||||||
enc = C.AMF_EncodeString(enc, pend, &av_setDataFrame )
|
enc = C.AMF_EncodeString(enc, pend, &av_setDataFrame )
|
||||||
|
|
||||||
// C-code: pkt.m_nBytesRead enc - pkt.m_body
|
// C-code: pkt.m_nBytesRead enc - pkt.m_body
|
||||||
|
|
Loading…
Reference in New Issue