rtmp: fix RTMP* handling

This commit is contained in:
Dan Kortschak 2018-05-30 15:52:33 +09:30
parent 369beab55c
commit fb64a47d89
2 changed files with 14 additions and 15 deletions

View File

@ -33,7 +33,7 @@ LICENSE
#include <log.h> #include <log.h>
#include <rtmp.h> #include <rtmp.h>
unsigned int start_session(RTMP* rtmp, char* url, uint connect_timeout) { RTMP* start_session(RTMP* rtmp, char* url, uint connect_timeout) {
printf("RTMP url: %s\n", url); printf("RTMP url: %s\n", url);
rtmp = RTMP_Alloc(); rtmp = RTMP_Alloc();
RTMP_Init(rtmp); RTMP_Init(rtmp);
@ -42,7 +42,7 @@ unsigned int start_session(RTMP* rtmp, char* url, uint connect_timeout) {
printf("Can't setup url!\n"); printf("Can't setup url!\n");
RTMP_Close(rtmp); RTMP_Close(rtmp);
RTMP_Free(rtmp); RTMP_Free(rtmp);
return 1; return NULL;
} }
RTMP_EnableWrite(rtmp); RTMP_EnableWrite(rtmp);
@ -51,17 +51,17 @@ unsigned int start_session(RTMP* rtmp, char* url, uint connect_timeout) {
printf("RTMP can't connect!\n"); printf("RTMP can't connect!\n");
RTMP_Close(rtmp); RTMP_Close(rtmp);
RTMP_Free(rtmp); RTMP_Free(rtmp);
return 1; return NULL;
} }
if (!RTMP_ConnectStream(rtmp, 0)) { if (!RTMP_ConnectStream(rtmp, 0)) {
printf("RTMP can't connect stream!\n"); printf("RTMP can't connect stream!\n");
RTMP_Close(rtmp); RTMP_Close(rtmp);
RTMP_Free(rtmp); RTMP_Free(rtmp);
return 1; return NULL;
} }
return 0; return rtmp;
} }
unsigned int write_frame(RTMP* rtmp, char* data, uint data_length) { unsigned int write_frame(RTMP* rtmp, char* data, uint data_length) {
@ -92,6 +92,5 @@ unsigned int end_session(RTMP* rtmp) {
RTMP_Close(rtmp); RTMP_Close(rtmp);
RTMP_Free(rtmp); RTMP_Free(rtmp);
rtmp = NULL;
return 0; return 0;
} }

View File

@ -35,7 +35,7 @@ package rtmp
#include <stdlib.h> #include <stdlib.h>
#include <rtmp.h> #include <rtmp.h>
int start_session(RTMP* rtmp, char* url, uint connect_timeout); RTMP* start_session(RTMP* rtmp, char* url, uint connect_timeout);
int write_frame(RTMP* rtmp, char* data, uint data_length); int write_frame(RTMP* rtmp, char* data, uint data_length);
int end_session(RTMP* rtmp); int end_session(RTMP* rtmp);
*/ */
@ -59,7 +59,6 @@ type session struct {
url string url string
timeout uint timeout uint
running bool
} }
var _ Session = (*session)(nil) var _ Session = (*session)(nil)
@ -75,19 +74,19 @@ func NewSession(url string, connectTimeout uint) Session {
// StartSession establishes an rtmp connection with the url passed into the // StartSession establishes an rtmp connection with the url passed into the
// constructor // constructor
func (s *session) StartSession() error { func (s *session) StartSession() error {
if s.running { if s.rtmp != nil {
return errors.New("rtmp: attempt to start already running session") return errors.New("rtmp: attempt to start already running session")
} }
if C.start_session(s.rtmp, C.CString(s.url), C.uint(s.timeout)) != 0 { s.rtmp = C.start_session(s.rtmp, C.CString(s.url), C.uint(s.timeout))
if s.rtmp == nil {
return errors.New("RTMP start error! Check rtmp log for details!") return errors.New("RTMP start error! Check rtmp log for details!")
} }
s.running = true
return nil return nil
} }
// Write writes a frame (flv tag) to the rtmp connection // Write writes a frame (flv tag) to the rtmp connection
func (s *session) Write(data []byte) error { func (s *session) Write(data []byte) error {
if !s.running { if s.rtmp == nil {
return errors.New("rtmp: attempt to write to non-running session") return errors.New("rtmp: attempt to write to non-running session")
} }
if C.write_frame(s.rtmp, (*C.char)(unsafe.Pointer(&data[0])), C.uint(len(data))) != 0 { if C.write_frame(s.rtmp, (*C.char)(unsafe.Pointer(&data[0])), C.uint(len(data))) != 0 {
@ -98,11 +97,12 @@ func (s *session) Write(data []byte) error {
// Close terminates the rtmp connection // Close terminates the rtmp connection
func (s *session) Close() error { func (s *session) Close() error {
if !s.running { if s.rtmp == nil {
return errors.New("Tried to stop rtmp session, but not running!") return errors.New("Tried to stop rtmp session, but not running!")
} }
s.running = false ret := C.end_session(s.rtmp)
if C.end_session(s.rtmp) != 0 { s.rtmp = nil
if ret != 0 {
return errors.New("RTMP end session error! Check rtmp log for details!") return errors.New("RTMP end session error! Check rtmp log for details!")
} }
return nil return nil