You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lal/pkg/hls/path.go

84 lines
2.4 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 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 (
"fmt"
"io/ioutil"
"strings"
)
// 本文件聚合以下功能:
// - 生成HLSm3u8文件+ts文件文件命名规则以及文件存放规则
// - HTTP请求HLS时request URI和文件路径的映射规则
// HTTP请求URI格式已经文件路径的映射规则
//
// 假设
// 流名称="test110"
// rootPath="/tmp/lal/hls/"
//
// 则
// http://127.0.0.1:8081/hls/test110/playlist.m3u8 -> /tmp/lal/hls/test110/playlist.m3u8
// http://127.0.0.1:8081/hls/test110/record.m3u8 -> /tmp/lal/hls/test110/record.m3u8
// http://127.0.0.1:8081/hls/test110/timestamp-0.ts -> /tmp/lal/hls/test110/timestamp-0.ts
type requestInfo struct {
fileName string
streamName string
fileType string
}
// RequestURI example:
// uri -> fileName streamName fileType
// http://127.0.0.1:8081/hls/test110/playlist.m3u8 -> playlist.m3u8 test110 m3u8
// http://127.0.0.1:8081/hls/test110/record.m3u8 -> record.m3u8 test110 m3u8
// http://127.0.0.1:8081/hls/test110/timestamp-0.ts -> timestamp-0.ts test110 ts
func parseRequestInfo(uri string) (ri requestInfo) {
ss := strings.Split(uri, "/")
if len(ss) < 2 {
return
}
ri.streamName = ss[len(ss)-2]
ri.fileName = ss[len(ss)-1]
ss = strings.Split(ri.fileName, ".")
if len(ss) < 2 {
return
}
ri.fileType = ss[len(ss)-1]
return
}
func readFileContent(rootOutPath string, ri requestInfo) ([]byte, error) {
filename := fmt.Sprintf("%s%s/%s", rootOutPath, ri.streamName, ri.fileName)
return ioutil.ReadFile(filename)
}
func getMuxerOutPath(rootOutPath string, streamName string) string {
return fmt.Sprintf("%s%s/", rootOutPath, streamName)
}
func getM3U8Filename(outpath string, streamName string) string {
return fmt.Sprintf("%s%s.m3u8", outpath, "playlist")
}
func getRecordM3U8Filename(outpath string, streamName string) string {
return fmt.Sprintf("%s%s.m3u8", outpath, "record")
}
func getTSFilenameWithPath(outpath string, filename string) string {
return fmt.Sprintf("%s%s", outpath, filename)
}
func getTSFilename(streamName string, id int, timestamp int) string {
return fmt.Sprintf("%d-%d.ts", timestamp, id)
}