diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 591af433..eedb2fa5 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -311,13 +311,42 @@ func (s *session) Open() error { return errors.New("rtmp: attempt to start already running session") } var err error - s.rtmp, err = C.start_session(s.rtmp, C.CString(s.url), C.uint(s.timeout)) + s.rtmp, err = startSession(s.rtmp, s.url, uint32(s.timeout)) if s.rtmp == nil { return err } return nil } +func startSession(rtmp *C.RTMP, u string, timeout uint32) (*C.RTMP, error) { + url := C.CString(u) + connect_timeout := C.int(timeout) + rtmp = C.RTMP_Alloc() + C.RTMP_Init(rtmp) + rtmp.Link.timeout = connect_timeout + if C.RTMP_SetupURL(rtmp, url) == 0 { + C.RTMP_Close(rtmp) + C.RTMP_Free(rtmp) + return nil, errors.New("rtmp startSession: Failed to setup URL!") + } + + C.RTMP_EnableWrite(rtmp) + C.RTMP_SetBufferMS(rtmp, 3600*1000) + if C.RTMP_Connect(rtmp, nil) == 0 { + C.RTMP_Close(rtmp) + C.RTMP_Free(rtmp) + return nil, errors.New("rtmp startSession: Failed to connect!") + } + + if C.RTMP_ConnectStream(rtmp, 0) == 0 { + C.RTMP_Close(rtmp) + C.RTMP_Free(rtmp) + return nil, errors.New("rtmp startSession: Failed to connect stream!") + } + + return rtmp, nil +} + // Close terminates the rtmp connection func (s *session) Close() error { if s.rtmp == nil {