I *think* I got the downsampling working perfectly
This commit is contained in:
+2
-26
@@ -7,8 +7,6 @@ import (
|
|||||||
"github.com/deluan/gosonic/stream"
|
"github.com/deluan/gosonic/stream"
|
||||||
"github.com/deluan/gosonic/utils"
|
"github.com/deluan/gosonic/utils"
|
||||||
"github.com/karlkfi/inject"
|
"github.com/karlkfi/inject"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,19 +17,6 @@ type StreamController struct {
|
|||||||
mf *domain.MediaFile
|
mf *domain.MediaFile
|
||||||
}
|
}
|
||||||
|
|
||||||
type flushWriter struct {
|
|
||||||
f http.Flusher
|
|
||||||
w io.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *flushWriter) Write(p []byte) (n int, err error) {
|
|
||||||
n, err = fw.w.Write(p)
|
|
||||||
if fw.f != nil {
|
|
||||||
fw.f.Flush()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *StreamController) Prepare() {
|
func (c *StreamController) Prepare() {
|
||||||
inject.ExtractAssignable(utils.Graph, &c.repo)
|
inject.ExtractAssignable(utils.Graph, &c.repo)
|
||||||
|
|
||||||
@@ -51,21 +36,12 @@ func (c *StreamController) Prepare() {
|
|||||||
c.mf = mf
|
c.mf = mf
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFlusher(w http.ResponseWriter) io.Writer {
|
|
||||||
fw := flushWriter{w: w}
|
|
||||||
if f, ok := w.(http.Flusher); ok {
|
|
||||||
fw.f = f
|
|
||||||
}
|
|
||||||
return &fw
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Investigate why it is not flushing before closing the connection
|
|
||||||
func (c *StreamController) Stream() {
|
func (c *StreamController) Stream() {
|
||||||
var maxBitRate int
|
var maxBitRate int
|
||||||
c.Ctx.Input.Bind(&maxBitRate, "maxBitRate")
|
c.Ctx.Input.Bind(&maxBitRate, "maxBitRate")
|
||||||
maxBitRate = utils.MinInt(c.mf.BitRate, maxBitRate)
|
maxBitRate = utils.MinInt(c.mf.BitRate, maxBitRate)
|
||||||
|
|
||||||
beego.Debug("Streaming file", maxBitRate, ":", c.mf.Path)
|
beego.Debug("Streaming file", ":", c.mf.Path)
|
||||||
beego.Debug("Bitrate", c.mf.BitRate, "MaxBitRate", maxBitRate)
|
beego.Debug("Bitrate", c.mf.BitRate, "MaxBitRate", maxBitRate)
|
||||||
|
|
||||||
if maxBitRate > 0 {
|
if maxBitRate > 0 {
|
||||||
@@ -76,7 +52,7 @@ func (c *StreamController) Stream() {
|
|||||||
c.Ctx.Output.Header("Cache-Control", "must-revalidate")
|
c.Ctx.Output.Header("Cache-Control", "must-revalidate")
|
||||||
c.Ctx.Output.Header("Pragma", "public")
|
c.Ctx.Output.Header("Pragma", "public")
|
||||||
|
|
||||||
err := stream.Stream(c.mf.Path, c.mf.BitRate, maxBitRate, createFlusher(c.Ctx.ResponseWriter))
|
err := stream.Stream(c.mf.Path, c.mf.BitRate, maxBitRate, c.Ctx.ResponseWriter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("Error streaming file id", c.id, ":", err)
|
beego.Error("Error streaming file id", c.id, ":", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,14 +12,19 @@ import (
|
|||||||
func Stream(path string, bitRate int, maxBitRate int, w io.Writer) error {
|
func Stream(path string, bitRate int, maxBitRate int, w io.Writer) error {
|
||||||
if maxBitRate > 0 && bitRate > maxBitRate {
|
if maxBitRate > 0 && bitRate > maxBitRate {
|
||||||
cmdLine, args := createDownsamplingCommand(path, maxBitRate)
|
cmdLine, args := createDownsamplingCommand(path, maxBitRate)
|
||||||
cmd := exec.Command(cmdLine, args...)
|
|
||||||
beego.Debug("Executing cmd:", cmdLine, args)
|
|
||||||
|
|
||||||
cmd.Stdout = w
|
beego.Debug("Executing cmd:", cmdLine, args)
|
||||||
|
cmd := exec.Command(cmdLine, args...)
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
err := cmd.Run()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("Error executing", cmdLine, ":", err)
|
beego.Error("Error executing", cmdLine, ":", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = cmd.Start(); err != nil {
|
||||||
|
beego.Error("Error executing", cmdLine, ":", err)
|
||||||
|
} else {
|
||||||
|
_, err = io.Copy(w, stdout)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user