2018-05-29 08:56:36 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
rtmp.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
rtmp.go is Copyright (C) 2017 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rtmp
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -I/usr/local/include/librtmp
|
2018-05-30 04:55:21 +03:00
|
|
|
#cgo LDFLAGS: -lrtmp -lz
|
2018-05-29 08:56:36 +03:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <rtmp.h>
|
|
|
|
|
2018-05-30 09:22:33 +03:00
|
|
|
RTMP* start_session(RTMP* rtmp, char* url, uint connect_timeout);
|
2018-05-29 08:56:36 +03:00
|
|
|
int write_frame(RTMP* rtmp, char* data, uint data_length);
|
|
|
|
int end_session(RTMP* rtmp);
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Session provides an interface for sending flv tags over rtmp.
|
|
|
|
type Session interface {
|
|
|
|
StartSession() error
|
2018-06-04 04:59:35 +03:00
|
|
|
Write([]byte) (int, error)
|
2018-05-29 08:56:36 +03:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// session provides parameters required for an rtmp communication session.
|
|
|
|
type session struct {
|
|
|
|
rtmp *C.RTMP
|
|
|
|
|
|
|
|
url string
|
|
|
|
timeout uint
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Session = (*session)(nil)
|
|
|
|
|
|
|
|
// NewSession returns a new session.
|
|
|
|
func NewSession(url string, connectTimeout uint) Session {
|
|
|
|
return &session{
|
|
|
|
url: url,
|
|
|
|
timeout: connectTimeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartSession establishes an rtmp connection with the url passed into the
|
|
|
|
// constructor
|
|
|
|
func (s *session) StartSession() error {
|
2018-05-30 09:22:33 +03:00
|
|
|
if s.rtmp != nil {
|
2018-05-29 08:56:36 +03:00
|
|
|
return errors.New("rtmp: attempt to start already running session")
|
|
|
|
}
|
2018-05-30 09:22:33 +03:00
|
|
|
s.rtmp = C.start_session(s.rtmp, C.CString(s.url), C.uint(s.timeout))
|
|
|
|
if s.rtmp == nil {
|
2018-05-29 08:56:36 +03:00
|
|
|
return errors.New("RTMP start error! Check rtmp log for details!")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes a frame (flv tag) to the rtmp connection
|
2018-06-04 04:59:35 +03:00
|
|
|
func (s *session) Write(data []byte) (int, error) {
|
2018-05-30 09:22:33 +03:00
|
|
|
if s.rtmp == nil {
|
2018-06-04 04:59:35 +03:00
|
|
|
return 0, errors.New("rtmp: attempt to write to non-running session")
|
2018-05-29 08:56:36 +03:00
|
|
|
}
|
|
|
|
if C.write_frame(s.rtmp, (*C.char)(unsafe.Pointer(&data[0])), C.uint(len(data))) != 0 {
|
2018-06-04 04:59:35 +03:00
|
|
|
return 0, errors.New("RTMP write error! Check rtmp log for details!")
|
2018-05-29 08:56:36 +03:00
|
|
|
}
|
2018-06-04 04:59:35 +03:00
|
|
|
return len(data), nil
|
2018-05-29 08:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close terminates the rtmp connection
|
|
|
|
func (s *session) Close() error {
|
2018-05-30 09:22:33 +03:00
|
|
|
if s.rtmp == nil {
|
2018-05-29 08:56:36 +03:00
|
|
|
return errors.New("Tried to stop rtmp session, but not running!")
|
|
|
|
}
|
2018-05-30 09:22:33 +03:00
|
|
|
ret := C.end_session(s.rtmp)
|
|
|
|
s.rtmp = nil
|
|
|
|
if ret != 0 {
|
2018-05-29 08:56:36 +03:00
|
|
|
return errors.New("RTMP end session error! Check rtmp log for details!")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|