Use TagLib# to keep MP3 data separate from AAC.

master
Icedream 2014-05-07 09:16:16 +02:00
parent 43fc3d1f80
commit c57da01fac
4 changed files with 48 additions and 9 deletions

View File

@ -48,11 +48,17 @@
<Reference Include="Newtonsoft.Json"> <Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="policy.2.0.taglib-sharp">
<HintPath>..\..\packages\taglib.2.1.0.0\lib\policy.2.0.taglib-sharp.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Web" /> <Reference Include="System.Web" />
<Reference Include="System.XML" /> <Reference Include="System.XML" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="taglib-sharp">
<HintPath>..\..\packages\taglib.2.1.0.0\lib\taglib-sharp.dll</HintPath>
</Reference>
<Reference Include="uhttpsharp"> <Reference Include="uhttpsharp">
<HintPath>..\..\packages\uHttpSharp.0.1.4.7\lib\net40\uhttpsharp.dll</HintPath> <HintPath>..\..\packages\uHttpSharp.0.1.4.7\lib\net40\uhttpsharp.dll</HintPath>
</Reference> </Reference>

View File

@ -1,12 +1,16 @@
using log4net; using System.IO;
using log4net;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using TagLib;
using TagLib.Aac;
using uhttpsharp; using uhttpsharp;
using uhttpsharp.Listeners; using uhttpsharp.Listeners;
using uhttpsharp.RequestProviders; using uhttpsharp.RequestProviders;
using File = System.IO.File;
namespace AFR.ShoutcastBridge namespace AFR.ShoutcastBridge
{ {
@ -164,21 +168,49 @@ namespace AFR.ShoutcastBridge
icecast.Url = shoutcast.StreamUrl; icecast.Url = shoutcast.StreamUrl;
icecast.Public = shoutcast.StreamPublic; icecast.Public = shoutcast.StreamPublic;
if (icecast.ContentType == "undefined") if (icecast.ContentType == "undefined")
switch (BitConverter.ToString(data.Take(2).ToArray()).Replace("-", "").ToLower()) {
var fmtcode = BitConverter.ToString(data.Take(2).ToArray()).Replace("-", "").ToLower();
switch (fmtcode)
{ {
case "4f67": // OGG container case "4f67": // OGG container
_sourcelog.DebugFormat("[{0}] Detected OGG audio container", srs.ClientEndPoint); _sourcelog.DebugFormat("[{0}] Detected OGG audio container", srs.ClientEndPoint);
icecast.ContentType = "audio/ogg"; icecast.ContentType = "audio/ogg";
break; break;
case "fff9": // AAC
_sourcelog.DebugFormat("[{0}] Detected AAC-LC data", srs.ClientEndPoint);
icecast.ContentType = "audio/aac";
break;
default: default:
_sourcelog.DebugFormat("[{0}] Assuming MP3 codec", srs.ClientEndPoint);
icecast.ContentType = "audio/mpeg"; var testpath = Path.GetTempFileName() + ".mp3"; // Will make TagLib try to parse the file as MP3
try
{
using (var fs = File.Open(testpath, FileMode.OpenOrCreate))
{
fs.Write(data, 0, data.Length);
fs.Flush();
}
}
catch (Exception err)
{
_sourcelog.ErrorFormat("[{0}] Failed writing temporary data for analysis: {1}", srs.ClientEndPoint, err);
throw;
}
try
{
var f = TagLib.File.Create(testpath);
if (f.PossiblyCorrupt)
throw new Exception();
_sourcelog.DebugFormat("[{0}] Detected MP3 codec", srs.ClientEndPoint);
icecast.ContentType = f.MimeType;
File.Delete(testpath);
}
catch
{
_sourcelog.DebugFormat("[{0}] Assuming AAC codec", srs.ClientEndPoint);
icecast.ContentType = "audio/aac";
}
break; break;
} }
}
if (!icecast.Open()) if (!icecast.Open())
{ {
_sourcelog.ErrorFormat("[{0}] Could not connect with Icecast, retrying on next packet", srs.ClientEndPoint); _sourcelog.ErrorFormat("[{0}] Could not connect with Icecast, retrying on next packet", srs.ClientEndPoint);

View File

@ -109,7 +109,7 @@ namespace AFR.ShoutcastBridge
} }
// Content // Content
var data = new byte[2048]; var data = new byte[16 * 1024];
try try
{ {
int length; int length;

View File

@ -2,5 +2,6 @@
<packages> <packages>
<package id="log4net" version="2.0.3" targetFramework="net45" /> <package id="log4net" version="2.0.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" /> <package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
<package id="taglib" version="2.1.0.0" targetFramework="net45" />
<package id="uHttpSharp" version="0.1.4.7" targetFramework="net45" /> <package id="uHttpSharp" version="0.1.4.7" targetFramework="net45" />
</packages> </packages>