mirror of https://github.com/q191201771/lal.git
1. [feat] 完成package hls的基础功能,并做了小范围重构 2. [feat] lals服务接入hls功能 3. [refactor] 将app目录下除lals的其他应用移入demo目录下 4. [feat] 新增两个demo:analyseflv和analysehls,分别用于拉取HTTP-FLV和HLS的流,并进行分析v0.12.3 -> CHANGELOG.md
parent
d7e77299b2
commit
41cdddbe2f
@ -1,98 +0,0 @@
|
||||
// Copyright 2019, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/lal
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/q191201771/lal/pkg/avc"
|
||||
"github.com/q191201771/lal/pkg/hevc"
|
||||
"github.com/q191201771/naza/pkg/bele"
|
||||
|
||||
"github.com/q191201771/lal/pkg/httpflv"
|
||||
"github.com/q191201771/naza/pkg/bitrate"
|
||||
"github.com/q191201771/naza/pkg/nazaatomic"
|
||||
log "github.com/q191201771/naza/pkg/nazalog"
|
||||
)
|
||||
|
||||
// TODO chef: 存储成 flv 文件
|
||||
|
||||
func main() {
|
||||
url := parseFlag()
|
||||
session := httpflv.NewPullSession()
|
||||
abr := bitrate.New()
|
||||
vbr := bitrate.New()
|
||||
var runFlag nazaatomic.Bool
|
||||
runFlag.Store(true)
|
||||
go func() {
|
||||
for runFlag.Load() {
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}()
|
||||
err := session.Pull(url, func(tag httpflv.Tag) {
|
||||
switch tag.Header.Type {
|
||||
case httpflv.TagTypeMetadata:
|
||||
log.Info(hex.Dump(tag.Payload()))
|
||||
case httpflv.TagTypeAudio:
|
||||
abr.Add(len(tag.Raw))
|
||||
case httpflv.TagTypeVideo:
|
||||
log.Infof("onReadFLVTag. %+v, isSeqHeader=%t, isKeyNalu=%t", tag.Header, tag.IsVideoKeySeqHeader(), tag.IsVideoKeyNalu())
|
||||
analysisVideoTag(tag)
|
||||
vbr.Add(len(tag.Raw))
|
||||
}
|
||||
})
|
||||
runFlag.Store(false)
|
||||
log.Assert(nil, err)
|
||||
}
|
||||
|
||||
const (
|
||||
typeUnknown uint8 = 1
|
||||
typeAVC uint8 = 2
|
||||
typeHEVC uint8 = 3
|
||||
)
|
||||
|
||||
var t uint8 = typeUnknown
|
||||
|
||||
func analysisVideoTag(tag httpflv.Tag) {
|
||||
if tag.IsVideoKeySeqHeader() {
|
||||
if tag.IsAVCKeySeqHeader() {
|
||||
t = typeAVC
|
||||
log.Info("AVC SH")
|
||||
} else if tag.IsHEVCKeySeqHeader() {
|
||||
t = typeHEVC
|
||||
log.Info("HEVC SH")
|
||||
}
|
||||
} else {
|
||||
body := tag.Raw[11:]
|
||||
|
||||
for i := 5; i != int(tag.Header.DataSize); {
|
||||
naluLen := bele.BEUint32(body[i:])
|
||||
switch t {
|
||||
case typeAVC:
|
||||
log.Infof("%s %s", avc.CalcNaluTypeReadable(body[i+4:]), hex.Dump(body[i+4:i+8]))
|
||||
case typeHEVC:
|
||||
log.Infof("%s %s", hevc.CalcNaluTypeReadable(body[i+4:]), hex.Dump(body[i+4:i+8]))
|
||||
}
|
||||
i = i + 4 + int(naluLen)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseFlag() string {
|
||||
url := flag.String("i", "", "specify http-flv url")
|
||||
flag.Parse()
|
||||
if *url == "" {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
return *url
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
github.com/q191201771/naza v0.12.2 h1:El5OSCPFrGGrZiyZ0aOvdystC15Ap7lC4MipVKdfVMY=
|
||||
github.com/q191201771/naza v0.12.2/go.mod h1:SE14GBGO9mAn6JZl3NlfWGtNOT7xQjxOG7f3YOdBThM=
|
||||
github.com/q191201771/naza v0.12.3 h1:0Z8hMa5RYNqsG1GjGfYyLFkuPLfuZ21iDx3BJEPK0p8=
|
||||
github.com/q191201771/naza v0.12.3/go.mod h1:SE14GBGO9mAn6JZl3NlfWGtNOT7xQjxOG7f3YOdBThM=
|
||||
|
@ -0,0 +1,72 @@
|
||||
// Copyright 2020, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/lal
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package hls
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/q191201771/naza/pkg/nazalog"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
addr string
|
||||
outPath string
|
||||
}
|
||||
|
||||
func NewServer(addr string, outPath string) *Server {
|
||||
return &Server{
|
||||
addr: addr,
|
||||
outPath: outPath,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) RunLoop() error {
|
||||
nazalog.Infof("start hls listen. addr=%s", s.addr)
|
||||
return http.ListenAndServe(s.addr, s)
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
||||
//nazalog.Debugf("%+v", req)
|
||||
|
||||
// TODO chef:
|
||||
// - check appname in URI path
|
||||
// - DIY 404 response body
|
||||
|
||||
ri := parseRequestInfo(req.RequestURI)
|
||||
//nazalog.Debugf("%+v", ri)
|
||||
|
||||
if ri.fileName == "" || ri.streamName == "" || (ri.fileType != "m3u8" && ri.fileType != "ts") {
|
||||
nazalog.Warnf("%+v", ri)
|
||||
resp.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := readFileContent(s.outPath, ri)
|
||||
if err != nil {
|
||||
nazalog.Warnf("%+v", err)
|
||||
resp.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
|
||||
switch ri.fileType {
|
||||
case "m3u8":
|
||||
resp.Header().Add("Content-Type", "application/x-mpegurl")
|
||||
//resp.Header().Add("Content-Type", "application/vnd.apple.mpegurl")
|
||||
case "ts":
|
||||
resp.Header().Add("Content-Type", "video/mp2t")
|
||||
}
|
||||
resp.Header().Add("Cache-Control", "no-cache")
|
||||
//resp.Header().Add("Access-Control-Allow-Origin", "*")
|
||||
//resp.Header().Add("Access-Control-Allow-Credentials", "true")
|
||||
//resp.Header().Add("Access-Control-Allow-Methods", "*")
|
||||
//resp.Header().Add("Access-Control-Allow-Headers", "Content-Type,Access-Token")
|
||||
//resp.Header().Add("Access-Control-Allow-Expose-Headers", "*")
|
||||
_, _ = resp.Write(content)
|
||||
return
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
// Copyright 2020, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/lal
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package hls
|
||||
|
||||
import "github.com/q191201771/naza/pkg/nazalog"
|
||||
|
||||
func SplitTS(content []byte) (ret [][]byte) {
|
||||
for {
|
||||
if len(content) < 188 {
|
||||
nazalog.Assert(0, len(content))
|
||||
break
|
||||
}
|
||||
|
||||
ret = append(ret, content[0:188])
|
||||
content = content[188:]
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue