Browse Source

HLS: add Access-Control-Allow-Origin to every HTTP response; add parameter hlsAllowOrigin (#415)

pull/441/head
aler9 4 years ago
parent
commit
7b1466146b
  1. 4
      internal/conf/conf.go
  2. 2
      internal/hlsconverter/converter.go
  3. 5
      internal/hlsserver/server.go
  4. 2
      main.go
  5. 7
      rtsp-simple-server.yml

4
internal/conf/conf.go

@ -100,6 +100,7 @@ type Conf struct { @@ -100,6 +100,7 @@ type Conf struct {
HLSAddress string `yaml:"hlsAddress"`
HLSSegmentCount int `yaml:"hlsSegmentCount"`
HLSSegmentDuration time.Duration `yaml:"hlsSegmentDuration"`
HLSAllowOrigin string `yaml:"hlsAllowOrigin"`
// path
Paths map[string]*PathConf `yaml:"paths"`
@ -266,6 +267,9 @@ func (conf *Conf) fillAndCheck() error { @@ -266,6 +267,9 @@ func (conf *Conf) fillAndCheck() error {
if conf.HLSSegmentDuration == 0 {
conf.HLSSegmentDuration = 1 * time.Second
}
if conf.HLSAllowOrigin == "" {
conf.HLSAllowOrigin = "*"
}
if len(conf.Paths) == 0 {
conf.Paths = map[string]*PathConf{

2
internal/hlsconverter/converter.go

@ -552,8 +552,6 @@ func (c *Converter) runRequestHandler(terminate chan struct{}, done chan struct{ @@ -552,8 +552,6 @@ func (c *Converter) runRequestHandler(terminate chan struct{}, done chan struct{
case preq := <-c.request:
req := preq
req.W.Header().Add("Access-Control-Allow-Origin", "*")
atomic.StoreInt64(&c.lastRequestTime, time.Now().Unix())
conf := c.path.Conf()

5
internal/hlsserver/server.go

@ -25,6 +25,7 @@ type Parent interface { @@ -25,6 +25,7 @@ type Parent interface {
type Server struct {
hlsSegmentCount int
hlsSegmentDuration time.Duration
hlsAllowOrigin string
readBufferCount int
stats *stats.Stats
pathMan *pathman.PathManager
@ -47,6 +48,7 @@ func New( @@ -47,6 +48,7 @@ func New(
address string,
hlsSegmentCount int,
hlsSegmentDuration time.Duration,
hlsAllowOrigin string,
readBufferCount int,
stats *stats.Stats,
pathMan *pathman.PathManager,
@ -62,6 +64,7 @@ func New( @@ -62,6 +64,7 @@ func New(
s := &Server{
hlsSegmentCount: hlsSegmentCount,
hlsSegmentDuration: hlsSegmentDuration,
hlsAllowOrigin: hlsAllowOrigin,
readBufferCount: readBufferCount,
stats: stats,
pathMan: pathMan,
@ -146,6 +149,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -146,6 +149,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// remove leading prefix
pa := r.URL.Path[1:]
w.Header().Add("Access-Control-Allow-Origin", s.hlsAllowOrigin)
if pa == "" || pa == "favicon.ico" {
w.WriteHeader(http.StatusNotFound)
return

2
main.go

@ -307,6 +307,7 @@ func (p *program) createResources(initial bool) error { @@ -307,6 +307,7 @@ func (p *program) createResources(initial bool) error {
p.conf.HLSAddress,
p.conf.HLSSegmentCount,
p.conf.HLSSegmentDuration,
p.conf.HLSAllowOrigin,
p.conf.ReadBufferCount,
p.stats,
p.pathMan,
@ -426,6 +427,7 @@ func (p *program) closeResources(newConf *conf.Conf) { @@ -426,6 +427,7 @@ func (p *program) closeResources(newConf *conf.Conf) {
newConf.HLSAddress != p.conf.HLSAddress ||
newConf.HLSSegmentCount != p.conf.HLSSegmentCount ||
newConf.HLSSegmentDuration != p.conf.HLSSegmentDuration ||
newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin ||
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
closeStats ||
closePathMan {

7
rtsp-simple-server.yml

@ -98,9 +98,12 @@ hlsAddress: :8888 @@ -98,9 +98,12 @@ hlsAddress: :8888
# decreasing segments decrease latency.
hlsSegmentCount: 3
# minimum duration of each segment.
# the real segment duration is also influenced by the interval of IDR frames
# (since the server edit the segment duration to include at least one IDR frame).
# the real segment duration is also influenced by the interval between IDR frames,
# since the server changes the segment duration to include at least one IDR frame in each.
hlsSegmentDuration: 1s
# value of the Access-Control-Allow-Origin header provided in every HTTP response.
# This allows to play the HLS stream from an external website.
hlsAllowOrigin: '*'
###############################################
# Path parameters

Loading…
Cancel
Save