Made type and variable names idiomatic, and merged C_RTMP into Session.

This commit is contained in:
scruzin 2019-01-07 16:20:35 +10:30
parent 6975f5172e
commit ad55d31577
3 changed files with 525 additions and 647 deletions

File diff suppressed because it is too large Load Diff

View File

@ -7,9 +7,11 @@ DESCRIPTION
AUTHORS AUTHORS
Saxon Nelson-Milton <saxon@ausocean.org> Saxon Nelson-Milton <saxon@ausocean.org>
Dan Kortschak <dan@ausocean.org>
Alan Noble <alan@ausocean.org>
LICENSE LICENSE
rtmp_headers.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean) rtmp_headers.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them 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 under the terms of the GNU General Public License as published by the
@ -112,102 +114,47 @@ const (
RTMP_MAX_HEADER_SIZE = 18 RTMP_MAX_HEADER_SIZE = 18
) )
// typedef struct RTMPChunk type chunk struct {
// rtmp.h +105
type C_RTMPChunk struct {
headerSize int32 headerSize int32
data []byte data []byte
header [RTMP_MAX_HEADER_SIZE]byte header [RTMP_MAX_HEADER_SIZE]byte
} }
// typedef struct RTMPPacket type packet struct {
// rtmp.h +113
type C_RTMPPacket struct {
headerType uint8 headerType uint8
packetType uint8 packetType uint8
hasAbsTimestamp bool hasAbsTimestamp bool
nChannel int32 channel int32
nTimeStamp uint32 timestamp uint32
nInfoField2 int32 info int32
nBodySize uint32 bodySize uint32
nBytesRead uint32 bytesRead uint32
chunk *C_RTMPChunk chunk *chunk
header []byte header []byte
body []byte body []byte
} }
// typedef struct RTMPSockBuf type link struct {
// rtmp.h +127 host string
// DELETED: subsumed by C_RTMP_LNK playpath string
tcUrl string
// RTMPPacket_IsReady(a) swfUrl string
// rtmp.h +142 pageUrl string
func C_RTMPPacket_IsReady(p *C_RTMPPacket) bool { app string
return p.nBytesRead == p.nBodySize auth string
flashVer string
token string
extras C_AMFObject
seekTime int32
lFlags int32
swfAge int32
protocol int32
timeout uint
port uint16
conn *net.TCPConn
} }
// typedef struct RTMP_LNK type method struct {
// rtmp.h +144
type C_RTMP_LNK struct {
host string
playpath0 string
playpath string
tcUrl string
swfUrl string
pageUrl string
app string
auth string
flashVer string
token string
extras C_AMFObject
seekTime int32
lFlags int32
swfAge int32
protocol int32
timeout uint
port uint16
conn *net.TCPConn
}
// typedef struct RTMPMethod
// rtmp.h +231
type C_RTMP_METHOD struct {
name string name string
num int32 num int32
} }
// typedef struct RTMP
// rtmp.h +237
type C_RTMP struct {
inChunkSize int32
outChunkSize int32
nBWCheckCounter int32
nBytesIn int32
nBytesInSent int32
nBufferMS int32
streamID int32
mediaChannel int32
pausing int32
nServerBW int32
nClientBW int32
nClientBW2 uint8
bPlaying bool
bSendEncoding bool
numInvokes int32
methodCalls []C_RTMP_METHOD
channelsAllocatedIn int32
channelsAllocatedOut int32
vecChannelsIn []*C_RTMPPacket
vecChannelsOut []*C_RTMPPacket
channelTimestamp []int32
fAudioCodecs float64
fVideoCodecs float64
fEncoding float64
fDuration float64
msgCounter int32
resplen int32
unackd int32
write C_RTMPPacket
defered []byte
Link C_RTMP_LNK
}

View File

