use user selected profile for transcoding

This commit is contained in:
sentriz
2020-03-12 00:27:49 +00:00
parent 5343b0e44b
commit 14481aab87
5 changed files with 79 additions and 65 deletions

View File

@@ -10,32 +10,11 @@ import (
"github.com/jinzhu/gorm"
"senan.xyz/g/gonic/db"
"senan.xyz/g/gonic/mime"
"senan.xyz/g/gonic/server/ctrlsubsonic/params"
"senan.xyz/g/gonic/server/ctrlsubsonic/spec"
"senan.xyz/g/gonic/server/encode"
)
// Put special clients that can't handle Opus here:
func encodeProfileFor(client string) string {
switch client {
case "Soundwaves":
return "mp3_rg"
case "Jamstash":
return "opus_rg"
default:
return "opus"
}
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// "raw" handlers are ones that don't always return a spec response.
// it could be a file, stream, etc. so you must either
// a) write to response writer
@@ -69,6 +48,49 @@ func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *s
return nil
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
type serveTrackOptions struct {
track *db.Track
pref *db.TranscodePreference
maxBitrate int
cachePath string
musicPath string
}
func serveTrackRaw(w http.ResponseWriter, r *http.Request, opts serveTrackOptions) {
log.Printf("serving raw %q\n", opts.track.Filename)
w.Header().Set("Content-Type", opts.track.MIME())
trackPath := path.Join(opts.musicPath, opts.track.RelPath())
http.ServeFile(w, r, trackPath)
}
func serveTrackEncode(w http.ResponseWriter, r *http.Request, opts serveTrackOptions) {
profile := encode.Profiles[opts.pref.Profile]
bitrate := encode.GetBitrate(opts.maxBitrate, profile)
trackPath := path.Join(opts.musicPath, opts.track.RelPath())
cacheKey := encode.CacheKey(trackPath, opts.pref.Profile, bitrate)
cacheFile := path.Join(opts.cachePath, cacheKey)
if fileExists(cacheFile) {
log.Printf("serving transcode `%s`: cache [%s/%s] hit!\n", opts.track.Filename, profile.Format, bitrate)
http.ServeFile(w, r, cacheFile)
return
}
log.Printf("serving transcode `%s`: cache [%s/%s] miss!\n", opts.track.Filename, profile.Format, bitrate)
if err := encode.Encode(w, trackPath, cacheFile, profile, bitrate); err != nil {
log.Printf("error encoding %q: %v\n", trackPath, err)
return
}
log.Printf("serving transcode `%s`: encoded to [%s/%s] successfully\n",
opts.track.Filename, profile.Format, bitrate)
}
func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetInt("id")
@@ -83,8 +105,8 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
if gorm.IsRecordNotFoundError(err) {
return spec.NewError(70, "media with id `%d` was not found", id)
}
user := r.Context().Value(CtxUser).(*db.User)
defer func() {
user := r.Context().Value(CtxUser).(*db.User)
play := db.Play{
AlbumID: track.Album.ID,
UserID: user.ID,
@@ -96,34 +118,24 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
play.Count++ // for getAlbumList?type=frequent
c.DB.Save(&play)
}()
client := params.GetOr("c", "generic")
maxBitrate, err := params.GetInt("maxBitRate")
if err != nil {
maxBitrate = 0
client := params.GetOr("c", "*")
servOpts := serveTrackOptions{
track: track,
musicPath: c.MusicPath,
}
absPath := path.Join(
c.MusicPath,
track.Album.LeftPath,
track.Album.RightPath,
track.Filename,
)
profileName := encodeProfileFor(client)
profile := encode.Profiles[profileName]
bitrate := encode.GetBitrate(maxBitrate, profile)
cacheKey := encode.CacheKey(absPath, profileName, bitrate)
cacheFile := path.Join(c.CachePath, cacheKey)
if fileExists(cacheFile) {
log.Printf("track `%s`: cache [%s/%s] hit!\n", track.Filename, profile.Format, bitrate)
http.ServeFile(w, r, cacheFile)
pref := &db.TranscodePreference{}
err = c.DB.
Where("user_id=? AND client=? COLLATE NOCASE", user.ID, client).
First(pref).
Error
if gorm.IsRecordNotFoundError(err) {
serveTrackRaw(w, r, servOpts)
return nil
}
log.Printf("track `%s`: cache [%s/%s] miss!\n", track.Filename, profile.Format, bitrate)
if err := encode.Encode(w, absPath, cacheFile, profile, bitrate); err != nil {
log.Printf("error encoding %q: %v\n", absPath, err)
}
log.Printf("track `%s`: encoded to [%s/%s] successfully\n",
track.Filename, profile.Format, bitrate)
servOpts.pref = pref
servOpts.maxBitrate = params.GetIntOr("maxBitRate", 0)
servOpts.cachePath = c.CachePath
serveTrackEncode(w, r, servOpts)
return nil
}
@@ -141,20 +153,9 @@ func (c *Controller) ServeDownload(w http.ResponseWriter, r *http.Request) *spec
if gorm.IsRecordNotFoundError(err) {
return spec.NewError(70, "media with id `%d` was not found", id)
}
absPath := path.Join(
c.MusicPath,
track.Album.LeftPath,
track.Album.RightPath,
track.Filename,
)
if mime, ok := mime.Types[track.Ext()]; ok {
w.Header().Set("Content-Type", mime)
}
http.ServeFile(w, r, absPath)
//
// We don't need to mark album/track as played
// if user just downloads a track, so bail out here:
serveTrackRaw(w, r, serveTrackOptions{
track: track,
musicPath: c.MusicPath,
})
return nil
}