From 834c2bc632fc0ff218caaf7cdb334b16731a5a3c Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 7 Sep 2018 20:40:46 +0930 Subject: [PATCH] rtmp: remove C_AVal --- rtmp/amf_headers.go | 37 --------------- rtmp/parseurl.go | 23 +++++---- rtmp/rtmp.go | 108 ++++++++++++++++++------------------------- rtmp/rtmp_headers.go | 30 ++++++------ 4 files changed, 70 insertions(+), 128 deletions(-) diff --git a/rtmp/amf_headers.go b/rtmp/amf_headers.go index f706fdb1..8ca2d429 100644 --- a/rtmp/amf_headers.go +++ b/rtmp/amf_headers.go @@ -31,11 +31,6 @@ LICENSE */ package rtmp -import ( - "fmt" - "unsafe" -) - const ( AMF_NUMBER = iota AMF_BOOLEAN @@ -62,38 +57,6 @@ const ( // amf.h +40 type C_AMFDataType int32 -// typedef struct C_AVal -// amf.h +57 -type C_AVal struct { - av_val *byte - av_len int32 -} - -func (s *C_AVal) String() string { return CAV(s) } - -// C_AVal is in amf.h -// amf.h +62 -func AVC(str string) C_AVal { - var aval C_AVal - if len(str) != 0 { - aval.av_val = &([]byte(str)[0]) - } else { - aval.av_val = nil - } - aval.av_len = int32(len(str)) - return aval -} - -func CAV(av *C_AVal) string { - if av.av_len <= 0 || av.av_val == nil { - if av.av_len < 0 { - panic(fmt.Sprintf("invalid length: %d", av.av_len)) - } - return "" - } - return string((*[_Gi]byte)(unsafe.Pointer(av.av_val))[:av.av_len]) -} - // typedef struct AMF_Object // amf.h +67 type C_AMFObject struct { diff --git a/rtmp/parseurl.go b/rtmp/parseurl.go index 19df4d19..5c8872a5 100644 --- a/rtmp/parseurl.go +++ b/rtmp/parseurl.go @@ -42,7 +42,7 @@ import ( // int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, AVal *playpath, AVal *app); // parseurl.c +33 -func C_RTMP_ParseURL(addr string) (protocol int32, host C_AVal, port uint16, playpath, app C_AVal, ok bool) { +func C_RTMP_ParseURL(addr string) (protocol int32, host string, port uint16, app, playpath string, ok bool) { u, err := url.Parse(addr) if err != nil { log.Printf("failed to parse addr: %v", err) @@ -69,7 +69,7 @@ func C_RTMP_ParseURL(addr string) (protocol int32, host C_AVal, port uint16, pla return protocol, host, port, app, playpath, false } - host = AVC(u.Host) + host = u.Host if p := u.Port(); p != "" { pi, err := strconv.Atoi(p) if err != nil { @@ -79,25 +79,24 @@ func C_RTMP_ParseURL(addr string) (protocol int32, host C_AVal, port uint16, pla } if !path.IsAbs(u.Path) { - return protocol, host, port, playpath, app, true + return protocol, host, port, app, playpath, true } elems := strings.SplitN(u.Path[1:], "/", 3) - app = AVC(elems[0]) - playpath = AVC(elems[1]) + app = elems[0] + playpath = elems[1] if len(elems) == 3 && len(elems[2]) != 0 { - inst := path.Join(elems[1:]...) - switch ext := path.Ext(inst); ext { + playpath = path.Join(elems[1:]...) + switch ext := path.Ext(playpath); ext { case ".f4v", ".mp4": - inst = "mp4:" + inst[:len(inst)-len(ext)] + playpath = "mp4:" + playpath[:len(playpath)-len(ext)] case ".mp3": - inst = "mp3:" + inst[:len(inst)-len(ext)] + playpath = "mp3:" + playpath[:len(playpath)-len(ext)] case ".flv": - inst = inst[:len(inst)-len(ext)] + playpath = playpath[:len(playpath)-len(ext)] } if u.RawQuery != "" { - inst += "?" + u.RawQuery + playpath += "?" + u.RawQuery } - playpath = AVC(inst) } return protocol, host, port, app, playpath, true diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 61484af0..00528432 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -52,6 +52,7 @@ import ( "log" "math/rand" "strconv" + "strings" "time" "unsafe" @@ -263,31 +264,21 @@ func C_RTMP_SetBufferMS(r *C_RTMP, size int32) { // void SocksSetup(RTMP *r, C_AVal* sockshost); // rtmp.c +410 -func C_SocksSetup(r *C_RTMP, sockshost *C_AVal) { - if sockshost.av_len != 0 { - socksport := strchr((*byte)(unsafe.Pointer(sockshost.av_val)), ':') - hostname := strdup((*byte)(unsafe.Pointer(sockshost.av_val))) - - if unsafe.Pointer(socksport) != nil { - (*[_Gi]byte)(unsafe.Pointer(hostname))[uintptr(decBytePtr(unsafe.Pointer(socksport), - int(uintptr(unsafe.Pointer(sockshost.av_val)))))] = '\000' - r.Link.sockshost.av_val = (*byte)(unsafe.Pointer(hostname)) - r.Link.sockshost.av_len = int32(strlen(hostname)) - - value, err := strconv.Atoi(string((*[_Gi]byte)(unsafe.Pointer(uintptr( - unsafe.Pointer(socksport)) + uintptr(1)))[:strlen((*byte)(unsafe.Pointer( - uintptr(unsafe.Pointer(socksport))+uintptr(1))))+1])) +func C_SocksSetup(r *C_RTMP, sockshost string) { + if sockshost != "" { + p := strings.SplitN(sockshost, ":", 2) + r.Link.sockshost = p[0] + r.Link.socksport = 1080 + if len(p) != 1 { + port, err := strconv.Atoi(p[1]) if err != nil { + port = 1080 log.Println("C_SocksSetup: bad string conversion!") } - if uintptr(unsafe.Pointer(socksport)) == 0 { - value = 1080 - } - r.Link.socksport = uint16(value) + r.Link.socksport = uint16(port) } } else { - r.Link.sockshost.av_val = nil - r.Link.sockshost.av_len = 0 + r.Link.sockshost = "" r.Link.socksport = 0 } } @@ -302,17 +293,17 @@ func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) { } r.Link.playpath = r.Link.playpath0 - if r.Link.tcUrl.av_len == 0 { - if r.Link.app.av_len != 0 { - r.Link.tcUrl = AVC(fmt.Sprintf("%v://%v:%v/%v", - RTMPProtocolStringsLower[r.Link.protocol], &r.Link.hostname, r.Link.port, &r.Link.app)) + if r.Link.tcUrl == "" { + if r.Link.app != "" { + r.Link.tcUrl = fmt.Sprintf("%v://%v:%v/%v", + RTMPProtocolStringsLower[r.Link.protocol], r.Link.hostname, r.Link.port, r.Link.app) r.Link.lFlags |= RTMP_LF_FTCU } else { - r.Link.tcUrl = AVC(addr) + r.Link.tcUrl = addr } } - C_SocksSetup(r, &r.Link.sockshost) + C_SocksSetup(r, r.Link.sockshost) if r.Link.port == 0 { switch { @@ -329,20 +320,11 @@ func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) { // int add_addr_info(struct sockaddr_in *service, AVal *host, int port) // rtmp.c +869 -func C_add_addr_info(service *C.sockaddr_in, host *C_AVal, port C.int) (ok bool) { - var hostname *byte - if (*[1 << 20]byte)(unsafe.Pointer(host.av_val))[host.av_len] != 0 { - name := make([]byte, host.av_len+1) - copy(name, (*[1 << 20]byte)(unsafe.Pointer(host.av_val))[:host.av_len]) - name[len(name)-1] = 0 - hostname = (*byte)(unsafe.Pointer(&name[0])) - } else { - hostname = host.av_val - } - - service.sin_addr.s_addr = C.inet_addr((*C.char)(unsafe.Pointer(hostname))) +func C_add_addr_info(service *C.sockaddr_in, hostname string, port uint16) (ok bool) { + h := (*C.char)(unsafe.Pointer(goStrToCStr(hostname))) + service.sin_addr.s_addr = C.inet_addr(h) if service.sin_addr.s_addr == C.INADDR_NONE { - host := C.gethostbyname((*C.char)(unsafe.Pointer(hostname))) + host := C.gethostbyname(h) if host == nil || *host.h_addr_list == nil { //RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname) return false @@ -350,7 +332,7 @@ func C_add_addr_info(service *C.sockaddr_in, host *C_AVal, port C.int) (ok bool) service.sin_addr = *(*C.struct_in_addr)(unsafe.Pointer(*host.h_addr_list)) } - service.sin_port = C.ushort(inet.Htons(uint16(port))) + service.sin_port = C.ushort(inet.Htons(port)) return true } @@ -426,7 +408,7 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { // TODO: port this var service C.sockaddr_in - if r.Link.hostname.av_len == 0 { + if r.Link.hostname == "" { return false } memset((*byte)(unsafe.Pointer(&service)), 0, int(unsafe.Sizeof(service))) @@ -435,12 +417,12 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { service.sin_family = C.AF_INET if r.Link.socksport != 0 { - if !C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.sockshost)), C.int(r.Link.socksport)) { + if !C_add_addr_info(&service, r.Link.sockshost, r.Link.socksport) { return false } } else { // connect directly - if !C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.hostname)), C.int(r.Link.port)) { + if !C_add_addr_info(&service, r.Link.hostname, r.Link.port) { return false } } @@ -461,7 +443,7 @@ func C_SocksNegotiate(r *C_RTMP) (ok bool) { memset((*byte)(unsafe.Pointer(&service)), 0, int(unsafe.Sizeof(service))) - C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.hostname)), C.int(r.Link.port)) + C_add_addr_info(&service, r.Link.hostname, r.Link.port) addr = int32(inet.Htonl(uint32(service.sin_addr.s_addr))) { @@ -711,7 +693,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(enc), 1))) - enc = C_AMF_EncodeNamedString(enc, pend, av_app, CAV(&r.Link.app)) + enc = C_AMF_EncodeNamedString(enc, pend, av_app, r.Link.app) if enc == nil { return false } @@ -722,21 +704,21 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } } - if r.Link.flashVer.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_flashVer, CAV(&r.Link.flashVer)) + if r.Link.flashVer != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_flashVer, r.Link.flashVer) if enc == nil { return false } } - if r.Link.swfUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_swfUrl, CAV(&r.Link.swfUrl)) + if r.Link.swfUrl != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_swfUrl, r.Link.swfUrl) if enc == nil { return false } } - if r.Link.tcUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_tcUrl, CAV(&r.Link.tcUrl)) + if r.Link.tcUrl != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_tcUrl, r.Link.tcUrl) if enc == nil { return false } @@ -763,8 +745,8 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { if enc == nil { return false } - if r.Link.pageUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_pageUrl, CAV(&r.Link.pageUrl)) + if r.Link.pageUrl != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_pageUrl, r.Link.pageUrl) if enc == nil { return false } @@ -791,12 +773,12 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) /* add auth string */ - if r.Link.auth.av_len != 0 { + if r.Link.auth != "" { enc = C_AMF_EncodeBoolean(enc, pend, r.Link.lFlags&RTMP_LF_AUTH != 0) if enc == nil { return false } - enc = C_AMF_EncodeString(enc, (*byte)(pend), CAV(&r.Link.auth)) + enc = C_AMF_EncodeString(enc, (*byte)(pend), r.Link.auth) if enc == nil { return false } @@ -873,7 +855,7 @@ func C_SendReleaseStream(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false } @@ -906,7 +888,7 @@ func C_SendFCPublish(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false } @@ -939,7 +921,7 @@ func C_SendFCUnpublish(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false @@ -974,7 +956,7 @@ func C_SendPublish(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false @@ -1157,7 +1139,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { //methodInvoked.av_val); switch methodInvoked { case av_connect: - if r.Link.token.av_len != 0 { + if r.Link.token != "" { panic("No support for link token") } @@ -1907,14 +1889,12 @@ func C_CloseInternal(r *C_RTMP, reconnect int32) { r.m_unackd = 0 if ((r.Link.lFlags & RTMP_LF_FTCU) != 0) && (reconnect == 0) { - //C.free(unsafe.Pointer(r.Link.app.av_val)) - r.Link.app.av_val = nil + r.Link.app = "" r.Link.lFlags ^= RTMP_LF_FAPU } if reconnect == 0 { - //C.free(unsafe.Pointer(r.Link.playpath0.av_val)) - r.Link.playpath0.av_val = nil + r.Link.playpath0 = "" } } diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index a6a9ec83..d9888681 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -154,21 +154,21 @@ func C_RTMPPacket_IsReady(p *C_RTMPPacket) bool { // typedef struct RTMP_LNK // rtmp.h +144 type C_RTMP_LNK struct { - hostname C_AVal - sockshost C_AVal - playpath0 C_AVal - playpath C_AVal - tcUrl C_AVal - swfUrl C_AVal - pageUrl C_AVal - app C_AVal - auth C_AVal - flashVer C_AVal - subscribepath C_AVal - usherToken C_AVal - token C_AVal - pubUser C_AVal - pubPasswd C_AVal + hostname string + sockshost string + playpath0 string + playpath string + tcUrl string + swfUrl string + pageUrl string + app string + auth string + flashVer string + subscribepath string + usherToken string + token string + pubUser string + pubPasswd string extras C_AMFObject edepth int32 seekTime int32