2018-01-09 07:26:34 +03:00
|
|
|
package camstreamer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CamStreamer struct {
|
|
|
|
RtspUrl string
|
|
|
|
RtpUrl string
|
|
|
|
RtpPort uint16
|
|
|
|
RtcpPort uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *CamStreamer)Connect()(session *RtpSession, err error){
|
|
|
|
var res string
|
|
|
|
sess := rtsp.NewSession()
|
|
|
|
|
|
|
|
if res, err = sess.Options(sc.RtspUrl); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res, err = sess.Describe(rtspUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
p, err := rtsp.ParseSdp(&io.LimitedReader{R: res.Body, N: res.ContentLength})
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
log.Printf("%+v", p)
|
|
|
|
res, err = sess.Setup(rtpUrl, fmt.Sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpPort, rtcpPort))
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
log.Println(res)
|
|
|
|
res, err = sess.Play(rtspUrl, res.Header.Get("Session"))
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
log.Println(res)
|
|
|
|
// create udp connection for rtp stuff
|
|
|
|
rtpLaddr, err := net.ResolveUDPAddr("udp", "192.168.0.109:17300")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Local rtp addr not set! %v\n", err)
|
|
|
|
}
|
|
|
|
rtpAddr, err := net.ResolveUDPAddr("udp", "192.168.0.50:17300")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Resolving rtp address didn't work! %v\n", err)
|
|
|
|
}
|
|
|
|
rtpConn, err := net.DialUDP("udp", rtpLaddr, rtpAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Conncection not established! %v\n", err)
|
|
|
|
}
|
|
|
|
// Create udp connection for rtcp stuff
|
|
|
|
rtcpLaddr, err := net.ResolveUDPAddr("udp", "192.168.0.109:17319")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Local RTCP address not resolved! %v\n", err)
|
|
|
|
}
|
|
|
|
rtcpAddr, err := net.ResolveUDPAddr("udp", "192.168.0.50:17301")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Remote RTCP address not resolved! %v\n", err)
|
|
|
|
}
|
|
|
|
rtcpConn, err := net.DialUDP("udp", rtcpLaddr, rtcpAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Connection not established! %v\n", err)
|
|
|
|
}
|
|
|
|
// let's create a session that will store useful stuff from the connections
|
|
|
|
rtpSession := NewSession(rtpConn, rtcpConn)
|
|
|
|
}
|
2018-01-10 06:57:56 +03:00
|
|
|
|
|
|
|
/*******************************************************
|
|
|
|
Testing stuff related to connection i.e. rtsp, rtp, rtcp
|
|
|
|
********************************************************/
|
|
|
|
const (
|
|
|
|
rtpPort = 17300
|
|
|
|
rtcpPort = 17319
|
|
|
|
rtspUrl = "rtsp://192.168.0.50:8554/CH002.sdp"
|
|
|
|
rtpUrl = "rtsp://192.168.0.50:8554/CH002.sdp/track1"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Let's see if we can connect to an rtsp device then read an rtp stream,
|
|
|
|
and then convert the rtp packets to mpegts packets and output. */
|
|
|
|
func TestRTSP(t *testing.T) {
|
|
|
|
sess := rtsp.NewSession()
|
|
|
|
res, err := sess.Options(rtspUrl)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = sess.Describe(rtspUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
p, err := rtsp.ParseSdp(&io.LimitedReader{R: res.Body, N: res.ContentLength})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
log.Printf("%+v", p)
|
|
|
|
res, err = sess.Setup(rtpUrl, fmt.Sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpPort, rtcpPort))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
log.Println(res)
|
|
|
|
res, err = sess.Play(rtspUrl, res.Header.Get("Session"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
log.Println(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRTP(t *testing.T) {
|
|
|
|
sess := rtsp.NewSession()
|
|
|
|
res, err := sess.Options(rtspUrl)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
res, err = sess.Describe(rtspUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
p, err := rtsp.ParseSdp(&io.LimitedReader{R: res.Body, N: res.ContentLength})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
log.Printf("%+v", p)
|
|
|
|
res, err = sess.Setup(rtpUrl, fmt.Sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpPort, rtcpPort))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
log.Println(res)
|
|
|
|
res, err = sess.Play(rtspUrl, res.Header.Get("Session"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shouldn't have got error: %v\n", err)
|
|
|
|
}
|
|
|
|
log.Println(res)
|
|
|
|
// create udp connection for rtp stuff
|
|
|
|
rtpLaddr, err := net.ResolveUDPAddr("udp", "192.168.0.109:17300")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Local rtp addr not set! %v\n", err)
|
|
|
|
}
|
|
|
|
rtpAddr, err := net.ResolveUDPAddr("udp", "192.168.0.50:17300")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Resolving rtp address didn't work! %v\n", err)
|
|
|
|
}
|
|
|
|
rtpConn, err := net.DialUDP("udp", rtpLaddr, rtpAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Conncection not established! %v\n", err)
|
|
|
|
}
|
|
|
|
// Create udp connection for rtcp stuff
|
|
|
|
rtcpLaddr, err := net.ResolveUDPAddr("udp", "192.168.0.109:17319")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Local RTCP address not resolved! %v\n", err)
|
|
|
|
}
|
|
|
|
rtcpAddr, err := net.ResolveUDPAddr("udp", "192.168.0.50:17301")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Remote RTCP address not resolved! %v\n", err)
|
|
|
|
}
|
|
|
|
rtcpConn, err := net.DialUDP("udp", rtcpLaddr, rtcpAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Connection not established! %v\n", err)
|
|
|
|
}
|
|
|
|
// let's create a session that will store useful stuff from the connections
|
|
|
|
rtpSession := NewSession(rtpConn, rtcpConn)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
t.Errorf("Should have got rtpPacket!")
|
|
|
|
case rtpPacket := <-rtpSession.RtpChan:
|
|
|
|
fmt.Printf("RTP packet: %v\n", rtpPacket)
|
|
|
|
}
|
|
|
|
}
|