Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
deepch committed Mar 29, 2023
1 parent 7d4a2cf commit e33c0f6
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 5 deletions.
14 changes: 9 additions & 5 deletions apiHTTPRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/sirupsen/logrus"
)

//Message resp struct
// Message resp struct
type Message struct {
Status int `json:"status"`
Payload interface{} `json:"payload"`
}

//HTTPAPIServer start http server routes
// HTTPAPIServer start http server routes
func HTTPAPIServer() {
//Set HTTP API mode
log.WithFields(logrus.Fields{
Expand Down Expand Up @@ -95,6 +95,9 @@ func HTTPAPIServer() {
//HLS
public.GET("/stream/:uuid/channel/:channel/hls/live/index.m3u8", HTTPAPIServerStreamHLSM3U8)
public.GET("/stream/:uuid/channel/:channel/hls/live/segment/:seq/file.ts", HTTPAPIServerStreamHLSTS)
//HLS remote record
//public.GET("/stream/:uuid/channel/:channel/hls/rr/:s/:e/index.m3u8", HTTPAPIServerStreamRRM3U8)
//public.GET("/stream/:uuid/channel/:channel/hls/rr/:s/:e/:seq/file.ts", HTTPAPIServerStreamRRTS)
//HLS LL
public.GET("/stream/:uuid/channel/:channel/hlsll/live/index.m3u8", HTTPAPIServerStreamHLSLLM3U8)
public.GET("/stream/:uuid/channel/:channel/hlsll/live/init.mp4", HTTPAPIServerStreamHLSLLInit)
Expand All @@ -103,7 +106,8 @@ func HTTPAPIServer() {
//MSE
public.GET("/stream/:uuid/channel/:channel/mse", HTTPAPIServerStreamMSE)
public.POST("/stream/:uuid/channel/:channel/webrtc", HTTPAPIServerStreamWebRTC)

//Save fragment to mp4
public.GET("/stream/:uuid/channel/:channel/save/mp4/fragment/:duration", HTTPAPIServerStreamSaveToMP4)
/*
HTTPS Mode Cert
# Key considerations for algorithm "RSA" ≥ 2048-bit
Expand Down Expand Up @@ -150,7 +154,7 @@ func HTTPAPIServer() {

}

//HTTPAPIServerIndex index file
// HTTPAPIServerIndex index file
func HTTPAPIServerIndex(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Storage.ServerHTTPPort(),
Expand Down Expand Up @@ -282,7 +286,7 @@ func HTTPAPIFullScreenMultiView(c *gin.Context) {
})
}

//CrossOrigin Access-Control-Allow-Origin any methods
// CrossOrigin Access-Control-Allow-Origin any methods
func CrossOrigin() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
Expand Down
129 changes: 129 additions & 0 deletions apiHTTPSaveMP4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"fmt"
"github.com/deepch/vdk/format/mp4"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"os"
"time"
)

// HTTPAPIServerStreamSaveToMP4 func
func HTTPAPIServerStreamSaveToMP4(c *gin.Context) {
var err error

requestLogger := log.WithFields(logrus.Fields{
"module": "http_save_mp4",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamSaveToMP4",
})

defer func() {
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Close",
}).Errorln(err)
}
}()

if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) {
requestLogger.WithFields(logrus.Fields{
"call": "StreamChannelExist",
}).Errorln(ErrorStreamNotFound.Error())
return
}

if !RemoteAuthorization("save", c.Param("uuid"), c.Param("channel"), c.Query("token"), c.ClientIP()) {
requestLogger.WithFields(logrus.Fields{
"call": "RemoteAuthorization",
}).Errorln(ErrorStreamUnauthorized.Error())
return
}
c.Writer.Write([]byte("await save started"))
go func() {
Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel"))
cid, ch, _, err := Storage.ClientAdd(c.Param("uuid"), c.Param("channel"), MSE)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "ClientAdd",
}).Errorln(err.Error())
return
}

defer Storage.ClientDelete(c.Param("uuid"), cid, c.Param("channel"))
codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel"))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "StreamCodecs",
}).Errorln(err.Error())
return
}
err = os.MkdirAll(fmt.Sprintf("save/%s/%s/", c.Param("uuid"), c.Param("channel")), 0755)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "MkdirAll",
}).Errorln(err.Error())
}
f, err := os.Create(fmt.Sprintf("save/%s/%s/%s.mp4", c.Param("uuid"), c.Param("channel"), time.Now().String()))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Create",
}).Errorln(err.Error())
}
defer f.Close()

muxer := mp4.NewMuxer(f)
err = muxer.WriteHeader(codecs)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "WriteHeader",
}).Errorln(err.Error())
return
}
defer muxer.WriteTrailer()

var videoStart bool
controlExit := make(chan bool, 10)
dur, err := time.ParseDuration(c.Param("duration"))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "ParseDuration",
}).Errorln(err.Error())
}
saveLimit := time.NewTimer(dur)
noVideo := time.NewTimer(10 * time.Second)
defer log.Println("client exit")
for {
select {
case <-controlExit:
requestLogger.WithFields(logrus.Fields{
"call": "controlExit",
}).Errorln("Client Reader Exit")
return
case <-saveLimit.C:
requestLogger.WithFields(logrus.Fields{
"call": "saveLimit",
}).Errorln("Saved Limit End")
return
case <-noVideo.C:
requestLogger.WithFields(logrus.Fields{
"call": "ErrorStreamNoVideo",
}).Errorln(ErrorStreamNoVideo.Error())
return
case pck := <-ch:
if pck.IsKeyFrame {
noVideo.Reset(10 * time.Second)
videoStart = true
}
if !videoStart {
continue
}
if err = muxer.WritePacket(*pck); err != nil {
return
}
}
}
}()
}

0 comments on commit e33c0f6

Please sign in to comment.