Skip to content
Snippets Groups Projects
Commit d8858609 authored by ale's avatar ale
Browse files

Fix the encoding format spec in the liquidsoap template

The right set of parameters depend on the format, move that logic to a
function in the code and out of the template.
parent b062f015
No related branches found
No related tags found
1 merge request!1v2.0
......@@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"strings"
"sync"
"text/template"
......@@ -22,8 +23,8 @@ set("log.unix_timestamps", false)
upstream = mksafe(input.http("{{.SourceURL}}", buffer=5.0))
{{if eq .Channels 1}}upstream = mean(upstream){{end}}
output.icecast(%{{.Format}}(samplerate={{.SampleRate}}, {{if gt .BitRate 0}}bitrate={{.BitRate}}, {{end}}{{if gt .Quality -1.0}}quality={{.Quality}}, {{end}}{{if .StereoMode}}stereo_mode="{{.StereoMode}}", {{end}}{{if eq .Channels 2}}stereo{{else}}mono{{end}}),
mount="{{.TargetMount}}", host="{{.TargetIP}}", port={{.TargetPort}}, user="{{.TargetUsername}}", password="{{.TargetPassword}}",
output.icecast({{.Format}},
mount="{{.TargetMount}}", host="{{.TargetHost}}", port={{.TargetPort}}, user="{{.TargetUsername}}", password="{{.TargetPassword}}",
upstream)
`
......@@ -52,21 +53,14 @@ type liquidsoapParams struct {
SourceURL string
// Target (downstream) connection parameters.
TargetIP string
TargetHost string
TargetPort int
TargetMount string
TargetUsername string
TargetPassword string
// Stream encoding parameters. One note on the 'float32'
// choice for Quality: text/template can't evaluate the 'gt'
// function successfully on a float64 type!
Format string
BitRate int32
SampleRate int32
Channels int32
StereoMode string
Quality float32
Format string
Channels int
}
// Create new parameters for liquidsoap for a transcoding mount. If
......@@ -80,17 +74,13 @@ type liquidsoapParams struct {
func newLiquidsoapParams(m *pb.Mount) *liquidsoapParams {
return &liquidsoapParams{
SourceURL: fmt.Sprintf("http://localhost%s", m.TranscodeParams.SourcePath),
TargetIP: "localhost",
TargetHost: "localhost",
TargetPort: 80,
TargetMount: m.Path,
TargetUsername: m.SourceUsername,
TargetPassword: m.SourcePassword,
Format: m.TranscodeParams.Format,
BitRate: m.TranscodeParams.BitRate,
SampleRate: m.TranscodeParams.SampleRate,
Channels: m.TranscodeParams.Channels,
StereoMode: m.TranscodeParams.StereoMode,
Quality: m.TranscodeParams.Quality,
Format: liquidsoapFormatString(m.TranscodeParams),
Channels: int(m.TranscodeParams.Channels),
}
}
......@@ -101,3 +91,33 @@ func makeLiquidsoapConfig(m *pb.Mount) ([]byte, error) {
}
return buf.Bytes(), nil
}
func liquidsoapFormatString(params *pb.EncodingParams) string {
var outp []string
switch params.Format {
case "mp3", "mp3.cbr", "mp3.vbr", "mp3.abr":
if params.Channels == 1 {
outp = append(outp, "mono")
} else {
outp = append(outp, "stereo")
outp = append(outp, fmt.Sprintf("stereo_mode=\"%s\"", params.StereoMode))
}
outp = append(outp, fmt.Sprintf("samplerate=%d", params.SampleRate))
switch params.Format {
case "mp3", "mp3.cbr", "mp3.abr":
outp = append(outp, fmt.Sprintf("bitrate=%d", params.BitRate))
case "mp3.vbr":
outp = append(outp, fmt.Sprintf("quality=%d", int(params.Quality)))
}
case "vorbis", "vorbis.cbr", "vorbis.abr":
outp = append(outp, fmt.Sprintf("channels=%d", params.Channels))
outp = append(outp, fmt.Sprintf("samplerate=%d", params.SampleRate))
switch params.Format {
case "vorbis":
outp = append(outp, fmt.Sprintf("quality=%g", params.Quality))
case "vorbis.abr", "vorbis.cbr":
outp = append(outp, fmt.Sprintf("bitrate=%d", params.BitRate))
}
}
return fmt.Sprintf("%%%s(%s)", params.Format, strings.Join(outp, ", "))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment