Big refactoring (#27)

Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com>
This commit is contained in:
Pierre-Emmanuel Jacquier
2019-12-13 01:29:07 +01:00
committed by GitHub
parent cb94ac2db7
commit 520fb7fd14
27 changed files with 3229 additions and 721 deletions

View File

@@ -7,10 +7,9 @@ import (
"os" "os"
"strings" "strings"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config" "github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/routes" "github.com/pierre-emmanuelJ/iptv-proxy/pkg/server"
homedir "github.com/mitchellh/go-homedir" homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -25,15 +24,6 @@ var rootCmd = &cobra.Command{
Short: "Reverse proxy on iptv m3u file and xtream codes server api", Short: "Reverse proxy on iptv m3u file and xtream codes server api",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
m3uURL := viper.GetString("m3u-url") m3uURL := viper.GetString("m3u-url")
var err error
var playlist m3u.Playlist
if m3uURL != "" {
playlist, err = m3u.Parse(m3uURL)
if err != nil {
log.Fatal(err)
}
}
remoteHostURL, err := url.Parse(m3uURL) remoteHostURL, err := url.Parse(m3uURL)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -61,7 +51,6 @@ var rootCmd = &cobra.Command{
} }
conf := &config.ProxyConfig{ conf := &config.ProxyConfig{
Playlist: &playlist,
HostConfig: &config.HostConfiguration{ HostConfig: &config.HostConfiguration{
Hostname: viper.GetString("hostname"), Hostname: viper.GetString("hostname"),
Port: viper.GetInt64("port"), Port: viper.GetInt64("port"),
@@ -78,7 +67,12 @@ var rootCmd = &cobra.Command{
CustomEndpoint: viper.GetString("custom-endpoint"), CustomEndpoint: viper.GetString("custom-endpoint"),
} }
if e := routes.Serve(conf); e != nil { server, err := server.NewServer(conf)
if err != nil {
log.Fatal(err)
}
if e := server.Serve(); e != nil {
log.Fatal(e) log.Fatal(e)
} }
}, },
@@ -111,7 +105,7 @@ func init() {
rootCmd.Flags().String("xtream-user", "", "Xtream-code user login") rootCmd.Flags().String("xtream-user", "", "Xtream-code user login")
rootCmd.Flags().String("xtream-password", "", "Xtream-code password login") rootCmd.Flags().String("xtream-password", "", "Xtream-code password login")
rootCmd.Flags().String("xtream-base-url", "", "Xtream-code base url e.g(http://expample.tv:8080)") rootCmd.Flags().String("xtream-base-url", "", "Xtream-code base url e.g(http://expample.tv:8080)")
rootCmd.Flags().Int("m3u-cache-expiration", 24, "M3U cache expiration in hour") rootCmd.Flags().Int("m3u-cache-expiration", 1, "M3U cache expiration in hour")
if e := viper.BindPFlags(rootCmd.Flags()); e != nil { if e := viper.BindPFlags(rootCmd.Flags()); e != nil {
log.Fatal("error binding PFlags to viper") log.Fatal("error binding PFlags to viper")

1
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/pierre-emmanuelJ/iptv-proxy
require ( require (
github.com/gin-contrib/cors v0.0.0-20190226021855-50921afdc5c1 github.com/gin-contrib/cors v0.0.0-20190226021855-50921afdc5c1
github.com/gin-gonic/gin v1.3.0 github.com/gin-gonic/gin v1.3.0
github.com/grafov/m3u8 v0.11.1
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jamesnetherton/m3u v0.1.1-0.20180924175816-16741c7f081c github.com/jamesnetherton/m3u v0.1.1-0.20180924175816-16741c7f081c
github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-homedir v1.1.0

4
go.sum
View File

@@ -14,6 +14,8 @@ github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/grafov/m3u8 v0.11.1 h1:igZ7EBIB2IAsPPazKwRKdbhxcoBKO3lO1UY57PZDeNA=
github.com/grafov/m3u8 v0.11.1/go.mod h1:nqzOkfBiZJENr52zTVd/Dcl03yzphIMbJqkXGu+u080=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@@ -42,6 +44,7 @@ github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
@@ -55,6 +58,7 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38= github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38=
github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tellytv/go.xtream-codes v0.0.0-20190114013623-9b74dcb500e4 h1:V5xNxrc8ApVSY2uHeVXDkByVB2iAgoOcmWOOu01oATM= github.com/tellytv/go.xtream-codes v0.0.0-20190114013623-9b74dcb500e4 h1:V5xNxrc8ApVSY2uHeVXDkByVB2iAgoOcmWOOu01oATM=
github.com/tellytv/go.xtream-codes v0.0.0-20190114013623-9b74dcb500e4/go.mod h1:gWtQ2uZJ49dBh4cWiFuz7Tb5ALxLB9hY1GFoz34lsGs= github.com/tellytv/go.xtream-codes v0.0.0-20190114013623-9b74dcb500e4/go.mod h1:gWtQ2uZJ49dBh4cWiFuz7Tb5ALxLB9hY1GFoz34lsGs=

View File

@@ -2,8 +2,6 @@ package config
import ( import (
"net/url" "net/url"
"github.com/jamesnetherton/m3u"
) )
// HostConfiguration containt host infos // HostConfiguration containt host infos
@@ -14,7 +12,6 @@ type HostConfiguration struct {
// ProxyConfig Contain original m3u playlist and HostConfiguration // ProxyConfig Contain original m3u playlist and HostConfiguration
type ProxyConfig struct { type ProxyConfig struct {
Playlist *m3u.Playlist
HostConfig *HostConfiguration HostConfig *HostConfiguration
XtreamUser string XtreamUser string
XtreamPassword string XtreamPassword string
@@ -24,6 +21,5 @@ type ProxyConfig struct {
CustomEndpoint string CustomEndpoint string
RemoteURL *url.URL RemoteURL *url.URL
HTTPS bool HTTPS bool
//XXX Very unsafe User, Password string
User, Password string
} }

View File

@@ -1,65 +0,0 @@
package m3u
import (
"fmt"
"net/url"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
)
// Marshall m3u.playlist struct to m3u file
func Marshall(p *m3u.Playlist) (string, error) {
result := "#EXTM3U\n"
for _, track := range p.Tracks {
result += "#EXTINF:"
result += fmt.Sprintf("%d, ", track.Length)
for i := range track.Tags {
if i == len(track.Tags)-1 {
result += fmt.Sprintf("%s=%q,", track.Tags[i].Name, track.Tags[i].Value)
continue
}
result += fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value)
}
result += fmt.Sprintf("%s\n%s\n", track.Name, track.URI)
}
return result, nil
}
// ReplaceURL replace original playlist url by proxy url
func ReplaceURL(playlist *m3u.Playlist, user, password string, hostConfig *config.HostConfiguration, https bool) (*m3u.Playlist, error) {
result := make([]m3u.Track, 0, len(playlist.Tracks))
for _, track := range playlist.Tracks {
oriURL, err := url.Parse(track.URI)
if err != nil {
return nil, err
}
protocol := "http"
if https {
protocol = "https"
}
uri := fmt.Sprintf(
"%s://%s:%d%s?username=%s&password=%s",
protocol,
hostConfig.Hostname,
hostConfig.Port,
oriURL.EscapedPath(),
url.QueryEscape(user),
url.QueryEscape(password),
)
destURL, err := url.Parse(uri)
if err != nil {
return nil, err
}
track.URI = destURL.String()
result = append(result, track)
}
return &m3u.Playlist{
Tracks: result,
}, nil
}

View File

@@ -1,261 +0,0 @@
package routes
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
proxyM3U "github.com/pierre-emmanuelJ/iptv-proxy/pkg/m3u"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
type proxy struct {
*config.ProxyConfig
*m3u.Track
newM3U []byte
}
// Serve the pfinder api
func Serve(proxyConfig *config.ProxyConfig) error {
router := gin.Default()
router.Use(cors.Default())
Routes(proxyConfig, router.Group("/"))
return router.Run(fmt.Sprintf(":%d", proxyConfig.HostConfig.Port))
}
// Routes adds the routes for the app to the RouterGroup r
func Routes(proxyConfig *config.ProxyConfig, r *gin.RouterGroup) {
p := &proxy{
proxyConfig,
nil,
nil,
}
r = r.Group(p.CustomEndpoint)
//Xtream service endopoints
if p.ProxyConfig.XtreamBaseURL != "" {
r.GET("/get.php", p.authenticate, p.xtreamGet)
r.POST("/get.php", p.authenticate, p.xtreamGet)
r.GET("/player_api.php", p.authenticate, p.xtreamPlayerAPIGET)
r.POST("/player_api.php", p.appAuthenticate, p.xtreamPlayerAPIPOST)
r.GET("/xmltv.php", p.authenticate, p.xtreamXMLTV)
r.GET(fmt.Sprintf("/%s/%s/:id", proxyConfig.User, proxyConfig.Password), p.xtreamStream)
r.GET(fmt.Sprintf("/live/%s/%s/:id", proxyConfig.User, proxyConfig.Password), p.xtreamStreamLive)
r.GET(fmt.Sprintf("/movie/%s/%s/:id", proxyConfig.User, proxyConfig.Password), p.xtreamStreamMovie)
r.GET(fmt.Sprintf("/series/%s/%s/:id", proxyConfig.User, proxyConfig.Password), p.xtreamStreamSeries)
r.GET(fmt.Sprintf("/hlsr/:token/%s/%s/:channel/:hash/:chunk", proxyConfig.User, proxyConfig.Password), p.hlsrStream)
if strings.Contains(p.XtreamBaseURL, p.RemoteURL.Host) &&
p.XtreamUser == p.RemoteURL.Query().Get("username") &&
p.XtreamPassword == p.RemoteURL.Query().Get("password") {
r.GET("/"+p.M3UFileName, p.authenticate, p.xtreamGetAuto)
// XXX Private need: for external Android app
r.POST("/"+p.M3UFileName, p.authenticate, p.xtreamGetAuto)
return
}
}
r.GET("/"+p.M3UFileName, p.authenticate, p.getM3U)
// XXX Private need: for external Android app
r.POST("/"+p.M3UFileName, p.authenticate, p.getM3U)
newM3U := []byte{}
var err error
if len(proxyConfig.Playlist.Tracks) > 0 {
newM3U, err = initm3u(proxyConfig)
if err != nil {
log.Fatal(err)
}
}
p.newM3U = newM3U
checkList := map[string]int8{}
for i, track := range proxyConfig.Playlist.Tracks {
oriURL, err := url.Parse(track.URI)
if err != nil {
return
}
tmp := &proxy{
nil,
&proxyConfig.Playlist.Tracks[i],
nil,
}
_, ok := checkList[oriURL.Path]
if ok {
log.Printf("[iptv-proxy] WARNING endpoint %q already exist, skipping it", oriURL.Path)
continue
}
r.GET(oriURL.Path, p.authenticate, tmp.reverseProxy)
checkList[oriURL.Path] = 0
}
}
func (p *proxy) getM3U(c *gin.Context) {
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, p.M3UFileName))
c.Data(http.StatusOK, "application/octet-stream", p.newM3U)
}
func (p *proxy) reverseProxy(c *gin.Context) {
rpURL, err := url.Parse(p.Track.URI)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
p.stream(c, rpURL)
}
func (p *proxy) stream(c *gin.Context, oriURL *url.URL) {
id := c.Param("id")
if strings.HasSuffix(id, ".m3u8") {
p.hlsStream(c, oriURL)
return
}
resp, err := http.Get(oriURL.String())
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
defer resp.Body.Close()
copyHTTPHeader(c, resp.Header)
c.Status(resp.StatusCode)
c.Stream(func(w io.Writer) bool {
io.Copy(w, resp.Body)
return false
})
}
func (p *proxy) hlsStream(c *gin.Context, oriURL *url.URL) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get(oriURL.String())
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusFound {
location, err := resp.Location()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
id := c.Param("id")
if strings.Contains(location.String(), id) {
hlsChannelsRedirectURLLock.Lock()
hlsChannelsRedirectURL[id] = *location
hlsChannelsRedirectURLLock.Unlock()
hlsResp, err := http.Get(location.String())
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
defer hlsResp.Body.Close()
b, err := ioutil.ReadAll(hlsResp.Body)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
body := string(b)
body = strings.ReplaceAll(body, "/"+p.XtreamUser+"/"+p.XtreamPassword+"/", "/"+p.User+"/"+p.Password+"/")
c.Data(http.StatusOK, hlsResp.Header.Get("Content-Type"), []byte(body))
return
}
c.AbortWithError(http.StatusInternalServerError, errors.New("Unable to HLS stream"))
return
}
c.Status(resp.StatusCode)
}
func copyHTTPHeader(c *gin.Context, header http.Header) {
for k, v := range header {
c.Header(k, strings.Join(v, ", "))
}
}
// AuthRequest handle auth credentials
type AuthRequest struct {
Username string `form:"username" binding:"required"`
Password string `form:"password" binding:"required"`
} // XXX very unsafe
func (p *proxy) authenticate(ctx *gin.Context) {
var authReq AuthRequest
if err := ctx.Bind(&authReq); err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
//XXX very unsafe
if p.ProxyConfig.User != authReq.Username || p.ProxyConfig.Password != authReq.Password {
ctx.AbortWithStatus(http.StatusUnauthorized)
}
}
func (p *proxy) appAuthenticate(c *gin.Context) {
contents, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
q, err := url.ParseQuery(string(contents))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if len(q["username"]) == 0 || len(q["password"]) == 0 {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("bad body url query parameters"))
return
}
log.Printf("[iptv-proxy] %v | %s |App Auth\n", time.Now().Format("2006/01/02 - 15:04:05"), c.ClientIP())
//XXX very unsafe
if p.ProxyConfig.User != q["username"][0] || p.ProxyConfig.Password != q["password"][0] {
c.AbortWithStatus(http.StatusUnauthorized)
}
c.Request.Body = ioutil.NopCloser(bytes.NewReader(contents))
}
func initm3u(p *config.ProxyConfig) ([]byte, error) {
playlist, err := proxyM3U.ReplaceURL(p.Playlist, p.User, p.Password, p.HostConfig, p.HTTPS)
if err != nil {
return nil, err
}
result, err := proxyM3U.Marshall(playlist)
if err != nil {
return nil, err
}
return []byte(result), nil
}

View File

@@ -1,363 +0,0 @@
package routes
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
proxyM3U "github.com/pierre-emmanuelJ/iptv-proxy/pkg/m3u"
xtreamapi "github.com/pierre-emmanuelJ/iptv-proxy/pkg/xtream-proxy"
)
type cacheMeta struct {
string
time.Time
}
var hlsChannelsRedirectURL map[string]url.URL = map[string]url.URL{}
var hlsChannelsRedirectURLLock = sync.RWMutex{}
// XXX Use key/value storage e.g: etcd, redis...
// and remove that dirty globals
var xtreamM3uCache map[string]cacheMeta = map[string]cacheMeta{}
var xtreamM3uCacheLock = sync.RWMutex{}
func (p *proxy) cacheXtreamM3u(m3uURL *url.URL) error {
playlist, err := m3u.Parse(m3uURL.String())
if err != nil {
return err
}
newM3U, err := xtreamReplaceURL(&playlist, p.User, p.Password, p.HostConfig, p.HTTPS)
if err != nil {
return err
}
result, err := proxyM3U.Marshall(newM3U)
if err != nil {
return err
}
xtreamM3uCacheLock.Lock()
path, err := writeCacheTmp([]byte(result), m3uURL.String())
if err != nil {
return err
}
xtreamM3uCache[m3uURL.String()] = cacheMeta{path, time.Now()}
xtreamM3uCacheLock.Unlock()
return nil
}
func writeCacheTmp(data []byte, url string) (string, error) {
filename := base64.StdEncoding.EncodeToString([]byte(url))
path := filepath.Join("/tmp", filename)
if err := ioutil.WriteFile(path, data, 0644); err != nil {
return "", err
}
return path, nil
}
func (p *proxy) xtreamGetAuto(c *gin.Context) {
newQuery := c.Request.URL.Query()
q := p.RemoteURL.Query()
for k, v := range q {
if k == "username" || k == "password" {
continue
}
newQuery.Add(k, strings.Join(v, ","))
}
c.Request.URL.RawQuery = newQuery.Encode()
p.xtreamGet(c)
}
func (p *proxy) xtreamGet(c *gin.Context) {
rawURL := fmt.Sprintf("%s/get.php?username=%s&password=%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword)
q := c.Request.URL.Query()
for k, v := range q {
if k == "username" || k == "password" {
continue
}
rawURL = fmt.Sprintf("%s&%s=%s", rawURL, k, strings.Join(v, ","))
}
println(rawURL)
m3uURL, err := url.Parse(rawURL)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
xtreamM3uCacheLock.RLock()
meta, ok := xtreamM3uCache[m3uURL.String()]
d := time.Now().Sub(meta.Time)
if !ok || d.Hours() >= float64(p.M3UCacheExpiration) {
log.Printf("[iptv-proxy] %v | %s | xtream cache m3u file\n", time.Now().Format("2006/01/02 - 15:04:05"), c.ClientIP())
xtreamM3uCacheLock.RUnlock()
if err := p.cacheXtreamM3u(m3uURL); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
} else {
xtreamM3uCacheLock.RUnlock()
}
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, p.M3UFileName))
xtreamM3uCacheLock.RLock()
path := xtreamM3uCache[m3uURL.String()].string
xtreamM3uCacheLock.RUnlock()
data, err := ioutil.ReadFile(path)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Data(http.StatusOK, "application/octet-stream", data)
}
func (p *proxy) xtreamPlayerAPIGET(c *gin.Context) {
p.xtreamPlayerAPI(c, c.Request.URL.Query())
}
func (p *proxy) xtreamPlayerAPIPOST(c *gin.Context) {
contents, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
q, err := url.ParseQuery(string(contents))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
p.xtreamPlayerAPI(c, q)
}
func (p *proxy) xtreamPlayerAPI(c *gin.Context, q url.Values) {
var action string
if len(q["action"]) > 0 {
action = q["action"][0]
}
protocol := "http"
if p.HTTPS {
protocol = "https"
}
client, err := xtreamapi.New(p.XtreamUser, p.XtreamPassword, p.XtreamBaseURL)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
var respBody interface{}
switch action {
case xtreamapi.GetLiveCategories:
respBody, err = client.GetLiveCategories()
case xtreamapi.GetLiveStreams:
respBody, err = client.GetLiveStreams("")
case xtreamapi.GetVodCategories:
respBody, err = client.GetVideoOnDemandCategories()
case xtreamapi.GetVodStreams:
respBody, err = client.GetVideoOnDemandStreams("")
case xtreamapi.GetVodInfo:
if len(q["vod_id"]) < 1 {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "vod_id"`))
return
}
respBody, err = client.GetVideoOnDemandInfo(q["vod_id"][0])
case xtreamapi.GetSeriesCategories:
respBody, err = client.GetSeriesCategories()
case xtreamapi.GetSeries:
respBody, err = client.GetSeries("")
case xtreamapi.GetSerieInfo:
if len(q["series_id"]) < 1 {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "series_id"`))
return
}
respBody, err = client.GetSeriesInfo(q["series_id"][0])
case xtreamapi.GetShortEPG:
if len(q["stream_id"]) < 1 {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "stream_id"`))
return
}
limit := 0
if len(q["limit"]) > 0 {
limit, err = strconv.Atoi(q["limit"][0])
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
respBody, err = client.GetShortEPG(q["stream_id"][0], limit)
case xtreamapi.GetSimpleDataTable:
if len(q["stream_id"]) < 1 {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "stream_id"`))
return
}
respBody, err = client.GetEPG(q["stream_id"][0])
default:
respBody, err = client.Login(p.User, p.Password, protocol+"://"+p.HostConfig.Hostname, int(p.HostConfig.Port), protocol)
}
log.Printf("[iptv-proxy] %v | %s |Action\t%s\n", time.Now().Format("2006/01/02 - 15:04:05"), c.ClientIP(), action)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, respBody)
}
func (p *proxy) xtreamXMLTV(c *gin.Context) {
client, err := xtreamapi.New(p.XtreamUser, p.XtreamPassword, p.XtreamBaseURL)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
resp, err := client.GetXMLTV()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Data(http.StatusOK, "application/xml", resp)
}
func (p *proxy) xtreamStream(c *gin.Context) {
id := c.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
p.stream(c, rpURL)
}
func (p *proxy) xtreamStreamLive(c *gin.Context) {
id := c.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/live/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
p.stream(c, rpURL)
}
func (p *proxy) xtreamStreamMovie(c *gin.Context) {
id := c.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/movie/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
p.stream(c, rpURL)
}
func (p *proxy) xtreamStreamSeries(c *gin.Context) {
id := c.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/series/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
p.stream(c, rpURL)
}
func (p *proxy) hlsrStream(c *gin.Context) {
hlsChannelsRedirectURLLock.RLock()
url, ok := hlsChannelsRedirectURL[c.Param("channel")+".m3u8"]
if !ok {
c.AbortWithError(http.StatusNotFound, errors.New("HSL redirect url not found"))
return
}
hlsChannelsRedirectURLLock.RUnlock()
req, err := url.Parse(
fmt.Sprintf(
"%s://%s/hlsr/%s/%s/%s/%s/%s/%s",
url.Scheme,
url.Host,
c.Param("token"),
p.XtreamUser,
p.XtreamPassword,
c.Param("channel"),
c.Param("hash"),
c.Param("chunk"),
),
)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
p.stream(c, req)
}
func xtreamReplaceURL(playlist *m3u.Playlist, user, password string, hostConfig *config.HostConfiguration, https bool) (*m3u.Playlist, error) {
result := make([]m3u.Track, 0, len(playlist.Tracks))
for _, track := range playlist.Tracks {
oriURL, err := url.Parse(track.URI)
if err != nil {
return nil, err
}
protocol := "http"
if https {
protocol = "https"
}
id := filepath.Base(oriURL.Path)
uri := fmt.Sprintf(
"%s://%s:%d/%s/%s/%s",
protocol,
hostConfig.Hostname,
hostConfig.Port,
url.QueryEscape(user),
url.QueryEscape(password),
url.QueryEscape(id),
)
destURL, err := url.Parse(uri)
if err != nil {
return nil, err
}
track.URI = destURL.String()
result = append(result, track)
}
return &m3u.Playlist{
Tracks: result,
}, nil
}

