From d00a2994e3ded26d971b15f96992dcd9c92054ac Mon Sep 17 00:00:00 2001 From: saxon Date: Sun, 22 Jul 2018 23:01:50 +0930 Subject: [PATCH] Ported RTMP_ConnectStream and tested --- rtmp/rtmp.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 62cc8175..02811cb8 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -385,7 +385,7 @@ func startSession(rtmp *C.RTMP, u string, timeout uint32) (*C.RTMP, error) { } // TODO: port this - if C.RTMP_ConnectStream(rtmp, 0) == 0 { + if rtmpConnectStream(rtmp, 0) == 0 { //C.RTMP_Close(rtmp) //C.RTMP_Free(rtmp) return nil, errors.New("rtmp startSession: Failed to connect stream!") @@ -600,14 +600,14 @@ func rtmpConnect(r *C.RTMP, cp *C.RTMPPacket) int { } // TODO: port this - if C.RTMP_Connect0(r, (*C.sockaddr)(unsafe.Pointer(&service))) == 0 { + if rtmpConnect0(r, (*C.sockaddr)(unsafe.Pointer(&service))) == 0 { return 0 } r.m_bSendCounter = 1 // TODO: port this - return int(C.RTMP_Connect1(r, cp)) + return int(rtmpConnect1(r, cp)) } func rtmpConnect0(r *C.RTMP, service *C.sockaddr) int { @@ -639,8 +639,7 @@ func rtmpConnect0(r *C.RTMP, service *C.sockaddr) int { } { - //C.SET_RCVTIMEO(tv, r.Link.timeout) - tv := r.Link.timeout * 1000 + tv := C.int(r.Link.timeout * 1000) if C.setsockopt(r.m_sb.sb_socket, C.SOL_SOCKET, C.SO_RCVTIMEO, unsafe.Pointer(&tv), C.uint(unsafe.Sizeof(tv))) != 0 { @@ -654,6 +653,25 @@ func rtmpConnect0(r *C.RTMP, service *C.sockaddr) int { return 1 } +func rtmpConnect1(r *C.RTMP, cp *C.RTMPPacket) int { + if debugMode { + log.Println("... connected, handshaking...") + } + if C.HandShake(r, 1) == 0 { + log.Println("rtmpConnect1: handshake failed!") + return 0 + } + if debugMode { + log.Println("... handshaked...") + } + + if C.SendConnectPacket(r, cp) == 0 { + log.Println("RTMP connect failed!") + return 0 + } + return 1 +} + /* func socksSetup(r *RTMP, sockshost *AVal) { if sockshost.av_len != 0 { @@ -662,6 +680,49 @@ func socksSetup(r *RTMP, sockshost *AVal) { } } */ + +func rtmpConnectStream(r *C.RTMP, seekTime int32) int { + var packet C.RTMPPacket + + if seekTime > 0 { + r.Link.seekTime = C.int(seekTime) + } + + r.m_mediaChannel = 0 + + // TODO: port is connected and read packet + for r.m_bPlaying == 0 && C.RTMP_IsConnected(r) != 0 && + C.RTMP_ReadPacket(r, &packet) != 0 { + + // TODO: port is ready + if rtmpPacketIsReady(&packet) != 0 { + if packet.m_nBodySize == 0 { + continue + } + + if packet.m_packetType == RTMP_PACKET_TYPE_AUDIO || + packet.m_packetType == RTMP_PACKET_TYPE_VIDEO || + packet.m_packetType == RTMP_PACKET_TYPE_INFO { + log.Println("rtmpConnectStream: got packet before play()! Ignoring.") + C.RTMPPacket_Free(&packet) + continue + } + + // TODO: port this + C.RTMP_ClientPacket(r, &packet) + C.RTMPPacket_Free(&packet) + } + } + return int(r.m_bPlaying) +} + +func rtmpPacketIsReady(p *C.RTMPPacket) int { + if p.m_nBytesRead == p.m_nBodySize { + return 1 + } + return 0 +} + func endSession(rtmp *C.RTMP) uint32 { if rtmp == nil { return 3