delete guess expected size feature
it it doing some really bad guesses for opus files
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
//nolint:deadcode
|
||||
package ctrlsubsonic
|
||||
|
||||
import (
|
||||
@@ -16,10 +17,10 @@ import (
|
||||
|
||||
jd "github.com/josephburnett/jd/lib"
|
||||
|
||||
"go.senan.xyz/gonic/server/ctrlbase"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/mockfs"
|
||||
"go.senan.xyz/gonic/server/ctrlbase"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/transcode"
|
||||
)
|
||||
|
||||
|
||||
@@ -12,12 +12,10 @@ import (
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"go.senan.xyz/gonic/iout"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/httprange"
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/transcode"
|
||||
)
|
||||
|
||||
@@ -296,30 +294,11 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
|
||||
|
||||
log.Printf("trancoding to %q with max bitrate %dk", profile.MIME(), profile.BitRate())
|
||||
|
||||
transcodeReader, err := c.Transcoder.Transcode(r.Context(), profile, audioPath)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", profile.MIME())
|
||||
if err := c.Transcoder.Transcode(r.Context(), profile, audioPath, w); err != nil {
|
||||
return spec.NewError(0, "error transcoding: %v", err)
|
||||
}
|
||||
defer transcodeReader.Close()
|
||||
|
||||
length := transcode.GuessExpectedSize(profile, time.Duration(file.AudioLength())*time.Second) // TODO: if there's no duration?
|
||||
rreq, err := httprange.Parse(r.Header.Get("Range"), length)
|
||||
if err != nil {
|
||||
return spec.NewError(0, "error parsing range: %v", err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", profile.MIME())
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", rreq.Length))
|
||||
w.Header().Set("Accept-Ranges", string(httprange.UnitBytes))
|
||||
|
||||
if rreq.Partial {
|
||||
w.WriteHeader(http.StatusPartialContent)
|
||||
w.Header().Set("Content-Range", fmt.Sprintf("%s %d-%d/%d", httprange.UnitBytes, rreq.Start, rreq.End, length))
|
||||
}
|
||||
|
||||
if err := iout.CopyRange(w, transcodeReader, int64(rreq.Start), int64(rreq.Length)); err != nil {
|
||||
log.Printf("error writing transcoded data: %v", err)
|
||||
}
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
package ctrlsubsonic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/matryer/is"
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/transcode"
|
||||
)
|
||||
|
||||
func TestServeStreamRaw(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||
t.Skipf("no ffmpeg in $PATH")
|
||||
}
|
||||
|
||||
is := is.New(t)
|
||||
contr := makeControllerAudio(t)
|
||||
|
||||
statFlac := stat(t, audioPath10s)
|
||||
|
||||
rr, req := makeHTTPMock(url.Values{"id": {"tr-1"}})
|
||||
serveRaw(t, contr, contr.ServeStream, rr, req)
|
||||
|
||||
is.Equal(rr.Code, http.StatusOK)
|
||||
is.Equal(rr.Header().Get("content-type"), "audio/flac")
|
||||
is.Equal(atoi(t, rr.Header().Get("content-length")), int(statFlac.Size()))
|
||||
is.Equal(atoi(t, rr.Header().Get("content-length")), rr.Body.Len())
|
||||
}
|
||||
|
||||
func TestServeStreamOpus(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||
t.Skipf("no ffmpeg in $PATH")
|
||||
}
|
||||
|
||||
is := is.New(t)
|
||||
contr := makeControllerAudio(t)
|
||||
|
||||
var user db.User
|
||||
is.NoErr(contr.DB.Where("name=?", mockUsername).Find(&user).Error)
|
||||
is.NoErr(contr.DB.Create(&db.TranscodePreference{UserID: user.ID, Client: mockClientName, Profile: "opus"}).Error)
|
||||
|
||||
rr, req := makeHTTPMock(url.Values{"id": {"tr-1"}})
|
||||
serveRaw(t, contr, contr.ServeStream, rr, req)
|
||||
|
||||
is.Equal(rr.Code, http.StatusOK)
|
||||
is.Equal(rr.Header().Get("content-type"), "audio/ogg")
|
||||
is.Equal(atoi(t, rr.Header().Get("content-length")), transcode.GuessExpectedSize(transcode.Opus, 10*time.Second))
|
||||
is.Equal(atoi(t, rr.Header().Get("content-length")), rr.Body.Len())
|
||||
}
|
||||
|
||||
func TestServeStreamOpusMaxBitrate(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||
t.Skipf("no ffmpeg in $PATH")
|
||||
}
|
||||
|
||||
is := is.New(t)
|
||||
contr := makeControllerAudio(t)
|
||||
|
||||
var user db.User
|
||||
is.NoErr(contr.DB.Where("name=?", mockUsername).Find(&user).Error)
|
||||
is.NoErr(contr.DB.Create(&db.TranscodePreference{UserID: user.ID, Client: mockClientName, Profile: "opus"}).Error)
|
||||
|
||||
const bitrate = 5
|
||||
|
||||
rr, req := makeHTTPMock(url.Values{"id": {"tr-1"}, "maxBitRate": {strconv.Itoa(bitrate)}})
|
||||
serveRaw(t, contr, contr.ServeStream, rr, req)
|
||||
|
||||
profile := transcode.WithBitrate(transcode.Opus, transcode.BitRate(bitrate))
|
||||
expectedLength := transcode.GuessExpectedSize(profile, 10*time.Second)
|
||||
|
||||
is.Equal(rr.Code, http.StatusOK)
|
||||
is.Equal(rr.Header().Get("content-type"), "audio/ogg")
|
||||
is.Equal(atoi(t, rr.Header().Get("content-length")), expectedLength)
|
||||
is.Equal(atoi(t, rr.Header().Get("content-length")), rr.Body.Len())
|
||||
}
|
||||
|
||||
func TestServeStreamMP3Range(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||
t.Skipf("no ffmpeg in $PATH")
|
||||
}
|
||||
|
||||
is := is.New(t)
|
||||
contr := makeControllerAudio(t)
|
||||
|
||||
var user db.User
|
||||
is.NoErr(contr.DB.Where("name=?", mockUsername).Find(&user).Error)
|
||||
is.NoErr(contr.DB.Create(&db.TranscodePreference{UserID: user.ID, Client: mockClientName, Profile: "mp3"}).Error)
|
||||
|
||||
var totalBytes []byte
|
||||
{
|
||||
rr, req := makeHTTPMock(url.Values{"id": {"tr-1"}})
|
||||
serveRaw(t, contr, contr.ServeStream, rr, req)
|
||||
is.Equal(rr.Code, http.StatusOK)
|
||||
is.Equal(rr.Header().Get("content-type"), "audio/mpeg")
|
||||
totalBytes = rr.Body.Bytes()
|
||||
}
|
||||
|
||||
const chunkSize = 2 << 16
|
||||
|
||||
var bytes []byte
|
||||
for i := 0; i < len(totalBytes); i += chunkSize {
|
||||
rr, req := makeHTTPMock(url.Values{"id": {"tr-1"}})
|
||||
req.Header.Set("range", fmt.Sprintf("bytes=%d-%d", i, min(i+chunkSize, len(totalBytes))-1))
|
||||
t.Log(req.Header.Get("range"))
|
||||
serveRaw(t, contr, contr.ServeStream, rr, req)
|
||||
is.Equal(rr.Code, http.StatusPartialContent)
|
||||
is.Equal(rr.Header().Get("content-type"), "audio/mpeg")
|
||||
is.True(atoi(t, rr.Header().Get("content-length")) == chunkSize || atoi(t, rr.Header().Get("content-length")) == len(totalBytes)%chunkSize)
|
||||
is.Equal(atoi(t, rr.Header().Get("content-length")), rr.Body.Len())
|
||||
bytes = append(bytes, rr.Body.Bytes()...)
|
||||
}
|
||||
|
||||
is.Equal(len(totalBytes), len(bytes))
|
||||
is.Equal(totalBytes, bytes)
|
||||
}
|
||||
|
||||
func stat(t *testing.T, path string) fs.FileInfo {
|
||||
t.Helper()
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("stat %q: %v", path, err)
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
func atoi(t *testing.T, in string) int {
|
||||
t.Helper()
|
||||
i, err := strconv.Atoi(in)
|
||||
if err != nil {
|
||||
t.Fatalf("atoi %q: %v", in, err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package httprange
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Unit string
|
||||
|
||||
const (
|
||||
UnitBytes Unit = "bytes"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
reg = regexp.MustCompile(`^(?P<unit>\w+)=(?P<start>(?:\d+)?)\s*-\s*(?P<end>(?:\d+)?)$`)
|
||||
unit = reg.SubexpIndex("unit")
|
||||
start = reg.SubexpIndex("start")
|
||||
end = reg.SubexpIndex("end")
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidRange = fmt.Errorf("invalid range")
|
||||
ErrUnknownUnit = fmt.Errorf("unknown range")
|
||||
)
|
||||
|
||||
type Range struct {
|
||||
Start, End, Length int // bytes
|
||||
Partial bool
|
||||
}
|
||||
|
||||
func Parse(in string, fullLength int) (Range, error) {
|
||||
parts := reg.FindStringSubmatch(in)
|
||||
if len(parts)-1 != reg.NumSubexp() {
|
||||
return Range{0, fullLength - 1, fullLength, false}, nil
|
||||
}
|
||||
|
||||
switch unit := parts[unit]; Unit(unit) {
|
||||
case UnitBytes:
|
||||
default:
|
||||
return Range{}, fmt.Errorf("%q: %w", unit, ErrUnknownUnit)
|
||||
}
|
||||
|
||||
start, _ := strconv.Atoi(parts[start])
|
||||
end, _ := strconv.Atoi(parts[end])
|
||||
length := fullLength
|
||||
partial := false
|
||||
|
||||
switch {
|
||||
case end > 0 && end < length:
|
||||
length = end - start + 1
|
||||
partial = true
|
||||
case end == 0 && length > 0:
|
||||
end = length - 1
|
||||
}
|
||||
|
||||
return Range{start, end, length, partial}, nil
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package httprange_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/matryer/is"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/httprange"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
full := func(start, end, length int) httprange.Range {
|
||||
return httprange.Range{Start: start, End: end, Length: length}
|
||||
}
|
||||
partial := func(start, end, length int) httprange.Range {
|
||||
return httprange.Range{Start: start, End: end, Length: length, Partial: true}
|
||||
}
|
||||
parse := func(in string, length int) httprange.Range {
|
||||
is.Helper()
|
||||
rrange, err := httprange.Parse(in, length)
|
||||
is.NoErr(err)
|
||||
return rrange
|
||||
}
|
||||
|
||||
is.Equal(parse("bytes=0-0", 0), full(0, 0, 0))
|
||||
is.Equal(parse("bytes=0-", 10), full(0, 9, 10))
|
||||
is.Equal(parse("bytes=0-49", 50), partial(0, 49, 50))
|
||||
is.Equal(parse("bytes=50-99", 100), partial(50, 99, 50))
|
||||
}
|
||||
Reference in New Issue
Block a user