152
pkg/server/handlers.go Normal file
View File

@@ -0,0 +1,152 @@
package server
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func (c *Config) getM3U(ctx *gin.Context) {
ctx.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, c.M3UFileName))
ctx.Header("Content-Type", "application/octet-stream")
ctx.File(c.proxyfiedM3UPath)
}
func (c *Config) reverseProxy(ctx *gin.Context) {
rpURL, err := url.Parse(c.track.URI)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.stream(ctx, rpURL)
}
func (c *Config) stream(ctx *gin.Context, oriURL *url.URL) {
id := ctx.Param("id")
if strings.HasSuffix(id, ".m3u8") {
c.hlsStream(ctx, oriURL)
return
}
resp, err := http.Get(oriURL.String())
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
defer resp.Body.Close()
copyHTTPHeader(ctx, resp.Header)
ctx.Status(resp.StatusCode)
ctx.Stream(func(w io.Writer) bool {
io.Copy(w, resp.Body)
return false
})
}
func (c *Config) hlsStream(ctx *gin.Context, oriURL *url.URL) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get(oriURL.String())
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusFound {
location, err := resp.Location()
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
id := ctx.Param("id")
if strings.Contains(location.String(), id) {
hlsChannelsRedirectURLLock.Lock()
hlsChannelsRedirectURL[id] = *location
hlsChannelsRedirectURLLock.Unlock()
hlsResp, err := http.Get(location.String())
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
defer hlsResp.Body.Close()
b, err := ioutil.ReadAll(hlsResp.Body)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
body := string(b)
body = strings.ReplaceAll(body, "/"+c.XtreamUser+"/"+c.XtreamPassword+"/", "/"+c.User+"/"+c.Password+"/")
ctx.Data(http.StatusOK, hlsResp.Header.Get("Content-Type"), []byte(body))
return
}
ctx.AbortWithError(http.StatusInternalServerError, errors.New("Unable to HLS stream"))
return
}
ctx.Status(resp.StatusCode)
}
func copyHTTPHeader(ctx *gin.Context, header http.Header) {
for k, v := range header {
ctx.Header(k, strings.Join(v, ", "))
}
}
// authRequest handle auth credentials
type authRequest struct {
Username string `form:"username" binding:"required"`
Password string `form:"password" binding:"required"`
}
func (c *Config) authenticate(ctx *gin.Context) {
var authReq authRequest
if err := ctx.Bind(&authReq); err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
if c.ProxyConfig.User != authReq.Username || c.ProxyConfig.Password != authReq.Password {
ctx.AbortWithStatus(http.StatusUnauthorized)
}
}
func (c *Config) appAuthenticate(ctx *gin.Context) {
contents, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
q, err := url.ParseQuery(string(contents))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
if len(q["username"]) == 0 || len(q["password"]) == 0 {
ctx.AbortWithError(http.StatusBadRequest, fmt.Errorf("bad body url query parameters"))
return
}
log.Printf("[iptv-proxy] %v | %s |App Auth\n", time.Now().Format("2006/01/02 - 15:04:05"), ctx.ClientIP())
if c.ProxyConfig.User != q["username"][0] || c.ProxyConfig.Password != q["password"][0] {
ctx.AbortWithStatus(http.StatusUnauthorized)
}
ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(contents))
}

72
pkg/server/routes.go Normal file
View File

@@ -0,0 +1,72 @@
package server
import (
"fmt"
"log"
"net/url"
"strings"
"github.com/gin-gonic/gin"
)
func (c *Config) routes(r *gin.RouterGroup) {
r = r.Group(c.CustomEndpoint)
//Xtream service endopoints
if c.ProxyConfig.XtreamBaseURL != "" {
c.xtreamRoutes(r)
if strings.Contains(c.XtreamBaseURL, c.RemoteURL.Host) &&
c.XtreamUser == c.RemoteURL.Query().Get("username") &&
c.XtreamPassword == c.RemoteURL.Query().Get("password") {
r.GET("/"+c.M3UFileName, c.authenticate, c.xtreamGetAuto)
// XXX Private need: for external Android app
r.POST("/"+c.M3UFileName, c.authenticate, c.xtreamGetAuto)
return
}
}
c.m3uRoutes(r)
}
func (c *Config) xtreamRoutes(r *gin.RouterGroup) {
r.GET("/get.php", c.authenticate, c.xtreamGet)
r.POST("/get.php", c.authenticate, c.xtreamGet)
r.GET("/player_api.php", c.authenticate, c.xtreamPlayerAPIGET)
r.POST("/player_api.php", c.appAuthenticate, c.xtreamPlayerAPIPOST)
r.GET("/xmltv.php", c.authenticate, c.xtreamXMLTV)
r.GET(fmt.Sprintf("/%s/%s/:id", c.User, c.Password), c.xtreamStream)
r.GET(fmt.Sprintf("/live/%s/%s/:id", c.User, c.Password), c.xtreamStreamLive)
r.GET(fmt.Sprintf("/movie/%s/%s/:id", c.User, c.Password), c.xtreamStreamMovie)
r.GET(fmt.Sprintf("/series/%s/%s/:id", c.User, c.Password), c.xtreamStreamSeries)
r.GET(fmt.Sprintf("/hlsr/:token/%s/%s/:channel/:hash/:chunk", c.User, c.Password), c.hlsrStream)
}
func (c *Config) m3uRoutes(r *gin.RouterGroup) {
r.GET("/"+c.M3UFileName, c.authenticate, c.getM3U)
// XXX Private need: for external Android app
r.POST("/"+c.M3UFileName, c.authenticate, c.getM3U)
// List to verify duplicate entry endpoints
checkList := map[string]int8{}
for i, track := range c.playlist.Tracks {
oriURL, err := url.Parse(track.URI)
if err != nil {
return
}
trackConfig := &Config{
ProxyConfig: c.ProxyConfig,
track: &c.playlist.Tracks[i],
}
_, ok := checkList[oriURL.Path]
if ok {
log.Printf("[iptv-proxy] WARNING endpoint %q already exist, skipping it", oriURL.Path)
continue
}
r.GET(fmt.Sprintf("/%s/%s/%s", c.User, c.Password, oriURL.Path), trackConfig.reverseProxy)
checkList[oriURL.Path] = 0
}
}

142
pkg/server/server.go Normal file
View File

@@ -0,0 +1,142 @@
package server
import (
"fmt"
"net/url"
"os"
"path/filepath"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
const (
defaultProxyfiedM3UPath = "/tmp/iptv-proxy.m3u"
)
// Config represent the server configuration
type Config struct {
*config.ProxyConfig
// M3U service part
playlist *m3u.Playlist
// this variable is set only for m3u proxy endpoints
track *m3u.Track
// path to the proxyfied m3u file
proxyfiedM3UPath string
}
// NewServer initialize a new server configuration
func NewServer(config *config.ProxyConfig) (*Config, error) {
var p m3u.Playlist
if config.RemoteURL.String() != "" {
var err error
p, err = m3u.Parse(config.RemoteURL.String())
if err != nil {
return nil, err
}
}
return &Config{
config,
&p,
nil,
defaultProxyfiedM3UPath,
}, nil
}
// Serve the iptv-proxy api
func (c *Config) Serve() error {
c.playlistInitialization()
router := gin.Default()
router.Use(cors.Default())
group := router.Group("/")
c.routes(group)
return router.Run(fmt.Sprintf(":%d", c.HostConfig.Port))
}
func (c *Config) playlistInitialization() error {
if len(c.playlist.Tracks) == 0 {
return nil
}
f, err := os.Create(c.proxyfiedM3UPath)
if err != nil {
return err
}
defer f.Close()
return c.marshallInto(f, false)
}
// MarshallInto a *bufio.Writer a Playlist.
func (c *Config) marshallInto(into *os.File, xtream bool) error {
into.WriteString("#EXTM3U\n")
for _, track := range c.playlist.Tracks {
into.WriteString("#EXTINF:")
into.WriteString(fmt.Sprintf("%d ", track.Length))
for i := range track.Tags {
if i == len(track.Tags)-1 {
into.WriteString(fmt.Sprintf("%s=%q", track.Tags[i].Name, track.Tags[i].Value))
continue
}
into.WriteString(fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value))
}
into.WriteString(", ")
uri, err := c.replaceURL(track.URI, xtream)
if err != nil {
return err
}
into.WriteString(fmt.Sprintf("%s\n%s\n", track.Name, uri))
}
return into.Sync()
}
// ReplaceURL replace original playlist url by proxy url
func (c *Config) replaceURL(uri string, xtream bool) (string, error) {
oriURL, err := url.Parse(uri)
if err != nil {
return "", err
}
protocol := "http"
if c.HTTPS {
protocol = "https"
}
customEnd := c.CustomEndpoint
if customEnd != "" {
customEnd = fmt.Sprintf("/%s", customEnd)
}
path := oriURL.EscapedPath()
if xtream {
path = fmt.Sprintf("/%s", filepath.Base(path))
}
newURI := fmt.Sprintf(
"%s://%s:%d%s/%s/%s%s",
protocol,
c.HostConfig.Hostname,
c.HostConfig.Port,
customEnd,
url.QueryEscape(c.User),
url.QueryEscape(c.Password),
path,
)
newURL, err := url.Parse(newURI)
if err != nil {
return "", err
}
return newURL.String(), nil
}

