mirror of https://bitbucket.org/ausocean/av.git
Made type and variable names idiomatic, and merged C_RTMP into Session.
This commit is contained in:
parent
6975f5172e
commit
ad55d31577
929
rtmp/rtmp.go
929
rtmp/rtmp.go
File diff suppressed because it is too large
Load Diff
|
@ -7,9 +7,11 @@ DESCRIPTION
|
|||
|
||||
AUTHORS
|
||||
Saxon Nelson-Milton <saxon@ausocean.org>
|
||||
Dan Kortschak <dan@ausocean.org>
|
||||
Alan Noble <alan@ausocean.org>
|
||||
|
||||
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
|
||||
under the terms of the GNU General Public License as published by the
|
||||
|
@ -112,45 +114,28 @@ const (
|
|||
RTMP_MAX_HEADER_SIZE = 18
|
||||
)
|
||||
|
||||
// typedef struct RTMPChunk
|
||||
// rtmp.h +105
|
||||
type C_RTMPChunk struct {
|
||||
type chunk struct {
|
||||
headerSize int32
|
||||
data []byte
|
||||
header [RTMP_MAX_HEADER_SIZE]byte
|
||||
}
|
||||
|
||||
// typedef struct RTMPPacket
|
||||
// rtmp.h +113
|
||||
type C_RTMPPacket struct {
|
||||
type packet struct {
|
||||
headerType uint8
|
||||
packetType uint8
|
||||
hasAbsTimestamp bool
|
||||
nChannel int32
|
||||
nTimeStamp uint32
|
||||
nInfoField2 int32
|
||||
nBodySize uint32
|
||||
nBytesRead uint32
|
||||
chunk *C_RTMPChunk
|
||||
channel int32
|
||||
timestamp uint32
|
||||
info int32
|
||||
bodySize uint32
|
||||
bytesRead uint32
|
||||
chunk *chunk
|
||||
header []byte
|
||||
body []byte
|
||||
}
|
||||
|
||||
// typedef struct RTMPSockBuf
|
||||
// rtmp.h +127
|
||||
// DELETED: subsumed by C_RTMP_LNK
|
||||
|
||||
// RTMPPacket_IsReady(a)
|
||||
// rtmp.h +142
|
||||
func C_RTMPPacket_IsReady(p *C_RTMPPacket) bool {
|
||||
return p.nBytesRead == p.nBodySize
|
||||
}
|
||||
|
||||
// typedef struct RTMP_LNK
|
||||
// rtmp.h +144
|
||||
type C_RTMP_LNK struct {
|
||||
type link struct {
|
||||
host string
|
||||
playpath0 string
|
||||
playpath string
|
||||
tcUrl string
|
||||
swfUrl string
|
||||
|
@ -169,45 +154,7 @@ type C_RTMP_LNK struct {
|
|||
conn *net.TCPConn
|
||||
}
|
||||
|
||||
// typedef struct RTMPMethod
|
||||
// rtmp.h +231
|
||||
type C_RTMP_METHOD struct {
|
||||
type method struct {
|
||||
name string
|
||||
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
|
||||
}
|
||||
|
|
122
rtmp/session.go
122
rtmp/session.go
|
@ -8,9 +8,10 @@ DESCRIPTION
|
|||
AUTHORS
|
||||
Saxon Nelson-Milton <saxon@ausocean.org>
|
||||
Dan Kortschak <dan@ausocean.org>
|
||||
Alan Noble <alan@ausocean.org>
|
||||
|
||||
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
|
||||
under the terms of the GNU General Public License as published by the
|
||||
|
@ -36,14 +37,37 @@ import (
|
|||
"errors"
|
||||
)
|
||||
|
||||
// session provides parameters required for an rtmp communication session.
|
||||
// Session holds the state for an RTMP session.
|
||||
type Session struct {
|
||||
rtmp *C_RTMP
|
||||
url string
|
||||
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 {
|
||||
return &Session{
|
||||
url: url,
|
||||
|
@ -51,48 +75,94 @@ func NewSession(url string, connectTimeout uint) *Session {
|
|||
}
|
||||
}
|
||||
|
||||
// Open establishes an rtmp connection with the url passed into the
|
||||
// constructor
|
||||
// Open establishes an rtmp connection with the url passed into the constructor.
|
||||
func (s *Session) Open() error {
|
||||
if s.rtmp != nil {
|
||||
if s.isConnected() {
|
||||
return errors.New("rtmp: attempt to start already running session")
|
||||
}
|
||||
var err error
|
||||
s.rtmp, err = startSession(s.rtmp, s.url, s.timeout)
|
||||
if s.rtmp == nil {
|
||||
err := s.start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close terminates the rtmp connection
|
||||
func (s *Session) Close() error {
|
||||
if s.rtmp == nil {
|
||||
return Err(3)
|
||||
// start does the heavylifting for Open().
|
||||
func (s *Session) start() error {
|
||||
s.init()
|
||||
err := C_RTMP_SetupURL(s, s.url)
|
||||
if err != nil {
|
||||
s.close()
|
||||
return err
|
||||
}
|
||||
ret := endSession(s.rtmp)
|
||||
s.rtmp = nil
|
||||
if ret != 0 {
|
||||
return Err(ret)
|
||||
|
||||
C_RTMP_EnableWrite(s)
|
||||
err = C_RTMP_Connect(s, nil)
|
||||
if err != nil {
|
||||
s.close()
|
||||
return err
|
||||
}
|
||||
|
||||
err = C_RTMP_ConnectStream(s, 0)
|
||||
if err != nil {
|
||||
s.close()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write writes a frame (flv tag) to the rtmp connection
|
||||
func (s *Session) Write(data []byte) (int, error) {
|
||||
if s.rtmp == nil {
|
||||
return 0, Err(3)
|
||||
// init initializes various RTMP defauls.
|
||||
// ToDo: define consts for the magic numbers.
|
||||
func (s *Session) init() {
|
||||
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)
|
||||
}
|
||||
|
||||
err := C_RTMP_Write(s.rtmp, data)
|
||||
err := C_RTMP_Write(s, data)
|
||||
if err != nil {
|
||||
//if C.RTMP_Write(s.rtmp, (*byte)(unsafe.Pointer(&data[0])), int32(len(data))) == 0 {
|
||||
// TODO: propagate err
|
||||
return 0, Err(2)
|
||||
}
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
// isConnected returns true if the RTMP connection is up.
|
||||
func (s *Session) isConnected() bool {
|
||||
return s.link.conn != nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue