2017-12-11 06:53:06 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-12-12 09:40:32 +03:00
|
|
|
//"bytes"
|
2017-12-11 06:53:06 +03:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
2017-12-12 09:40:32 +03:00
|
|
|
_"time"
|
2017-12-11 06:53:06 +03:00
|
|
|
|
2017-12-11 07:54:49 +03:00
|
|
|
"../packet"
|
2017-12-11 06:53:06 +03:00
|
|
|
|
|
|
|
"github.com/beatgammit/rtsp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(flag.Args()) >= 1 {
|
2017-12-11 07:54:49 +03:00
|
|
|
rtspUrl := flag.Args()[0]
|
|
|
|
rtpUrl := flag.Args()[1]
|
|
|
|
|
|
|
|
sess := rtsp.NewSession()
|
|
|
|
res, err := sess.Options(rtspUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Options:")
|
|
|
|
fmt.Println(res)
|
|
|
|
|
|
|
|
res, err = sess.Describe(rtspUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Describe:")
|
|
|
|
fmt.Println(res)
|
|
|
|
|
|
|
|
p, err := rtsp.ParseSdp(&io.LimitedReader{R: res.Body, N: res.ContentLength})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
log.Printf("%+v", p)
|
2017-12-11 06:53:06 +03:00
|
|
|
|
|
|
|
fmt.Println("Setting up!")
|
2017-12-11 07:54:49 +03:00
|
|
|
rtpPort, rtcpPort := 17300, 17319
|
|
|
|
res, err = sess.Setup(rtpUrl, fmt.Sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpPort, rtcpPort))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
log.Println(res)
|
2017-12-11 06:53:06 +03:00
|
|
|
|
|
|
|
fmt.Println("Playing !")
|
2017-12-11 07:54:49 +03:00
|
|
|
res, err = sess.Play(rtspUrl, res.Header.Get("Session"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
log.Println(res)
|
|
|
|
|
|
|
|
// create udp connection for rtp stuff
|
|
|
|
rtpLaddr, err := net.ResolveUDPAddr("udp", "192.168.0.109:17300")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Local rtp addr not set!")
|
|
|
|
}
|
|
|
|
rtpAddr, err := net.ResolveUDPAddr("udp", "192.168.0.50:17300")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Resolving rtp address didn't work!")
|
|
|
|
}
|
|
|
|
rtpConn, err := net.DialUDP("udp", rtpLaddr, rtpAddr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Rtp dial didn't work!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create udp connection for rtcp stuff
|
|
|
|
rtcpLaddr, err := net.ResolveUDPAddr("udp", "192.168.0.109:17319")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Local ")
|
|
|
|
}
|
|
|
|
rtcpAddr, err := net.ResolveUDPAddr("udp", "192.168.0.50:17301")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("resolving rtcp address didn't work!")
|
|
|
|
}
|
|
|
|
rtcpConn, err := net.DialUDP("udp", rtcpLaddr, rtcpAddr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Rtcp dial didnt't work!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// let's create a session that will store useful stuff from the connections
|
2017-12-13 03:03:37 +03:00
|
|
|
rtpSession := packet.NewSession(rtpConn, rtcpConn)
|
2017-12-12 09:40:32 +03:00
|
|
|
converter := packet.NewRtpToTsConverter()
|
|
|
|
go converter.Convert()
|
2017-12-11 07:54:49 +03:00
|
|
|
for {
|
2017-12-12 09:40:32 +03:00
|
|
|
select{
|
|
|
|
default:
|
|
|
|
case rtpPacket := <-rtpSession.RtpChan:
|
|
|
|
converter.RtpChan<-rtpPacket
|
|
|
|
case tsPacket:=<-converter.TsChan:
|
2017-12-13 03:03:37 +03:00
|
|
|
fmt.Println(tsPacket.ToByteSlice())
|
2017-12-12 09:40:32 +03:00
|
|
|
}
|
2017-12-11 07:54:49 +03:00
|
|
|
}
|
|
|
|
}
|
2017-12-11 06:53:06 +03:00
|
|
|
}
|