257
pkg/server/xtreamHandles.go Normal file
View File

@@ -0,0 +1,257 @@
package server
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/jamesnetherton/m3u"
xtreamapi "github.com/pierre-emmanuelJ/iptv-proxy/pkg/xtream-proxy"
)
type cacheMeta struct {
string
time.Time
}
var hlsChannelsRedirectURL map[string]url.URL = map[string]url.URL{}
var hlsChannelsRedirectURLLock = sync.RWMutex{}
// XXX Use key/value storage e.g: etcd, redis...
// and remove that dirty globals
var xtreamM3uCache map[string]cacheMeta = map[string]cacheMeta{}
var xtreamM3uCacheLock = sync.RWMutex{}
func (c *Config) cacheXtreamM3u(m3uURL *url.URL) error {
xtreamM3uCacheLock.Lock()
playlist, err := m3u.Parse(m3uURL.String())
if err != nil {
return err
}
tmp := c.playlist
c.playlist = &playlist
filename := base64.StdEncoding.EncodeToString([]byte(m3uURL.String()))
path := filepath.Join("/tmp", filename)
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
if err := c.marshallInto(f, true); err != nil {
return err
}
xtreamM3uCache[m3uURL.String()] = cacheMeta{path, time.Now()}
c.playlist = tmp
xtreamM3uCacheLock.Unlock()
return nil
}
func (c *Config) xtreamGetAuto(ctx *gin.Context) {
newQuery := ctx.Request.URL.Query()
q := c.RemoteURL.Query()
for k, v := range q {
if k == "username" || k == "password" {
continue
}
newQuery.Add(k, strings.Join(v, ","))
}
ctx.Request.URL.RawQuery = newQuery.Encode()
c.xtreamGet(ctx)
}
func (c *Config) xtreamGet(ctx *gin.Context) {
rawURL := fmt.Sprintf("%s/get.php?username=%s&password=%s", c.XtreamBaseURL, c.XtreamUser, c.XtreamPassword)
q := ctx.Request.URL.Query()
for k, v := range q {
if k == "username" || k == "password" {
continue
}
rawURL = fmt.Sprintf("%s&%s=%s", rawURL, k, strings.Join(v, ","))
}
m3uURL, err := url.Parse(rawURL)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
xtreamM3uCacheLock.RLock()
meta, ok := xtreamM3uCache[m3uURL.String()]
d := time.Now().Sub(meta.Time)
if !ok || d.Hours() >= float64(c.M3UCacheExpiration) {
log.Printf("[iptv-proxy] %v | %s | xtream cache m3u file\n", time.Now().Format("2006/01/02 - 15:04:05"), ctx.ClientIP())
xtreamM3uCacheLock.RUnlock()
if err := c.cacheXtreamM3u(m3uURL); err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
} else {
xtreamM3uCacheLock.RUnlock()
}
ctx.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, c.M3UFileName))
xtreamM3uCacheLock.RLock()
path := xtreamM3uCache[m3uURL.String()].string
xtreamM3uCacheLock.RUnlock()
ctx.Header("Content-Type", "application/octet-stream")
ctx.File(path)
}
func (c *Config) xtreamPlayerAPIGET(ctx *gin.Context) {
c.xtreamPlayerAPI(ctx, ctx.Request.URL.Query())
}
func (c *Config) xtreamPlayerAPIPOST(ctx *gin.Context) {
contents, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
q, err := url.ParseQuery(string(contents))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.xtreamPlayerAPI(ctx, q)
}
func (c *Config) xtreamPlayerAPI(ctx *gin.Context, q url.Values) {
var action string
if len(q["action"]) > 0 {
action = q["action"][0]
}
client, err := xtreamapi.New(c.XtreamUser, c.XtreamPassword, c.XtreamBaseURL)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
resp, httpcode, err := client.Action(c.ProxyConfig, action, q)
if err != nil {
ctx.AbortWithError(httpcode, err)
return
}
log.Printf("[iptv-proxy] %v | %s |Action\t%s\n", time.Now().Format("2006/01/02 - 15:04:05"), ctx.ClientIP(), action)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, resp)
}
func (c *Config) xtreamXMLTV(ctx *gin.Context) {
client, err := xtreamapi.New(c.XtreamUser, c.XtreamPassword, c.XtreamBaseURL)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
resp, err := client.GetXMLTV()
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.Data(http.StatusOK, "application/xml", resp)
}
func (c *Config) xtreamStream(ctx *gin.Context) {
id := ctx.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.XtreamBaseURL, c.XtreamUser, c.XtreamPassword, id))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.stream(ctx, rpURL)
}
func (c *Config) xtreamStreamLive(ctx *gin.Context) {
id := ctx.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/live/%s/%s/%s", c.XtreamBaseURL, c.XtreamUser, c.XtreamPassword, id))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.stream(ctx, rpURL)
}
func (c *Config) xtreamStreamMovie(ctx *gin.Context) {
id := ctx.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/movie/%s/%s/%s", c.XtreamBaseURL, c.XtreamUser, c.XtreamPassword, id))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.stream(ctx, rpURL)
}
func (c *Config) xtreamStreamSeries(ctx *gin.Context) {
id := ctx.Param("id")
rpURL, err := url.Parse(fmt.Sprintf("%s/series/%s/%s/%s", c.XtreamBaseURL, c.XtreamUser, c.XtreamPassword, id))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.stream(ctx, rpURL)
}
func (c *Config) hlsrStream(ctx *gin.Context) {
hlsChannelsRedirectURLLock.RLock()
url, ok := hlsChannelsRedirectURL[ctx.Param("channel")+".m3u8"]
if !ok {
ctx.AbortWithError(http.StatusNotFound, errors.New("HSL redirect url not found"))
return
}
hlsChannelsRedirectURLLock.RUnlock()
req, err := url.Parse(
fmt.Sprintf(
"%s://%s/hlsr/%s/%s/%s/%s/%s/%s",
url.Scheme,
url.Host,
ctx.Param("token"),
c.XtreamUser,
c.XtreamPassword,
ctx.Param("channel"),
ctx.Param("hash"),
ctx.Param("chunk"),
),
)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.stream(ctx, req)
}

View File

