From b3b1b048147d764eca8f903556f9d35456494af7 Mon Sep 17 00:00:00 2001 From: scruzin Date: Fri, 11 Jan 2019 00:29:51 +1030 Subject: [PATCH] Merged Session.start()/close() into Open()/Close() respectively. --- rtmp/session.go | 46 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/rtmp/session.go b/rtmp/session.go index 2ea8e05f..aa1ee80b 100644 --- a/rtmp/session.go +++ b/rtmp/session.go @@ -127,63 +127,46 @@ func NewSession(url string, timeout uint, log Log) *Session { // Open establishes an rtmp connection with the url passed into the constructor. func (s *Session) Open() error { + s.log(DebugLevel, pkg+"Session.Open") if s.isConnected() { return errConnected } - err := s.start() - if err != nil { - return err - } - return nil -} - -// start does the heavylifting for Open(). -func (s *Session) start() error { - s.log(DebugLevel, pkg+"Session.start") err := setupURL(s) if err != nil { - s.close() return err } s.enableWrite() err = connect(s) if err != nil { - s.close() + s.Close() return err } err = connectStream(s) if err != nil { - s.close() + s.Close() return err } return nil } -// Close terminates the rtmp connection, +// Close terminates the rtmp connection. +// NB: The session object is cleared completely. func (s *Session) Close() error { + s.log(DebugLevel, pkg+"Session.Close") if !s.isConnected() { return errNotConnected } - 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() { - s.log(DebugLevel, pkg+"Session.close") - if s.isConnected() { - if s.streamID > 0 { - if s.link.protocol&RTMP_FEATURE_WRITE != 0 { - sendFCUnpublish(s) - } - sendDeleteStream(s, float64(s.streamID)) + if s.streamID > 0 { + if s.link.protocol&RTMP_FEATURE_WRITE != 0 { + sendFCUnpublish(s) } - s.link.conn.Close() + sendDeleteStream(s, float64(s.streamID)) } + s.link.conn.Close() *s = Session{} + return nil } // Write writes a frame (flv tag) to the rtmp connection. @@ -216,7 +199,10 @@ func (s *Session) Write(data []byte) (int, error) { } // I/O functions -// read from an RTMP connection. + +// read from an RTMP connection. Sends a bytes received message if the +// number of bytes received (nBytesIn) is greater than the number sent +// (nBytesInSent) by 10% of the bandwidth. func (s *Session) read(buf []byte) error { err := s.link.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(s.link.timeout))) if err != nil {