2018-08-24 03:55:36 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
session.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
2019-01-07 08:50:35 +03:00
|
|
|
Alan Noble <alan@ausocean.org>
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
LICENSE
|
2019-01-07 08:50:35 +03:00
|
|
|
session.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2018-08-24 03:22:51 +03:00
|
|
|
package rtmp
|
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// Session holds the state for an RTMP session.
|
2018-08-24 03:22:51 +03:00
|
|
|
type Session struct {
|
2019-01-07 08:50:35 +03:00
|
|
|
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
|
2019-01-07 16:29:41 +03:00
|
|
|
deferred []byte
|
2019-01-07 08:50:35 +03:00
|
|
|
link link
|
2019-01-07 16:29:41 +03:00
|
|
|
log Log
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 16:29:41 +03:00
|
|
|
// Log defines the RTMP logging function.
|
|
|
|
type Log func(level int8, message string, params ...interface{})
|
|
|
|
|
|
|
|
// Log levels used by Log.
|
|
|
|
const (
|
|
|
|
DebugLevel int8 = -1
|
|
|
|
InfoLevel int8 = 0
|
|
|
|
WarnLevel int8 = 1
|
|
|
|
ErrorLevel int8 = 2
|
|
|
|
FatalLevel int8 = 5
|
|
|
|
)
|
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// NewSession returns a new Session.
|
2019-01-07 16:29:41 +03:00
|
|
|
func NewSession(url string, timeout uint, log Log) *Session {
|
2018-08-24 03:22:51 +03:00
|
|
|
return &Session{
|
2019-01-09 14:35:04 +03:00
|
|
|
url: url,
|
|
|
|
inChunkSize: 128,
|
|
|
|
outChunkSize: 128,
|
|
|
|
clientBW: 2500000,
|
|
|
|
clientBW2: 2,
|
|
|
|
serverBW: 2500000,
|
|
|
|
audioCodecs: 3191.0,
|
|
|
|
videoCodecs: 252.0,
|
|
|
|
log: log,
|
|
|
|
link: link{
|
|
|
|
timeout: timeout,
|
|
|
|
swfAge: 30,
|
|
|
|
},
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// Open establishes an rtmp connection with the url passed into the constructor.
|
2018-08-24 03:22:51 +03:00
|
|
|
func (s *Session) Open() error {
|
2019-01-07 08:50:35 +03:00
|
|
|
if s.isConnected() {
|
2019-01-07 16:29:41 +03:00
|
|
|
return errConnected
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
err := s.start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// start does the heavylifting for Open().
|
|
|
|
func (s *Session) start() error {
|
2019-01-09 10:03:19 +03:00
|
|
|
s.log(DebugLevel, pkg+"Session.start")
|
2019-01-07 10:30:42 +03:00
|
|
|
err := setupURL(s, s.url)
|
2019-01-07 08:50:35 +03:00
|
|
|
if err != nil {
|
|
|
|
s.close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-07 10:30:42 +03:00
|
|
|
s.enableWrite()
|
2019-01-07 14:15:00 +03:00
|
|
|
err = connect(s)
|
2019-01-07 08:50:35 +03:00
|
|
|
if err != nil {
|
|
|
|
s.close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:15:00 +03:00
|
|
|
err = connectStream(s)
|
2019-01-07 08:50:35 +03:00
|
|
|
if err != nil {
|
|
|
|
s.close()
|
2018-08-24 03:22:51 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// Close terminates the rtmp connection,
|
2018-08-24 03:22:51 +03:00
|
|
|
func (s *Session) Close() error {
|
2019-01-07 08:50:35 +03:00
|
|
|
if !s.isConnected() {
|
2019-01-07 10:30:42 +03:00
|
|
|
return errNotConnected
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
s.close()
|
2018-08-24 03:22:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// 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() {
|
2019-01-09 10:03:19 +03:00
|
|
|
s.log(DebugLevel, pkg+"Session.close")
|
2019-01-07 08:50:35 +03:00
|
|
|
if s.isConnected() {
|
|
|
|
if s.streamID > 0 {
|
|
|
|
if s.link.protocol&RTMP_FEATURE_WRITE != 0 {
|
2019-01-07 10:30:42 +03:00
|
|
|
sendFCUnpublish(s)
|
2019-01-07 08:50:35 +03:00
|
|
|
}
|
2019-01-07 10:30:42 +03:00
|
|
|
sendDeleteStream(s, float64(s.streamID))
|
2019-01-07 08:50:35 +03:00
|
|
|
}
|
|
|
|
s.link.conn.Close()
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-07 14:28:19 +03:00
|
|
|
*s = Session{}
|
2019-01-07 08:50:35 +03:00
|
|
|
}
|
2018-08-24 03:22:51 +03:00
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// Write writes a frame (flv tag) to the rtmp connection.
|
2019-01-07 14:41:35 +03:00
|
|
|
func (s *Session) Write(data []byte) (int, error) {
|
2019-01-07 08:50:35 +03:00
|
|
|
if !s.isConnected() {
|
2019-01-07 10:30:42 +03:00
|
|
|
return 0, errNotConnected
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-07 10:30:42 +03:00
|
|
|
err := s.write(data)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
2019-01-07 10:30:42 +03:00
|
|
|
return 0, err
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
|
|
|
return len(data), nil
|
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
|
|
|
|
// isConnected returns true if the RTMP connection is up.
|
|
|
|
func (s *Session) isConnected() bool {
|
|
|
|
return s.link.conn != nil
|
|
|
|
}
|
2019-01-07 10:30:42 +03:00
|
|
|
|
|
|
|
// enableWrite enables the current session for writing.
|
|
|
|
func (s *Session) enableWrite() {
|
|
|
|
s.link.protocol |= RTMP_FEATURE_WRITE
|
|
|
|
}
|