@@ -1,26 +1,34 @@
package xtreamproxy package xtreamproxy
import ( import (
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
xtream "github.com/tellytv/go.xtream-codes" xtream "github.com/tellytv/go.xtream-codes"
) )
const ( const (
GetLiveCategories = "get_live_categories" getLiveCategories = "get_live_categories"
GetLiveStreams = "get_live_streams" getLiveStreams = "get_live_streams"
GetVodCategories = "get_vod_categories" getVodCategories = "get_vod_categories"
GetVodStreams = "get_vod_streams" getVodStreams = "get_vod_streams"
GetVodInfo = "get_vod_info" getVodInfo = "get_vod_info"
GetSeriesCategories = "get_series_categories" getSeriesCategories = "get_series_categories"
GetSeries = "get_series" getSeries = "get_series"
GetSerieInfo = "get_series_info" getSerieInfo = "get_series_info"
GetShortEPG = "get_short_epg" getShortEPG = "get_short_epg"
GetSimpleDataTable = "get_simple_data_table" getSimpleDataTable = "get_simple_data_table"
) )
// Client represent an xtream client
type Client struct { type Client struct {
*xtream.XtreamClient *xtream.XtreamClient
} }
// New new xtream client
func New(user, password, baseURL string) (*Client, error) { func New(user, password, baseURL string) (*Client, error) {
cli, err := xtream.NewClient(user, password, baseURL) cli, err := xtream.NewClient(user, password, baseURL)
if err != nil { if err != nil {
@@ -30,13 +38,14 @@ func New(user, password, baseURL string) (*Client, error) {
return &Client{cli}, nil return &Client{cli}, nil
} }
type Login struct { type login struct {
UserInfo xtream.UserInfo `json:"user_info"` UserInfo xtream.UserInfo `json:"user_info"`
ServerInfo xtream.ServerInfo `json:"server_info"` ServerInfo xtream.ServerInfo `json:"server_info"`
} }
func (c *Client) Login(proxyUser, proxyPassword, proxyURL string, proxyPort int, protocol string) (Login, error) { // Login xtream login
req := Login{ func (c *Client) login(proxyUser, proxyPassword, proxyURL string, proxyPort int, protocol string) (login, error) {
req := login{
UserInfo: xtream.UserInfo{ UserInfo: xtream.UserInfo{
Username: proxyUser, Username: proxyUser,
Password: proxyPassword, Password: proxyPassword,
@@ -64,3 +73,66 @@ func (c *Client) Login(proxyUser, proxyPassword, proxyURL string, proxyPort int,
return req, nil return req, nil
} }
// Action execute an xtream action.
func (c *Client) Action(config *config.ProxyConfig, action string, q url.Values) (respBody interface{}, httpcode int, err error) {
protocol := "http"
if config.HTTPS {
protocol = "https"
}
switch action {
case getLiveCategories:
respBody, err = c.GetLiveCategories()
case getLiveStreams:
respBody, err = c.GetLiveStreams("")
case getVodCategories:
respBody, err = c.GetVideoOnDemandCategories()
case getVodStreams:
respBody, err = c.GetVideoOnDemandStreams("")
case getVodInfo:
if len(q["vod_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "vod_id"`)
httpcode = http.StatusBadRequest
return
}
respBody, err = c.GetVideoOnDemandInfo(q["vod_id"][0])
case getSeriesCategories:
respBody, err = c.GetSeriesCategories()
case getSeries:
respBody, err = c.GetSeries("")
case getSerieInfo:
if len(q["series_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "series_id"`)
httpcode = http.StatusBadRequest
return
}
respBody, err = c.GetSeriesInfo(q["series_id"][0])
case getShortEPG:
if len(q["stream_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "stream_id"`)
httpcode = http.StatusBadRequest
return
}
limit := 0
if len(q["limit"]) > 0 {
limit, err = strconv.Atoi(q["limit"][0])
if err != nil {
httpcode = http.StatusInternalServerError
return
}
}
respBody, err = c.GetShortEPG(q["stream_id"][0], limit)
case getSimpleDataTable:
if len(q["stream_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "stream_id"`)
httpcode = http.StatusBadRequest
return
}
respBody, err = c.GetEPG(q["stream_id"][0])
default:
respBody, err = c.login(config.User, config.Password, protocol+"://"+config.HostConfig.Hostname, int(config.HostConfig.Port), protocol)
}
return
}

13
vendor/github.com/grafov/m3u8/.drone.yml generated vendored Normal file
View File

@@ -0,0 +1,13 @@
kind: pipeline
name: default
workspace:
base: /go
path: src/github.com/grafov/m3u8
steps:
- name: test
image: golang
commands:
- go get
- go test

22
vendor/github.com/grafov/m3u8/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,22 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
*~
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go

19
vendor/github.com/grafov/m3u8/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,19 @@
language: go
# Versions of go that are explicitly supported.
go:
- 1.6.3
- 1.7.3
- 1.8.x
- tip
# Required for coverage.
before_install:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
script:
- go build -a -v ./...
- diff <(gofmt -d .) <("")
- go test -v -covermode=count -coverprofile=coverage.out
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci

24
vendor/github.com/grafov/m3u8/AUTHORS generated vendored Normal file
View File

@@ -0,0 +1,24 @@
There are many persons contribute their code (including small patches)
to the project. They listed below in an alphabetical order:
- Alexander I.Grafov <grafov@gmail.com>
- Andrew Sinclair <ajsinclair@gmail.com>
- Andrey Chernov <chernov@bradburylab.com>
- Bradley Falzon <brad@teambrad.net>
- Denys Smirnov <denis.smirnov.91@gmail.com>
- Fabrizio (Misto) Milo <mistobaan@gmail.com>
- Hori Ryota <hori.ryota@gmail.com>
- Jamie Stackhouse <jamie.stackhouse@redspace.com>
- Julian Cooper <jcooper@brightcove.com>
- Kz26
- Lei Gao
- Makombo
- Michael Bow <mbow@brightcove.com>
- Scott Kidder <skidder@brightcove.com>
- Vishal Kumar Tuniki <vishal24.tuniki@gmail.com>
- Yevgen Flerko <md2k@md2k.net>
- Zac Shenker <zshenker@brightcove.com>
- Matthew Neil [mjneil](https://github.com/mjneil)
If you want to be added to this list (or removed for any reason)
just open an issue about it.

3
vendor/github.com/grafov/m3u8/Gomfile generated vendored Normal file
View File

@@ -0,0 +1,3 @@
group :development do
gom 'github.com/grafov/m3u8', :goos => [:linux]
end

29
vendor/github.com/grafov/m3u8/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,29 @@
Copyright (c) 2013-2016 Alexander I.Grafov <grafov@gmail.com>
Copyright (c) 2013-2016 The Project Developers.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

96
vendor/github.com/grafov/m3u8/M3U8.md generated vendored Normal file
View File

@@ -0,0 +1,96 @@
<!--*- mode:markdown;mode:orgtbl -*-->
<!---
Part of M3U8 parser & generator library.
This doc explaines M3U8 tag occurence in different versions
of HLS protocol and their status in Go library.
Copyright 2013-2016 The Project Developers.
See the AUTHORS and LICENSE files at the top-level directory of this distribution
and at https://github.com/grafov/m3u8/
ॐ तारे तुत्तारे तुरे स्व
-->
M3U8 tags cheatsheet
====================
The table above describes tags of M3U8, their occurence in playlists of different types and their support status
in the go-library.
Legend for playlist types:
* MAS is master playlist
* MED is media playlist
<!--- Note: markdown table below prepared in Emacs Orgmode and automatically converted to Github Markdown format -->
<!--- BEGIN RECEIVE ORGTBL specs -->
| Tag | Occured in | Proto ver | In Go lib since |
|---|---|---|---|
| EXT-X-ALLOW-CACHE | MED | 1 | 0.1 |
| EXT-X-BYTERANGE | MED | 4 | 0.1 |
| EXT-X-DISCONTINUITY | MED | 1 | 0.2 |
| EXT-X-DISCONTINUITY-SEQUENCE | MED | 6 | |
| EXT-X-ENDLIST | MED | 1 | 0.1 |
| EXT-X-I-FRAME-STREAM-INF | MAS | 4 | 0.3 |
| EXT-X-I-FRAMES-ONLY | MED | 4 | 0.3 |
| EXT-X-INDEPENDENT-SEGMENTS | MAS | 6 | |
| EXT-X-KEY | MED | 1 | 0.1 |
| EXT-X-MAP | MED | 5 | 0.3 |
| EXT-X-MEDIA | MAS | 4 | 0.1 |
| EXT-X-MEDIA-SEQUENCE | MED | 1 | 0.1 |
| EXT-X-PLAYLIST-TYPE | MED | 3 | 0.2 |
| EXT-X-PROGRAM-DATE-TIME | MED | 1 | 0.2 |
| EXT-X-SESSION-DATA | MAS | 7 | |
| EXT-X-START | MAS | 6 | |
| EXT-X-STREAM-INF | MAS | 1 | 0.1 |
| EXT-X-TARGETDURATION | MED | 1 | 0.1 |
| EXT-X-VERSION | MAS | 2 | 0.1 |
| EXTINF | MED | 1 | 0.1 |
| EXTM3U | MAS,MED | 1 | 0.1 |
<!--- END RECEIVE ORGTBL specs -->
<!---
#+ORGTBL: SEND specs orgtbl-to-gfm
| Tag | Occured in | Proto ver | In Go lib since |
|------------------------------+------------+-----------+-----------------|
| | | <l> | <l> |
| EXT-X-ALLOW-CACHE | MED | 1 | 0.1 |
| EXT-X-BYTERANGE | MED | 4 | 0.1 |
| EXT-X-DISCONTINUITY | MED | 1 | 0.2 |
| EXT-X-DISCONTINUITY-SEQUENCE | MED | 6 | |
| EXT-X-ENDLIST | MED | 1 | 0.1 |
| EXT-X-I-FRAME-STREAM-INF | MAS | 4 | 0.3 |
| EXT-X-I-FRAMES-ONLY | MED | 4 | 0.3 |
| EXT-X-INDEPENDENT-SEGMENTS | MAS | 6 | |
| EXT-X-KEY | MED | 1 | 0.1 |
| EXT-X-MAP | MED | 5 | 0.3 |
| EXT-X-MEDIA | MAS | 4 | 0.1 |
| EXT-X-MEDIA-SEQUENCE | MED | 1 | 0.1 |
| EXT-X-PLAYLIST-TYPE | MED | 3 | 0.2 |
| EXT-X-PROGRAM-DATE-TIME | MED | 1 | 0.2 |
| EXT-X-SESSION-DATA | MAS | 7 | |
| EXT-X-START | MAS | 6 | |
| EXT-X-STREAM-INF | MAS | 1 | 0.1 |
| EXT-X-TARGETDURATION | MED | 1 | 0.1 |
| EXT-X-VERSION | MAS | 2 | 0.1 |
| EXTINF | MED | 1 | 0.1 |
| EXTM3U | MAS,MED | 1 | 0.1 |
-->
IETF drafts notes
-----------------
[IETF](http://ietf.org) document currently in Draft status. Different versions of the document introduce changes of HLS protocol playlist formats. Latest version of the HLS protocol is version 7.
http://tools.ietf.org/html/draft-pantos-http-live-streaming
* Version 1 of the HLS protocol described in draft00-draft02.
* Version 2 of the HLS protocol described in draft03-draft04.
* Version 3 of the HLS protocol described in draft05-draft06.
* Version 4 of the HLS protocol described in draft07-draft08.
* Version 5 of the HLS protocol described in draft09-draft11.
* Version 6 of the HLS protocol described in draft12-draft13.
* Version 7 of the HLS protocol described in draft14-draft19.

148
vendor/github.com/grafov/m3u8/README.md generated vendored Normal file
View File

@@ -0,0 +1,148 @@
<!--*- mode:markdown -*-->
M3U8
====
This is the most complete opensource library for parsing and generating of M3U8 playlists
used in HTTP Live Streaming (Apple HLS) for internet video translations.
M3U8 is simple text format and parsing library for it must be simple too. It does not offer
ways to play HLS or handle playlists over HTTP. So library features are:
* Support HLS specs up to version 5 of the protocol.
* Parsing and generation of master-playlists and media-playlists.
* Autodetect input streams as master or media playlists.
* Offer structures for keeping playlists metadata.
* Encryption keys support for use with DRM systems like [Verimatrix](http://verimatrix.com) etc.
* Support for non standard [Google Widevine](http://www.widevine.com) tags.
The library covered by BSD 3-clause license. See [LICENSE](LICENSE) for the full text.
Versions 0.8 and below was covered by GPL v3. License was changed from the version 0.9 and upper.
See the list of the library authors at [AUTHORS](AUTHORS) file.
Install
-------
go get github.com/grafov/m3u8
or get releases from https://github.com/grafov/m3u8/releases
Documentation [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/grafov/m3u8)
-------------
Package online documentation (examples included) available at:
* http://gowalker.org/github.com/grafov/m3u8
* http://godoc.org/github.com/grafov/m3u8
Supported by the HLS protocol tags and their library support explained in [M3U8 cheatsheet](M3U8.md).
Examples
--------
Parse playlist:
```go
f, err := os.Open("playlist.m3u8")
if err != nil {
panic(err)
}
p, listType, err := m3u8.DecodeFrom(bufio.NewReader(f), true)
if err != nil {
panic(err)
}
switch listType {
case m3u8.MEDIA:
mediapl := p.(*m3u8.MediaPlaylist)
fmt.Printf("%+v\n", mediapl)
case m3u8.MASTER:
masterpl := p.(*m3u8.MasterPlaylist)
fmt.Printf("%+v\n", masterpl)
}
```
Then you get filled with parsed data structures. For master playlists you get ``Master`` struct with slice consists of pointers to ``Variant`` structures (which represent playlists to each bitrate).
For media playlist parser returns ``MediaPlaylist`` structure with slice of ``Segments``. Each segment is of ``MediaSegment`` type.
See ``structure.go`` or full documentation (link below).
You may use API methods to fill structures or create them manually to generate playlists. Example of media playlist generation:
```go
p, e := m3u8.NewMediaPlaylist(3, 10) // with window of size 3 and capacity 10
if e != nil {
panic(fmt.Sprintf("Creating of media playlist failed: %s", e))
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 6.0, "")
if e != nil {
panic(fmt.Sprintf("Add segment #%d to a media playlist failed: %s", i, e))
}
}
fmt.Println(p.Encode().String())
```
Custom Tags
-----------
M3U8 supports parsing and writing of custom tags. You must implement both the `CustomTag` and `CustomDecoder` interface for each custom tag that may be encountered in the playlist. Look at the template files in `example/template/` for examples on parsing custom playlist and segment tags.
Library structure
-----------------
Library has compact code and bundled in three files:
* `structure.go` — declares all structures related to playlists and their properties
* `reader.go` — playlist parser methods
* `writer.go` — playlist generator methods
Each file has own test suite placed in `*_test.go` accordingly.
Related links
-------------
* http://en.wikipedia.org/wiki/M3U
* http://en.wikipedia.org/wiki/HTTP_Live_Streaming
* http://gonze.com/playlists/playlist-format-survey.html
Library usage
-------------
This library was successfully used in streaming software developed for company where I worked several
years ago. It was tested then in generating of VOD and Live streams and parsing of Widevine Live streams.
Also the library used in opensource software so you may look at these apps for usage examples:
* [HLS downloader](https://github.com/kz26/gohls)
* [Another HLS downloader](https://github.com/Makombo/hlsdownloader)
* [HLS utils](https://github.com/archsh/hls-utils)
* [M3U8 reader](https://github.com/jeongmin/m3u8-reader)
M3U8 parsing/generation in other languages
------------------------------------------
* https://github.com/globocom/m3u8 in Python
* https://github.com/zencoder/m3uzi in Ruby
* https://github.com/Jeanvf/M3U8Paser in Objective C
* https://github.com/tedconf/node-m3u8 in Javascript
* http://sourceforge.net/projects/m3u8parser/ in Java
* https://github.com/karlll/erlm3u8 in Erlang
Project status [![Go Report Card](https://goreportcard.com/badge/grafov/m3u8)](https://goreportcard.com/report/grafov/m3u8)
--------------
[![Build Status](https://travis-ci.org/grafov/m3u8.png?branch=master)](https://travis-ci.org/grafov/m3u8) [![Build Status](https://cloud.drone.io/api/badges/grafov/m3u8/status.svg)](https://cloud.drone.io/grafov/m3u8) [![Coverage Status](https://coveralls.io/repos/github/grafov/m3u8/badge.svg?branch=master)](https://coveralls.io/github/grafov/m3u8?branch=master)
Project maintainers:
* Lei Gao @leikao
* Bradley Falzon @bradleyfalzon
* Alexander Grafov @grafov
State of code coverage: https://gocover.io/github.com/grafov/m3u8
Roadmap
-------
To version 1.0:
* Support all M3U8 tags up to latest version of specs.
* Code coverage by unit tests up to 90%

57
vendor/github.com/grafov/m3u8/doc.go generated vendored Normal file
View File

@@ -0,0 +1,57 @@
/* Package M3U8 is parser & generator library for Apple HLS.
This is a most complete opensource library for parsing and generating of M3U8 playlists used in HTTP Live Streaming (Apple HLS) for internet video translations.
M3U8 is simple text format and parsing library for it must be simple too. It did not offer ways to play HLS or handle playlists over HTTP. Library features are:
* Support HLS specs up to version 5 of the protocol.
* Parsing and generation of master-playlists and media-playlists.
* Autodetect input streams as master or media playlists.
* Offer structures for keeping playlists metadata.
* Encryption keys support for usage with DRM systems like Verimatrix (http://verimatrix.com) etc.
* Support for non standard Google Widevine (http://www.widevine.com) tags.
Library coded accordingly with IETF draft http://tools.ietf.org/html/draft-pantos-http-live-streaming
Examples of usage may be found in *_test.go files of a package. Also see below some simple examples.
Create simple media playlist with sliding window of 3 segments and maximum of 50 segments.
p, e := NewMediaPlaylist(3, 50)
if e != nil {
panic(fmt.Sprintf("Create media playlist failed: %s", e))
}
for i := 0; i < 5; i++ {
e = p.Add(fmt.Sprintf("test%d.ts", i), 5.0)
if e != nil {
panic(fmt.Sprintf("Add segment #%d to a media playlist failed: %s", i, e))
}
}
fmt.Println(p.Encode(true).String())
We add 5 testX.ts segments to playlist then encode it to M3U8 format and convert to string.
Next example shows parsing of master playlist:
f, err := os.Open("sample-playlists/master.m3u8")
if err != nil {
fmt.Println(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Playlist object: %+v\n", p)
We are open playlist from the file and parse it as master playlist.
*/
package m3u8
// Copyright 2013-2017 The Project Developers.
// See the AUTHORS and LICENSE files at the top-level directory of this distribution
// and at https://github.com/grafov/m3u8/
// ॐ तारे तुत्तारे तुरे स्व

3
vendor/github.com/grafov/m3u8/go.mod generated vendored Normal file
View File

@@ -0,0 +1,3 @@
module github.com/grafov/m3u8
go 1.12

18
vendor/github.com/grafov/m3u8/nut.json generated vendored Normal file
View File

@@ -0,0 +1,18 @@
{
"Version": "0.1.0",
"Vendor": "grafov",
"Authors": [
{
"FullName": "Alexander I.Grafov",
"Email": "grafov@gmail.com"
}
],
"ExtraFiles": [
"README.md",
"M3U8.md",
"LICENSE",
"TODO.org",
"sample-playlists"
],
"Homepage": "http://github.com/grafov/m3u8"
}

846
vendor/github.com/grafov/m3u8/reader.go generated vendored Normal file
View File

@@ -0,0 +1,846 @@
package m3u8
/*
Part of M3U8 parser & generator library.
This file defines functions related to playlist parsing.
Copyright 2013-2017 The Project Developers.
See the AUTHORS and LICENSE files at the top-level directory of this distribution
and at https://github.com/grafov/m3u8/
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"bytes"
"errors"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"time"
)
var reKeyValue = regexp.MustCompile(`([a-zA-Z0-9_-]+)=("[^"]+"|[^",]+)`)
// Allow globally apply and/or override Time Parser function.
// Available variants:
// * FullTimeParse - implements full featured ISO/IEC 8601:2004
// * StrictTimeParse - implements only RFC3339 Nanoseconds format
var TimeParse func(value string) (time.Time, error) = FullTimeParse
// Decode parses a master playlist passed from the buffer. If `strict`
// parameter is true then it returns first syntax error.
func (p *MasterPlaylist) Decode(data bytes.Buffer, strict bool) error {
return p.decode(&data, strict)
}
// DecodeFrom parses a master playlist passed from the io.Reader
// stream. If `strict` parameter is true then it returns first syntax
// error.
func (p *MasterPlaylist) DecodeFrom(reader io.Reader, strict bool) error {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(reader)
if err != nil {
return err
}
return p.decode(buf, strict)
}
// WithCustomDecoders adds custom tag decoders to the master playlist for decoding
func (p *MasterPlaylist) WithCustomDecoders(customDecoders []CustomDecoder) Playlist {
// Create the map if it doesn't already exist
if p.Custom == nil {
p.Custom = make(map[string]CustomTag)
}
p.customDecoders = customDecoders
return p
}
// Parse master playlist. Internal function.
func (p *MasterPlaylist) decode(buf *bytes.Buffer, strict bool) error {
var eof bool
state := new(decodingState)
for !eof {
line, err := buf.ReadString('\n')
if err == io.EOF {
eof = true
} else if err != nil {
break
}
err = decodeLineOfMasterPlaylist(p, state, line, strict)
if strict && err != nil {
return err
}
}
if strict && !state.m3u {
return errors.New("#EXTM3U absent")
}
return nil
}
// Decode parses a media playlist passed from the buffer. If `strict`
// parameter is true then return first syntax error.
func (p *MediaPlaylist) Decode(data bytes.Buffer, strict bool) error {
return p.decode(&data, strict)
}
// DecodeFrom parses a media playlist passed from the io.Reader
// stream. If `strict` parameter is true then it returns first syntax
// error.
func (p *MediaPlaylist) DecodeFrom(reader io.Reader, strict bool) error {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(reader)
if err != nil {
return err
}
return p.decode(buf, strict)
}
// WithCustomDecoders adds custom tag decoders to the media playlist for decoding
func (p *MediaPlaylist) WithCustomDecoders(customDecoders []CustomDecoder) Playlist {
// Create the map if it doesn't already exist
if p.Custom == nil {
p.Custom = make(map[string]CustomTag)
}
p.customDecoders = customDecoders
return p
}
func (p *MediaPlaylist) decode(buf *bytes.Buffer, strict bool) error {
var eof bool
var line string
var err error
state := new(decodingState)
wv := new(WV)
for !eof {
if line, err = buf.ReadString('\n'); err == io.EOF {
eof = true
} else if err != nil {
break
}
err = decodeLineOfMediaPlaylist(p, wv, state, line, strict)
if strict && err != nil {
return err
}
}
if state.tagWV {
p.WV = wv
}
if strict && !state.m3u {
return errors.New("#EXTM3U absent")
}
return nil
}
// Decode detects type of playlist and decodes it. It accepts bytes
// buffer as input.
func Decode(data bytes.Buffer, strict bool) (Playlist, ListType, error) {
return decode(&data, strict, nil)
}
// DecodeFrom detects type of playlist and decodes it. It accepts data
// conformed with io.Reader.
func DecodeFrom(reader io.Reader, strict bool) (Playlist, ListType, error) {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(reader)
if err != nil {
return nil, 0, err
}
return decode(buf, strict, nil)
}
// DecodeWith detects the type of playlist and decodes it. It accepts either bytes.Buffer
// or io.Reader as input. Any custom decoders provided will be used during decoding.
func DecodeWith(input interface{}, strict bool, customDecoders []CustomDecoder) (Playlist, ListType, error) {
switch v := input.(type) {
case bytes.Buffer:
return decode(&v, strict, customDecoders)
case io.Reader:
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(v)
if err != nil {
return nil, 0, err
}
return decode(buf, strict, customDecoders)
default:
return nil, 0, errors.New("input must be bytes.Buffer or io.Reader type")
}
}
// Detect playlist type and decode it. May be used as decoder for both
// master and media playlists.
func decode(buf *bytes.Buffer, strict bool, customDecoders []CustomDecoder) (Playlist, ListType, error) {
var eof bool
var line string
var master *MasterPlaylist
var media *MediaPlaylist
var listType ListType
var err error
state := new(decodingState)
wv := new(WV)
master = NewMasterPlaylist()
media, err = NewMediaPlaylist(8, 1024) // Winsize for VoD will become 0, capacity auto extends
if err != nil {
return nil, 0, fmt.Errorf("Create media playlist failed: %s", err)
}
// If we have custom tags to parse
if customDecoders != nil {
media = media.WithCustomDecoders(customDecoders).(*MediaPlaylist)
master = master.WithCustomDecoders(customDecoders).(*MasterPlaylist)
state.custom = make(map[string]CustomTag)
}
for !eof {
if line, err = buf.ReadString('\n'); err == io.EOF {
eof = true
} else if err != nil {
break
}
// fixes the issues https://github.com/grafov/m3u8/issues/25
// TODO: the same should be done in decode functions of both Master- and MediaPlaylists
// so some DRYing would be needed.
if len(line) < 1 || line == "\r" {
continue
}
err = decodeLineOfMasterPlaylist(master, state, line, strict)
if strict && err != nil {
return master, state.listType, err
}
err = decodeLineOfMediaPlaylist(media, wv, state, line, strict)
if strict && err != nil {
return media, state.listType, err
}
}
if state.listType == MEDIA && state.tagWV {
media.WV = wv
}
if strict && !state.m3u {
return nil, listType, errors.New("#EXTM3U absent")
}
switch state.listType {
case MASTER:
return master, MASTER, nil
case MEDIA:
if media.Closed || media.MediaType == EVENT {
// VoD and Event's should show the entire playlist
media.SetWinSize(0)
}
return media, MEDIA, nil
}
return nil, state.listType, errors.New("Can't detect playlist type")
}
// DecodeAttributeList turns an attribute list into a key, value map. You should trim
// any characters not part of the attribute list, such as the tag and ':'.
func DecodeAttributeList(line string) map[string]string {
return decodeParamsLine(line)
}
func decodeParamsLine(line string) map[string]string {
out := make(map[string]string)
for _, kv := range reKeyValue.FindAllStringSubmatch(line, -1) {
k, v := kv[1], kv[2]
out[k] = strings.Trim(v, ` "`)
}
return out
}
// Parse one line of master playlist.
func decodeLineOfMasterPlaylist(p *MasterPlaylist, state *decodingState, line string, strict bool) error {
var err error
line = strings.TrimSpace(line)
// check for custom tags first to allow custom parsing of existing tags
if p.Custom != nil {
for _, v := range p.customDecoders {
if strings.HasPrefix(line, v.TagName()) {
t, err := v.Decode(line)
if strict && err != nil {
return err
}
p.Custom[t.TagName()] = t
}
}
}
switch {
case line == "#EXTM3U": // start tag first
state.m3u = true
case strings.HasPrefix(line, "#EXT-X-VERSION:"): // version tag
state.listType = MASTER
_, err = fmt.Sscanf(line, "#EXT-X-VERSION:%d", &p.ver)
if strict && err != nil {
return err
}
case line == "#EXT-X-INDEPENDENT-SEGMENTS":
p.SetIndependentSegments(true)
case strings.HasPrefix(line, "#EXT-X-MEDIA:"):
var alt Alternative
state.listType = MASTER
for k, v := range decodeParamsLine(line[13:]) {
switch k {
case "TYPE":
alt.Type = v
case "GROUP-ID":
alt.GroupId = v
case "LANGUAGE":
alt.Language = v
case "NAME":
alt.Name = v
case "DEFAULT":
if strings.ToUpper(v) == "YES" {
alt.Default = true
} else if strings.ToUpper(v) == "NO" {
alt.Default = false
} else if strict {
return errors.New("value must be YES or NO")
}
case "AUTOSELECT":
alt.Autoselect = v
case "FORCED":
alt.Forced = v
case "CHARACTERISTICS":
alt.Characteristics = v
case "SUBTITLES":
alt.Subtitles = v
case "URI":
alt.URI = v
}
}
state.alternatives = append(state.alternatives, &alt)
case !state.tagStreamInf && strings.HasPrefix(line, "#EXT-X-STREAM-INF:"):
state.tagStreamInf = true
state.listType = MASTER
state.variant = new(Variant)
if len(state.alternatives) > 0 {
state.variant.Alternatives = state.alternatives
state.alternatives = nil
}
p.Variants = append(p.Variants, state.variant)
for k, v := range decodeParamsLine(line[18:]) {
switch k {
case "PROGRAM-ID":
var val int
val, err = strconv.Atoi(v)
if strict && err != nil {
return err
}
state.variant.ProgramId = uint32(val)
case "BANDWIDTH":
var val int
val, err = strconv.Atoi(v)
if strict && err != nil {
return err
}
state.variant.Bandwidth = uint32(val)
case "CODECS":
state.variant.Codecs = v
case "RESOLUTION":
state.variant.Resolution = v
case "AUDIO":
state.variant.Audio = v
case "VIDEO":
state.variant.Video = v
case "SUBTITLES":
state.variant.Subtitles = v
case "CLOSED-CAPTIONS":
state.variant.Captions = v
case "NAME":
state.variant.Name = v
case "AVERAGE-BANDWIDTH":
var val int
val, err = strconv.Atoi(v)
if strict && err != nil {
return err
}
state.variant.AverageBandwidth = uint32(val)
case "FRAME-RATE":
if state.variant.FrameRate, err = strconv.ParseFloat(v, 64); strict && err != nil {
return err
}
case "VIDEO-RANGE":
state.variant.VideoRange = v
case "HDCP-LEVEL":
state.variant.HDCPLevel = v
}
}
case state.tagStreamInf && !strings.HasPrefix(line, "#"):
state.tagStreamInf = false
state.variant.URI = line
case strings.HasPrefix(line, "#EXT-X-I-FRAME-STREAM-INF:"):
state.listType = MASTER
state.variant = new(Variant)
state.variant.Iframe = true
if len(state.alternatives) > 0 {
state.variant.Alternatives = state.alternatives
state.alternatives = nil
}
p.Variants = append(p.Variants, state.variant)
for k, v := range decodeParamsLine(line[26:]) {
switch k {
case "URI":
state.variant.URI = v
case "PROGRAM-ID":
var val int
val, err = strconv.Atoi(v)
if strict && err != nil {
return err
}
state.variant.ProgramId = uint32(val)
case "BANDWIDTH":
var val int
val, err = strconv.Atoi(v)
if strict && err != nil {
return err
}
state.variant.Bandwidth = uint32(val)
case "CODECS":
state.variant.Codecs = v
case "RESOLUTION":
state.variant.Resolution = v
case "AUDIO":
state.variant.Audio = v
case "VIDEO":
state.variant.Video = v
case "AVERAGE-BANDWIDTH":
var val int
val, err = strconv.Atoi(v)
if strict && err != nil {
return err
}
state.variant.AverageBandwidth = uint32(val)
case "VIDEO-RANGE":
state.variant.VideoRange = v
case "HDCP-LEVEL":
state.variant.HDCPLevel = v
}
}
case strings.HasPrefix(line, "#"):
// comments are ignored
}
return err
}
// Parse one line of media playlist.
func decodeLineOfMediaPlaylist(p *MediaPlaylist, wv *WV, state *decodingState, line string, strict bool) error {
var err error
line = strings.TrimSpace(line)
// check for custom tags first to allow custom parsing of existing tags
if p.Custom != nil {
for _, v := range p.customDecoders {
if strings.HasPrefix(line, v.TagName()) {
t, err := v.Decode(line)
if strict && err != nil {
return err
}
if v.SegmentTag() {
state.tagCustom = true
state.custom[v.TagName()] = t
} else {
p.Custom[v.TagName()] = t
}
}
}
}
switch {
case !state.tagInf && strings.HasPrefix(line, "#EXTINF:"):
state.tagInf = true
state.listType = MEDIA
sepIndex := strings.Index(line, ",")
if sepIndex == -1 {
if strict {
return fmt.Errorf("could not parse: %q", line)
}
sepIndex = len(line)
}
duration := line[8:sepIndex]
if len(duration) > 0 {
if state.duration, err = strconv.ParseFloat(duration, 64); strict && err != nil {
return fmt.Errorf("Duration parsing error: %s", err)
}
}
if len(line) > sepIndex {
state.title = line[sepIndex+1:]
}
case !strings.HasPrefix(line, "#"):
if state.tagInf {
err := p.Append(line, state.duration, state.title)
if err == ErrPlaylistFull {
// Extend playlist by doubling size, reset internal state, try again.
// If the second Append fails, the if err block will handle it.
// Retrying instead of being recursive was chosen as the state maybe
// modified non-idempotently.
p.Segments = append(p.Segments, make([]*MediaSegment, p.Count())...)
p.capacity = uint(len(p.Segments))
p.tail = p.count
err = p.Append(line, state.duration, state.title)
}
// Check err for first or subsequent Append()
if err != nil {
return err
}
state.tagInf = false
}
if state.tagRange {
if err = p.SetRange(state.limit, state.offset); strict && err != nil {
return err
}
state.tagRange = false
}
if state.tagSCTE35 {
state.tagSCTE35 = false
if err = p.SetSCTE35(state.scte); strict && err != nil {
return err
}
}
if state.tagDiscontinuity {
state.tagDiscontinuity = false
if err = p.SetDiscontinuity(); strict && err != nil {
return err
}
}
if state.tagProgramDateTime && p.Count() > 0 {
state.tagProgramDateTime = false
if err = p.SetProgramDateTime(state.programDateTime); strict && err != nil {
return err
}
}
// If EXT-X-KEY appeared before reference to segment (EXTINF) then it linked to this segment
if state.tagKey {
p.Segments[p.last()].Key = &Key{state.xkey.Method, state.xkey.URI, state.xkey.IV, state.xkey.Keyformat, state.xkey.Keyformatversions}
// First EXT-X-KEY may appeared in the header of the playlist and linked to first segment
// but for convenient playlist generation it also linked as default playlist key
if p.Key == nil {
p.Key = state.xkey
}
state.tagKey = false
}
// If EXT-X-MAP appeared before reference to segment (EXTINF) then it linked to this segment
if state.tagMap {
p.Segments[p.last()].Map = &Map{state.xmap.URI, state.xmap.Limit, state.xmap.Offset}
// First EXT-X-MAP may appeared in the header of the playlist and linked to first segment
// but for convenient playlist generation it also linked as default playlist map
if p.Map == nil {
p.Map = state.xmap
}
state.tagMap = false
}
// if segment custom tag appeared before EXTINF then it links to this segment
if state.tagCustom {
p.Segments[p.last()].Custom = state.custom
state.custom = make(map[string]CustomTag)
state.tagCustom = false
}
// start tag first
case line == "#EXTM3U":
state.m3u = true
case line == "#EXT-X-ENDLIST":
state.listType = MEDIA
p.Closed = true
case strings.HasPrefix(line, "#EXT-X-VERSION:"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#EXT-X-VERSION:%d", &p.ver); strict && err != nil {
return err
}
case strings.HasPrefix(line, "#EXT-X-TARGETDURATION:"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#EXT-X-TARGETDURATION:%f", &p.TargetDuration); strict && err != nil {
return err
}
case strings.HasPrefix(line, "#EXT-X-MEDIA-SEQUENCE:"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#EXT-X-MEDIA-SEQUENCE:%d", &p.SeqNo); strict && err != nil {
return err
}
case strings.HasPrefix(line, "#EXT-X-PLAYLIST-TYPE:"):
state.listType = MEDIA
var playlistType string
_, err = fmt.Sscanf(line, "#EXT-X-PLAYLIST-TYPE:%s", &playlistType)
if err != nil {
if strict {
return err
}
} else {
switch playlistType {
case "EVENT":
p.MediaType = EVENT
case "VOD":
p.MediaType = VOD
}
}
case strings.HasPrefix(line, "#EXT-X-DISCONTINUITY-SEQUENCE:"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#EXT-X-DISCONTINUITY-SEQUENCE:%d", &p.DiscontinuitySeq); strict && err != nil {
return err
}
case strings.HasPrefix(line, "#EXT-X-START:"):
state.listType = MEDIA
for k, v := range decodeParamsLine(line[13:]) {
switch k {
case "TIME-OFFSET":
st, err := strconv.ParseFloat(v, 64)
if err != nil {
return fmt.Errorf("Invalid TIME-OFFSET: %s: %v", v, err)
}
p.StartTime = st
case "PRECISE":
p.StartTimePrecise = v == "YES"
}
}
case strings.HasPrefix(line, "#EXT-X-KEY:"):
state.listType = MEDIA
state.xkey = new(Key)
for k, v := range decodeParamsLine(line[11:]) {
switch k {
case "METHOD":
state.xkey.Method = v
case "URI":
state.xkey.URI = v
case "IV":
state.xkey.IV = v
case "KEYFORMAT":
state.xkey.Keyformat = v
case "KEYFORMATVERSIONS":
state.xkey.Keyformatversions = v
}
}
state.tagKey = true
case strings.HasPrefix(line, "#EXT-X-MAP:"):
state.listType = MEDIA
state.xmap = new(Map)
for k, v := range decodeParamsLine(line[11:]) {
switch k {
case "URI":
state.xmap.URI = v
case "BYTERANGE":
if _, err = fmt.Sscanf(v, "%d@%d", &state.xmap.Limit, &state.xmap.Offset); strict && err != nil {
return fmt.Errorf("Byterange sub-range length value parsing error: %s", err)
}
}
}
state.tagMap = true
case !state.tagProgramDateTime && strings.HasPrefix(line, "#EXT-X-PROGRAM-DATE-TIME:"):
state.tagProgramDateTime = true
state.listType = MEDIA
if state.programDateTime, err = TimeParse(line[25:]); strict && err != nil {
return err
}
case !state.tagRange && strings.HasPrefix(line, "#EXT-X-BYTERANGE:"):
state.tagRange = true
state.listType = MEDIA
state.offset = 0
params := strings.SplitN(line[17:], "@", 2)
if state.limit, err = strconv.ParseInt(params[0], 10, 64); strict && err != nil {
return fmt.Errorf("Byterange sub-range length value parsing error: %s", err)
}
if len(params) > 1 {
if state.offset, err = strconv.ParseInt(params[1], 10, 64); strict && err != nil {
return fmt.Errorf("Byterange sub-range offset value parsing error: %s", err)
}
}
case !state.tagSCTE35 && strings.HasPrefix(line, "#EXT-SCTE35:"):
state.tagSCTE35 = true
state.listType = MEDIA
state.scte = new(SCTE)
state.scte.Syntax = SCTE35_67_2014
for attribute, value := range decodeParamsLine(line[12:]) {
switch attribute {
case "CUE":
state.scte.Cue = value
case "ID":
state.scte.ID = value
case "TIME":
state.scte.Time, _ = strconv.ParseFloat(value, 64)
}
}
case !state.tagSCTE35 && strings.HasPrefix(line, "#EXT-OATCLS-SCTE35:"):
// EXT-OATCLS-SCTE35 contains the SCTE35 tag, EXT-X-CUE-OUT contains duration
state.tagSCTE35 = true
state.scte = new(SCTE)
state.scte.Syntax = SCTE35_OATCLS
state.scte.Cue = line[19:]
case state.tagSCTE35 && state.scte.Syntax == SCTE35_OATCLS && strings.HasPrefix(line, "#EXT-X-CUE-OUT:"):
// EXT-OATCLS-SCTE35 contains the SCTE35 tag, EXT-X-CUE-OUT contains duration
state.scte.Time, _ = strconv.ParseFloat(line[15:], 64)
state.scte.CueType = SCTE35Cue_Start
case !state.tagSCTE35 && strings.HasPrefix(line, "#EXT-X-CUE-OUT-CONT:"):
state.tagSCTE35 = true
state.scte = new(SCTE)
state.scte.Syntax = SCTE35_OATCLS
state.scte.CueType = SCTE35Cue_Mid
for attribute, value := range decodeParamsLine(line[20:]) {
switch attribute {
case "SCTE35":
state.scte.Cue = value
case "Duration":
state.scte.Time, _ = strconv.ParseFloat(value, 64)
case "ElapsedTime":
state.scte.Elapsed, _ = strconv.ParseFloat(value, 64)
}
}
case !state.tagSCTE35 && line == "#EXT-X-CUE-IN":
state.tagSCTE35 = true
state.scte = new(SCTE)
state.scte.Syntax = SCTE35_OATCLS
state.scte.CueType = SCTE35Cue_End
case !state.tagDiscontinuity && strings.HasPrefix(line, "#EXT-X-DISCONTINUITY"):
state.tagDiscontinuity = true
state.listType = MEDIA
case strings.HasPrefix(line, "#EXT-X-I-FRAMES-ONLY"):
state.listType = MEDIA
p.Iframe = true
case strings.HasPrefix(line, "#WV-AUDIO-CHANNELS"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-AUDIO-CHANNELS %d", &wv.AudioChannels); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-AUDIO-FORMAT"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-AUDIO-FORMAT %d", &wv.AudioFormat); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-AUDIO-PROFILE-IDC"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-AUDIO-PROFILE-IDC %d", &wv.AudioProfileIDC); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-AUDIO-SAMPLE-SIZE"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-AUDIO-SAMPLE-SIZE %d", &wv.AudioSampleSize); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-AUDIO-SAMPLING-FREQUENCY"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-AUDIO-SAMPLING-FREQUENCY %d", &wv.AudioSamplingFrequency); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-CYPHER-VERSION"):
state.listType = MEDIA
wv.CypherVersion = line[19:]
state.tagWV = true
case strings.HasPrefix(line, "#WV-ECM"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-ECM %s", &wv.ECM); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-VIDEO-FORMAT"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-VIDEO-FORMAT %d", &wv.VideoFormat); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-VIDEO-FRAME-RATE"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-VIDEO-FRAME-RATE %d", &wv.VideoFrameRate); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-VIDEO-LEVEL-IDC"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-VIDEO-LEVEL-IDC %d", &wv.VideoLevelIDC); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-VIDEO-PROFILE-IDC"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-VIDEO-PROFILE-IDC %d", &wv.VideoProfileIDC); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#WV-VIDEO-RESOLUTION"):
state.listType = MEDIA
wv.VideoResolution = line[21:]
state.tagWV = true
case strings.HasPrefix(line, "#WV-VIDEO-SAR"):
state.listType = MEDIA
if _, err = fmt.Sscanf(line, "#WV-VIDEO-SAR %s", &wv.VideoSAR); strict && err != nil {
return err
}
if err == nil {
state.tagWV = true
}
case strings.HasPrefix(line, "#"):
// comments are ignored
}
return err
}
// StrictTimeParse implements RFC3339 with Nanoseconds accuracy.
func StrictTimeParse(value string) (time.Time, error) {
return time.Parse(DATETIME, value)
}
// FullTimeParse implements ISO/IEC 8601:2004.
func FullTimeParse(value string) (time.Time, error) {
layouts := []string{
"2006-01-02T15:04:05.999999999Z0700",
"2006-01-02T15:04:05.999999999Z07:00",
"2006-01-02T15:04:05.999999999Z07",
}
var (
err error
t time.Time
)
for _, layout := range layouts {
if t, err = time.Parse(layout, value); err == nil {
return t, nil
}
}
return t, err
}

332
vendor/github.com/grafov/m3u8/structure.go generated vendored Normal file
View File

@@ -0,0 +1,332 @@
package m3u8
/*
Part of M3U8 parser & generator library.
This file defines data structures related to package.
Copyright 2013-2017 The Project Developers.
See the AUTHORS and LICENSE files at the top-level directory of this distribution
and at https://github.com/grafov/m3u8/
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"bytes"
"io"
"time"
)
const (
/*
Compatibility rules described in section 7:
Clients and servers MUST implement protocol version 2 or higher to use:
o The IV attribute of the EXT-X-KEY tag.
Clients and servers MUST implement protocol version 3 or higher to use:
o Floating-point EXTINF duration values.
Clients and servers MUST implement protocol version 4 or higher to use:
o The EXT-X-BYTERANGE tag.
o The EXT-X-I-FRAME-STREAM-INF tag.
o The EXT-X-I-FRAMES-ONLY tag.
o The EXT-X-MEDIA tag.
o The AUDIO and VIDEO attributes of the EXT-X-STREAM-INF tag.
*/
minver = uint8(3)
DATETIME = time.RFC3339Nano // Format for EXT-X-PROGRAM-DATE-TIME defined in section 3.4.5
)
type ListType uint
const (
// use 0 for not defined type
MASTER ListType = iota + 1
MEDIA
)
// for EXT-X-PLAYLIST-TYPE tag
type MediaType uint
const (
// use 0 for not defined type
EVENT MediaType = iota + 1
VOD
)
// SCTE35Syntax defines the format of the SCTE-35 cue points which do not use
// the draft-pantos-http-live-streaming-19 EXT-X-DATERANGE tag and instead
// have their own custom tags
type SCTE35Syntax uint
const (
// SCTE35_67_2014 will be the default due to backwards compatibility reasons.
SCTE35_67_2014 SCTE35Syntax = iota // SCTE35_67_2014 defined in http://www.scte.org/documents/pdf/standards/SCTE%2067%202014.pdf
SCTE35_OATCLS // SCTE35_OATCLS is a non-standard but common format
)
// SCTE35CueType defines the type of cue point, used by readers and writers to
// write a different syntax
type SCTE35CueType uint
const (
SCTE35Cue_Start SCTE35CueType = iota // SCTE35Cue_Start indicates an out cue point
SCTE35Cue_Mid // SCTE35Cue_Mid indicates a segment between start and end cue points
SCTE35Cue_End // SCTE35Cue_End indicates an in cue point
)
/*
This structure represents a single bitrate playlist aka media playlist.
It related to both a simple media playlists and a sliding window media playlists.
URI lines in the Playlist point to media segments.
Simple Media Playlist file sample:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:5220
#EXTINF:5219.2,
http://media.example.com/entire.ts
#EXT-X-ENDLIST
Sample of Sliding Window Media Playlist, using HTTPS:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:8
#EXT-X-MEDIA-SEQUENCE:2680
#EXTINF:7.975,
https://priv.example.com/fileSequence2680.ts
#EXTINF:7.941,
https://priv.example.com/fileSequence2681.ts
#EXTINF:7.975,
https://priv.example.com/fileSequence2682.ts
*/
type MediaPlaylist struct {
TargetDuration float64
SeqNo uint64 // EXT-X-MEDIA-SEQUENCE
Segments []*MediaSegment
Args string // optional arguments placed after URIs (URI?Args)
Iframe bool // EXT-X-I-FRAMES-ONLY
Closed bool // is this VOD (closed) or Live (sliding) playlist?
MediaType MediaType
DiscontinuitySeq uint64 // EXT-X-DISCONTINUITY-SEQUENCE
StartTime float64
StartTimePrecise bool
durationAsInt bool // output durations as integers of floats?
keyformat int
winsize uint // max number of segments displayed in an encoded playlist; need set to zero for VOD playlists
capacity uint // total capacity of slice used for the playlist
head uint // head of FIFO, we add segments to head
tail uint // tail of FIFO, we remove segments from tail
count uint // number of segments added to the playlist
buf bytes.Buffer
ver uint8
Key *Key // EXT-X-KEY is optional encryption key displayed before any segments (default key for the playlist)
Map *Map // EXT-X-MAP is optional tag specifies how to obtain the Media Initialization Section (default map for the playlist)
WV *WV // Widevine related tags outside of M3U8 specs
Custom map[string]CustomTag
customDecoders []CustomDecoder
}
/*
This structure represents a master playlist which combines media playlists for multiple bitrates.
URI lines in the playlist identify media playlists.
Sample of Master Playlist file:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1280000
http://example.com/low.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2560000
http://example.com/mid.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=7680000
http://example.com/hi.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=65000,CODECS="mp4a.40.5"
http://example.com/audio-only.m3u8
*/
type MasterPlaylist struct {
Variants []*Variant
Args string // optional arguments placed after URI (URI?Args)
CypherVersion string // non-standard tag for Widevine (see also WV struct)
buf bytes.Buffer
ver uint8
independentSegments bool
Custom map[string]CustomTag
customDecoders []CustomDecoder
}
// This structure represents variants for master playlist.
// Variants included in a master playlist and point to media playlists.
type Variant struct {
URI string
Chunklist *MediaPlaylist
VariantParams
}
// This structure represents additional parameters for a variant
// used in EXT-X-STREAM-INF and EXT-X-I-FRAME-STREAM-INF
type VariantParams struct {
ProgramId uint32
Bandwidth uint32
AverageBandwidth uint32 // EXT-X-STREAM-INF only
Codecs string
Resolution string
Audio string // EXT-X-STREAM-INF only
Video string
Subtitles string // EXT-X-STREAM-INF only
Captions string // EXT-X-STREAM-INF only
Name string // EXT-X-STREAM-INF only (non standard Wowza/JWPlayer extension to name the variant/quality in UA)
Iframe bool // EXT-X-I-FRAME-STREAM-INF
VideoRange string
HDCPLevel string
FrameRate float64 // EXT-X-STREAM-INF
Alternatives []*Alternative // EXT-X-MEDIA
}
// This structure represents EXT-X-MEDIA tag in variants.
type Alternative struct {
GroupId string
URI string
Type string
Language string
Name string
Default bool
Autoselect string
Forced string
Characteristics string
Subtitles string
}
// This structure represents a media segment included in a media playlist.
// Media segment may be encrypted.
// Widevine supports own tags for encryption metadata.
type MediaSegment struct {
SeqId uint64
Title string // optional second parameter for EXTINF tag
URI string
Duration float64 // first parameter for EXTINF tag; duration must be integers if protocol version is less than 3 but we are always keep them float
Limit int64 // EXT-X-BYTERANGE <n> is length in bytes for the file under URI
Offset int64 // EXT-X-BYTERANGE [@o] is offset from the start of the file under URI
Key *Key // EXT-X-KEY displayed before the segment and means changing of encryption key (in theory each segment may have own key)
Map *Map // EXT-X-MAP displayed before the segment
Discontinuity bool // EXT-X-DISCONTINUITY indicates an encoding discontinuity between the media segment that follows it and the one that preceded it (i.e. file format, number and type of tracks, encoding parameters, encoding sequence, timestamp sequence)
SCTE *SCTE // SCTE-35 used for Ad signaling in HLS
ProgramDateTime time.Time // EXT-X-PROGRAM-DATE-TIME tag associates the first sample of a media segment with an absolute date and/or time
Custom map[string]CustomTag
}
// SCTE holds custom, non EXT-X-DATERANGE, SCTE-35 tags
type SCTE struct {
Syntax SCTE35Syntax // Syntax defines the format of the SCTE-35 cue tag
CueType SCTE35CueType // CueType defines whether the cue is a start, mid, end (if applicable)
Cue string
ID string
Time float64
Elapsed float64
}
// This structure represents information about stream encryption.
//
// Realizes EXT-X-KEY tag.
type Key struct {
Method string
URI string
IV string
Keyformat string
Keyformatversions string
}
// This structure represents specifies how to obtain the Media
// Initialization Section required to parse the applicable
// Media Segments.
// It applies to every Media Segment that appears after it in the
// Playlist until the next EXT-X-MAP tag or until the end of the
// playlist.
//
// Realizes EXT-MAP tag.
type Map struct {
URI string
Limit int64 // <n> is length in bytes for the file under URI
Offset int64 // [@o] is offset from the start of the file under URI
}
// This structure represents metadata for Google Widevine playlists.
// This format not described in IETF draft but provied by Widevine Live Packager as
// additional tags with #WV-prefix.
type WV struct {
AudioChannels uint
AudioFormat uint
AudioProfileIDC uint
AudioSampleSize uint
AudioSamplingFrequency uint
CypherVersion string
ECM string
VideoFormat uint
VideoFrameRate uint
VideoLevelIDC uint
VideoProfileIDC uint
VideoResolution string
VideoSAR string
}
// Interface applied to various playlist types.
type Playlist interface {
Encode() *bytes.Buffer
Decode(bytes.Buffer, bool) error
DecodeFrom(reader io.Reader, strict bool) error
WithCustomDecoders([]CustomDecoder) Playlist
String() string
}
// Interface for decoding custom and unsupported tags
type CustomDecoder interface {
// TagName should return the full indentifier including the leading '#' as well as the
// trailing ':' if the tag also contains a value or attribute list
TagName() string
// Decode parses a line from the playlist and returns the CustomTag representation
Decode(line string) (CustomTag, error)
// SegmentTag should return true if this CustomDecoder should apply per segment.
// Should returns false if it a MediaPlaylist header tag.
// This value is ignored for MasterPlaylists.
SegmentTag() bool
}
// Interface for encoding custom and unsupported tags
type CustomTag interface {
// TagName should return the full indentifier including the leading '#' as well as the
// trailing ':' if the tag also contains a value or attribute list
TagName() string
// Encode should return the complete tag string as a *bytes.Buffer. This will
// be used by Playlist.Decode to write the tag to the m3u8.
// Return nil to not write anything to the m3u8.
Encode() *bytes.Buffer
// String should return the encoded tag as a string.
String() string
}
// Internal structure for decoding a line of input stream with a list type detection
type decodingState struct {
listType ListType
m3u bool
tagWV bool
tagStreamInf bool
tagInf bool
tagSCTE35 bool
tagRange bool
tagDiscontinuity bool
tagProgramDateTime bool
tagKey bool
tagMap bool
tagCustom bool
programDateTime time.Time
limit int64
offset int64
duration float64
title string
variant *Variant
alternatives []*Alternative
xkey *Key
xmap *Map
scte *SCTE
custom map[string]CustomTag
}

895
vendor/github.com/grafov/m3u8/writer.go generated vendored Normal file
View File

@@ -0,0 +1,895 @@
package m3u8
/*
Part of M3U8 parser & generator library.
This file defines functions related to playlist generation.
Copyright 2013-2017 The Project Developers.
See the AUTHORS and LICENSE files at the top-level directory of this distribution
and at https://github.com/grafov/m3u8/
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"bytes"
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"
)
var (
ErrPlaylistFull = errors.New("playlist is full")
)
// Set version of the playlist accordingly with section 7
func version(ver *uint8, newver uint8) {
if *ver < newver {
*ver = newver
}
}
func strver(ver uint8) string {
return strconv.FormatUint(uint64(ver), 10)
}
// Create new empty master playlist.
// Master playlist consists of variants.
func NewMasterPlaylist() *MasterPlaylist {
p := new(MasterPlaylist)
p.ver = minver
return p
}
// Append variant to master playlist.
// This operation does reset playlist cache.
func (p *MasterPlaylist) Append(uri string, chunklist *MediaPlaylist, params VariantParams) {
v := new(Variant)
v.URI = uri
v.Chunklist = chunklist
v.VariantParams = params
p.Variants = append(p.Variants, v)
if len(v.Alternatives) > 0 {
// From section 7:
// The EXT-X-MEDIA tag and the AUDIO, VIDEO and SUBTITLES attributes of
// the EXT-X-STREAM-INF tag are backward compatible to protocol version
// 1, but playback on older clients may not be desirable. A server MAY
// consider indicating a EXT-X-VERSION of 4 or higher in the Master
// Playlist but is not required to do so.
version(&p.ver, 4) // so it is optional and in theory may be set to ver.1
// but more tests required
}
p.buf.Reset()
}
func (p *MasterPlaylist) ResetCache() {
p.buf.Reset()
}
// Generate output in M3U8 format.
func (p *MasterPlaylist) Encode() *bytes.Buffer {
if p.buf.Len() > 0 {
return &p.buf
}
p.buf.WriteString("#EXTM3U\n#EXT-X-VERSION:")
p.buf.WriteString(strver(p.ver))
p.buf.WriteRune('\n')
if p.IndependentSegments() {
p.buf.WriteString("#EXT-X-INDEPENDENT-SEGMENTS\n")
}
// Write any custom master tags
if p.Custom != nil {
for _, v := range p.Custom {
if customBuf := v.Encode(); customBuf != nil {
p.buf.WriteString(customBuf.String())
p.buf.WriteRune('\n')
}
}
}
var altsWritten map[string]bool = make(map[string]bool)
for _, pl := range p.Variants {
if pl.Alternatives != nil {
for _, alt := range pl.Alternatives {
// Make sure that we only write out an alternative once
altKey := fmt.Sprintf("%s-%s-%s-%s", alt.Type, alt.GroupId, alt.Name, alt.Language)
if altsWritten[altKey] {
continue
}
altsWritten[altKey] = true
p.buf.WriteString("#EXT-X-MEDIA:")
if alt.Type != "" {
p.buf.WriteString("TYPE=") // Type should not be quoted
p.buf.WriteString(alt.Type)
}
if alt.GroupId != "" {
p.buf.WriteString(",GROUP-ID=\"")
p.buf.WriteString(alt.GroupId)
p.buf.WriteRune('"')
}
if alt.Name != "" {
p.buf.WriteString(",NAME=\"")
p.buf.WriteString(alt.Name)
p.buf.WriteRune('"')
}
p.buf.WriteString(",DEFAULT=")
if alt.Default {
p.buf.WriteString("YES")
} else {
p.buf.WriteString("NO")
}
if alt.Autoselect != "" {
p.buf.WriteString(",AUTOSELECT=")
p.buf.WriteString(alt.Autoselect)
}
if alt.Language != "" {
p.buf.WriteString(",LANGUAGE=\"")
p.buf.WriteString(alt.Language)
p.buf.WriteRune('"')
}
if alt.Forced != "" {
p.buf.WriteString(",FORCED=\"")
p.buf.WriteString(alt.Forced)
p.buf.WriteRune('"')
}
if alt.Characteristics != "" {
p.buf.WriteString(",CHARACTERISTICS=\"")
p.buf.WriteString(alt.Characteristics)
p.buf.WriteRune('"')
}
if alt.Subtitles != "" {
p.buf.WriteString(",SUBTITLES=\"")
p.buf.WriteString(alt.Subtitles)
p.buf.WriteRune('"')
}
if alt.URI != "" {
p.buf.WriteString(",URI=\"")
p.buf.WriteString(alt.URI)
p.buf.WriteRune('"')
}
p.buf.WriteRune('\n')
}
}
if pl.Iframe {
p.buf.WriteString("#EXT-X-I-FRAME-STREAM-INF:PROGRAM-ID=")
p.buf.WriteString(strconv.FormatUint(uint64(pl.ProgramId), 10))
p.buf.WriteString(",BANDWIDTH=")
p.buf.WriteString(strconv.FormatUint(uint64(pl.Bandwidth), 10))
if pl.AverageBandwidth != 0 {
p.buf.WriteString(",AVERAGE-BANDWIDTH=")
p.buf.WriteString(strconv.FormatUint(uint64(pl.AverageBandwidth), 10))
}
if pl.Codecs != "" {
p.buf.WriteString(",CODECS=\"")
p.buf.WriteString(pl.Codecs)
p.buf.WriteRune('"')
}
if pl.Resolution != "" {
p.buf.WriteString(",RESOLUTION=") // Resolution should not be quoted
p.buf.WriteString(pl.Resolution)
}
if pl.Video != "" {
p.buf.WriteString(",VIDEO=\"")
p.buf.WriteString(pl.Video)
p.buf.WriteRune('"')
}
if pl.VideoRange != "" {
p.buf.WriteString(",VIDEO-RANGE=")
p.buf.WriteString(pl.VideoRange)
}
if pl.HDCPLevel != "" {
p.buf.WriteString(",HDCP-LEVEL=")
p.buf.WriteString(pl.HDCPLevel)
}
if pl.URI != "" {
p.buf.WriteString(",URI=\"")
p.buf.WriteString(pl.URI)
p.buf.WriteRune('"')
}
p.buf.WriteRune('\n')
} else {
p.buf.WriteString("#EXT-X-STREAM-INF:PROGRAM-ID=")
p.buf.WriteString(strconv.FormatUint(uint64(pl.ProgramId), 10))
p.buf.WriteString(",BANDWIDTH=")
p.buf.WriteString(strconv.FormatUint(uint64(pl.Bandwidth), 10))
if pl.AverageBandwidth != 0 {
p.buf.WriteString(",AVERAGE-BANDWIDTH=")
p.buf.WriteString(strconv.FormatUint(uint64(pl.AverageBandwidth), 10))
}
if pl.Codecs != "" {
p.buf.WriteString(",CODECS=\"")
p.buf.WriteString(pl.Codecs)
p.buf.WriteRune('"')
}
if pl.Resolution != "" {
p.buf.WriteString(",RESOLUTION=") // Resolution should not be quoted
p.buf.WriteString(pl.Resolution)
}
if pl.Audio != "" {
p.buf.WriteString(",AUDIO=\"")
p.buf.WriteString(pl.Audio)
p.buf.WriteRune('"')
}
if pl.Video != "" {
p.buf.WriteString(",VIDEO=\"")
p.buf.WriteString(pl.Video)
p.buf.WriteRune('"')
}
if pl.Captions != "" {
p.buf.WriteString(",CLOSED-CAPTIONS=")
if pl.Captions == "NONE" {
p.buf.WriteString(pl.Captions) // CC should not be quoted when eq NONE
} else {
p.buf.WriteRune('"')
p.buf.WriteString(pl.Captions)
p.buf.WriteRune('"')
}
}
if pl.Subtitles != "" {
p.buf.WriteString(",SUBTITLES=\"")
p.buf.WriteString(pl.Subtitles)
p.buf.WriteRune('"')
}
if pl.Name != "" {
p.buf.WriteString(",NAME=\"")
p.buf.WriteString(pl.Name)
p.buf.WriteRune('"')
}
if pl.FrameRate != 0 {
p.buf.WriteString(",FRAME-RATE=")
p.buf.WriteString(strconv.FormatFloat(pl.FrameRate, 'f', 3, 64))
}
if pl.VideoRange != "" {
p.buf.WriteString(",VIDEO-RANGE=")
p.buf.WriteString(pl.VideoRange)
}
if pl.HDCPLevel != "" {
p.buf.WriteString(",HDCP-LEVEL=")
p.buf.WriteString(pl.HDCPLevel)
}
p.buf.WriteRune('\n')
p.buf.WriteString(pl.URI)
if p.Args != "" {
if strings.Contains(pl.URI, "?") {
p.buf.WriteRune('&')
} else {
p.buf.WriteRune('?')
}
p.buf.WriteString(p.Args)
}
p.buf.WriteRune('\n')
}
}
return &p.buf
}
// SetCustomTag sets the provided tag on the master playlist for its TagName
func (p *MasterPlaylist) SetCustomTag(tag CustomTag) {
if p.Custom == nil {
p.Custom = make(map[string]CustomTag)
}
p.Custom[tag.TagName()] = tag
}
// Version returns the current playlist version number
func (p *MasterPlaylist) Version() uint8 {
return p.ver
}
// SetVersion sets the playlist version number, note the version maybe changed
// automatically by other Set methods.
func (p *MasterPlaylist) SetVersion(ver uint8) {
p.ver = ver
}
// IndependentSegments returns true if all media samples in a segment can be
// decoded without information from other segments.
func (p *MasterPlaylist) IndependentSegments() bool {
return p.independentSegments
}
// SetIndependentSegments sets whether all media samples in a segment can be
// decoded without information from other segments.
func (p *MasterPlaylist) SetIndependentSegments(b bool) {
p.independentSegments = b
}
// For compatibility with Stringer interface
// For example fmt.Printf("%s", sampleMediaList) will encode
// playist and print its string representation.
func (p *MasterPlaylist) String() string {
return p.Encode().String()
}
// Creates new media playlist structure.
// Winsize defines how much items will displayed on playlist generation.
// Capacity is total size of a playlist.
func NewMediaPlaylist(winsize uint, capacity uint) (*MediaPlaylist, error) {
p := new(MediaPlaylist)
p.ver = minver
p.capacity = capacity
if err := p.SetWinSize(winsize); err != nil {
return nil, err
}
p.Segments = make([]*MediaSegment, capacity)
return p, nil
}
// last returns the previously written segment's index
func (p *MediaPlaylist) last() uint {
if p.tail == 0 {
return p.capacity - 1
}
return p.tail - 1
}
// Remove current segment from the head of chunk slice form a media playlist. Useful for sliding playlists.
// This operation does reset playlist cache.
func (p *MediaPlaylist) Remove() (err error) {
if p.count == 0 {
return errors.New("playlist is empty")
}
p.head = (p.head + 1) % p.capacity
p.count--
if !p.Closed {
p.SeqNo++
}
p.buf.Reset()
return nil
}
// Append general chunk to the tail of chunk slice for a media playlist.
// This operation does reset playlist cache.
func (p *MediaPlaylist) Append(uri string, duration float64, title string) error {
seg := new(MediaSegment)
seg.URI = uri
seg.Duration = duration
seg.Title = title
return p.AppendSegment(seg)
}
// AppendSegment appends a MediaSegment to the tail of chunk slice for a media playlist.
// This operation does reset playlist cache.
func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error {
if p.head == p.tail && p.count > 0 {
return ErrPlaylistFull
}
seg.SeqId = p.SeqNo
if p.count > 0 {
seg.SeqId = p.Segments[(p.capacity+p.tail-1)%p.capacity].SeqId + 1
}
p.Segments[p.tail] = seg
p.tail = (p.tail + 1) % p.capacity
p.count++
if p.TargetDuration < seg.Duration {
p.TargetDuration = math.Ceil(seg.Duration)
}
p.buf.Reset()
return nil
}
// Combines two operations: firstly it removes one chunk from the head of chunk slice and move pointer to
// next chunk. Secondly it appends one chunk to the tail of chunk slice. Useful for sliding playlists.
// This operation does reset cache.
func (p *MediaPlaylist) Slide(uri string, duration float64, title string) {
if !p.Closed && p.count >= p.winsize {
p.Remove()
}
p.Append(uri, duration, title)
}
// Reset playlist cache. Next called Encode() will regenerate playlist from the chunk slice.
func (p *MediaPlaylist) ResetCache() {
p.buf.Reset()
}
// Generate output in M3U8 format. Marshal `winsize` elements from bottom of the `segments` queue.
func (p *MediaPlaylist) Encode() *bytes.Buffer {
if p.buf.Len() > 0 {
return &p.buf
}
p.buf.WriteString("#EXTM3U\n#EXT-X-VERSION:")
p.buf.WriteString(strver(p.ver))
p.buf.WriteRune('\n')
// Write any custom master tags
if p.Custom != nil {
for _, v := range p.Custom {
if customBuf := v.Encode(); customBuf != nil {
p.buf.WriteString(customBuf.String())
p.buf.WriteRune('\n')
}
}
}
// default key (workaround for Widevine)
if p.Key != nil {
p.buf.WriteString("#EXT-X-KEY:")
p.buf.WriteString("METHOD=")
p.buf.WriteString(p.Key.Method)
if p.Key.Method != "NONE" {
p.buf.WriteString(",URI=\"")
p.buf.WriteString(p.Key.URI)
p.buf.WriteRune('"')
if p.Key.IV != "" {
p.buf.WriteString(",IV=")
p.buf.WriteString(p.Key.IV)
}
if p.Key.Keyformat != "" {
p.buf.WriteString(",KEYFORMAT=\"")
p.buf.WriteString(p.Key.Keyformat)
p.buf.WriteRune('"')
}
if p.Key.Keyformatversions != "" {
p.buf.WriteString(",KEYFORMATVERSIONS=\"")
p.buf.WriteString(p.Key.Keyformatversions)
p.buf.WriteRune('"')
}
}
p.buf.WriteRune('\n')
}
if p.Map != nil {
p.buf.WriteString("#EXT-X-MAP:")
p.buf.WriteString("URI=\"")
p.buf.WriteString(p.Map.URI)
p.buf.WriteRune('"')
if p.Map.Limit > 0 {
p.buf.WriteString(",BYTERANGE=")
p.buf.WriteString(strconv.FormatInt(p.Map.Limit, 10))
p.buf.WriteRune('@')
p.buf.WriteString(strconv.FormatInt(p.Map.Offset, 10))
}
p.buf.WriteRune('\n')
}
if p.MediaType > 0 {
p.buf.WriteString("#EXT-X-PLAYLIST-TYPE:")
switch p.MediaType {
case EVENT:
p.buf.WriteString("EVENT\n")
p.buf.WriteString("#EXT-X-ALLOW-CACHE:NO\n")
case VOD:
p.buf.WriteString("VOD\n")
}
}
p.buf.WriteString("#EXT-X-MEDIA-SEQUENCE:")
p.buf.WriteString(strconv.FormatUint(p.SeqNo, 10))
p.buf.WriteRune('\n')
p.buf.WriteString("#EXT-X-TARGETDURATION:")
p.buf.WriteString(strconv.FormatInt(int64(math.Ceil(p.TargetDuration)), 10)) // due section 3.4.2 of M3U8 specs EXT-X-TARGETDURATION must be integer
p.buf.WriteRune('\n')
if p.StartTime > 0.0 {
p.buf.WriteString("#EXT-X-START:TIME-OFFSET=")
p.buf.WriteString(strconv.FormatFloat(p.StartTime, 'f', -1, 64))
if p.StartTimePrecise {
p.buf.WriteString(",PRECISE=YES")
}
p.buf.WriteRune('\n')
}
if p.DiscontinuitySeq != 0 {
p.buf.WriteString("#EXT-X-DISCONTINUITY-SEQUENCE:")
p.buf.WriteString(strconv.FormatUint(uint64(p.DiscontinuitySeq), 10))
p.buf.WriteRune('\n')
}
if p.Iframe {
p.buf.WriteString("#EXT-X-I-FRAMES-ONLY\n")
}
// Widevine tags
if p.WV != nil {
if p.WV.AudioChannels != 0 {
p.buf.WriteString("#WV-AUDIO-CHANNELS ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.AudioChannels), 10))
p.buf.WriteRune('\n')
}
if p.WV.AudioFormat != 0 {
p.buf.WriteString("#WV-AUDIO-FORMAT ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.AudioFormat), 10))
p.buf.WriteRune('\n')
}
if p.WV.AudioProfileIDC != 0 {
p.buf.WriteString("#WV-AUDIO-PROFILE-IDC ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.AudioProfileIDC), 10))
p.buf.WriteRune('\n')
}
if p.WV.AudioSampleSize != 0 {
p.buf.WriteString("#WV-AUDIO-SAMPLE-SIZE ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.AudioSampleSize), 10))
p.buf.WriteRune('\n')
}
if p.WV.AudioSamplingFrequency != 0 {
p.buf.WriteString("#WV-AUDIO-SAMPLING-FREQUENCY ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.AudioSamplingFrequency), 10))
p.buf.WriteRune('\n')
}
if p.WV.CypherVersion != "" {
p.buf.WriteString("#WV-CYPHER-VERSION ")
p.buf.WriteString(p.WV.CypherVersion)
p.buf.WriteRune('\n')
}
if p.WV.ECM != "" {
p.buf.WriteString("#WV-ECM ")
p.buf.WriteString(p.WV.ECM)
p.buf.WriteRune('\n')
}
if p.WV.VideoFormat != 0 {
p.buf.WriteString("#WV-VIDEO-FORMAT ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.VideoFormat), 10))
p.buf.WriteRune('\n')
}
if p.WV.VideoFrameRate != 0 {
p.buf.WriteString("#WV-VIDEO-FRAME-RATE ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.VideoFrameRate), 10))
p.buf.WriteRune('\n')
}
if p.WV.VideoLevelIDC != 0 {
p.buf.WriteString("#WV-VIDEO-LEVEL-IDC")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.VideoLevelIDC), 10))
p.buf.WriteRune('\n')
}
if p.WV.VideoProfileIDC != 0 {
p.buf.WriteString("#WV-VIDEO-PROFILE-IDC ")
p.buf.WriteString(strconv.FormatUint(uint64(p.WV.VideoProfileIDC), 10))
p.buf.WriteRune('\n')
}
if p.WV.VideoResolution != "" {
p.buf.WriteString("#WV-VIDEO-RESOLUTION ")
p.buf.WriteString(p.WV.VideoResolution)
p.buf.WriteRune('\n')
}
if p.WV.VideoSAR != "" {
p.buf.WriteString("#WV-VIDEO-SAR ")
p.buf.WriteString(p.WV.VideoSAR)
p.buf.WriteRune('\n')
}
}
var (
seg *MediaSegment
durationCache = make(map[float64]string)
)
head := p.head
count := p.count
for i := uint(0); (i < p.winsize || p.winsize == 0) && count > 0; count-- {
seg = p.Segments[head]
head = (head + 1) % p.capacity
if seg == nil { // protection from badly filled chunklists
continue
}
if p.winsize > 0 { // skip for VOD playlists, where winsize = 0
i++
}
if seg.SCTE != nil {
switch seg.SCTE.Syntax {
case SCTE35_67_2014:
p.buf.WriteString("#EXT-SCTE35:")
p.buf.WriteString("CUE=\"")
p.buf.WriteString(seg.SCTE.Cue)
p.buf.WriteRune('"')
if seg.SCTE.ID != "" {
p.buf.WriteString(",ID=\"")
p.buf.WriteString(seg.SCTE.ID)
p.buf.WriteRune('"')
}
if seg.SCTE.Time != 0 {
p.buf.WriteString(",TIME=")
p.buf.WriteString(strconv.FormatFloat(seg.SCTE.Time, 'f', -1, 64))
}
p.buf.WriteRune('\n')
case SCTE35_OATCLS:
switch seg.SCTE.CueType {
case SCTE35Cue_Start:
p.buf.WriteString("#EXT-OATCLS-SCTE35:")
p.buf.WriteString(seg.SCTE.Cue)
p.buf.WriteRune('\n')
p.buf.WriteString("#EXT-X-CUE-OUT:")
p.buf.WriteString(strconv.FormatFloat(seg.SCTE.Time, 'f', -1, 64))
p.buf.WriteRune('\n')
case SCTE35Cue_Mid:
p.buf.WriteString("#EXT-X-CUE-OUT-CONT:")
p.buf.WriteString("ElapsedTime=")
p.buf.WriteString(strconv.FormatFloat(seg.SCTE.Elapsed, 'f', -1, 64))
p.buf.WriteString(",Duration=")
p.buf.WriteString(strconv.FormatFloat(seg.SCTE.Time, 'f', -1, 64))
p.buf.WriteString(",SCTE35=")
p.buf.WriteString(seg.SCTE.Cue)
p.buf.WriteRune('\n')
case SCTE35Cue_End:
p.buf.WriteString("#EXT-X-CUE-IN")
p.buf.WriteRune('\n')
}
}
}
// check for key change
if seg.Key != nil && p.Key != seg.Key {
p.buf.WriteString("#EXT-X-KEY:")
p.buf.WriteString("METHOD=")
p.buf.WriteString(seg.Key.Method)
if seg.Key.Method != "NONE" {
p.buf.WriteString(",URI=\"")
p.buf.WriteString(seg.Key.URI)
p.buf.WriteRune('"')
if seg.Key.IV != "" {
p.buf.WriteString(",IV=")
p.buf.WriteString(seg.Key.IV)
}
if seg.Key.Keyformat != "" {
p.buf.WriteString(",KEYFORMAT=\"")
p.buf.WriteString(seg.Key.Keyformat)
p.buf.WriteRune('"')
}
if seg.Key.Keyformatversions != "" {
p.buf.WriteString(",KEYFORMATVERSIONS=\"")
p.buf.WriteString(seg.Key.Keyformatversions)
p.buf.WriteRune('"')
}
}
p.buf.WriteRune('\n')
}
if seg.Discontinuity {
p.buf.WriteString("#EXT-X-DISCONTINUITY\n")
}
// ignore segment Map if default playlist Map is present
if p.Map == nil && seg.Map != nil {
p.buf.WriteString("#EXT-X-MAP:")
p.buf.WriteString("URI=\"")
p.buf.WriteString(seg.Map.URI)
p.buf.WriteRune('"')
if seg.Map.Limit > 0 {
p.buf.WriteString(",BYTERANGE=")
p.buf.WriteString(strconv.FormatInt(seg.Map.Limit, 10))
p.buf.WriteRune('@')
p.buf.WriteString(strconv.FormatInt(seg.Map.Offset, 10))
}
p.buf.WriteRune('\n')
}
if !seg.ProgramDateTime.IsZero() {
p.buf.WriteString("#EXT-X-PROGRAM-DATE-TIME:")
p.buf.WriteString(seg.ProgramDateTime.Format(DATETIME))
p.buf.WriteRune('\n')
}
if seg.Limit > 0 {
p.buf.WriteString("#EXT-X-BYTERANGE:")
p.buf.WriteString(strconv.FormatInt(seg.Limit, 10))
p.buf.WriteRune('@')
p.buf.WriteString(strconv.FormatInt(seg.Offset, 10))
p.buf.WriteRune('\n')
}
// Add Custom Segment Tags here
if seg.Custom != nil {
for _, v := range seg.Custom {
if customBuf := v.Encode(); customBuf != nil {
p.buf.WriteString(customBuf.String())
p.buf.WriteRune('\n')
}
}
}
p.buf.WriteString("#EXTINF:")
if str, ok := durationCache[seg.Duration]; ok {
p.buf.WriteString(str)
} else {
if p.durationAsInt {
// Old Android players has problems with non integer Duration.
durationCache[seg.Duration] = strconv.FormatInt(int64(math.Ceil(seg.Duration)), 10)
} else {
// Wowza Mediaserver and some others prefer floats.
durationCache[seg.Duration] = strconv.FormatFloat(seg.Duration, 'f', 3, 32)
}
p.buf.WriteString(durationCache[seg.Duration])
}
p.buf.WriteRune(',')
p.buf.WriteString(seg.Title)
p.buf.WriteRune('\n')
p.buf.WriteString(seg.URI)
if p.Args != "" {
p.buf.WriteRune('?')
p.buf.WriteString(p.Args)
}
p.buf.WriteRune('\n')
}
if p.Closed {
p.buf.WriteString("#EXT-X-ENDLIST\n")
}
return &p.buf
}
// For compatibility with Stringer interface
// For example fmt.Printf("%s", sampleMediaList) will encode
// playist and print its string representation.
func (p *MediaPlaylist) String() string {
return p.Encode().String()
}
// TargetDuration will be int on Encode
func (p *MediaPlaylist) DurationAsInt(yes bool) {
if yes {
// duration must be integers if protocol version is less than 3
version(&p.ver, 3)
}
p.durationAsInt = yes
}
// Count tells us the number of items that are currently in the media playlist
func (p *MediaPlaylist) Count() uint {
return p.count
}
// Close sliding playlist and make them fixed.
func (p *MediaPlaylist) Close() {
if p.buf.Len() > 0 {
p.buf.WriteString("#EXT-X-ENDLIST\n")
}
p.Closed = true
}
// Set encryption key appeared once in header of the playlist (pointer to MediaPlaylist.Key).
// It useful when keys not changed during playback.
// Set tag for the whole list.
func (p *MediaPlaylist) SetDefaultKey(method, uri, iv, keyformat, keyformatversions string) error {
// A Media Playlist MUST indicate a EXT-X-VERSION of 5 or higher if it
// contains:
// - The KEYFORMAT and KEYFORMATVERSIONS attributes of the EXT-X-KEY tag.
if keyformat != "" || keyformatversions != "" {
version(&p.ver, 5)
}
p.Key = &Key{method, uri, iv, keyformat, keyformatversions}
return nil
}
// Set default Media Initialization Section values for playlist (pointer to MediaPlaylist.Map).
// Set EXT-X-MAP tag for the whole playlist.
func (p *MediaPlaylist) SetDefaultMap(uri string, limit, offset int64) {
version(&p.ver, 5) // due section 4
p.Map = &Map{uri, limit, offset}
}
// Mark medialist as consists of only I-frames (Intra frames).
// Set tag for the whole list.
func (p *MediaPlaylist) SetIframeOnly() {
version(&p.ver, 4) // due section 4.3.3
p.Iframe = true
}
// Set encryption key for the current segment of media playlist (pointer to Segment.Key)
func (p *MediaPlaylist) SetKey(method, uri, iv, keyformat, keyformatversions string) error {
if p.count == 0 {
return errors.New("playlist is empty")
}
// A Media Playlist MUST indicate a EXT-X-VERSION of 5 or higher if it
// contains:
// - The KEYFORMAT and KEYFORMATVERSIONS attributes of the EXT-X-KEY tag.
if keyformat != "" || keyformatversions != "" {
version(&p.ver, 5)
}
p.Segments[p.last()].Key = &Key{method, uri, iv, keyformat, keyformatversions}
return nil
}
// Set map for the current segment of media playlist (pointer to Segment.Map)
func (p *MediaPlaylist) SetMap(uri string, limit, offset int64) error {
if p.count == 0 {
return errors.New("playlist is empty")
}
version(&p.ver, 5) // due section 4
p.Segments[p.last()].Map = &Map{uri, limit, offset}
return nil
}
// Set limit and offset for the current media segment (EXT-X-BYTERANGE support for protocol version 4).
func (p *MediaPlaylist) SetRange(limit, offset int64) error {
if p.count == 0 {
return errors.New("playlist is empty")
}
version(&p.ver, 4) // due section 3.4.1
p.Segments[p.last()].Limit = limit
p.Segments[p.last()].Offset = offset
return nil
}
// SetSCTE sets the SCTE cue format for the current media segment.
//
// Deprecated: Use SetSCTE35 instead.
func (p *MediaPlaylist) SetSCTE(cue string, id string, time float64) error {
return p.SetSCTE35(&SCTE{Syntax: SCTE35_67_2014, Cue: cue, ID: id, Time: time})
}
// SetSCTE35 sets the SCTE cue format for the current media segment
func (p *MediaPlaylist) SetSCTE35(scte35 *SCTE) error {
if p.count == 0 {
return errors.New("playlist is empty")
}
p.Segments[p.last()].SCTE = scte35
return nil
}
// Set discontinuity flag for the current media segment.
// EXT-X-DISCONTINUITY indicates an encoding discontinuity between the media segment
// that follows it and the one that preceded it (i.e. file format, number and type of tracks,
// encoding parameters, encoding sequence, timestamp sequence).
func (p *MediaPlaylist) SetDiscontinuity() error {
if p.count == 0 {
return errors.New("playlist is empty")
}
p.Segments[p.last()].Discontinuity = true
return nil
}
// Set program date and time for the current media segment.
// EXT-X-PROGRAM-DATE-TIME tag associates the first sample of a
// media segment with an absolute date and/or time. It applies only
// to the current media segment.
// Date/time format is YYYY-MM-DDThh:mm:ssZ (ISO8601) and includes time zone.
func (p *MediaPlaylist) SetProgramDateTime(value time.Time) error {
if p.count == 0 {
return errors.New("playlist is empty")
}
p.Segments[p.last()].ProgramDateTime = value
return nil
}
// SetCustomTag sets the provided tag on the media playlist for its TagName
func (p *MediaPlaylist) SetCustomTag(tag CustomTag) {
if p.Custom == nil {
p.Custom = make(map[string]CustomTag)
}
p.Custom[tag.TagName()] = tag
}
// SetCustomTag sets the provided tag on the current media segment for its TagName
func (p *MediaPlaylist) SetCustomSegmentTag(tag CustomTag) error {
if p.count == 0 {
return errors.New("playlist is empty")
}
last := p.Segments[p.last()]
if last.Custom == nil {
last.Custom = make(map[string]CustomTag)
}
last.Custom[tag.TagName()] = tag
return nil
}
// Version returns the current playlist version number
func (p *MediaPlaylist) Version() uint8 {
return p.ver
}
// SetVersion sets the playlist version number, note the version maybe changed
// automatically by other Set methods.
func (p *MediaPlaylist) SetVersion(ver uint8) {
p.ver = ver
}
// WinSize returns the playlist's window size.
func (p *MediaPlaylist) WinSize() uint {
return p.winsize
}
// SetWinSize overwrites the playlist's window size.
func (p *MediaPlaylist) SetWinSize(winsize uint) error {
if winsize > p.capacity {
return errors.New("capacity must be greater than winsize or equal")
}
p.winsize = winsize
return nil
}

2
vendor/modules.txt vendored
View File

@@ -11,6 +11,8 @@ github.com/gin-gonic/gin/json
github.com/gin-gonic/gin/render github.com/gin-gonic/gin/render
# github.com/golang/protobuf v1.2.0 # github.com/golang/protobuf v1.2.0
github.com/golang/protobuf/proto github.com/golang/protobuf/proto
# github.com/grafov/m3u8 v0.11.1
github.com/grafov/m3u8
# github.com/hashicorp/hcl v1.0.0 # github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/hcl github.com/hashicorp/hcl
github.com/hashicorp/hcl/hcl/ast github.com/hashicorp/hcl/hcl/ast