Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose functionality for client side applications #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions srtgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,27 +277,41 @@ func (s SrtSocket) Connect() error {
return nil
}

// Read data from the SRT socket
func (s SrtSocket) Read(b []byte) (n int, err error) {
// ReadMsg from the socket including SRT control data.
func (s SrtSocket) ReadMsg(b []byte) (n int, ctrl SrtMsgCtrl, err error) {
if !s.blocking {
len := C.int(2)
timeoutMs := C.int64_t(s.pollTimeout)
ready := [2]C.int{SRT_INVALID_SOCK, SRT_INVALID_SOCK}

if C.srt_epoll_wait(s.epollIo, &ready[0], &len, nil, nil, timeoutMs, nil, nil, nil, nil) == SRT_ERROR {
if C.srt_getlasterror(nil) == C.SRT_ETIMEOUT {
return 0, nil
return
}
return 0, fmt.Errorf("error in read:epoll %s", C.GoString(C.srt_getlasterror_str()))
err = fmt.Errorf("error in read:epoll %s", C.GoString(C.srt_getlasterror_str()))
return
}
}

res := C.srt_recvmsg2(s.socket, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)), nil)
var c C.SRT_MSGCTRL

res := C.srt_recvmsg2(s.socket, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)), &c)

if res == SRT_ERROR {
return 0, fmt.Errorf("error in read::recv %s", C.GoString(C.srt_getlasterror_str()))
err = fmt.Errorf("error in read::recv %s", C.GoString(C.srt_getlasterror_str()))
return
}

return int(res), nil
n = int(res)
ctrl = newSrtMsgCtrl(&c)

return
}

// Read data from the SRT socket
func (s SrtSocket) Read(b []byte) (n int, err error) {
n, _, err = s.ReadMsg(b)
return
}

// Write data to the SRT socket
Expand Down Expand Up @@ -621,3 +635,8 @@ func (s SrtSocket) postconfiguration(sck *SrtSocket) error {
err := setSocketOptions(sck.socket, bindingPost, s.options)
return err
}

// Now - Time in microseconds elapsed since epoch using SRT internal clock
func Now() int64 {
return int64(C.srt_time_now())
}
24 changes: 24 additions & 0 deletions srtmsgctrl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package srtgo

// #cgo LDFLAGS: -lsrt
// #include <srt/srt.h>
import "C"

type SrtMsgCtrl struct {
MsgTTL int // TTL for a message (millisec), default -1 (no TTL limitation)
InOrder int // Whether a message is allowed to supersede partially lost one. Unused in stream and live mode.
Boundary int // 0:mid pkt, 1(01b):end of frame, 2(11b):complete frame, 3(10b): start of frame
SrcTime int64 // source time since epoch (usec), 0: use internal time (sender)
PktSeq int32 // sequence number of the first packet in received message (unused for sending)
MsgNo int32 // message number (output value for both sending and receiving)
}

func newSrtMsgCtrl(ctrl *C.SRT_MSGCTRL) (res SrtMsgCtrl) {
res.MsgTTL = int(ctrl.msgttl)
res.InOrder = int(ctrl.inorder)
res.Boundary = int(ctrl.boundary)
res.SrcTime = int64(ctrl.srctime)
res.PktSeq = int32(ctrl.pktseq)
res.MsgNo = int32(ctrl.msgno)
return
}