Skip to content

Commit

Permalink
add rtsp server
Browse files Browse the repository at this point in the history
  • Loading branch information
deepch committed Sep 21, 2020
1 parent 0993369 commit fa2e00d
Show file tree
Hide file tree
Showing 20 changed files with 887 additions and 110 deletions.
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
agreement
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ RTSP Stream to WebBrowser MSE or WebRTC or HLS, full native! not use ffmpeg or g
- [Installation from binary](#Installation from binary)
- [Installation from source](#Installation from source)
- [Configuration](#Configuration)
- [Command-Line Arguments](#Command-Line Arguments)
- [API Documentation](#API documentation)
- [Limitations](#Limitations)
- [Performance](#Performance)
Expand Down Expand Up @@ -278,15 +277,25 @@ curl http://demo:[email protected]:8083/stream/demo/delete
###### Query
```bash
GET /stream/:uuid/hls/live/index.m3u8
curl http://127.0.0.1:8083/stream/demo/hls/live/index.m3u8
curl http://127.0.0.1:8083/stream/demo/channel/0/hls/live/index.m3u8
```

###### Response
```bash
index.m3u8
```
```bash
ffplay http://127.0.0.1:8083/stream/demo/hls/live/index.m3u8
ffplay http://127.0.0.1:8083/stream/demo/channel/0/hls/live/index.m3u8
```

#### Stream rtsp play
###### Query

```bash
ffplay -rtsp_transport tcp rtsp://127.0.0.1/demo1/1
```
```bash
ffplay -rtsp_transport tcp rtsp://127.0.0.1/demo1/0
```

## Limitations
Expand Down
23 changes: 19 additions & 4 deletions RTSPtoWeb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ import (
"os"
"os/signal"
"syscall"

"github.com/sirupsen/logrus"
)

func main() {
loggingPrintln("Server start")
log.WithFields(logrus.Fields{
"module": "main",
"func": "main",
}).Info("Server CORE start")
go HTTPAPIServer()
go RTSPServer()
go Storage.StreamRunAll()
signalChanel := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(signalChanel, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signalChanel
loggingPrintln("Server receive signal", sig)
log.WithFields(logrus.Fields{
"module": "main",
"func": "main",
}).Info("Server receive signal", sig)
done <- true
}()
loggingPrintln("Server start success a wait signals")
log.WithFields(logrus.Fields{
"module": "main",
"func": "main",
}).Info("Server start success a wait signals")
<-done
loggingPrintln("Server stop working by signal")
log.WithFields(logrus.Fields{
"module": "main",
"func": "main",
}).Info("Server stop working by signal")
}
89 changes: 78 additions & 11 deletions apiHTTPHLS.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (

"github.com/deepch/vdk/format/ts"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

//HTTPAPIServerStreamHLSTS send client m3u8 play list
func HTTPAPIServerStreamHLSM3U8(c *gin.Context) {
if !Storage.StreamChannelExist(c.Param("uuid"), stringToInt(c.Param("channel"))) {
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSM3U8",
"call": "StreamChannelExist",
}).Errorln(ErrorStreamNotFound.Error())
return
}
c.Header("Content-Type", "application/x-mpegURL")
Expand All @@ -22,14 +29,26 @@ func HTTPAPIServerStreamHLSM3U8(c *gin.Context) {
index, seq, err := Storage.StreamHLSm3u8(c.Param("uuid"), stringToInt(c.Param("channel")))
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSM3U8",
"call": "StreamHLSm3u8",
}).Errorln(err.Error())
return
}
if seq >= 6 {
_, err := c.Writer.Write([]byte(index))
if err != nil {
c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSM3U8",
"call": "Write",
}).Errorln(err.Error())
return
}
return
Expand All @@ -42,13 +61,25 @@ func HTTPAPIServerStreamHLSM3U8(c *gin.Context) {
func HTTPAPIServerStreamHLSTS(c *gin.Context) {
if !Storage.StreamChannelExist(c.Param("uuid"), stringToInt(c.Param("channel"))) {
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "StreamChannelExist",
}).Errorln(ErrorStreamNotFound.Error())
return
}
codecs, err := Storage.StreamCodecs(c.Param("uuid"), stringToInt(c.Param("channel")))
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "StreamCodecs",
}).Errorln(err.Error())
return
}
outfile := bytes.NewBuffer([]byte{})
Expand All @@ -57,39 +88,75 @@ func HTTPAPIServerStreamHLSTS(c *gin.Context) {
err = Muxer.WriteHeader(codecs)
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "WriteHeader",
}).Errorln(err.Error())
return
}
seqData, err := Storage.StreamHLSTS(c.Param("uuid"), stringToInt(c.Param("channel")), stringToInt(c.Param("seq")))
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "StreamHLSTS",
}).Errorln(err.Error())
return
}
if len(seqData) == 0 {
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotHLSSegments.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: ErrorStreamNotHLSSegments.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "seqData",
}).Errorln(ErrorStreamNotHLSSegments.Error())
return
}
for _, v := range seqData {
v.CompositionTime = 1
err = Muxer.WritePacket(*v)
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "WritePacket",
}).Errorln(err.Error())
return
}
}
err = Muxer.WriteTrailer()
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "WriteTrailer",
}).Errorln(err.Error())
return
}
_, err = c.Writer.Write(outfile.Bytes())
if err != nil {
c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
log.WithFields(logrus.Fields{
"module": "http_hls",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamHLSTS",
"call": "Write",
}).Errorln(err.Error())
return
}

Expand Down
Loading

0 comments on commit fa2e00d

Please sign in to comment.