Skip to content

🎧 Your Personal Streaming Service

I *think* I got the downsampling working perfectly

Deluan3db18d8bd6be
diff --git a/api/stream.go b/api/stream.go
index caf4c7ca5..d17cd9049 100644
--- a/api/stream.go
+++ b/api/stream.go
@@ -7,8 +7,6 @@ import (
"github.com/deluan/gosonic/stream"
"github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
- "io"
- "net/http"
"strconv"
)
@@ -19,19 +17,6 @@ type StreamController struct {
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() {
inject.ExtractAssignable(utils.Graph, &c.repo)
@@ -51,21 +36,12 @@ func (c *StreamController) Prepare() {
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() {
var maxBitRate int
c.Ctx.Input.Bind(&maxBitRate, "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)
if maxBitRate > 0 {
@@ -76,7 +52,7 @@ func (c *StreamController) Stream() {
c.Ctx.Output.Header("Cache-Control", "must-revalidate")
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 {
beego.Error("Error streaming file id", c.id, ":", err)
}
diff --git a/stream/downsampling.go b/stream/downsampling.go
index 4492c3575..5eadd0e34 100644
--- a/stream/downsampling.go
+++ b/stream/downsampling.go
@@ -12,14 +12,19 @@ import (
func Stream(path string, bitRate int, maxBitRate int, w io.Writer) error {
if maxBitRate > 0 && bitRate > 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
- err := cmd.Run()
+ stdout, err := cmd.StdoutPipe()
if err != nil {
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
} else {