rtmp: pull verbosity out of C code

This commit is contained in:
Dan Kortschak 2018-06-20 13:56:40 +09:30
parent 090ad746a6
commit 3a31812e0a
1 changed files with 28 additions and 7 deletions

View File

@ -43,6 +43,7 @@ import "C"
import (
"errors"
"strconv"
"unsafe"
)
@ -77,9 +78,10 @@ func (s *session) Open() error {
if s.rtmp != nil {
return errors.New("rtmp: attempt to start already running session")
}
s.rtmp = C.start_session(s.rtmp, C.CString(s.url), C.uint(s.timeout))
var err error
s.rtmp, err = 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 err
}
return nil
}
@ -87,10 +89,11 @@ func (s *session) Open() error {
// Write writes a frame (flv tag) to the rtmp connection
func (s *session) Write(data []byte) (int, error) {
if s.rtmp == nil {
return 0, errors.New("rtmp: attempt to write to non-running session")
return 0, Err(3)
}
if C.write_frame(s.rtmp, (*C.char)(unsafe.Pointer(&data[0])), C.uint(len(data))) != 0 {
return 0, errors.New("RTMP write error! Check rtmp log for details!")
ret := C.write_frame(s.rtmp, (*C.char)(unsafe.Pointer(&data[0])), C.uint(len(data)))
if ret != 0 {
return 0, Err(ret)
}
return len(data), nil
}
@ -98,12 +101,30 @@ func (s *session) Write(data []byte) (int, error) {
// Close terminates the rtmp connection
func (s *session) Close() error {
if s.rtmp == nil {
return errors.New("Tried to stop rtmp session, but not running!")
return Err(3)
}
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 Err(ret)
}
return nil
}
var rtmpErrs = [...]string{
1: "rtmp: not connected",
2: "rtmp: write error",
3: "rtmp: not started",
}
type Err uint
func (e Err) Error() string {
if 0 <= int(e) && int(e) < len(rtmpErrs) {
s := rtmpErrs[e]
if s != "" {
return s
}
}
return "rtmp: " + strconv.Itoa(int(e))
}