|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/deepch/vdk/format/mp4f" |
| 5 | + |
| 6 | + "github.com/gin-gonic/gin" |
| 7 | + "github.com/sirupsen/logrus" |
| 8 | +) |
| 9 | + |
| 10 | +//HTTPAPIServerStreamHLSLLInit send client ts segment |
| 11 | +func HTTPAPIServerStreamHLSLLInit(c *gin.Context) { |
| 12 | + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { |
| 13 | + c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) |
| 14 | + log.WithFields(logrus.Fields{ |
| 15 | + "module": "http_hlsll", |
| 16 | + "stream": c.Param("uuid"), |
| 17 | + "channel": c.Param("channel"), |
| 18 | + "func": "HTTPAPIServerStreamHLSLLInit", |
| 19 | + "call": "StreamChannelExist", |
| 20 | + }).Errorln(ErrorStreamNotFound.Error()) |
| 21 | + return |
| 22 | + } |
| 23 | + c.Header("Content-Type", "application/x-mpegURL") |
| 24 | + Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel")) |
| 25 | + codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) |
| 26 | + if err != nil { |
| 27 | + c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) |
| 28 | + log.WithFields(logrus.Fields{ |
| 29 | + "module": "http_hlsll", |
| 30 | + "stream": c.Param("uuid"), |
| 31 | + "channel": c.Param("channel"), |
| 32 | + "func": "HTTPAPIServerStreamHLSLLInit", |
| 33 | + "call": "StreamChannelCodecs", |
| 34 | + }).Errorln(err.Error()) |
| 35 | + return |
| 36 | + } |
| 37 | + Muxer := mp4f.NewMuxer(nil) |
| 38 | + err = Muxer.WriteHeader(codecs) |
| 39 | + if err != nil { |
| 40 | + c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) |
| 41 | + log.WithFields(logrus.Fields{ |
| 42 | + "module": "http_hlsll", |
| 43 | + "stream": c.Param("uuid"), |
| 44 | + "channel": c.Param("channel"), |
| 45 | + "func": "HTTPAPIServerStreamHLSLLInit", |
| 46 | + "call": "WriteHeader", |
| 47 | + }).Errorln(err.Error()) |
| 48 | + return |
| 49 | + } |
| 50 | + c.Header("Content-Type", "video/mp4") |
| 51 | + _, buf := Muxer.GetInit(codecs) |
| 52 | + _, err = c.Writer.Write(buf) |
| 53 | + if err != nil { |
| 54 | + c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) |
| 55 | + log.WithFields(logrus.Fields{ |
| 56 | + "module": "http_hlsll", |
| 57 | + "stream": c.Param("uuid"), |
| 58 | + "channel": c.Param("channel"), |
| 59 | + "func": "HTTPAPIServerStreamHLSLLInit", |
| 60 | + "call": "Write", |
| 61 | + }).Errorln(err.Error()) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +//HTTPAPIServerStreamHLSLLM3U8 send client m3u8 play list |
| 68 | +func HTTPAPIServerStreamHLSLLM3U8(c *gin.Context) { |
| 69 | + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { |
| 70 | + c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) |
| 71 | + log.WithFields(logrus.Fields{ |
| 72 | + "module": "http_hlsll", |
| 73 | + "stream": c.Param("uuid"), |
| 74 | + "channel": c.Param("channel"), |
| 75 | + "func": "HTTPAPIServerStreamHLSLLM3U8", |
| 76 | + "call": "StreamChannelExist", |
| 77 | + }).Errorln(ErrorStreamNotFound.Error()) |
| 78 | + return |
| 79 | + } |
| 80 | + c.Header("Content-Type", "application/x-mpegURL") |
| 81 | + Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel")) |
| 82 | + index, err := Storage.HLSMuxerM3U8(c.Param("uuid"), c.Param("channel"), stringToInt(c.DefaultQuery("_HLS_msn", "-1")), stringToInt(c.DefaultQuery("_HLS_part", "-1"))) |
| 83 | + if err != nil { |
| 84 | + log.WithFields(logrus.Fields{ |
| 85 | + "module": "http_hlsll", |
| 86 | + "stream": c.Param("uuid"), |
| 87 | + "channel": c.Param("channel"), |
| 88 | + "func": "HTTPAPIServerStreamHLSLLM3U8", |
| 89 | + "call": "HLSMuxerM3U8", |
| 90 | + }).Errorln(ErrorStreamNotFound.Error()) |
| 91 | + return |
| 92 | + } |
| 93 | + _, err = c.Writer.Write([]byte(index)) |
| 94 | + if err != nil { |
| 95 | + log.WithFields(logrus.Fields{ |
| 96 | + "module": "http_hlsll", |
| 97 | + "stream": c.Param("uuid"), |
| 98 | + "channel": c.Param("channel"), |
| 99 | + "func": "HTTPAPIServerStreamHLSLLM3U8", |
| 100 | + "call": "Write", |
| 101 | + }).Errorln(ErrorStreamNotFound.Error()) |
| 102 | + return |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +//HTTPAPIServerStreamHLSLLM4Segment send client ts segment |
| 107 | +func HTTPAPIServerStreamHLSLLM4Segment(c *gin.Context) { |
| 108 | + c.Header("Content-Type", "video/mp4") |
| 109 | + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { |
| 110 | + c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) |
| 111 | + log.WithFields(logrus.Fields{ |
| 112 | + "module": "http_hlsll", |
| 113 | + "stream": c.Param("uuid"), |
| 114 | + "channel": c.Param("channel"), |
| 115 | + "func": "HTTPAPIServerStreamHLSLLM4Segment", |
| 116 | + "call": "StreamChannelExist", |
| 117 | + }).Errorln(ErrorStreamNotFound.Error()) |
| 118 | + return |
| 119 | + } |
| 120 | + codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) |
| 121 | + if err != nil { |
| 122 | + c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) |
| 123 | + log.WithFields(logrus.Fields{ |
| 124 | + "module": "http_hlsll", |
| 125 | + "stream": c.Param("uuid"), |
| 126 | + "channel": c.Param("channel"), |
| 127 | + "func": "HTTPAPIServerStreamHLSLLM4Segment", |
| 128 | + "call": "StreamChannelCodecs", |
| 129 | + }).Errorln(err.Error()) |
| 130 | + return |
| 131 | + } |
| 132 | + if codecs == nil { |
| 133 | + log.WithFields(logrus.Fields{ |
| 134 | + "module": "http_hlsll", |
| 135 | + "stream": c.Param("uuid"), |
| 136 | + "channel": c.Param("channel"), |
| 137 | + "func": "HTTPAPIServerStreamHLSLLM4Segment", |
| 138 | + "call": "StreamCodecs", |
| 139 | + }).Errorln("Codec Null") |
| 140 | + return |
| 141 | + } |
| 142 | + Muxer := mp4f.NewMuxer(nil) |
| 143 | + err = Muxer.WriteHeader(codecs) |
| 144 | + if err != nil { |
| 145 | + log.WithFields(logrus.Fields{ |
| 146 | + "module": "http_hlsll", |
| 147 | + "stream": c.Param("uuid"), |
| 148 | + "channel": c.Param("channel"), |
| 149 | + "func": "HTTPAPIServerStreamHLSLLM4Segment", |
| 150 | + "call": "WriteHeader", |
| 151 | + }).Errorln(err.Error()) |
| 152 | + return |
| 153 | + } |
| 154 | + seqData, err := Storage.HLSMuxerSegment(c.Param("uuid"), c.Param("channel"), stringToInt(c.Param("segment"))) |
| 155 | + if err != nil { |
| 156 | + log.WithFields(logrus.Fields{ |
| 157 | + "module": "http_hlsll", |
| 158 | + "stream": c.Param("uuid"), |
| 159 | + "channel": c.Param("channel"), |
| 160 | + "func": "HTTPAPIServerStreamHLSLLM4Segment", |
| 161 | + "call": "HLSMuxerSegment", |
| 162 | + }).Errorln(err.Error()) |
| 163 | + return |
| 164 | + } |
| 165 | + for _, v := range seqData { |
| 166 | + err = Muxer.WritePacket4(*v) |
| 167 | + if err != nil { |
| 168 | + log.WithFields(logrus.Fields{ |
| 169 | + "module": "http_hlsll", |
| 170 | + "stream": c.Param("uuid"), |
| 171 | + "channel": c.Param("channel"), |
| 172 | + "func": "HTTPAPIServerStreamHLSLLM4Segment", |
| 173 | + "call": "WritePacket4", |
| 174 | + }).Errorln(err.Error()) |
| 175 | + return |
| 176 | + } |
| 177 | + } |
| 178 | + buf := Muxer.Finalize() |
| 179 | + _, err = c.Writer.Write(buf) |
| 180 | + if err != nil { |
| 181 | + log.WithFields(logrus.Fields{ |
| 182 | + "module": "http_hlsll", |
| 183 | + "stream": c.Param("uuid"), |
| 184 | + "channel": c.Param("channel"), |
| 185 | + "func": "HTTPAPIServerStreamHLSLLM4Segment", |
| 186 | + "call": "Write", |
| 187 | + }).Errorln(err.Error()) |
| 188 | + return |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +//HTTPAPIServerStreamHLSLLM4Fragment send client ts segment |
| 193 | +func HTTPAPIServerStreamHLSLLM4Fragment(c *gin.Context) { |
| 194 | + c.Header("Content-Type", "video/mp4") |
| 195 | + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { |
| 196 | + c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) |
| 197 | + log.WithFields(logrus.Fields{ |
| 198 | + "module": "http_hlsll", |
| 199 | + "stream": c.Param("uuid"), |
| 200 | + "channel": c.Param("channel"), |
| 201 | + "func": "HTTPAPIServerStreamHLSLLM4Fragment", |
| 202 | + "call": "StreamChannelExist", |
| 203 | + }).Errorln(ErrorStreamNotFound.Error()) |
| 204 | + return |
| 205 | + } |
| 206 | + codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) |
| 207 | + if err != nil { |
| 208 | + c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) |
| 209 | + log.WithFields(logrus.Fields{ |
| 210 | + "module": "http_hlsll", |
| 211 | + "stream": c.Param("uuid"), |
| 212 | + "channel": c.Param("channel"), |
| 213 | + "func": "HTTPAPIServerStreamHLSLLM4Fragment", |
| 214 | + "call": "StreamChannelCodecs", |
| 215 | + }).Errorln(err.Error()) |
| 216 | + return |
| 217 | + } |
| 218 | + if codecs == nil { |
| 219 | + log.WithFields(logrus.Fields{ |
| 220 | + "module": "http_hlsll", |
| 221 | + "stream": c.Param("uuid"), |
| 222 | + "channel": c.Param("channel"), |
| 223 | + "func": "HTTPAPIServerStreamHLSLLM4Fragment", |
| 224 | + "call": "StreamCodecs", |
| 225 | + }).Errorln("Codec Null") |
| 226 | + return |
| 227 | + } |
| 228 | + Muxer := mp4f.NewMuxer(nil) |
| 229 | + err = Muxer.WriteHeader(codecs) |
| 230 | + if err != nil { |
| 231 | + log.WithFields(logrus.Fields{ |
| 232 | + "module": "http_hlsll", |
| 233 | + "stream": c.Param("uuid"), |
| 234 | + "channel": c.Param("channel"), |
| 235 | + "func": "HTTPAPIServerStreamHLSLLM4Fragment", |
| 236 | + "call": "WriteHeader", |
| 237 | + }).Errorln(err.Error()) |
| 238 | + return |
| 239 | + } |
| 240 | + seqData, err := Storage.HLSMuxerFragment(c.Param("uuid"), c.Param("channel"), stringToInt(c.Param("segment")), stringToInt(c.Param("fragment"))) |
| 241 | + if err != nil { |
| 242 | + log.WithFields(logrus.Fields{ |
| 243 | + "module": "http_hlsll", |
| 244 | + "stream": c.Param("uuid"), |
| 245 | + "channel": c.Param("channel"), |
| 246 | + "func": "HTTPAPIServerStreamHLSLLM4Fragment", |
| 247 | + "call": "HLSMuxerFragment", |
| 248 | + }).Errorln(err.Error()) |
| 249 | + return |
| 250 | + } |
| 251 | + for _, v := range seqData { |
| 252 | + err = Muxer.WritePacket4(*v) |
| 253 | + if err != nil { |
| 254 | + log.WithFields(logrus.Fields{ |
| 255 | + "module": "http_hlsll", |
| 256 | + "stream": c.Param("uuid"), |
| 257 | + "channel": c.Param("channel"), |
| 258 | + "func": "HTTPAPIServerStreamHLSLLM4Fragment", |
| 259 | + "call": "WritePacket4", |
| 260 | + }).Errorln(err.Error()) |
| 261 | + return |
| 262 | + } |
| 263 | + } |
| 264 | + buf := Muxer.Finalize() |
| 265 | + _, err = c.Writer.Write(buf) |
| 266 | + if err != nil { |
| 267 | + log.WithFields(logrus.Fields{ |
| 268 | + "module": "http_hlsll", |
| 269 | + "stream": c.Param("uuid"), |
| 270 | + "channel": c.Param("channel"), |
| 271 | + "func": "HTTPAPIServerStreamHLSLLM4Fragment", |
| 272 | + "call": "Write", |
| 273 | + }).Errorln(err.Error()) |
| 274 | + return |
| 275 | + } |
| 276 | +} |
0 commit comments