av/rtmp/rtmp.go

826 lines
21 KiB
Go
Raw Normal View History

/*
NAME
rtmp.go
DESCRIPTION
See Readme.md
AUTHORS
Saxon Nelson-Milton <saxon@ausocean.org>
Dan Kortschak <dan@ausocean.org>
Alan Noble <alan@ausocean.org>
LICENSE
rtmp.go is Copyright (C) 2017-2019 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
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
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
*/
package rtmp
import (
"bytes"
"encoding/binary"
"errors"
2018-07-24 15:00:50 +03:00
"math/rand"
"net"
"strconv"
"time"
)
2018-08-08 10:36:16 +03:00
const (
2019-01-07 16:29:41 +03:00
pkg = "rtmp:"
2018-08-08 10:36:16 +03:00
minDataSize = 11
)
2018-09-06 12:01:14 +03:00
const (
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"
)
// RTMP protocol strings.
var rtmpProtocolStrings = [...]string{
"rtmp",
"rtmpt",
"rtmpe",
"rtmpte",
"rtmps",
"rtmpts",
"",
"",
"rtmfp",
}
// RTMP errors.
var (
2019-01-06 04:36:16 +03:00
errUnknownScheme = errors.New("rtmp: unknown scheme")
2019-01-07 16:29:41 +03:00
errConnected = errors.New("rtmp: already connected")
errNotConnected = errors.New("rtmp: not connected")
2019-01-06 04:36:16 +03:00
errHandshake = errors.New("rtmp: handshake failed")
errConnSend = errors.New("rtmp: connection send error")
errConnStream = errors.New("rtmp: connection stream error")
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")
2019-01-10 15:22:43 +03:00
errUnimplemented = errors.New("rtmp: unimplemented feature")
)
// setupURL parses the RTMP URL.
func setupURL(s *Session, addr string) (err error) {
s.link.protocol, s.link.host, s.link.port, s.link.app, s.link.playpath, err = 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
if s.link.tcUrl == "" {
if s.link.app != "" {
2019-01-07 16:29:41 +03:00
s.link.tcUrl = rtmpProtocolStrings[s.link.protocol] + "://" + s.link.host + ":" + strconv.Itoa(int(s.link.port)) + "/" + s.link.app
s.link.lFlags |= RTMP_LF_FTCU
2018-07-19 18:35:21 +03:00
} else {
s.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
if s.link.port == 0 {
2018-07-19 18:35:21 +03:00
switch {
case (s.link.protocol & RTMP_FEATURE_SSL) != 0:
s.link.port = 433
2019-01-07 16:29:41 +03:00
s.log(FatalLevel, pkg+"SSL not supported")
case (s.link.protocol & RTMP_FEATURE_HTTP) != 0:
s.link.port = 80
2018-07-19 18:35:21 +03:00
default:
s.link.port = 1935
2018-07-19 18:35:21 +03:00
}
}
return nil
2018-07-15 21:09:36 +03:00
}
// connect establishes an RTMP connection.
func connect(s *Session) error {
addr, err := net.ResolveTCPAddr("tcp4", s.link.host+":"+strconv.Itoa(int(s.link.port)))
if err != nil {
return err
}
s.link.conn, err = net.DialTCP("tcp4", nil, addr)
if err != nil {
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"dial failed", "error", err.Error())
return err
}
2019-01-07 16:29:41 +03:00
s.log(DebugLevel, pkg+"connected")
err = handshake(s)
if err != nil {
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"handshake failed", "error", err.Error())
return errHandshake
2018-07-22 16:31:50 +03:00
}
2019-01-07 16:29:41 +03:00
s.log(DebugLevel, pkg+"handshaked")
err = sendConnectPacket(s)
if err != nil {
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"sendConnect failed", "error", err.Error())
return errConnSend
2018-07-22 16:31:50 +03:00
}
return nil
2018-07-22 16:31:50 +03:00
}
// connectStream reads a packet and handles it
func connectStream(s *Session) error {
2019-01-10 05:19:52 +03:00
var err error
for !s.isPlaying && s.isConnected() {
2019-01-10 05:19:52 +03:00
pkt := packet{}
err = pkt.read(s)
if err != nil {
break
}
if pkt.bodySize == 0 {
continue
}
if pkt.packetType == RTMP_PACKET_TYPE_AUDIO ||
pkt.packetType == RTMP_PACKET_TYPE_VIDEO ||
pkt.packetType == RTMP_PACKET_TYPE_INFO {
2019-01-07 16:29:41 +03:00
s.log(DebugLevel, pkg+"got packet before play; ignoring")
continue
}
2019-01-10 05:19:52 +03:00
err = handlePacket(s, &pkt)
if err != nil {
break
}
}
if !s.isPlaying {
2019-01-10 05:19:52 +03:00
return err
}
return nil
}
// handlePacket handles a packet that the client has received.
// NB: cases have been commented out that are not currently used by AusOcean
2019-01-10 05:19:52 +03:00
func handlePacket(s *Session, pkt *packet) error {
switch pkt.packetType {
case RTMP_PACKET_TYPE_CHUNK_SIZE:
if pkt.bodySize >= 4 {
s.inChunkSize = int32(C_AMF_DecodeInt32(pkt.body[:4]))
}
case RTMP_PACKET_TYPE_BYTES_READ_REPORT:
s.serverBW = int32(C_AMF_DecodeInt32(pkt.body[:4]))
case RTMP_PACKET_TYPE_CONTROL:
s.log(FatalLevel, pkg+"unsupported packet type RTMP_PACKET_TYPE_CONTROL")
case RTMP_PACKET_TYPE_SERVER_BW:
s.serverBW = int32(C_AMF_DecodeInt32(pkt.body[:4]))
case RTMP_PACKET_TYPE_CLIENT_BW:
s.clientBW = int32(C_AMF_DecodeInt32(pkt.body[:4]))
if pkt.bodySize > 4 {
s.clientBW2 = pkt.body[4]
} else {
s.clientBW2 = 0xff
}
case RTMP_PACKET_TYPE_AUDIO:
s.log(FatalLevel, pkg+"unsupported packet type RTMP_PACKET_TYPE_AUDIO")
case RTMP_PACKET_TYPE_VIDEO:
s.log(FatalLevel, pkg+"unsupported packet type RTMP_PACKET_TYPE_VIDEO")
case RTMP_PACKET_TYPE_FLEX_MESSAGE:
s.log(FatalLevel, pkg+"unsupported packet type RTMP_PACKET_TYPE_FLEX_MESSAGE")
case RTMP_PACKET_TYPE_INFO:
s.log(FatalLevel, pkg+"unsupported packet type RTMP_PACKET_TYPE_INFO")
case RTMP_PACKET_TYPE_INVOKE:
err := handleInvoke(s, pkt.body[:pkt.bodySize])
if err != nil {
2018-09-06 05:20:58 +03:00
// This will never happen with the methods we implement.
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"unexpected error from handleInvoke", "error", err.Error())
2019-01-10 05:19:52 +03:00
return err
}
case RTMP_PACKET_TYPE_FLASH_VIDEO:
s.log(FatalLevel, pkg+"unsupported packet type RTMP_PACKET_TYPE_FLASH_VIDEO")
default:
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"unknown packet type", "type", pkt.packetType)
}
2019-01-10 05:19:52 +03:00
return nil
}
func sendConnectPacket(s *Session) error {
var pbuf [4096]byte
pkt := packet{
channel: RTMP_CHANNEL_CONTROL,
headerType: RTMP_PACKET_SIZE_LARGE,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av_connect)
if enc == nil {
return errEncoding
}
s.numInvokes += 1
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_OBJECT
enc = enc[1:]
enc = C_AMF_EncodeNamedString(enc, av_app, s.link.app)
if enc == nil {
return errEncoding
}
if s.link.protocol&RTMP_FEATURE_WRITE != 0 {
enc = C_AMF_EncodeNamedString(enc, av_type, av_nonprivate)
if enc == nil {
return errEncoding
}
}
if s.link.flashVer != "" {
enc = C_AMF_EncodeNamedString(enc, av_flashVer, s.link.flashVer)
if enc == nil {
return errEncoding
}
}
if s.link.swfUrl != "" {
enc = C_AMF_EncodeNamedString(enc, av_swfUrl, s.link.swfUrl)
if enc == nil {
return errEncoding
}
}
if s.link.tcUrl != "" {
enc = C_AMF_EncodeNamedString(enc, av_tcUrl, s.link.tcUrl)
if enc == nil {
return errEncoding
}
}
if s.link.protocol&RTMP_FEATURE_WRITE == 0 {
enc = C_AMF_EncodeNamedBoolean(enc, av_fpad, false)
if enc == nil {
return errEncoding
}
enc = C_AMF_EncodeNamedNumber(enc, av_capabilities, 15)
if enc == nil {
return errEncoding
}
enc = C_AMF_EncodeNamedNumber(enc, av_audioCodecs, s.audioCodecs)
if enc == nil {
return errEncoding
}
enc = C_AMF_EncodeNamedNumber(enc, av_videoCodecs, s.videoCodecs)
if enc == nil {
return errEncoding
}
enc = C_AMF_EncodeNamedNumber(enc, av_videoFunction, 1)
if enc == nil {
return errEncoding
}
if s.link.pageUrl != "" {
enc = C_AMF_EncodeNamedString(enc, av_pageUrl, s.link.pageUrl)
if enc == nil {
return errEncoding
}
}
}
if s.encoding != 0.0 || s.sendEncoding {
enc = C_AMF_EncodeNamedNumber(enc, av_objectEncoding, s.encoding)
if enc == nil {
return errEncoding
}
}
2019-01-10 05:19:52 +03:00
copy(enc, []byte{0, 0, AMF_OBJECT_END})
enc = enc[3:]
2019-01-10 05:19:52 +03:00
// add auth string
if s.link.auth != "" {
enc = C_AMF_EncodeBoolean(enc, s.link.lFlags&RTMP_LF_AUTH != 0)
if enc == nil {
return errEncoding
}
enc = C_AMF_EncodeString(enc, s.link.auth)
if enc == nil {
return errEncoding
}
}
for i := range s.link.extras.o_props {
enc = C_AMF_PropEncode(&s.link.extras.o_props[i], enc)
if enc == nil {
return errEncoding
}
}
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
return pkt.write(s, true) // response expected
2018-07-25 04:26:59 +03:00
}
func sendCreateStream(s *Session) error {
var pbuf [256]byte
pkt := packet{
channel: RTMP_CHANNEL_CONTROL,
headerType: RTMP_PACKET_SIZE_MEDIUM,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av_createStream)
if enc == nil {
return errEncoding
}
s.numInvokes++
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_NULL
enc = enc[1:]
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
return pkt.write(s, true) // response expected
}
func sendReleaseStream(s *Session) error {
var pbuf [1024]byte
pkt := packet{
channel: RTMP_CHANNEL_CONTROL,
headerType: RTMP_PACKET_SIZE_MEDIUM,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av_releaseStream)
if enc == nil {
return errEncoding
}
s.numInvokes++
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_NULL
enc = enc[1:]
enc = C_AMF_EncodeString(enc, s.link.playpath)
if enc == nil {
return errEncoding
}
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
return pkt.write(s, false)
}
func sendFCPublish(s *Session) error {
var pbuf [1024]byte
pkt := packet{
channel: RTMP_CHANNEL_CONTROL,
headerType: RTMP_PACKET_SIZE_MEDIUM,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av_FCPublish)
if enc == nil {
return errEncoding
}
s.numInvokes++
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_NULL
enc = enc[1:]
enc = C_AMF_EncodeString(enc, s.link.playpath)
if enc == nil {
return errEncoding
}
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
return pkt.write(s, false)
}
func sendFCUnpublish(s *Session) error {
2018-09-06 04:56:52 +03:00
var pbuf [1024]byte
pkt := packet{
channel: RTMP_CHANNEL_CONTROL,
headerType: RTMP_PACKET_SIZE_MEDIUM,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av_FCUnpublish)
if enc == nil {
return errEncoding
}
s.numInvokes++
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_NULL
enc = enc[1:]
enc = C_AMF_EncodeString(enc, s.link.playpath)
2018-09-06 04:56:52 +03:00
if enc == nil {
return errEncoding
2018-09-06 04:56:52 +03:00
}
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
2018-09-06 04:56:52 +03:00
return pkt.write(s, false)
}
func sendPublish(s *Session) error {
var pbuf [1024]byte
pkt := packet{
channel: RTMP_CHANNEL_SOURCE,
headerType: RTMP_PACKET_SIZE_LARGE,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av_publish)
if enc == nil {
return errEncoding
}
s.numInvokes++
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_NULL
enc = enc[1:]
enc = C_AMF_EncodeString(enc, s.link.playpath)
if enc == nil {
return errEncoding
}
enc = C_AMF_EncodeString(enc, av_live)
if enc == nil {
return errEncoding
2018-07-22 16:31:50 +03:00
}
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
2018-07-22 16:31:50 +03:00
return pkt.write(s, true) // response expected
2018-07-22 16:31:50 +03:00
}
func sendDeleteStream(s *Session, dStreamId float64) error {
2018-09-06 05:15:48 +03:00
var pbuf [256]byte
pkt := packet{
channel: RTMP_CHANNEL_CONTROL,
headerType: RTMP_PACKET_SIZE_MEDIUM,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av_deleteStream)
if enc == nil {
return errEncoding
}
s.numInvokes++
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_NULL
enc = enc[1:]
enc = C_AMF_EncodeNumber(enc, dStreamId)
if enc == nil {
return errEncoding
}
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
2018-09-06 05:15:48 +03:00
/* no response expected */
return pkt.write(s, false)
2018-09-06 05:15:48 +03:00
}
// sendBytesReceived tells the server how many bytes the client has received.
func sendBytesReceived(s *Session) error {
var pbuf [256]byte
pkt := packet{
channel: RTMP_CHANNEL_BYTES_READ,
headerType: RTMP_PACKET_SIZE_MEDIUM,
packetType: RTMP_PACKET_TYPE_BYTES_READ_REPORT,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
2019-01-07 06:03:07 +03:00
}
enc := pkt.body
2019-01-07 06:03:07 +03:00
s.nBytesInSent = s.nBytesIn
enc = C_AMF_EncodeInt32(enc, s.nBytesIn)
if enc == nil {
return errEncoding
}
pkt.bodySize = 4
return pkt.write(s, false)
}
func sendCheckBW(s *Session) error {
var pbuf [256]byte
pkt := packet{
channel: RTMP_CHANNEL_CONTROL,
headerType: RTMP_PACKET_SIZE_LARGE,
packetType: RTMP_PACKET_TYPE_INVOKE,
header: pbuf[:],
body: pbuf[RTMP_MAX_HEADER_SIZE:],
}
enc := pkt.body
enc = C_AMF_EncodeString(enc, av__checkbw)
if enc == nil {
return errEncoding
}
s.numInvokes++
enc = C_AMF_EncodeNumber(enc, float64(s.numInvokes))
if enc == nil {
return errEncoding
}
enc[0] = AMF_NULL
enc = enc[1:]
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
return pkt.write(s, false)
}
func eraseMethod(m []method, i int) []method {
2018-09-12 04:59:38 +03:00
copy(m[i:], m[i+1:])
m[len(m)-1] = method{}
2018-09-12 04:59:38 +03:00
return m[:len(m)-1]
}
// int handleInvoke handles a packet invoke request
// Side effects: s.isPlaying set to true upon av_NetStream_Publish_Start
func handleInvoke(s *Session, body []byte) error {
if body[0] != 0x02 {
return errInvalidBody
2018-08-14 11:52:37 +03:00
}
var obj C_AMFObject
nRes := C_AMF_Decode(&obj, body, 0)
2018-08-14 11:52:37 +03:00
if nRes < 0 {
return errDecoding
2018-08-14 11:52:37 +03:00
}
meth := C_AMFProp_GetString(C_AMF_GetProp(&obj, "", 0))
s.log(DebugLevel, pkg+"invoking method "+meth)
txn := C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 1))
2018-09-06 11:33:55 +03:00
switch meth {
2018-09-06 12:01:14 +03:00
case av__result:
2018-09-06 12:30:28 +03:00
var methodInvoked string
for i, m := range s.methodCalls {
2018-09-12 04:59:38 +03:00
if float64(m.num) == txn {
methodInvoked = m.name
s.methodCalls = eraseMethod(s.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 == "" {
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"received result without matching request", "id", txn)
2018-09-06 11:33:55 +03:00
goto leave
}
2019-01-09 10:03:19 +03:00
s.log(DebugLevel, pkg+"received result for "+methodInvoked)
2019-01-07 16:29:41 +03:00
2018-09-06 12:30:28 +03:00
switch methodInvoked {
case av_connect:
if s.link.token != "" {
s.log(FatalLevel, pkg+"no support for link token")
2018-09-06 11:33:55 +03:00
}
if (s.link.protocol & RTMP_FEATURE_WRITE) != 0 {
sendReleaseStream(s)
sendFCPublish(s)
2018-09-06 11:33:55 +03:00
} else {
s.log(FatalLevel, pkg+"link protocol has no RTMP_FEATURE_WRITE")
2018-09-06 11:33:55 +03:00
}
sendCreateStream(s)
if (s.link.protocol & RTMP_FEATURE_WRITE) == 0 {
s.log(FatalLevel, pkg+"link protocol has no RTMP_FEATURE_WRITE")
2018-09-06 11:33:55 +03:00
}
2018-09-06 12:30:28 +03:00
case av_createStream:
s.streamID = int32(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 3)))
if s.link.protocol&RTMP_FEATURE_WRITE != 0 {
sendPublish(s)
2018-09-06 11:33:55 +03:00
} else {
s.log(FatalLevel, pkg+"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:
s.log(FatalLevel, pkg+"unsupported method av_play/av_publish")
2018-08-13 05:45:43 +03:00
}
2018-09-06 11:33:55 +03:00
2018-09-06 12:01:14 +03:00
case av_onBWDone:
2019-01-09 15:21:07 +03:00
if s.checkCounter == 0 { // ToDo: why is this always zero?
sendCheckBW(s)
}
2018-09-06 12:01:14 +03:00
case av_onFCUnsubscribe, av_onFCSubscribe:
s.log(FatalLevel, pkg+"unsupported method av_onFCUnsubscribe/av_onFCSubscribe")
2018-09-06 12:01:14 +03:00
case av_ping:
s.log(FatalLevel, pkg+"unsupported method av_ping")
2018-09-06 12:01:14 +03:00
case av__onbwcheck:
s.log(FatalLevel, pkg+"unsupported method av_onbwcheck")
2018-09-06 12:01:14 +03:00
case av__onbwdone:
s.log(FatalLevel, pkg+"unsupported method av_onbwdone")
2018-09-06 12:01:14 +03:00
case av_close:
s.log(FatalLevel, pkg+"unsupported method av_close")
2018-09-06 12:01:14 +03:00
case av_onStatus:
var obj2 C_AMFObject
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))
2019-01-09 14:55:10 +03:00
level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, av_level, -1))
2019-01-07 16:29:41 +03:00
s.log(DebugLevel, pkg+"onStatus", "code", code, "level", level)
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:
s.log(FatalLevel, pkg+"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:
s.log(FatalLevel, pkg+"unsupported method av_NetStream_Play_Start/av_NetStream_Play_PublishNotify")
2018-09-06 12:01:14 +03:00
case av_NetStream_Publish_Start:
2019-01-10 05:19:52 +03:00
s.log(DebugLevel, pkg+"playing")
s.isPlaying = true
for i, m := range s.methodCalls {
2018-09-12 04:59:38 +03:00
if m.name == av_publish {
s.methodCalls = eraseMethod(s.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
}
2019-01-10 05:19:52 +03:00
// ToDo: handle case when av_publish method not found
2018-09-06 12:01:14 +03:00
case av_NetStream_Play_Complete, av_NetStream_Play_Stop, av_NetStream_Play_UnpublishNotify:
s.log(FatalLevel, pkg+"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:
s.log(FatalLevel, pkg+"unsupported method av_netStream_Seek_Notify")
2018-09-06 12:01:14 +03:00
case av_NetStream_Pause_Notify:
s.log(FatalLevel, pkg+"unsupported method av_NetStream_Pause_Notify")
}
2018-09-06 12:01:14 +03:00
case av_playlist_ready:
s.log(FatalLevel, pkg+"unsupported method av_playlist_ready")
default:
s.log(FatalLevel, pkg+"unknown method "+meth)
}
leave:
C_AMF_Reset(&obj)
return nil
}
func handshake(s *Session) error {
var clientbuf [RTMP_SIG_SIZE + 1]byte
2018-09-19 08:27:13 +03:00
clientsig := clientbuf[1:]
var serversig [RTMP_SIG_SIZE]byte
clientbuf[0] = RTMP_CHANNEL_CONTROL
binary.BigEndian.PutUint32(clientsig, uint32(time.Now().UnixNano()/1000000))
2018-09-19 08:27:13 +03:00
copy(clientsig[4:8], []byte{0, 0, 0, 0})
2018-06-27 21:02:16 +03:00
for i := 8; i < RTMP_SIG_SIZE; i++ {
2018-09-19 08:27:13 +03:00
clientsig[i] = byte(rand.Intn(256))
}
err := s.write(clientbuf[:])
if err != nil {
return err
}
2019-01-10 05:19:52 +03:00
s.log(DebugLevel, pkg+"handshake sent")
2018-07-12 22:51:17 +03:00
2018-09-19 08:27:13 +03:00
var typ [1]byte
err = s.read(typ[:])
if err != nil {
return err
}
2019-01-10 05:19:52 +03:00
s.log(DebugLevel, pkg+"handshake received")
2018-07-14 15:40:01 +03:00
2018-09-19 05:20:46 +03:00
if typ[0] != clientbuf[0] {
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"handshake type mismatch", "sent", clientbuf[0], "received", typ)
}
err = s.read(serversig[:])
if err != nil {
return err
2018-07-05 09:55:46 +03:00
}
// decode server response
2018-09-19 08:27:13 +03:00
suptime := binary.BigEndian.Uint32(serversig[:4])
2019-01-07 16:29:41 +03:00
s.log(DebugLevel, pkg+"server uptime", "uptime", suptime)
2018-08-11 07:18:21 +03:00
// 2nd part of handshake
err = s.write(serversig[:])
if err != nil {
return err
2018-08-11 07:18:21 +03:00
}
err = s.read(serversig[:])
if err != nil {
return err
}
if !bytes.Equal(serversig[:RTMP_SIG_SIZE], clientbuf[1:RTMP_SIG_SIZE+1]) {
2019-01-07 16:29:41 +03:00
s.log(WarnLevel, pkg+"signature mismatch", "serversig", serversig[:RTMP_SIG_SIZE], "clientsig", clientbuf[1:RTMP_SIG_SIZE+1])
}
return nil
2018-08-11 07:18:21 +03:00
}