2018-05-29 08:56:36 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
rtmp.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
2018-08-24 03:55:36 +03:00
|
|
|
AUTHORS
|
2018-05-29 08:56:36 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
rtmp.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
|
2018-08-30 14:01:19 +03:00
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2018-08-23 23:44:37 +03:00
|
|
|
|
2018-08-30 14:01:19 +03:00
|
|
|
Derived from librtmp under the GNU Lesser General Public License 2.1
|
|
|
|
Copyright (C) 2005-2008 Team XBMC http://www.xbmc.org
|
|
|
|
Copyright (C) 2008-2009 Andrej Stepanchuk
|
|
|
|
Copyright (C) 2009-2010 Howard Chu
|
2018-05-29 08:56:36 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
2018-09-06 05:56:35 +03:00
|
|
|
"bytes"
|
2018-09-14 14:58:19 +03:00
|
|
|
"encoding/binary"
|
2018-07-16 06:30:58 +03:00
|
|
|
"errors"
|
2018-07-18 05:51:31 +03:00
|
|
|
"fmt"
|
2019-01-06 02:41:31 +03:00
|
|
|
"io"
|
2018-07-16 06:30:58 +03:00
|
|
|
"log"
|
2018-07-24 15:00:50 +03:00
|
|
|
"math/rand"
|
2019-01-06 07:12:51 +03:00
|
|
|
"net"
|
2018-07-14 08:29:00 +03:00
|
|
|
"strconv"
|
2018-08-11 05:51:22 +03:00
|
|
|
"time"
|
2018-05-29 08:56:36 +03:00
|
|
|
)
|
|
|
|
|
2018-08-08 10:36:16 +03:00
|
|
|
const (
|
|
|
|
minDataSize = 11
|
|
|
|
debugMode = false
|
2018-08-24 00:28:22 +03:00
|
|
|
length = 512
|
2018-07-13 18:43:17 +03:00
|
|
|
)
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
const (
|
|
|
|
// av_setDataFrame is a static const global in rtmp.c
|
|
|
|
setDataFrame = "@setDataFrame"
|
|
|
|
|
|
|
|
av__checkbw = "_checkbw"
|
|
|
|
av__onbwcheck = "_onbwcheck"
|
|
|
|
av__onbwdone = "_onbwdone"
|
|
|
|
av__result = "_result"
|
|
|
|
av_app = "app"
|
|
|
|
av_audioCodecs = "audioCodecs"
|
|
|
|
av_capabilities = "capabilities"
|
|
|
|
av_close = "close"
|
|
|
|
av_code = "code"
|
2018-09-06 12:30:28 +03:00
|
|
|
av_connect = "connect"
|
|
|
|
av_createStream = "createStream"
|
2018-09-06 12:01:14 +03:00
|
|
|
av_deleteStream = "deleteStream"
|
|
|
|
av_FCPublish = "FCPublish"
|
|
|
|
av_FCUnpublish = "FCUnpublish"
|
|
|
|
av_flashVer = "flashVer"
|
|
|
|
av_fpad = "fpad"
|
|
|
|
av_level = "level"
|
|
|
|
av_live = "live"
|
|
|
|
av_NetConnection_Connect_InvalidApp = "NetConnection.Connect.InvalidApp"
|
|
|
|
av_NetStream_Failed = "NetStream.Failed"
|
|
|
|
av_NetStream_Pause_Notify = "NetStream.Pause.Notify"
|
|
|
|
av_NetStream_Play_Complete = "NetStream.Play.Complete"
|
|
|
|
av_NetStream_Play_Failed = "NetStream.Play.Failed"
|
|
|
|
av_NetStream_Play_PublishNotify = "NetStream.Play.PublishNotify"
|
|
|
|
av_NetStream_Play_Start = "NetStream.Play.Start"
|
|
|
|
av_NetStream_Play_Stop = "NetStream.Play.Stop"
|
|
|
|
av_NetStream_Play_StreamNotFound = "NetStream.Play.StreamNotFound"
|
|
|
|
av_NetStream_Play_UnpublishNotify = "NetStream.Play.UnpublishNotify"
|
|
|
|
av_NetStream_Publish_Start = "NetStream.Publish.Start"
|
|
|
|
av_NetStream_Seek_Notify = "NetStream.Seek.Notify"
|
|
|
|
av_nonprivate = "nonprivate"
|
|
|
|
av_objectEncoding = "objectEncoding"
|
|
|
|
av_onBWDone = "onBWDone"
|
|
|
|
av_onFCSubscribe = "onFCSubscribe"
|
|
|
|
av_onFCUnsubscribe = "onFCUnsubscribe"
|
|
|
|
av_onStatus = "onStatus"
|
|
|
|
av_pageUrl = "pageUrl"
|
|
|
|
av_ping = "ping"
|
2018-09-06 12:30:28 +03:00
|
|
|
av_play = "play"
|
2018-09-06 12:01:14 +03:00
|
|
|
av_playlist_ready = "playlist_ready"
|
2018-09-06 12:30:28 +03:00
|
|
|
av_publish = "publish"
|
2018-09-06 12:01:14 +03:00
|
|
|
av_releaseStream = "releaseStream"
|
2018-09-06 12:30:28 +03:00
|
|
|
av_secureToken = "secureToken"
|
|
|
|
av_set_playlist = "set_playlist"
|
2018-09-06 12:01:14 +03:00
|
|
|
av_swfUrl = "swfUrl"
|
|
|
|
av_tcUrl = "tcUrl"
|
|
|
|
av_type = "type"
|
|
|
|
av_videoCodecs = "videoCodecs"
|
|
|
|
av_videoFunction = "videoFunction"
|
2018-07-26 10:36:44 +03:00
|
|
|
)
|
2018-07-16 06:30:58 +03:00
|
|
|
|
2018-08-24 00:28:22 +03:00
|
|
|
var RTMPT_cmds = []string{
|
|
|
|
"open",
|
|
|
|
"send",
|
|
|
|
"idle",
|
|
|
|
"close",
|
|
|
|
}
|
|
|
|
|
2018-08-14 20:39:57 +03:00
|
|
|
var (
|
|
|
|
packetSize = [...]int{12, 8, 4, 1}
|
|
|
|
RTMPProtocolStringsLower = [...]string{
|
|
|
|
"rtmp",
|
|
|
|
"rtmpt",
|
|
|
|
"rtmpe",
|
|
|
|
"rtmpte",
|
|
|
|
"rtmps",
|
|
|
|
"rtmpts",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"rtmfp",
|
|
|
|
}
|
|
|
|
)
|
2018-07-19 18:35:21 +03:00
|
|
|
|
2019-01-06 01:50:32 +03:00
|
|
|
var (
|
2019-01-06 04:36:16 +03:00
|
|
|
errUnknownScheme = errors.New("rtmp: unknown scheme")
|
|
|
|
errHandshake = errors.New("rtmp: handshake failed")
|
|
|
|
errConnSend = errors.New("rtmp: connection send error")
|
|
|
|
errConnStream = errors.New("rtmp: connection stream error")
|
2019-01-06 07:12:51 +03:00
|
|
|
errInvalidHeader = errors.New("rtmp: invalid header")
|
|
|
|
errInvalidBody = errors.New("rtmp: invalid body")
|
|
|
|
errTinyPacket = errors.New("rtmp: packet too small")
|
|
|
|
errEncoding = errors.New("rtmp: encoding error")
|
|
|
|
errDecoding = errors.New("rtmp: decoding error")
|
|
|
|
errCopying = errors.New("rtmp: copying error")
|
2019-01-06 01:50:32 +03:00
|
|
|
)
|
|
|
|
|
2019-01-06 02:07:21 +03:00
|
|
|
func startSession(rtmp *C_RTMP, u string, timeout uint) (*C_RTMP, error) {
|
2018-08-06 09:12:16 +03:00
|
|
|
rtmp = C_RTMP_Alloc()
|
2018-08-07 05:34:04 +03:00
|
|
|
C_RTMP_Init(rtmp)
|
2019-01-06 02:07:21 +03:00
|
|
|
rtmp.Link.timeout = timeout
|
2019-01-06 01:50:32 +03:00
|
|
|
err := C_RTMP_SetupURL(rtmp, u)
|
|
|
|
if err != nil {
|
2018-08-25 17:19:54 +03:00
|
|
|
C_RTMP_Close(rtmp)
|
2019-01-06 01:50:32 +03:00
|
|
|
return nil, err
|
2018-07-19 09:35:14 +03:00
|
|
|
}
|
|
|
|
|
2018-08-07 05:37:42 +03:00
|
|
|
C_RTMP_EnableWrite(rtmp)
|
2018-08-07 05:38:48 +03:00
|
|
|
C_RTMP_SetBufferMS(rtmp, 3600*1000)
|
2019-01-06 01:50:32 +03:00
|
|
|
err = C_RTMP_Connect(rtmp, nil)
|
2019-01-05 15:43:19 +03:00
|
|
|
if err != nil {
|
2018-08-25 17:19:54 +03:00
|
|
|
C_RTMP_Close(rtmp)
|
2019-01-06 01:50:32 +03:00
|
|
|
return nil, err
|
2018-07-19 09:35:14 +03:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:50:32 +03:00
|
|
|
err = C_RTMP_ConnectStream(rtmp, 0)
|
|
|
|
if err != nil {
|
2018-08-25 17:19:54 +03:00
|
|
|
C_RTMP_Close(rtmp)
|
2019-01-06 01:50:32 +03:00
|
|
|
return nil, err
|
2018-07-19 09:35:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rtmp, nil
|
|
|
|
}
|
2018-08-07 07:49:11 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
func endSession(rtmp *C_RTMP) uint32 {
|
|
|
|
if rtmp == nil {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
2018-08-25 17:19:54 +03:00
|
|
|
C_RTMP_Close(rtmp)
|
2018-08-24 12:20:04 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:13:34 +03:00
|
|
|
// uint32_t RTMP_GetTime();
|
|
|
|
// rtmp.c +156
|
|
|
|
func C_RTMP_GetTime() int32 {
|
|
|
|
return int32(time.Now().UnixNano() / 1000000)
|
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int RTMPPacket_Alloc(RTMPPacket* p, uint32_t nSize);
|
|
|
|
// rtmp.c +189
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_RTMPPacket_Alloc(p *C_RTMPPacket, nSize uint32) {
|
2018-09-12 09:15:14 +03:00
|
|
|
buf := make([]byte, RTMP_MAX_HEADER_SIZE+nSize)
|
2018-09-26 08:22:07 +03:00
|
|
|
p.m_header = buf
|
2018-09-19 08:01:42 +03:00
|
|
|
p.m_body = buf[RTMP_MAX_HEADER_SIZE:]
|
2018-08-24 12:20:04 +03:00
|
|
|
p.m_nBytesRead = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// void RTMPPacket_Free(RTMPPacket* p);
|
|
|
|
// rtmp.c +203
|
|
|
|
func C_RTMPPacket_Free(p *C_RTMPPacket) {
|
|
|
|
if p.m_body != nil {
|
|
|
|
p.m_body = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 07:49:11 +03:00
|
|
|
// RTMP* RTMP_IsConnected();
|
|
|
|
// rtmp.c +317
|
2018-08-18 07:21:07 +03:00
|
|
|
func C_RTMP_Alloc() *C_RTMP {
|
2018-08-25 16:02:24 +03:00
|
|
|
return &C_RTMP{}
|
2018-07-19 10:29:03 +03:00
|
|
|
}
|
2018-07-19 10:54:37 +03:00
|
|
|
|
2018-08-07 07:49:11 +03:00
|
|
|
// void RTMP_Init(RTMP *r);
|
|
|
|
// rtmp.c +329
|
2018-08-18 07:21:07 +03:00
|
|
|
func C_RTMP_Init(r *C_RTMP) {
|
2018-08-25 16:02:24 +03:00
|
|
|
*r = C_RTMP{}
|
2018-07-19 11:00:30 +03:00
|
|
|
r.m_inChunkSize = RTMP_DEFAULT_CHUNKSIZE
|
|
|
|
r.m_outChunkSize = RTMP_DEFAULT_CHUNKSIZE
|
|
|
|
r.m_nBufferMS = 30000
|
|
|
|
r.m_nClientBW = 2500000
|
|
|
|
r.m_nClientBW2 = 2
|
|
|
|
r.m_nServerBW = 2500000
|
|
|
|
r.m_fAudioCodecs = 3191.0
|
|
|
|
r.m_fVideoCodecs = 252.0
|
|
|
|
r.Link.timeout = 30
|
|
|
|
r.Link.swfAge = 30
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:13:34 +03:00
|
|
|
// void RTMP_EnableWrite(RTMP *r);
|
|
|
|
// rtmp.c +351
|
|
|
|
func C_RTMP_EnableWrite(r *C_RTMP) {
|
|
|
|
r.Link.protocol |= RTMP_FEATURE_WRITE
|
|
|
|
}
|
|
|
|
|
|
|
|
// int RTMP_IsConnected(RTMP *r);
|
|
|
|
// rtmp.c +363
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_RTMP_IsConnected(r *C_RTMP) bool {
|
2019-01-06 08:24:49 +03:00
|
|
|
return r.Link.conn != nil
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// void RTMP_SetBufferMS(RTMP *r, int size);
|
|
|
|
// rtmp.c +381
|
|
|
|
func C_RTMP_SetBufferMS(r *C_RTMP, size int32) {
|
2018-09-12 05:37:43 +03:00
|
|
|
r.m_nBufferMS = size
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// void SocksSetup(RTMP *r, C_AVal* sockshost);
|
|
|
|
// rtmp.c +410
|
2019-01-05 15:43:19 +03:00
|
|
|
// DELETED
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2018-08-07 07:49:11 +03:00
|
|
|
// int RTMP_SetupURL(RTMP *r, char* url);
|
|
|
|
// rtmp.c +757
|
2018-08-08 10:47:59 +03:00
|
|
|
// NOTE: code dealing with rtmp over http has been disregarded
|
2019-01-06 01:50:32 +03:00
|
|
|
func C_RTMP_SetupURL(r *C_RTMP, addr string) (err error) {
|
|
|
|
r.Link.protocol, r.Link.host, r.Link.port, r.Link.app, r.Link.playpath0, err = C_RTMP_ParseURL(addr)
|
|
|
|
if err != nil {
|
2019-01-06 04:36:16 +03:00
|
|
|
return err
|
2018-07-15 21:09:36 +03:00
|
|
|
}
|
2018-07-19 18:35:21 +03:00
|
|
|
r.Link.playpath = r.Link.playpath0
|
|
|
|
|
2018-09-07 14:10:46 +03:00
|
|
|
if r.Link.tcUrl == "" {
|
|
|
|
if r.Link.app != "" {
|
|
|
|
r.Link.tcUrl = fmt.Sprintf("%v://%v:%v/%v",
|
2019-01-05 15:43:19 +03:00
|
|
|
RTMPProtocolStringsLower[r.Link.protocol], r.Link.host, r.Link.port, r.Link.app)
|
2018-09-07 06:00:25 +03:00
|
|
|
r.Link.lFlags |= RTMP_LF_FTCU
|
2018-07-19 18:35:21 +03:00
|
|
|
} else {
|
2018-09-07 14:10:46 +03:00
|
|
|
r.Link.tcUrl = addr
|
2018-07-19 18:35:21 +03:00
|
|
|
}
|
2018-07-15 21:09:36 +03:00
|
|
|
}
|
2018-07-22 17:01:19 +03:00
|
|
|
|
2018-07-19 18:35:21 +03:00
|
|
|
if r.Link.port == 0 {
|
|
|
|
switch {
|
2018-07-20 08:44:39 +03:00
|
|
|
case (r.Link.protocol & RTMP_FEATURE_SSL) != 0:
|
2018-07-19 18:35:21 +03:00
|
|
|
r.Link.port = 433
|
2018-07-20 08:44:39 +03:00
|
|
|
case (r.Link.protocol & RTMP_FEATURE_HTTP) != 0:
|
2018-07-19 18:35:21 +03:00
|
|
|
r.Link.port = 80
|
|
|
|
default:
|
|
|
|
r.Link.port = 1935
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 01:50:32 +03:00
|
|
|
return nil
|
2018-07-15 21:09:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
// int RTMP_Connect(RTMP *r, RTMPPacket* cp);
|
|
|
|
// rtmp.c +1032
|
|
|
|
func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) error {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp4", r.Link.host+":"+strconv.Itoa(int(r.Link.port)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-06 08:24:49 +03:00
|
|
|
r.Link.conn, err = net.DialTCP("tcp4", nil, addr)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-22 16:31:50 +03:00
|
|
|
if debugMode {
|
|
|
|
log.Println("... connected, handshaking...")
|
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_HandShake(r, 1)
|
|
|
|
if err != nil {
|
2018-08-07 05:42:11 +03:00
|
|
|
log.Println("C_RTMP_Connect1: handshake failed!")
|
2019-01-06 01:50:32 +03:00
|
|
|
return errHandshake
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
|
|
|
if debugMode {
|
|
|
|
log.Println("... handshaked...")
|
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_SendConnectPacket(r, cp)
|
|
|
|
if err != nil {
|
2018-07-22 16:31:50 +03:00
|
|
|
log.Println("RTMP connect failed!")
|
2019-01-06 01:50:32 +03:00
|
|
|
return errConnSend
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
2019-01-06 01:50:32 +03:00
|
|
|
return nil
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
// int RTMP_Connect1(RTMP* r, RTMPPacket* cp);
|
|
|
|
// rtmp.c +978
|
|
|
|
// DELETED - subsumed by RTMP_Connect
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int RTMP_ConnectStream(RTMP* r, int seekTime);
|
|
|
|
// rtmp.c +1099
|
2019-01-06 01:50:32 +03:00
|
|
|
// Side effects: r.m_bPlaying is set true upon successful connection
|
|
|
|
func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var packet C_RTMPPacket
|
|
|
|
|
|
|
|
if seekTime > 0 {
|
2018-09-12 05:37:43 +03:00
|
|
|
r.Link.seekTime = seekTime
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
r.m_mediaChannel = 0
|
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
for !r.m_bPlaying && C_RTMP_IsConnected(r) {
|
|
|
|
err := C_RTMP_ReadPacket(r, &packet)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
// TODO: port is ready
|
2018-09-06 12:10:47 +03:00
|
|
|
if C_RTMPPacket_IsReady(&packet) {
|
2018-08-24 12:20:04 +03:00
|
|
|
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("C_RTMP_ConnectStream: got packet before play()! Ignoring.")
|
|
|
|
C_RTMPPacket_Free(&packet)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
C_RTMP_ClientPacket(r, &packet)
|
|
|
|
C_RTMPPacket_Free(&packet)
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 01:50:32 +03:00
|
|
|
|
|
|
|
if !r.m_bPlaying {
|
|
|
|
return errConnStream
|
|
|
|
}
|
|
|
|
return nil
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// int RTMP_ClientPacket()
|
|
|
|
// rtmp.c +1226
|
|
|
|
// NOTE cases have been commented out that are not currently used by AusOcean
|
|
|
|
func C_RTMP_ClientPacket(r *C_RTMP, packet *C_RTMPPacket) int32 {
|
2018-09-06 05:20:58 +03:00
|
|
|
var bHasMediaPacket int32
|
2018-08-24 12:20:04 +03:00
|
|
|
switch packet.m_packetType {
|
|
|
|
|
|
|
|
case RTMP_PACKET_TYPE_CHUNK_SIZE:
|
|
|
|
// TODO: port this
|
|
|
|
C_HandleChangeChunkSize(r, packet)
|
2018-08-25 16:52:22 +03:00
|
|
|
|
|
|
|
case RTMP_PACKET_TYPE_BYTES_READ_REPORT:
|
|
|
|
// TODO: usue new logger here
|
|
|
|
//RTMP_Log(RTMP_LOGDEBUG, "%s, received: bytes read report", __FUNCTION__);
|
|
|
|
|
|
|
|
case RTMP_PACKET_TYPE_CONTROL:
|
|
|
|
panic("Unsupported packet type RTMP_PACKET_TYPE_CONTROL")
|
2018-08-24 12:20:04 +03:00
|
|
|
/*
|
2018-08-25 16:52:22 +03:00
|
|
|
log.Println("RTMP_PACKET_TYPE_CONTROL")
|
|
|
|
// TODO: port this
|
|
|
|
C.HandleCtrl(r, packet)
|
2018-08-24 12:20:04 +03:00
|
|
|
*/
|
|
|
|
case RTMP_PACKET_TYPE_SERVER_BW:
|
|
|
|
// TODO: port this
|
|
|
|
C_HandlServerBW(r, packet)
|
|
|
|
|
|
|
|
case RTMP_PACKET_TYPE_CLIENT_BW:
|
|
|
|
// TODO: port this
|
|
|
|
C_HandleClientBW(r, packet)
|
|
|
|
|
2018-08-25 16:52:22 +03:00
|
|
|
case RTMP_PACKET_TYPE_AUDIO:
|
|
|
|
panic("Unsupported packet type RTMP_PACKET_TYPE_AUDIO")
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2018-08-25 16:52:22 +03:00
|
|
|
case RTMP_PACKET_TYPE_VIDEO:
|
|
|
|
panic("Unsupported packet type RTMP_PACKET_TYPE_VIDEO")
|
2018-08-28 12:48:13 +03:00
|
|
|
|
2018-08-25 16:52:22 +03:00
|
|
|
case RTMP_PACKET_TYPE_FLEX_MESSAGE:
|
|
|
|
panic("Unsupported packet type RTMP_PACKET_TYPE_FLEX_MESSAGE")
|
|
|
|
|
|
|
|
case RTMP_PACKET_TYPE_INFO:
|
|
|
|
panic("Unsupported packet type RTMP_PACKET_TYPE_INFO")
|
2018-08-24 12:20:04 +03:00
|
|
|
|
|
|
|
case RTMP_PACKET_TYPE_INVOKE:
|
|
|
|
log.Println("RTMP_PACKET_TYPE_INVOKE:")
|
|
|
|
// TODO use new logger here
|
|
|
|
//RTMP_Log(RTMP_LOGDEBUG, "%s, received: invoke %u bytes", __FUNCTION__,packet.m_nBodySize);
|
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
err := C_HandleInvoke(r, packet.m_body[:packet.m_nBodySize])
|
|
|
|
if err != nil {
|
2018-09-06 05:20:58 +03:00
|
|
|
// This will never happen with the methods we implement.
|
2018-08-24 12:20:04 +03:00
|
|
|
log.Println("HasMediaPacket")
|
|
|
|
bHasMediaPacket = 2
|
|
|
|
}
|
2018-08-25 16:52:22 +03:00
|
|
|
|
|
|
|
case RTMP_PACKET_TYPE_FLASH_VIDEO:
|
|
|
|
panic("Unsupported packet type RTMP_PACKET_TYPE_FLASH_VIDEO")
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2018-08-25 16:52:22 +03:00
|
|
|
default:
|
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s, unknown packet type received: 0x%02x", __FUNCTION__,packet.m_packetType);
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
return bHasMediaPacket
|
|
|
|
}
|
|
|
|
|
2018-08-07 07:49:11 +03:00
|
|
|
// int ReadN(RTMP* r, char* buffer, int n);
|
|
|
|
// rtmp.c +1390
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_ReadN(r *C_RTMP, buf []byte) error {
|
2019-01-06 08:24:49 +03:00
|
|
|
err := r.Link.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(r.Link.timeout)))
|
2019-01-06 02:41:31 +03:00
|
|
|
if err != nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
2019-01-06 02:41:31 +03:00
|
|
|
}
|
2019-01-06 08:24:49 +03:00
|
|
|
n, err := io.ReadFull(r.Link.conn, buf)
|
2019-01-06 02:41:31 +03:00
|
|
|
if err != nil {
|
|
|
|
if debugMode {
|
|
|
|
log.Printf("C_ReadN error: %v\n", err)
|
2018-07-28 07:57:30 +03:00
|
|
|
}
|
2019-01-06 02:41:31 +03:00
|
|
|
C_RTMP_Close(r)
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
2018-07-28 07:57:30 +03:00
|
|
|
}
|
2019-01-06 02:41:31 +03:00
|
|
|
r.m_nBytesIn += int32(n)
|
|
|
|
if r.m_nBytesIn > (r.m_nBytesInSent + r.m_nClientBW/10) {
|
2019-01-06 07:12:51 +03:00
|
|
|
err := C_SendBytesReceived(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 02:41:31 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-07-28 08:25:25 +03:00
|
|
|
}
|
2018-07-28 07:57:30 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int WriteN(RTMP* r, const char* buffer, int n);
|
|
|
|
// rtmp.c +1502
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_WriteN(r *C_RTMP, buf []byte) error {
|
2019-01-06 08:24:49 +03:00
|
|
|
err := r.Link.conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(r.Link.timeout)))
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-06 08:24:49 +03:00
|
|
|
_, err = r.Link.conn.Write(buf)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
if debugMode {
|
|
|
|
log.Printf("C_WriteN, RTMP send error: %v\n", err)
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
C_RTMP_Close(r)
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// int SendConnectPacket(RTMP* r, RTMPPacket* cp);
|
|
|
|
// rtmp.c +1579
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
if cp != nil {
|
|
|
|
return C_RTMP_SendPacket(r, cp, 1)
|
|
|
|
}
|
|
|
|
|
2018-09-19 06:06:39 +03:00
|
|
|
var pbuf [4096]byte
|
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x03,
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_LARGE,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:06:39 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-07-28 15:00:08 +03:00
|
|
|
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeString(enc, av_connect)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-07-25 04:26:59 +03:00
|
|
|
r.m_numInvokes += 1
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:06:39 +03:00
|
|
|
enc[0] = AMF_OBJECT
|
|
|
|
enc = enc[1:]
|
2018-07-28 15:00:08 +03:00
|
|
|
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedString(enc, av_app, r.Link.app)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
if r.Link.protocol&RTMP_FEATURE_WRITE != 0 {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedString(enc, av_type, av_nonprivate)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 14:10:46 +03:00
|
|
|
if r.Link.flashVer != "" {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedString(enc, av_flashVer, r.Link.flashVer)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-07 14:10:46 +03:00
|
|
|
if r.Link.swfUrl != "" {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedString(enc, av_swfUrl, r.Link.swfUrl)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 14:10:46 +03:00
|
|
|
if r.Link.tcUrl != "" {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedString(enc, av_tcUrl, r.Link.tcUrl)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Link.protocol&RTMP_FEATURE_WRITE == 0 {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedBoolean(enc, av_fpad, false)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedNumber(enc, av_capabilities, 15)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedNumber(enc, av_audioCodecs, r.m_fAudioCodecs)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedNumber(enc, av_videoCodecs, r.m_fVideoCodecs)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedNumber(enc, av_videoFunction, 1)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2018-09-07 14:10:46 +03:00
|
|
|
if r.Link.pageUrl != "" {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedString(enc, av_pageUrl, r.Link.pageUrl)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:37:35 +03:00
|
|
|
if r.m_fEncoding != 0.0 || r.m_bSendEncoding {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeNamedNumber(enc, av_objectEncoding, r.m_fEncoding)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 06:06:39 +03:00
|
|
|
if copy(enc, []byte{0, 0, AMF_OBJECT_END}) != 3 {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errCopying // TODO: is this even possible?
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = enc[3:]
|
2018-07-26 10:36:44 +03:00
|
|
|
|
|
|
|
/* add auth string */
|
2018-09-07 14:10:46 +03:00
|
|
|
if r.Link.auth != "" {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeBoolean(enc, r.Link.lFlags&RTMP_LF_AUTH != 0)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_EncodeString(enc, r.Link.auth)
|
2018-07-26 10:36:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 03:50:44 +03:00
|
|
|
for i := range r.Link.extras.o_props {
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = C_AMF_PropEncode(&r.Link.extras.o_props[i], enc)
|
2018-09-15 03:50:44 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 06:06:39 +03:00
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-07-26 10:36:44 +03:00
|
|
|
|
2018-08-07 06:03:40 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 1)
|
2018-07-25 04:26:59 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int RTMP_SendCreateStream(RTMP* r);
|
|
|
|
// rtmp.c +1725
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_RTMP_SendCreateStream(r *C_RTMP) error {
|
2018-08-24 04:13:34 +03:00
|
|
|
var pbuf [256]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x03, /* control channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_MEDIUM,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
enc = C_AMF_EncodeString(enc, av_createStream)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
r.m_numInvokes++
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc[0] = AMF_NULL
|
|
|
|
enc = enc[1:]
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2018-09-19 06:29:04 +03:00
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2018-09-06 05:20:58 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 1)
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int SendReleaseStream(RTMP* r);
|
|
|
|
// rtmp.c +1816
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendReleaseStream(r *C_RTMP) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [1024]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x03, /* control channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_MEDIUM,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
enc = C_AMF_EncodeString(enc, av_releaseStream)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
r.m_numInvokes++
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc[0] = AMF_NULL
|
|
|
|
enc = enc[1:]
|
|
|
|
enc = C_AMF_EncodeString(enc, r.Link.playpath)
|
2018-08-24 12:20:04 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2018-09-06 05:20:58 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 0)
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int SendFCPublish(RTMP* r);
|
|
|
|
// rtmp.c +1846
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendFCPublish(r *C_RTMP) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [1024]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x03, /* control channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_MEDIUM,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
enc = C_AMF_EncodeString(enc, av_FCPublish)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
r.m_numInvokes++
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc[0] = AMF_NULL
|
|
|
|
enc = enc[1:]
|
|
|
|
enc = C_AMF_EncodeString(enc, r.Link.playpath)
|
2018-08-24 12:20:04 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2018-09-06 05:20:58 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 0)
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
|
|
|
|
2018-08-25 17:43:08 +03:00
|
|
|
// int SendFCUnpublish(RTMP *r);
|
|
|
|
// rtmp.c +1875
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendFCUnpublish(r *C_RTMP) error {
|
2018-09-06 04:56:52 +03:00
|
|
|
var pbuf [1024]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x03, /* control channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_MEDIUM,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
enc = C_AMF_EncodeString(enc, av_FCUnpublish)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-06 04:56:52 +03:00
|
|
|
r.m_numInvokes++
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc[0] = AMF_NULL
|
|
|
|
enc = enc[1:]
|
|
|
|
enc = C_AMF_EncodeString(enc, r.Link.playpath)
|
2018-09-06 04:56:52 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-09-06 04:56:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 06:29:04 +03:00
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-09-06 04:56:52 +03:00
|
|
|
|
2018-09-06 05:20:58 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 0)
|
2018-08-25 17:43:08 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int SendPublish(RTMP* r);
|
|
|
|
// rtmp.c +1908
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendPublish(r *C_RTMP) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [1024]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x04, /* source channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_LARGE,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: r.m_stream_id,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
enc = C_AMF_EncodeString(enc, av_publish)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
r.m_numInvokes++
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc[0] = AMF_NULL
|
|
|
|
enc = enc[1:]
|
|
|
|
enc = C_AMF_EncodeString(enc, r.Link.playpath)
|
2018-08-24 12:20:04 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeString(enc, av_live)
|
2018-08-24 12:20:04 +03:00
|
|
|
if enc == nil {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errEncoding
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 06:29:04 +03:00
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-07-22 16:31:50 +03:00
|
|
|
|
2018-09-06 05:20:58 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 1)
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
|
|
|
|
2018-09-06 05:15:48 +03:00
|
|
|
// int
|
|
|
|
// SendDeleteStream(RTMP *r, double dStreamId)
|
|
|
|
// rtmp.c +1942
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendDeleteStream(r *C_RTMP, dStreamId float64) error {
|
2018-09-06 05:15:48 +03:00
|
|
|
var pbuf [256]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x03, /* control channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_MEDIUM,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
enc = C_AMF_EncodeString(enc, av_deleteStream)
|
2019-01-06 07:12:51 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-06 05:15:48 +03:00
|
|
|
r.m_numInvokes++
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:12:51 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc[0] = AMF_NULL
|
|
|
|
enc = enc[1:]
|
|
|
|
enc = C_AMF_EncodeNumber(enc, dStreamId)
|
2019-01-06 07:12:51 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-09-06 05:15:48 +03:00
|
|
|
|
|
|
|
/* no response expected */
|
2018-09-06 05:20:58 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 0)
|
2018-09-06 05:15:48 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int SendBytesReceived(RTMP* r);
|
|
|
|
// rtmp.c +2080
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendBytesReceived(r *C_RTMP) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [256]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x02, /* control channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_MEDIUM,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_BYTES_READ_REPORT,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-08-12 12:07:39 +03:00
|
|
|
|
2018-09-19 06:29:04 +03:00
|
|
|
r.m_nBytesInSent = r.m_nBytesIn
|
2019-01-06 07:49:58 +03:00
|
|
|
enc = C_AMF_EncodeInt32(enc, r.m_nBytesIn)
|
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
packet.m_nBodySize = 4
|
2018-08-12 12:07:39 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 0)
|
|
|
|
}
|
2018-08-12 13:07:56 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int SendCheckBW(RTMP* r);
|
|
|
|
// rtmp.c +2105
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_SendCheckBW(r *C_RTMP) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [256]byte
|
2018-09-19 06:29:04 +03:00
|
|
|
packet := C_RTMPPacket{
|
|
|
|
m_nChannel: 0x03, /* control channel (invoke) */
|
|
|
|
m_headerType: RTMP_PACKET_SIZE_LARGE,
|
|
|
|
m_packetType: RTMP_PACKET_TYPE_INVOKE,
|
|
|
|
m_nTimeStamp: 0,
|
|
|
|
m_nInfoField2: 0,
|
|
|
|
m_hasAbsTimestamp: false,
|
2018-09-26 08:22:07 +03:00
|
|
|
m_header: pbuf[:],
|
2018-09-19 08:01:42 +03:00
|
|
|
m_body: pbuf[RTMP_MAX_HEADER_SIZE:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
enc := packet.m_body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
|
|
|
enc = C_AMF_EncodeString(enc, av__checkbw)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
r.m_numInvokes++
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
enc[0] = AMF_NULL
|
|
|
|
enc = enc[1:]
|
2018-08-12 12:07:39 +03:00
|
|
|
|
2018-09-19 06:29:04 +03:00
|
|
|
packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
|
2018-08-12 13:17:53 +03:00
|
|
|
|
2018-09-06 05:20:58 +03:00
|
|
|
return C_RTMP_SendPacket(r, &packet, 0)
|
2018-08-12 13:17:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// void AV_erase(C_RTMP_METHOD* vals, int* num, int i, int freeit);
|
|
|
|
// rtmp.c +2393
|
2018-09-12 04:59:38 +03:00
|
|
|
func C_AV_erase(m []C_RTMP_METHOD, i int) []C_RTMP_METHOD {
|
|
|
|
copy(m[i:], m[i+1:])
|
|
|
|
m[len(m)-1] = C_RTMP_METHOD{}
|
|
|
|
return m[:len(m)-1]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:34:14 +03:00
|
|
|
// int HandleInvoke(RTMP* r, const char* body, unsigned int nBodySize);
|
|
|
|
// rtmp.c +2912
|
2019-01-06 01:50:32 +03:00
|
|
|
// Side effects: r.m_bPlaying set to true upon av_NetStream_Publish_Start
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_HandleInvoke(r *C_RTMP, body []byte) error {
|
2018-09-19 07:35:42 +03:00
|
|
|
if body[0] != 0x02 {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errInvalidBody
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-09-12 05:37:43 +03:00
|
|
|
var obj C_AMFObject
|
2018-09-19 07:35:42 +03:00
|
|
|
nRes := C_AMF_Decode(&obj, body, 0)
|
2018-08-14 11:52:37 +03:00
|
|
|
if nRes < 0 {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errDecoding
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-08-14 10:06:42 +03:00
|
|
|
|
2018-08-14 20:17:40 +03:00
|
|
|
// NOTE we don't really need this ?? still functions without it
|
|
|
|
//C.AMF_Dump(&obj)
|
2018-08-14 20:39:57 +03:00
|
|
|
//C.AMFProp_GetString(C_AMF_GetProp(&obj, nil, 0), &method)
|
2018-09-06 10:00:39 +03:00
|
|
|
method := C_AMFProp_GetString(C_AMF_GetProp(&obj, "", 0))
|
2018-09-12 05:37:43 +03:00
|
|
|
txn := C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 1))
|
2018-08-14 06:15:03 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val);
|
2018-09-06 11:33:55 +03:00
|
|
|
|
|
|
|
switch method {
|
2018-09-06 12:01:14 +03:00
|
|
|
case av__result:
|
2018-09-06 12:30:28 +03:00
|
|
|
var methodInvoked string
|
2018-09-12 04:59:38 +03:00
|
|
|
for i, m := range r.m_methodCalls {
|
|
|
|
if float64(m.num) == txn {
|
|
|
|
methodInvoked = m.name
|
|
|
|
r.m_methodCalls = C_AV_erase(r.m_methodCalls, i)
|
2018-09-06 11:33:55 +03:00
|
|
|
break
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-09-06 11:33:55 +03:00
|
|
|
}
|
2018-09-06 12:30:28 +03:00
|
|
|
if methodInvoked == "" {
|
2018-08-14 16:00:47 +03:00
|
|
|
// TODO use new logger here
|
2018-09-06 11:33:55 +03:00
|
|
|
//RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request",
|
|
|
|
//__FUNCTION__, txn);
|
|
|
|
goto leave
|
|
|
|
}
|
|
|
|
// TODO use new logger here
|
|
|
|
//RTMP_Log(RTMP_LOGDEBUG, "%s, received result for method call <%s>", __FUNCTION__,
|
|
|
|
//methodInvoked.av_val);
|
2018-09-06 12:30:28 +03:00
|
|
|
switch methodInvoked {
|
|
|
|
case av_connect:
|
2018-09-07 14:10:46 +03:00
|
|
|
if r.Link.token != "" {
|
2018-09-06 11:33:55 +03:00
|
|
|
panic("No support for link token")
|
2018-08-28 12:10:10 +03:00
|
|
|
|
2018-09-06 11:33:55 +03:00
|
|
|
}
|
|
|
|
if (r.Link.protocol & RTMP_FEATURE_WRITE) != 0 {
|
|
|
|
C_SendReleaseStream(r)
|
|
|
|
C_SendFCPublish(r)
|
|
|
|
} else {
|
|
|
|
panic("Link protocol has no RTMP_FEATURE_WRITE")
|
|
|
|
}
|
2018-08-28 12:10:10 +03:00
|
|
|
|
2018-09-06 11:33:55 +03:00
|
|
|
C_RTMP_SendCreateStream(r)
|
|
|
|
if (r.Link.protocol & RTMP_FEATURE_WRITE) == 0 {
|
|
|
|
panic("Link protocol has no RTMP_FEATURE_WRITE")
|
|
|
|
}
|
2018-08-14 16:00:47 +03:00
|
|
|
|
2018-09-06 12:30:28 +03:00
|
|
|
case av_createStream:
|
2018-09-06 11:33:55 +03:00
|
|
|
r.m_stream_id = int32(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 3)))
|
2018-08-28 12:10:10 +03:00
|
|
|
|
2018-09-06 11:33:55 +03:00
|
|
|
if r.Link.protocol&RTMP_FEATURE_WRITE != 0 {
|
|
|
|
C_SendPublish(r)
|
|
|
|
} else {
|
|
|
|
panic("Link protocol has no RTMP_FEATURE_WRITE")
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-09-06 11:33:55 +03:00
|
|
|
|
2018-09-06 12:30:28 +03:00
|
|
|
case av_play, av_publish:
|
2018-09-06 11:33:55 +03:00
|
|
|
panic("Unsupported method av_play/av_publish")
|
2018-08-13 05:45:43 +03:00
|
|
|
}
|
2018-09-06 11:33:55 +03:00
|
|
|
//C.free(unsafe.Pointer(methodInvoked.av_val))
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_onBWDone:
|
2018-08-28 12:10:10 +03:00
|
|
|
if r.m_nBWCheckCounter == 0 {
|
|
|
|
C_SendCheckBW(r)
|
2018-08-14 16:00:47 +03:00
|
|
|
}
|
2018-08-28 12:10:10 +03:00
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_onFCUnsubscribe, av_onFCSubscribe:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_onFCUnsubscribe/av_onFCSubscribe")
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_ping:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_ping")
|
2018-08-28 12:48:13 +03:00
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av__onbwcheck:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_onbwcheck")
|
2018-08-28 12:48:13 +03:00
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av__onbwdone:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_onbwdone")
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_close:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_close")
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_onStatus:
|
2018-08-28 12:10:10 +03:00
|
|
|
var obj2 C_AMFObject
|
2018-09-06 10:00:39 +03:00
|
|
|
C_AMFProp_GetObject(C_AMF_GetProp(&obj, "", 3), &obj2)
|
2018-09-06 12:01:14 +03:00
|
|
|
code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, av_code, -1))
|
2018-09-06 09:40:15 +03:00
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, av_level, -1)) // Not used.
|
2018-09-06 09:40:15 +03:00
|
|
|
_ = level
|
2018-08-28 12:10:10 +03:00
|
|
|
|
|
|
|
// TODO use new logger
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val);
|
2018-09-06 11:33:55 +03:00
|
|
|
|
|
|
|
switch code {
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_NetStream_Failed, av_NetStream_Play_Failed,
|
|
|
|
av_NetStream_Play_StreamNotFound, av_NetConnection_Connect_InvalidApp:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_NetStream/av_NetStream_Play_Failed/av_netSTream_Play_StreamNotFound/av_netConnection_Connect_invalidApp")
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_NetStream_Play_Start, av_NetStream_Play_PublishNotify:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_NetStream_Play_Start/av_NetStream_Play_PublishNotify")
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_NetStream_Publish_Start:
|
2018-09-06 05:20:58 +03:00
|
|
|
r.m_bPlaying = true
|
2018-09-12 04:59:38 +03:00
|
|
|
for i, m := range r.m_methodCalls {
|
|
|
|
if m.name == av_publish {
|
|
|
|
r.m_methodCalls = C_AV_erase(r.m_methodCalls, i)
|
2018-08-14 11:52:37 +03:00
|
|
|
break
|
2018-08-13 05:45:43 +03:00
|
|
|
}
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-08-28 12:10:10 +03:00
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_NetStream_Play_Complete, av_NetStream_Play_Stop, av_NetStream_Play_UnpublishNotify:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_NetStream_Play_Complete/av_NetStream_Play_Stop/av_NetStream_Play_UnpublishNotify")
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_NetStream_Seek_Notify:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_netStream_Seek_Notify")
|
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_NetStream_Pause_Notify:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_NetStream_Pause_Notify")
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-28 12:10:10 +03:00
|
|
|
|
2018-09-06 12:01:14 +03:00
|
|
|
case av_playlist_ready:
|
2018-08-28 12:10:10 +03:00
|
|
|
panic("Unsupported method av_playlist_ready")
|
|
|
|
|
|
|
|
default:
|
2018-09-06 11:36:45 +03:00
|
|
|
panic(fmt.Sprintf("unknown method: %q", method))
|
2018-08-14 21:49:18 +03:00
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
leave:
|
|
|
|
C_AMF_Reset(&obj)
|
2018-09-06 05:20:58 +03:00
|
|
|
// None of the methods we implement will result in a true return.
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-14 21:49:18 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// void HandleChangeChunkSize(RTMP* r, const RTMPPacket* packet);
|
|
|
|
// rtmp.c +3345
|
|
|
|
func C_HandleChangeChunkSize(r *C_RTMP, packet *C_RTMPPacket) {
|
|
|
|
if packet.m_nBodySize >= 4 {
|
|
|
|
//r.m_inChunkSize = int32(C.AMF_DecodeInt32((*byte)(unsafe.Pointer(packet.m_body))))
|
2018-09-19 08:01:42 +03:00
|
|
|
r.m_inChunkSize = int32(C_AMF_DecodeInt32(packet.m_body[:4]))
|
2018-08-24 12:20:04 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s, received: chunk size change to %d", __FUNCTION__, r.m_inChunkSize);
|
2018-08-14 21:49:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// void HandleServerBW(RTMP* r, const RTMPPacket* packet);
|
|
|
|
// rtmp.c +3508
|
|
|
|
func C_HandlServerBW(r *C_RTMP, packet *C_RTMPPacket) {
|
2018-09-19 08:01:42 +03:00
|
|
|
r.m_nServerBW = int32(C_AMF_DecodeInt32(packet.m_body[:4]))
|
2018-08-24 12:20:04 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s: server BW = %d", __FUNCTION__, r.m_nServerBW);
|
2018-08-14 21:53:15 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// void HandleClientBW(RTMP* r, const RTMPPacket* packet);
|
|
|
|
// rtmp.c +3515
|
|
|
|
func C_HandleClientBW(r *C_RTMP, packet *C_RTMPPacket) {
|
2018-09-19 08:01:42 +03:00
|
|
|
r.m_nClientBW = int32(C_AMF_DecodeInt32(packet.m_body[:4]))
|
2018-08-24 12:20:04 +03:00
|
|
|
//r.m_nClientBW = int32(C.AMF_DecodeInt32((*byte)(unsafe.Pointer(packet.m_body))))
|
|
|
|
|
|
|
|
if packet.m_nBodySize > 4 {
|
2018-09-19 08:01:42 +03:00
|
|
|
r.m_nClientBW2 = packet.m_body[4]
|
2018-08-14 06:15:03 +03:00
|
|
|
} else {
|
2018-09-12 05:37:43 +03:00
|
|
|
r.m_nClientBW2 = 0xff
|
2018-08-14 06:15:03 +03:00
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s: client BW = %d %d", __FUNCTION__, r.m_nClientBW,
|
|
|
|
//r.m_nClientBW2);
|
2018-08-14 06:15:03 +03:00
|
|
|
}
|
2018-08-13 05:45:43 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// static int DecodeInt32LE(const char* data);
|
|
|
|
// rtmp.c +3527
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_DecodeInt32LE(data []byte) int32 {
|
|
|
|
return int32(data[3])<<24 | int32(data[2])<<16 | int32(data[1])<<8 | int32(data[0])
|
2018-08-14 21:04:18 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int EncodeInt32LE(char* output, int nVal);
|
|
|
|
// rtmp.c +3537
|
2018-09-14 14:58:19 +03:00
|
|
|
func C_EncodeInt32LE(dst []byte, v int32) int32 {
|
|
|
|
binary.LittleEndian.PutUint32(dst, uint32(v))
|
2018-08-24 12:20:04 +03:00
|
|
|
return 4
|
2018-08-11 08:04:52 +03:00
|
|
|
}
|
|
|
|
|
2018-08-08 08:37:54 +03:00
|
|
|
// int RTMP_ReadPacket(RTMP* r, RTMPPacket* packet);
|
|
|
|
// rtmp.c +3550
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) error {
|
2018-09-19 05:20:46 +03:00
|
|
|
var hbuf [RTMP_MAX_HEADER_SIZE]byte
|
2018-09-26 07:22:28 +03:00
|
|
|
header := hbuf[:]
|
2018-08-02 15:39:05 +03:00
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
err := C_ReadN(r, header[:1])
|
|
|
|
if err != nil {
|
2018-08-07 06:01:30 +03:00
|
|
|
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet header!")
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
2018-09-26 07:22:28 +03:00
|
|
|
packet.m_headerType = (header[0] & 0xc0) >> 6
|
|
|
|
packet.m_nChannel = int32(header[0] & 0x3f)
|
|
|
|
header = header[1:]
|
2018-08-02 15:39:05 +03:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case packet.m_nChannel == 0:
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_ReadN(r, header[:1])
|
|
|
|
if err != nil {
|
2018-08-07 06:01:30 +03:00
|
|
|
log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header 2nd byte.")
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
2018-09-26 07:22:28 +03:00
|
|
|
header = header[1:]
|
|
|
|
packet.m_nChannel = int32(header[0]) + 64
|
2018-08-02 15:39:05 +03:00
|
|
|
|
|
|
|
case packet.m_nChannel == 1:
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_ReadN(r, header[:2])
|
|
|
|
if err != nil {
|
2018-08-07 06:01:30 +03:00
|
|
|
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet 3rd byte")
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
2018-09-26 07:22:28 +03:00
|
|
|
header = header[2:]
|
|
|
|
packet.m_nChannel = int32(binary.BigEndian.Uint16(header[:2])) + 64
|
2018-08-02 15:39:05 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet.m_nChannel >= r.m_channelsAllocatedIn {
|
2018-09-12 05:37:43 +03:00
|
|
|
n := packet.m_nChannel + 10
|
2018-09-12 07:26:48 +03:00
|
|
|
timestamp := append(r.m_channelTimestamp, make([]int32, 10)...)
|
2018-08-11 07:18:21 +03:00
|
|
|
|
2018-09-12 10:06:18 +03:00
|
|
|
var packets []*C_RTMPPacket
|
|
|
|
if r.m_vecChannelsIn == nil {
|
|
|
|
packets = make([]*C_RTMPPacket, n)
|
|
|
|
} else {
|
|
|
|
packets = append(r.m_vecChannelsIn[:packet.m_nChannel:packet.m_nChannel], make([]*C_RTMPPacket, 10)...)
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 05:37:43 +03:00
|
|
|
r.m_channelTimestamp = timestamp
|
2018-08-02 15:39:05 +03:00
|
|
|
r.m_vecChannelsIn = packets
|
|
|
|
|
2018-09-12 07:26:48 +03:00
|
|
|
for i := int(r.m_channelsAllocatedIn); i < len(r.m_channelTimestamp); i++ {
|
|
|
|
r.m_channelTimestamp[i] = 0
|
|
|
|
}
|
2018-09-12 10:06:18 +03:00
|
|
|
for i := int(r.m_channelsAllocatedIn); i < int(n); i++ {
|
|
|
|
r.m_vecChannelsIn[i] = nil
|
|
|
|
}
|
2018-09-12 05:37:43 +03:00
|
|
|
r.m_channelsAllocatedIn = n
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:52:44 +03:00
|
|
|
nSize := packetSize[packet.m_headerType]
|
2018-08-02 15:39:05 +03:00
|
|
|
switch {
|
|
|
|
case nSize == RTMP_LARGE_HEADER_SIZE:
|
2018-09-12 10:06:18 +03:00
|
|
|
packet.m_hasAbsTimestamp = true
|
2018-08-02 15:39:05 +03:00
|
|
|
case nSize < RTMP_LARGE_HEADER_SIZE:
|
2018-09-12 10:06:18 +03:00
|
|
|
if r.m_vecChannelsIn[packet.m_nChannel] != nil {
|
|
|
|
*packet = *(r.m_vecChannelsIn[packet.m_nChannel])
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nSize--
|
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
if nSize > 0 {
|
|
|
|
err = C_ReadN(r, header[:nSize])
|
|
|
|
if err != nil {
|
|
|
|
log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header.")
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:22:28 +03:00
|
|
|
hSize := len(hbuf) - len(header) + nSize
|
2018-08-02 15:39:05 +03:00
|
|
|
|
|
|
|
if nSize >= 3 {
|
2018-09-26 07:22:28 +03:00
|
|
|
packet.m_nTimeStamp = C_AMF_DecodeInt24(header[:3])
|
2018-08-02 15:39:05 +03:00
|
|
|
|
|
|
|
if nSize >= 6 {
|
2018-09-26 07:22:28 +03:00
|
|
|
packet.m_nBodySize = C_AMF_DecodeInt24(header[3:6])
|
2018-08-02 15:39:05 +03:00
|
|
|
packet.m_nBytesRead = 0
|
|
|
|
|
|
|
|
if nSize > 6 {
|
2018-09-26 07:22:28 +03:00
|
|
|
packet.m_packetType = header[6]
|
2018-08-02 15:39:05 +03:00
|
|
|
|
|
|
|
if nSize == 11 {
|
2018-09-26 07:22:28 +03:00
|
|
|
packet.m_nInfoField2 = C_DecodeInt32LE(header[7:11])
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:52:44 +03:00
|
|
|
extendedTimestamp := packet.m_nTimeStamp == 0xffffff
|
|
|
|
if extendedTimestamp {
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_ReadN(r, header[nSize:nSize+4])
|
|
|
|
if err != nil {
|
2018-08-02 15:39:05 +03:00
|
|
|
log.Println("RTMPRead_Packet: Failed to read extended timestamp")
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
|
|
|
// TODO: port this
|
2018-09-26 07:22:28 +03:00
|
|
|
packet.m_nTimeStamp = C_AMF_DecodeInt32(header[nSize : nSize+4])
|
2018-08-02 15:39:05 +03:00
|
|
|
hSize += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet.m_nBodySize > 0 && packet.m_body == nil {
|
|
|
|
// TODO: port this
|
2019-01-06 07:12:51 +03:00
|
|
|
C_RTMPPacket_Alloc(packet, packet.m_nBodySize)
|
2018-09-12 05:37:43 +03:00
|
|
|
packet.m_headerType = (hbuf[0] & 0xc0) >> 6
|
2018-08-06 07:30:37 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:52:44 +03:00
|
|
|
nToRead := int32(packet.m_nBodySize - packet.m_nBytesRead)
|
|
|
|
nChunk := r.m_inChunkSize
|
2018-08-06 07:30:37 +03:00
|
|
|
|
|
|
|
if nToRead < nChunk {
|
|
|
|
nChunk = nToRead
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet.m_chunk != nil {
|
2018-09-26 06:52:44 +03:00
|
|
|
packet.m_chunk.c_headerSize = int32(hSize)
|
2018-09-12 07:10:17 +03:00
|
|
|
copy(packet.m_chunk.c_header[:], hbuf[:hSize])
|
2018-09-19 08:01:42 +03:00
|
|
|
packet.m_chunk.c_chunk = packet.m_body[packet.m_nBytesRead : packet.m_nBytesRead+uint32(nChunk)]
|
2018-08-06 07:30:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_ReadN(r, packet.m_body[packet.m_nBytesRead:][:nChunk])
|
|
|
|
if err != nil {
|
2018-08-07 06:01:30 +03:00
|
|
|
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet body")
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
2018-08-06 07:30:37 +03:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:21:07 +03:00
|
|
|
packet.m_nBytesRead += uint32(nChunk)
|
2018-08-06 07:30:37 +03:00
|
|
|
|
|
|
|
// keep the packet as ref for other packets on this channel
|
2018-09-12 10:06:18 +03:00
|
|
|
if r.m_vecChannelsIn[packet.m_nChannel] == nil {
|
|
|
|
r.m_vecChannelsIn[packet.m_nChannel] = &C_RTMPPacket{}
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
2018-09-12 10:06:18 +03:00
|
|
|
*(r.m_vecChannelsIn[packet.m_nChannel]) = *packet
|
2018-08-06 07:30:37 +03:00
|
|
|
|
2018-09-26 06:52:44 +03:00
|
|
|
if extendedTimestamp {
|
2018-09-12 10:06:18 +03:00
|
|
|
r.m_vecChannelsIn[packet.m_nChannel].m_nTimeStamp = 0xffffff
|
2018-08-06 07:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: port this
|
2018-09-06 12:10:47 +03:00
|
|
|
if C_RTMPPacket_IsReady(packet) {
|
2018-09-12 10:06:18 +03:00
|
|
|
if !packet.m_hasAbsTimestamp {
|
2018-08-06 07:30:37 +03:00
|
|
|
// timestamps seem to always be relative
|
2018-09-12 07:26:48 +03:00
|
|
|
packet.m_nTimeStamp += uint32(r.m_channelTimestamp[packet.m_nChannel])
|
2018-08-10 09:40:08 +03:00
|
|
|
}
|
2018-09-12 07:26:48 +03:00
|
|
|
r.m_channelTimestamp[packet.m_nChannel] = int32(packet.m_nTimeStamp)
|
2018-08-10 09:40:08 +03:00
|
|
|
|
2018-09-12 10:06:18 +03:00
|
|
|
r.m_vecChannelsIn[packet.m_nChannel].m_body = nil
|
|
|
|
r.m_vecChannelsIn[packet.m_nChannel].m_nBytesRead = 0
|
|
|
|
r.m_vecChannelsIn[packet.m_nChannel].m_hasAbsTimestamp = false
|
2018-08-06 07:30:37 +03:00
|
|
|
} else {
|
|
|
|
packet.m_body = nil /* so it won't be erased on free */
|
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-08-02 15:39:05 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// int HandShake(RTMP* r, int FP9HandShake);
|
|
|
|
// rtmp.c +3744
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_HandShake(r *C_RTMP, FP9HandShake int32) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var clientbuf [RTMP_SIG_SIZE + 1]byte
|
2018-09-19 08:27:13 +03:00
|
|
|
clientsig := clientbuf[1:]
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
var serversig [RTMP_SIG_SIZE]byte
|
2018-07-05 09:55:46 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
clientbuf[0] = 0x03 // not encrypted
|
2018-07-05 09:55:46 +03:00
|
|
|
|
2018-09-19 08:27:13 +03:00
|
|
|
binary.BigEndian.PutUint32(clientsig, uint32(C_RTMP_GetTime()))
|
|
|
|
copy(clientsig[4:8], []byte{0, 0, 0, 0})
|
2018-06-27 21:02:16 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
for i := 8; i < RTMP_SIG_SIZE; i++ {
|
2018-09-19 08:27:13 +03:00
|
|
|
clientsig[i] = byte(rand.Intn(256))
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-07-10 13:36:49 +03:00
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
err := C_WriteN(r, clientbuf[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-07-12 22:51:17 +03:00
|
|
|
|
2018-09-19 08:27:13 +03:00
|
|
|
var typ [1]byte
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_ReadN(r, typ[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-07-14 15:40:01 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
if debugMode {
|
2018-09-19 05:20:46 +03:00
|
|
|
log.Printf("C_HandShake: Type answer: %v\n", typ[0])
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-09-19 05:20:46 +03:00
|
|
|
if typ[0] != clientbuf[0] {
|
2018-08-31 04:43:59 +03:00
|
|
|
log.Printf("C_HandShake: type mismatch: client sent %v, server sent: %v\n",
|
2018-08-24 12:20:04 +03:00
|
|
|
clientbuf[0], typ)
|
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_ReadN(r, serversig[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-05 09:55:46 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// decode server response
|
2018-09-19 08:27:13 +03:00
|
|
|
suptime := binary.BigEndian.Uint32(serversig[:4])
|
|
|
|
_ = suptime
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s: Server Uptime : %d", __FUNCTION__, suptime)
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__,
|
|
|
|
// serversig[4], serversig[5], serversig[6], serversig[7])
|
2018-08-11 07:18:21 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// 2nd part of handshake
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_WriteN(r, serversig[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-11 07:18:21 +03:00
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
err = C_ReadN(r, serversig[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-06 05:56:35 +03:00
|
|
|
if !bytes.Equal(serversig[:RTMP_SIG_SIZE], clientbuf[1:RTMP_SIG_SIZE+1]) {
|
|
|
|
log.Printf("Client signature does not match: %q != %q",
|
|
|
|
serversig[:RTMP_SIG_SIZE], clientbuf[1:RTMP_SIG_SIZE+1])
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-08-11 07:18:21 +03:00
|
|
|
}
|
|
|
|
|
2018-08-08 08:37:54 +03:00
|
|
|
// int RTMP_SendPacket(RTMP* r, RTMPPacket* packet, int queue);
|
|
|
|
// rtmp.c +3896
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) error {
|
2018-08-18 07:21:07 +03:00
|
|
|
var prevPacket *C_RTMPPacket
|
2018-09-26 07:37:54 +03:00
|
|
|
var last int
|
2018-07-14 09:06:58 +03:00
|
|
|
|
|
|
|
if packet.m_nChannel >= r.m_channelsAllocatedOut {
|
2018-07-18 05:26:32 +03:00
|
|
|
n := int(packet.m_nChannel + 10)
|
2018-07-14 09:06:58 +03:00
|
|
|
|
2018-09-12 10:27:54 +03:00
|
|
|
var packets []*C_RTMPPacket
|
|
|
|
if r.m_vecChannelsOut == nil {
|
|
|
|
packets = make([]*C_RTMPPacket, n)
|
|
|
|
} else {
|
|
|
|
packets = append(r.m_vecChannelsOut[:packet.m_nChannel:packet.m_nChannel], make([]*C_RTMPPacket, 10)...)
|
2018-07-14 09:06:58 +03:00
|
|
|
}
|
2018-09-12 10:27:54 +03:00
|
|
|
r.m_vecChannelsOut = packets
|
2018-07-28 15:11:42 +03:00
|
|
|
|
2018-09-12 10:27:54 +03:00
|
|
|
for i := int(r.m_channelsAllocatedOut); i < n; i++ {
|
|
|
|
r.m_vecChannelsOut[i] = nil
|
|
|
|
}
|
2018-07-28 16:02:59 +03:00
|
|
|
|
2018-08-18 07:21:07 +03:00
|
|
|
r.m_channelsAllocatedOut = int32(n)
|
2018-07-14 09:06:58 +03:00
|
|
|
}
|
2018-09-12 10:27:54 +03:00
|
|
|
prevPacket = r.m_vecChannelsOut[packet.m_nChannel]
|
2018-07-15 09:51:20 +03:00
|
|
|
|
|
|
|
if prevPacket != nil && packet.m_headerType != RTMP_PACKET_SIZE_LARGE {
|
|
|
|
// compress a bit by using the prev packet's attributes
|
2018-09-26 07:37:54 +03:00
|
|
|
if prevPacket.m_nBodySize == packet.m_nBodySize && prevPacket.m_packetType == packet.m_packetType && packet.m_headerType == RTMP_PACKET_SIZE_MEDIUM {
|
2018-07-15 09:51:20 +03:00
|
|
|
packet.m_headerType = RTMP_PACKET_SIZE_SMALL
|
|
|
|
}
|
|
|
|
|
2018-09-26 07:37:54 +03:00
|
|
|
if prevPacket.m_nTimeStamp == packet.m_nTimeStamp && packet.m_headerType == RTMP_PACKET_SIZE_SMALL {
|
2018-07-19 09:08:10 +03:00
|
|
|
packet.m_headerType = RTMP_PACKET_SIZE_MINIMUM
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 08:07:26 +03:00
|
|
|
last = int(prevPacket.m_nTimeStamp)
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if packet.m_headerType > 3 {
|
|
|
|
log.Printf("Sanity failed! trying to send header of type: 0x%02x.",
|
|
|
|
packet.m_headerType)
|
2019-01-06 07:12:51 +03:00
|
|
|
return errInvalidHeader
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 09:31:16 +03:00
|
|
|
var headBytes []byte
|
|
|
|
var origIdx int
|
2018-07-16 08:07:26 +03:00
|
|
|
if packet.m_body != nil {
|
2018-09-26 08:36:50 +03:00
|
|
|
// Span from -packetsize for the type to the start of the body.
|
2018-09-26 10:36:06 +03:00
|
|
|
headBytes = packet.m_header
|
2018-09-26 09:31:16 +03:00
|
|
|
origIdx = RTMP_MAX_HEADER_SIZE - packetSize[packet.m_headerType]
|
2018-07-15 09:51:20 +03:00
|
|
|
} else {
|
2018-09-26 08:07:53 +03:00
|
|
|
// Allocate a new header and allow 6 bytes of movement backward.
|
2018-09-26 07:37:54 +03:00
|
|
|
var hbuf [RTMP_MAX_HEADER_SIZE]byte
|
2018-09-26 09:31:16 +03:00
|
|
|
headBytes = hbuf[:]
|
|
|
|
origIdx = 6
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 08:36:50 +03:00
|
|
|
var cSize int
|
2018-07-15 09:51:20 +03:00
|
|
|
switch {
|
|
|
|
case packet.m_nChannel > 319:
|
|
|
|
cSize = 2
|
|
|
|
case packet.m_nChannel > 63:
|
|
|
|
cSize = 1
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:36:50 +03:00
|
|
|
hSize := packetSize[packet.m_headerType]
|
2018-07-15 09:51:20 +03:00
|
|
|
if cSize != 0 {
|
2018-09-26 09:31:16 +03:00
|
|
|
origIdx -= cSize
|
2018-07-16 12:20:17 +03:00
|
|
|
hSize += cSize
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:36:50 +03:00
|
|
|
t := uint32(int(packet.m_nTimeStamp) - last)
|
2018-07-16 12:20:17 +03:00
|
|
|
if t >= 0xffffff {
|
2018-09-26 09:31:16 +03:00
|
|
|
origIdx -= 4
|
2018-07-16 12:20:17 +03:00
|
|
|
hSize += 4
|
|
|
|
log.Printf("Larger timestamp than 24-bit: 0x%v", t)
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 09:31:16 +03:00
|
|
|
headerIdx := origIdx
|
2018-07-15 09:51:20 +03:00
|
|
|
|
2018-09-26 09:31:16 +03:00
|
|
|
c := packet.m_headerType << 6
|
2018-07-15 09:51:20 +03:00
|
|
|
switch cSize {
|
|
|
|
case 0:
|
|
|
|
c |= byte(packet.m_nChannel)
|
|
|
|
case 1:
|
2018-09-26 08:36:50 +03:00
|
|
|
// Do nothing.
|
2018-07-15 09:51:20 +03:00
|
|
|
case 2:
|
2018-09-26 08:36:50 +03:00
|
|
|
c |= 1
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
2018-09-26 09:31:16 +03:00
|
|
|
headBytes[headerIdx] = c
|
|
|
|
headerIdx++
|
2018-07-16 12:20:17 +03:00
|
|
|
|
|
|
|
if cSize != 0 {
|
|
|
|
tmp := packet.m_nChannel - 64
|
2018-09-26 09:31:16 +03:00
|
|
|
headBytes[headerIdx] = byte(tmp & 0xff)
|
|
|
|
headerIdx++
|
2018-07-16 12:20:17 +03:00
|
|
|
|
|
|
|
if cSize == 2 {
|
2018-09-26 09:31:16 +03:00
|
|
|
headBytes[headerIdx] = byte(tmp >> 8)
|
|
|
|
headerIdx++
|
2018-07-16 12:20:17 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-15 09:51:20 +03:00
|
|
|
|
2018-09-26 08:36:50 +03:00
|
|
|
if packetSize[packet.m_headerType] > 1 {
|
2018-07-15 09:51:20 +03:00
|
|
|
res := t
|
|
|
|
if t > 0xffffff {
|
|
|
|
res = 0xffffff
|
|
|
|
}
|
2018-09-26 09:31:16 +03:00
|
|
|
C_AMF_EncodeInt24(headBytes[headerIdx:], int32(res))
|
|
|
|
headerIdx += 3 // 24bits
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 08:36:50 +03:00
|
|
|
if packetSize[packet.m_headerType] > 4 {
|
2018-09-26 09:31:16 +03:00
|
|
|
C_AMF_EncodeInt24(headBytes[headerIdx:], int32(packet.m_nBodySize))
|
|
|
|
headerIdx += 3 // 24bits
|
|
|
|
headBytes[headerIdx] = packet.m_packetType
|
|
|
|
headerIdx++
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 08:36:50 +03:00
|
|
|
if packetSize[packet.m_headerType] > 8 {
|
2018-09-26 09:31:16 +03:00
|
|
|
n := int(C_EncodeInt32LE(headBytes[headerIdx:headerIdx+4], packet.m_nInfoField2))
|
|
|
|
headerIdx += n
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if t >= 0xffffff {
|
2018-09-26 09:31:16 +03:00
|
|
|
C_AMF_EncodeInt32(headBytes[headerIdx:], int32(t))
|
|
|
|
headerIdx += 4 // 32bits
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 08:36:50 +03:00
|
|
|
nSize := int(packet.m_nBodySize)
|
2018-09-12 10:27:54 +03:00
|
|
|
nChunkSize := int(r.m_outChunkSize)
|
2018-07-15 09:51:20 +03:00
|
|
|
|
|
|
|
if debugMode {
|
2019-01-06 08:24:49 +03:00
|
|
|
if r.Link.conn != nil {
|
2019-01-06 09:47:51 +03:00
|
|
|
log.Printf("C_RTMP_SendPacket: %v=>%v, size=%v", r.Link.conn.LocalAddr(), r.Link.conn.RemoteAddr(), nSize)
|
2018-09-30 12:29:01 +03:00
|
|
|
}
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:36:06 +03:00
|
|
|
// TODO(kortschak): Rewrite this horrific peice of premature optimisation.
|
2019-01-06 09:47:51 +03:00
|
|
|
// NB: RTMP wants packets in chunks which are 128 bytes by default, but the server may request a different size.
|
2018-09-26 10:36:06 +03:00
|
|
|
for nSize+hSize != 0 {
|
|
|
|
if nChunkSize > nSize {
|
2018-07-15 09:51:20 +03:00
|
|
|
nChunkSize = nSize
|
|
|
|
}
|
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
err := C_WriteN(r, headBytes[origIdx:][:nChunkSize+hSize])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nSize -= nChunkSize
|
2019-01-06 09:47:51 +03:00
|
|
|
origIdx += nChunkSize + hSize
|
2018-07-15 09:51:20 +03:00
|
|
|
hSize = 0
|
|
|
|
|
|
|
|
if nSize > 0 {
|
2018-09-26 10:36:06 +03:00
|
|
|
origIdx -= 1 + cSize
|
|
|
|
hSize = 1 + cSize
|
2018-07-15 09:51:20 +03:00
|
|
|
|
|
|
|
if t >= 0xffffff {
|
2018-09-26 10:36:06 +03:00
|
|
|
origIdx -= 4
|
2018-07-15 09:51:20 +03:00
|
|
|
hSize += 4
|
|
|
|
}
|
|
|
|
|
2018-09-26 10:36:06 +03:00
|
|
|
headBytes[origIdx] = 0xc0 | c
|
2018-07-15 09:51:20 +03:00
|
|
|
|
|
|
|
if cSize != 0 {
|
2018-07-15 10:18:38 +03:00
|
|
|
tmp := int(packet.m_nChannel) - 64
|
2018-09-26 10:36:06 +03:00
|
|
|
headBytes[origIdx+1] = byte(tmp)
|
2018-07-15 09:51:20 +03:00
|
|
|
|
2018-07-15 10:18:38 +03:00
|
|
|
if cSize == 2 {
|
2018-09-26 10:36:06 +03:00
|
|
|
headBytes[origIdx+2] = byte(tmp >> 8)
|
2018-07-15 10:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if t >= 0xffffff {
|
2018-09-26 10:36:06 +03:00
|
|
|
extendedTimestamp := headBytes[origIdx+1+cSize:]
|
|
|
|
C_AMF_EncodeInt32(extendedTimestamp[:4], int32(t))
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-15 10:18:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We invoked a remote method
|
|
|
|
// TODO: port the const
|
2018-07-19 09:08:10 +03:00
|
|
|
if packet.m_packetType == RTMP_PACKET_TYPE_INVOKE {
|
2018-09-19 08:01:42 +03:00
|
|
|
buf := packet.m_body[1:]
|
|
|
|
method := C_AMF_DecodeString(buf)
|
2018-07-15 10:18:38 +03:00
|
|
|
|
|
|
|
if debugMode {
|
2018-09-06 07:13:40 +03:00
|
|
|
log.Printf("invoking %v", method)
|
2018-07-15 10:18:38 +03:00
|
|
|
}
|
|
|
|
// keep it in call queue till result arrives
|
|
|
|
if queue != 0 {
|
2018-09-19 08:01:42 +03:00
|
|
|
buf = buf[3+len(method):]
|
|
|
|
txn := int32(C_AMF_DecodeNumber(buf[:8]))
|
2018-09-12 04:59:38 +03:00
|
|
|
r.m_methodCalls = append(r.m_methodCalls, C_RTMP_METHOD{name: method, num: txn})
|
2018-07-15 10:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:27:54 +03:00
|
|
|
if r.m_vecChannelsOut[packet.m_nChannel] == nil {
|
|
|
|
r.m_vecChannelsOut[packet.m_nChannel] = &C_RTMPPacket{}
|
2018-07-15 09:51:20 +03:00
|
|
|
}
|
2018-09-12 10:27:54 +03:00
|
|
|
*(r.m_vecChannelsOut[packet.m_nChannel]) = *packet
|
2018-07-14 09:06:58 +03:00
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-05-29 08:56:36 +03:00
|
|
|
}
|
2018-07-15 09:51:20 +03:00
|
|
|
|
2018-08-25 17:43:08 +03:00
|
|
|
// void RTMP_Close(RTMP *r);
|
|
|
|
// rtmp.c +4168
|
|
|
|
func C_RTMP_Close(r *C_RTMP) {
|
2018-09-19 07:38:09 +03:00
|
|
|
C_CloseInternal(r, false)
|
2018-08-25 17:43:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static void CloseInternal(RTMP *r, int reconnect);
|
|
|
|
// rtmp.c +4175
|
2018-09-19 07:38:09 +03:00
|
|
|
func C_CloseInternal(r *C_RTMP, reconnect bool) {
|
2018-09-06 05:15:48 +03:00
|
|
|
var i int32
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2019-01-06 07:12:51 +03:00
|
|
|
if C_RTMP_IsConnected(r) {
|
2018-09-06 05:15:48 +03:00
|
|
|
if r.m_stream_id > 0 {
|
2018-09-12 05:37:43 +03:00
|
|
|
i = r.m_stream_id
|
2018-09-06 05:15:48 +03:00
|
|
|
if r.Link.protocol&RTMP_FEATURE_WRITE != 0 {
|
|
|
|
C_SendFCUnpublish(r)
|
2018-08-25 17:43:08 +03:00
|
|
|
}
|
2018-09-06 05:15:48 +03:00
|
|
|
C_SendDeleteStream(r, float64(i))
|
2018-08-25 17:43:08 +03:00
|
|
|
}
|
2019-01-06 08:24:49 +03:00
|
|
|
err := r.Link.conn.Close()
|
2019-01-06 00:55:46 +03:00
|
|
|
if err != nil && debugMode {
|
|
|
|
log.Printf("C_RTMPSockBuf_Close error: %v\n", err)
|
|
|
|
}
|
2018-09-06 05:15:48 +03:00
|
|
|
}
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-06 05:15:48 +03:00
|
|
|
r.m_stream_id = -1
|
2019-01-06 08:24:49 +03:00
|
|
|
r.Link.conn = nil
|
2018-09-06 05:15:48 +03:00
|
|
|
r.m_nBWCheckCounter = 0
|
|
|
|
r.m_nBytesIn = 0
|
|
|
|
r.m_nBytesInSent = 0
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-06 05:15:48 +03:00
|
|
|
r.m_write.m_nBytesRead = 0
|
|
|
|
C_RTMPPacket_Free(&r.m_write)
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-06 05:15:48 +03:00
|
|
|
// NOTE: C frees - not using in our case
|
|
|
|
for i := 0; i < int(r.m_channelsAllocatedIn); i++ {
|
2018-09-12 10:06:18 +03:00
|
|
|
if r.m_vecChannelsIn[i] != nil {
|
|
|
|
r.m_vecChannelsIn[i] = nil
|
2018-09-06 05:15:48 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-06 05:15:48 +03:00
|
|
|
//C.free(unsafe.Pointer(r.m_vecChannelsOut))
|
|
|
|
r.m_vecChannelsOut = nil
|
|
|
|
r.m_channelsAllocatedOut = 0
|
|
|
|
r.m_methodCalls = nil //C_AV_clear(r.m_methodCalls, r.m_numCalls)
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-12 04:59:38 +03:00
|
|
|
r.m_methodCalls = r.m_methodCalls[:0]
|
2018-09-06 05:15:48 +03:00
|
|
|
r.m_numInvokes = 0
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-06 05:20:58 +03:00
|
|
|
r.m_bPlaying = false
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-06 05:15:48 +03:00
|
|
|
r.m_msgCounter = 0
|
|
|
|
r.m_resplen = 0
|
|
|
|
r.m_unackd = 0
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-19 07:38:09 +03:00
|
|
|
if ((r.Link.lFlags & RTMP_LF_FTCU) != 0) && !reconnect {
|
2018-09-07 14:10:46 +03:00
|
|
|
r.Link.app = ""
|
2018-09-06 05:15:48 +03:00
|
|
|
r.Link.lFlags ^= RTMP_LF_FAPU
|
|
|
|
}
|
2018-08-25 17:43:08 +03:00
|
|
|
|
2018-09-19 07:38:09 +03:00
|
|
|
if !reconnect {
|
2018-09-07 14:10:46 +03:00
|
|
|
r.Link.playpath0 = ""
|
2018-09-06 05:15:48 +03:00
|
|
|
}
|
2018-08-25 17:43:08 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 09:51:32 +03:00
|
|
|
/// int RTMP_Write(RTMP* r, const char* buf, int size);
|
2018-09-02 07:10:18 +03:00
|
|
|
// rtmp.c +5136
|
2019-01-06 07:12:51 +03:00
|
|
|
func C_RTMP_Write(r *C_RTMP, buf []byte) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
// TODO: port RTMPPacket
|
|
|
|
var pkt = &r.m_write
|
2018-09-15 04:55:07 +03:00
|
|
|
var enc []byte
|
2018-09-02 03:49:29 +03:00
|
|
|
size := len(buf)
|
2018-09-06 05:20:58 +03:00
|
|
|
var num int
|
2018-08-24 12:20:04 +03:00
|
|
|
|
|
|
|
pkt.m_nChannel = 0x04
|
2018-09-12 05:37:43 +03:00
|
|
|
pkt.m_nInfoField2 = r.m_stream_id
|
2018-09-02 05:02:11 +03:00
|
|
|
for len(buf) != 0 {
|
2018-08-24 12:20:04 +03:00
|
|
|
if pkt.m_nBytesRead == 0 {
|
|
|
|
if size < minDataSize {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errTinyPacket
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 16:53:07 +03:00
|
|
|
if buf[0] == 'F' && buf[1] == 'L' && buf[2] == 'V' {
|
|
|
|
buf = buf[13:]
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 05:37:43 +03:00
|
|
|
pkt.m_packetType = buf[0]
|
2018-09-01 16:53:07 +03:00
|
|
|
buf = buf[1:]
|
2018-09-12 15:12:10 +03:00
|
|
|
pkt.m_nBodySize = C_AMF_DecodeInt24(buf[:3])
|
2018-09-01 16:53:07 +03:00
|
|
|
buf = buf[3:]
|
2018-09-12 15:12:10 +03:00
|
|
|
pkt.m_nTimeStamp = C_AMF_DecodeInt24(buf[:3])
|
2018-09-01 16:53:07 +03:00
|
|
|
buf = buf[3:]
|
|
|
|
pkt.m_nTimeStamp |= uint32(buf[0]) << 24
|
|
|
|
buf = buf[4:]
|
2018-08-24 12:20:04 +03:00
|
|
|
|
|
|
|
if ((pkt.m_packetType == RTMP_PACKET_TYPE_AUDIO ||
|
|
|
|
pkt.m_packetType == RTMP_PACKET_TYPE_VIDEO) &&
|
|
|
|
pkt.m_nTimeStamp == 0) || pkt.m_packetType == RTMP_PACKET_TYPE_INFO {
|
|
|
|
|
|
|
|
pkt.m_headerType = RTMP_PACKET_SIZE_LARGE
|
|
|
|
|
|
|
|
if pkt.m_packetType == RTMP_PACKET_TYPE_INFO {
|
|
|
|
pkt.m_nBodySize += 16
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pkt.m_headerType = RTMP_PACKET_SIZE_MEDIUM
|
|
|
|
}
|
|
|
|
// TODO: Port this
|
2019-01-06 07:12:51 +03:00
|
|
|
C_RTMPPacket_Alloc(pkt, pkt.m_nBodySize)
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2018-09-19 08:01:42 +03:00
|
|
|
enc = pkt.m_body[:pkt.m_nBodySize]
|
2018-08-24 12:20:04 +03:00
|
|
|
if pkt.m_packetType == RTMP_PACKET_TYPE_INFO {
|
2018-09-15 04:55:07 +03:00
|
|
|
enc = C_AMF_EncodeString(enc, setDataFrame)
|
2019-01-06 07:49:58 +03:00
|
|
|
if enc == nil {
|
|
|
|
return errEncoding
|
|
|
|
}
|
2018-09-19 08:01:42 +03:00
|
|
|
pkt.m_nBytesRead = uint32(len(pkt.m_body) - len(enc))
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2018-09-19 08:01:42 +03:00
|
|
|
enc = pkt.m_body[:pkt.m_nBodySize][pkt.m_nBytesRead:]
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
num = int(pkt.m_nBodySize - pkt.m_nBytesRead)
|
2018-09-02 05:02:11 +03:00
|
|
|
if num > len(buf) {
|
|
|
|
num = len(buf)
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 16:53:07 +03:00
|
|
|
copy(enc[:num], buf[:num])
|
2018-08-24 12:20:04 +03:00
|
|
|
pkt.m_nBytesRead += uint32(num)
|
2018-09-01 16:53:07 +03:00
|
|
|
buf = buf[num:]
|
2018-08-24 12:20:04 +03:00
|
|
|
if pkt.m_nBytesRead == pkt.m_nBodySize {
|
2019-01-06 07:12:51 +03:00
|
|
|
err := C_RTMP_SendPacket(r, pkt, 0)
|
2018-08-24 12:20:04 +03:00
|
|
|
C_RTMPPacket_Free(pkt)
|
|
|
|
pkt.m_nBytesRead = 0
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-09-02 07:01:52 +03:00
|
|
|
if len(buf) < 4 {
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-09-02 07:01:52 +03:00
|
|
|
}
|
2018-09-01 16:53:07 +03:00
|
|
|
buf = buf[4:]
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-07-15 12:26:59 +03:00
|
|
|
}
|
2018-07-18 05:32:02 +03:00
|
|
|
|
2018-06-20 07:26:40 +03:00
|
|
|
var rtmpErrs = [...]string{
|
|
|
|
1: "rtmp: not connected",
|
|
|
|
2: "rtmp: write error",
|
|
|
|
3: "rtmp: not started",
|
|
|
|
}
|
|
|
|
|
|
|
|
type Err uint
|
|
|
|
|
|
|
|
func (e Err) Error() string {
|
|
|
|
if 0 <= int(e) && int(e) < len(rtmpErrs) {
|
|
|
|
s := rtmpErrs[e]
|
|
|
|
if s != "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "rtmp: " + strconv.Itoa(int(e))
|
|
|
|
}
|