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 ( ...@@ -5,6 +5,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings"
"sync" "sync"
"text/template" "text/template"
...@@ -22,8 +23,8 @@ set("log.unix_timestamps", false) ...@@ -22,8 +23,8 @@ set("log.unix_timestamps", false)
upstream = mksafe(input.http("{{.SourceURL}}", buffer=5.0)) upstream = mksafe(input.http("{{.SourceURL}}", buffer=5.0))
{{if eq .Channels 1}}upstream = mean(upstream){{end}} {{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}}), output.icecast({{.Format}},
mount="{{.TargetMount}}", host="{{.TargetIP}}", port={{.TargetPort}}, user="{{.TargetUsername}}", password="{{.TargetPassword}}", mount="{{.TargetMount}}", host="{{.TargetHost}}", port={{.TargetPort}}, user="{{.TargetUsername}}", password="{{.TargetPassword}}",
upstream) upstream)
` `
...@@ -52,21 +53,14 @@ type liquidsoapParams struct { ...@@ -52,21 +53,14 @@ type liquidsoapParams struct {
SourceURL string SourceURL string
// Target (downstream) connection parameters. // Target (downstream) connection parameters.
TargetIP string TargetHost string
TargetPort int TargetPort int
TargetMount string TargetMount string
TargetUsername string TargetUsername string
TargetPassword string TargetPassword string
// Stream encoding parameters. One note on the 'float32' Format string
// choice for Quality: text/template can't evaluate the 'gt' Channels int
// function successfully on a float64 type!
Format string
BitRate int32
SampleRate int32
Channels int32
StereoMode string
Quality float32
} }
// Create new parameters for liquidsoap for a transcoding mount. If // Create new parameters for liquidsoap for a transcoding mount. If
...@@ -80,17 +74,13 @@ type liquidsoapParams struct { ...@@ -80,17 +74,13 @@ type liquidsoapParams struct {
func newLiquidsoapParams(m *pb.Mount) *liquidsoapParams { func newLiquidsoapParams(m *pb.Mount) *liquidsoapParams {
return &liquidsoapParams{ return &liquidsoapParams{
SourceURL: fmt.Sprintf("http://localhost%s", m.TranscodeParams.SourcePath), SourceURL: fmt.Sprintf("http://localhost%s", m.TranscodeParams.SourcePath),
TargetIP: "localhost", TargetHost: "localhost",
TargetPort: 80, TargetPort: 80,
TargetMount: m.Path, TargetMount: m.Path,
TargetUsername: m.SourceUsername, TargetUsername: m.SourceUsername,
TargetPassword: m.SourcePassword, TargetPassword: m.SourcePassword,
Format: m.TranscodeParams.Format, Format: liquidsoapFormatString(m.TranscodeParams),
BitRate: m.TranscodeParams.BitRate, Channels: int(m.TranscodeParams.Channels),
SampleRate: m.TranscodeParams.SampleRate,
Channels: m.TranscodeParams.Channels,
StereoMode: m.TranscodeParams.StereoMode,
Quality: m.TranscodeParams.Quality,
} }
} }
...@@ -101,3 +91,33 @@ func makeLiquidsoapConfig(m *pb.Mount) ([]byte, error) { ...@@ -101,3 +91,33 @@ func makeLiquidsoapConfig(m *pb.Mount) ([]byte, error) {
} }
return buf.Bytes(), nil 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