Skip to content

Commit

Permalink
fix all war and style
Browse files Browse the repository at this point in the history
  • Loading branch information
deepch committed Aug 15, 2020
1 parent 5bb8cd1 commit 5cc4573
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 151 deletions.
File renamed without changes.
57 changes: 31 additions & 26 deletions apiHTTPHLS.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ package main

import (
"bytes"
"log"
"time"

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

//ready
//HTTPAPIServerStreamHLSTS send client m3u8 play list
func HTTPAPIServerStreamHLSM3U8(c *gin.Context) {
uuid := c.Param("uuid")
if !Storage.StreamExist(uuid) {
c.IndentedJSON(500, "Stream Not Found")
if !Storage.StreamExist(c.Param("uuid")) {
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
return
}
c.Header("Content-Type", "application/x-mpegURL")
Storage.StreamRun(uuid)
Storage.StreamRun(c.Param("uuid"))
//If stream mode on_demand need wait ready segment's
for i := 0; i < 40; i++ {
index, seq, err := Storage.StreamHLSm3u8(uuid)
index, seq, err := Storage.StreamHLSm3u8(c.Param("uuid"))
if err != nil {
c.IndentedJSON(500, err)
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
if seq >= 6 {
_, err := c.Writer.Write([]byte(index))
if err != nil {
c.IndentedJSON(400, err.Error())
c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
return
Expand All @@ -38,53 +38,58 @@ func HTTPAPIServerStreamHLSM3U8(c *gin.Context) {
}
}

//ready
//HTTPAPIServerStreamHLSTS send client ts segment
func HTTPAPIServerStreamHLSTS(c *gin.Context) {
uuid := c.Param("uuid")
//Check Has Stream
if !Storage.StreamExist(uuid) {
log.Println("Not Found Error")
if !Storage.StreamExist(c.Param("uuid")) {
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
return
}
outfile := bytes.NewBuffer([]byte{})
codecs, err := Storage.StreamCodecs(uuid)
codecs, err := Storage.StreamCodecs(c.Param("uuid"))
if err != nil {
c.IndentedJSON(500, err.Error())
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
outfile := bytes.NewBuffer([]byte{})
Muxer := ts.NewMuxer(outfile)
Muxer.PaddingToMakeCounterCont = true
err = Muxer.WriteHeader(codecs)
if err != nil {
c.IndentedJSON(500, err.Error())
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
data, err := Storage.StreamHLSTS(uuid, stringToInt(c.Param("seq")))
seqData, err := Storage.StreamHLSTS(c.Param("uuid"), stringToInt(c.Param("seq")))
if err != nil {
c.IndentedJSON(500, err.Error())
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
if len(data) == 0 {
c.IndentedJSON(500, "No Segment Found")
if len(seqData) == 0 {
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotHLSSegments.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: ErrorStreamNotHLSSegments.Error()})
return
}
for _, v := range data {
for _, v := range seqData {
v.CompositionTime = 1
err = Muxer.WritePacket(*v)
if err != nil {
c.IndentedJSON(500, err.Error())
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
}
err = Muxer.WriteTrailer()
if err != nil {
c.IndentedJSON(500, err.Error())
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
_, err = c.Writer.Write(outfile.Bytes())
if err != nil {
c.IndentedJSON(400, err.Error())
c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
loggingPrintln(c.Param("uuid"), Message{Status: 0, Payload: err.Error()})
return
}

Expand Down
37 changes: 25 additions & 12 deletions apiHTTPMSE.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,59 @@ import (
)

func HTTPAPIServerStreamMSE(ws *websocket.Conn) {
defer ws.Close()
uuid := ws.Request().FormValue("uuid")
if !Storage.StreamExist(uuid) {
defer func() {
err := ws.Close()
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err})
}()
if !Storage.StreamExist(ws.Request().FormValue("uuid")) {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
return
}
err := ws.SetWriteDeadline(time.Now().Add(5 * time.Second))
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
cid, ch, err := Storage.ClientAdd(uuid)
cid, ch, err := Storage.ClientAdd(ws.Request().FormValue("uuid"))
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
defer Storage.ClientDelete(uuid, cid)
Storage.StreamRun(uuid)
codecs, err := Storage.StreamCodecs(uuid)
defer Storage.ClientDelete(ws.Request().FormValue("uuid"), cid)
Storage.StreamRun(ws.Request().FormValue("uuid"))
codecs, err := Storage.StreamCodecs(ws.Request().FormValue("uuid"))
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}

muxerMSE := mp4f.NewMuxer(nil)
err = muxerMSE.WriteHeader(codecs)
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
meta, init := muxerMSE.GetInit(codecs)
err = websocket.Message.Send(ws, append([]byte{9}, meta...))
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
err = websocket.Message.Send(ws, init)
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
var videoStart bool
go func() {
defer func() {
err := ws.Close()
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err})
}()
for {
var message string
err := websocket.Message.Receive(ws, &message)
if err != nil {
err = ws.Close()
if err != nil {
return
}
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
}
Expand All @@ -60,6 +69,7 @@ func HTTPAPIServerStreamMSE(ws *websocket.Conn) {
for {
select {
case <-noVideo.C:
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: ErrorStreamNoVideo.Error()})
return
case pck := <-ch:
if pck.IsKeyFrame {
Expand All @@ -71,15 +81,18 @@ func HTTPAPIServerStreamMSE(ws *websocket.Conn) {
}
ready, buf, err := muxerMSE.WritePacket(*pck, false)
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
if ready {
err := ws.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
err = websocket.Message.Send(ws, buf)
if err != nil {
loggingPrintln(ws.Request().FormValue("uuid"), Message{Status: 0, Payload: err.Error()})
return
}
}
Expand Down
40 changes: 13 additions & 27 deletions apiHTTPRouter.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package main

import (
"encoding/json"
"log"
"net/http"
"os"
"time"

"github.com/gin-gonic/gin"
"golang.org/x/net/websocket"
)

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

//HTTPAPIServer start http server routes
func HTTPAPIServer() {
//Set HTTP API mode
var public *gin.Engine
Expand Down Expand Up @@ -38,6 +44,7 @@ func HTTPAPIServer() {
privat.GET("/stream/:uuid/delete", HTTPAPIServerStreamDelete)
privat.GET("/stream/:uuid/reload", HTTPAPIServerStreamReload)
privat.GET("/stream/:uuid/info", HTTPAPIServerStreamInfo)
privat.GET("/stream/:uuid/codec", HTTPAPIServerStreamCodec)
/*
Stream video elements
*/
Expand All @@ -48,25 +55,6 @@ func HTTPAPIServer() {
handler.ServeHTTP(c.Writer, c.Request)
})
public.POST("/stream/:uuid/webrtc", HTTPAPIServerStreamWebRTC)
//TODO Fix It
public.GET("/codec/:uuid", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
if Storage.StreamExist(c.Param("uuid")) {
codecs, _ := Storage.StreamCodecs(c.Param("uuid"))
if codecs == nil {
return
}
b, err := json.Marshal(codecs)
log.Println(string(b), err)
if err == nil {
_, err = c.Writer.Write(b)
if err == nil {
log.Println("Write Codec Info error", err)
return
}
}
}
})
/*
Static HTML Files Demo Mode
*/
Expand All @@ -75,23 +63,21 @@ func HTTPAPIServer() {
}
err := public.Run(Storage.ServerHTTPPort())
if err != nil {
log.Fatalln(err)
loggingPrintln(Message{Status: 0, Payload: err.Error()})
os.Exit(1)
}
}

//HTTPAPIServerIndex index file
func HTTPAPIServerIndex(c *gin.Context) {
//fi, all := Storage.List()
//sort.Strings(all)
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Storage.ServerHTTPPort(),
"streams": Storage.Streams,
// "uuid": fi,
// "uuidMap": all,
"version": time.Now().String(),
})

}

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

//TODO add to next version
Loading

0 comments on commit 5cc4573

Please sign in to comment.