@ -8,9 +8,10 @@ DESCRIPTION
AUTHORS AUTHORS
Saxon Nelson-Milton <saxon@ausocean.org> Saxon Nelson-Milton <saxon@ausocean.org>
Dan Kortschak <dan@ausocean.org> Dan Kortschak <dan@ausocean.org>
Alan Noble <alan@ausocean.org>
LICENSE LICENSE
session.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean) session.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them 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 under the terms of the GNU General Public License as published by the
@ -36,14 +37,37 @@ import (
"errors" "errors"
) )
// session provides parameters required for an rtmp communication session. // Session holds the state for an RTMP session.
type Session struct { type Session struct {
rtmp *C_RTMP url string
url string timeout uint
timeout uint inChunkSize int32
outChunkSize int32
bwCheckCounter int32
nBytesIn int32
nBytesInSent int32
streamID int32
serverBW int32
clientBW int32
clientBW2 uint8
isPlaying bool
sendEncoding bool
numInvokes int32
methodCalls []method
channelsAllocatedIn int32
channelsAllocatedOut int32
vecChannelsIn []*packet
vecChannelsOut []*packet
channelTimestamp []int32
audioCodecs float64
videoCodecs float64
encoding float64
write packet
defered []byte
link link
} }
// NewSession returns a new session. // NewSession returns a new Session.
func NewSession(url string, connectTimeout uint) *Session { func NewSession(url string, connectTimeout uint) *Session {
return &Session{ return &Session{
url: url, url: url,
@ -51,48 +75,94 @@ func NewSession(url string, connectTimeout uint) *Session {
} }
} }
// Open establishes an rtmp connection with the url passed into the // Open establishes an rtmp connection with the url passed into the constructor.
// constructor
func (s *Session) Open() error { func (s *Session) Open() error {
if s.rtmp != nil { if s.isConnected() {
return errors.New("rtmp: attempt to start already running session") return errors.New("rtmp: attempt to start already running session")
} }
var err error err := s.start()
s.rtmp, err = startSession(s.rtmp, s.url, s.timeout) if err != nil {
if s.rtmp == nil {
return err return err
} }
return nil return nil
} }
// Close terminates the rtmp connection // start does the heavylifting for Open().
func (s *Session) Close() error { func (s *Session) start() error {
if s.rtmp == nil { s.init()
return Err(3) err := C_RTMP_SetupURL(s, s.url)
if err != nil {
s.close()
return err
} }
ret := endSession(s.rtmp)
s.rtmp = nil C_RTMP_EnableWrite(s)
if ret != 0 { err = C_RTMP_Connect(s, nil)
return Err(ret) if err != nil {
s.close()
return err
}
err = C_RTMP_ConnectStream(s, 0)
if err != nil {
s.close()
return err
} }
return nil return nil
} }
// Write writes a frame (flv tag) to the rtmp connection // init initializes various RTMP defauls.
func (s *Session) Write(data []byte) (int, error) { // ToDo: define consts for the magic numbers.
if s.rtmp == nil { func (s *Session) init() {
return 0, Err(3) s.inChunkSize = RTMP_DEFAULT_CHUNKSIZE
} s.outChunkSize = RTMP_DEFAULT_CHUNKSIZE
s.clientBW = 2500000
s.clientBW2 = 2
s.serverBW = 2500000
s.audioCodecs = 3191.0
s.videoCodecs = 252.0
s.link.timeout = s.timeout
s.link.swfAge = 30
}
if !C_RTMP_IsConnected(s.rtmp) { // Close terminates the rtmp connection,
func (s *Session) Close() error {
if !s.isConnected() {
return Err(3)
}
s.close()
return nil
}
// close does the heavylifting for Close().
// Any errors are ignored as it is often called in response to an earlier error.
func (s *Session) close() {
if s.isConnected() {
if s.streamID > 0 {
if s.link.protocol&RTMP_FEATURE_WRITE != 0 {
C_SendFCUnpublish(s)
}
C_SendDeleteStream(s, float64(s.streamID))
}
s.link.conn.Close()
}
s = &Session{}
}
// Write writes a frame (flv tag) to the rtmp connection.
func (s *Session) Write(data []byte) (int, error) {
if !s.isConnected() {
return 0, Err(1) return 0, Err(1)
} }
err := C_RTMP_Write(s, data)
err := C_RTMP_Write(s.rtmp, data)
if err != nil { if err != nil {
//if C.RTMP_Write(s.rtmp, (*byte)(unsafe.Pointer(&data[0])), int32(len(data))) == 0 {
// TODO: propagate err // TODO: propagate err
return 0, Err(2) return 0, Err(2)
} }
return len(data), nil return len(data), nil
} }
// isConnected returns true if the RTMP connection is up.
func (s *Session) isConnected() bool {
return s.link.conn != nil
}