diff --git a/rtmp/rtmp.c b/rtmp/rtmp.c index 5a63bdf1..26aa44e0 100644 --- a/rtmp/rtmp.c +++ b/rtmp/rtmp.c @@ -33,7 +33,7 @@ LICENSE #include #include -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); rtmp = RTMP_Alloc(); RTMP_Init(rtmp); @@ -42,7 +42,7 @@ unsigned int start_session(RTMP* rtmp, char* url, uint connect_timeout) { printf("Can't setup url!\n"); RTMP_Close(rtmp); RTMP_Free(rtmp); - return 1; + return NULL; } RTMP_EnableWrite(rtmp); @@ -51,17 +51,17 @@ unsigned int start_session(RTMP* rtmp, char* url, uint connect_timeout) { printf("RTMP can't connect!\n"); RTMP_Close(rtmp); RTMP_Free(rtmp); - return 1; + return NULL; } if (!RTMP_ConnectStream(rtmp, 0)) { printf("RTMP can't connect stream!\n"); RTMP_Close(rtmp); RTMP_Free(rtmp); - return 1; + return NULL; } - return 0; + return rtmp; } 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_Free(rtmp); - rtmp = NULL; return 0; } diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 24420927..784f1a94 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -35,7 +35,7 @@ package rtmp #include #include -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 end_session(RTMP* rtmp); */ @@ -59,7 +59,6 @@ type session struct { url string timeout uint - running bool } 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 // constructor func (s *session) StartSession() error { - if s.running { + if s.rtmp != nil { 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!") } - s.running = true return nil } // Write writes a frame (flv tag) to the rtmp connection 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") } 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 func (s *session) Close() error { - if !s.running { + if s.rtmp == nil { return errors.New("Tried to stop rtmp session, but not running!") } - s.running = false - if C.end_session(s.rtmp) != 0 { + ret := C.end_session(s.rtmp) + s.rtmp = nil + if ret != 0 { return errors.New("RTMP end session error! Check rtmp log for details!") } return nil