Added GetAudioStreamUrl to ApiClient

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-09-09 09:56:04 -04:00
parent 5525d108d3
commit 9f1a7845dd
6 changed files with 73 additions and 53 deletions

View File

@ -1,9 +1,10 @@
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Net;
namespace MediaBrowser.Api.HttpHandlers
@ -12,7 +13,7 @@ namespace MediaBrowser.Api.HttpHandlers
/// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
/// </summary>
[Export(typeof(BaseHandler))]
public class AudioHandler : BaseMediaHandler<Audio>
public class AudioHandler : BaseMediaHandler<Audio, AudioOutputFormats>
{
public override bool HandlesRequest(HttpListenerRequest request)
{
@ -20,54 +21,29 @@ namespace MediaBrowser.Api.HttpHandlers
}
/// <summary>
/// Overriding to provide mp3 as a default, since pretty much every device supports it
/// We can output these formats directly, but we cannot encode to them.
/// </summary>
protected override IEnumerable<string> OutputFormats
protected override IEnumerable<AudioOutputFormats> UnsupportedOutputEncodingFormats
{
get
{
IEnumerable<string> vals = base.OutputFormats;
return vals.Any() ? vals : new string[] { "mp3" };
return new AudioOutputFormats[] { AudioOutputFormats.Aac, AudioOutputFormats.Flac, AudioOutputFormats.Wav, AudioOutputFormats.Wma };
}
}
/// <summary>
/// We can output these files directly, but we can't encode them
/// </summary>
protected override IEnumerable<string> UnsupportedOutputEncodingFormats
private int? GetMaxAcceptedBitRate(AudioOutputFormats audioFormat)
{
get
{
return new string[] { "wma", "aac" };
}
}
public IEnumerable<int> AudioBitRates
{
get
{
string val = QueryString["audiobitrates"];
if (string.IsNullOrEmpty(val))
{
return new int[] { };
}
return val.Split(',').Select(v => int.Parse(v));
}
return GetMaxAcceptedBitRate(audioFormat.ToString());
}
private int? GetMaxAcceptedBitRate(string audioFormat)
{
if (!AudioBitRates.Any())
if (audioFormat.Equals("mp3", System.StringComparison.OrdinalIgnoreCase))
{
return null;
return 320000;
}
int index = OutputFormats.ToList().IndexOf(audioFormat);
return AudioBitRates.ElementAt(index);
return null;
}
/// <summary>
@ -81,7 +57,7 @@ namespace MediaBrowser.Api.HttpHandlers
}
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
int? bitrate = GetMaxAcceptedBitRate(currentFormat);
// If the bitrate is greater than our desired bitrate, we need to transcode
@ -113,7 +89,7 @@ namespace MediaBrowser.Api.HttpHandlers
{
List<string> audioTranscodeParams = new List<string>();
string outputFormat = GetConversionOutputFormat();
AudioOutputFormats outputFormat = GetConversionOutputFormat();
int? bitrate = GetMaxAcceptedBitRate(outputFormat);

View File

@ -13,36 +13,36 @@ using System.Threading.Tasks;
namespace MediaBrowser.Api.HttpHandlers
{
public abstract class BaseMediaHandler<T> : BaseHandler
where T : BaseItem, new()
public abstract class BaseMediaHandler<TBaseItemType, TOutputType> : BaseHandler
where TBaseItemType : BaseItem, new()
{
/// <summary>
/// Supported values: mp3,flac,ogg,wav,asf,wma,aac
/// </summary>
protected virtual IEnumerable<string> OutputFormats
protected virtual IEnumerable<TOutputType> OutputFormats
{
get
{
return QueryString["outputformats"].Split(',');
return QueryString["outputformats"].Split(',').Select(o => (TOutputType)Enum.Parse(typeof(TOutputType), o, true));
}
}
/// <summary>
/// These formats can be outputted directly but cannot be encoded to
/// </summary>
protected virtual IEnumerable<string> UnsupportedOutputEncodingFormats
protected virtual IEnumerable<TOutputType> UnsupportedOutputEncodingFormats
{
get
{
return new string[] { };
return new TOutputType[] { };
}
}
private T _LibraryItem;
private TBaseItemType _LibraryItem;
/// <summary>
/// Gets the library item that will be played, if any
/// </summary>
protected T LibraryItem
protected TBaseItemType LibraryItem
{
get
{
@ -52,7 +52,7 @@ namespace MediaBrowser.Api.HttpHandlers
if (!string.IsNullOrEmpty(id))
{
_LibraryItem = Kernel.Instance.GetItemById(Guid.Parse(id)) as T;
_LibraryItem = Kernel.Instance.GetItemById(Guid.Parse(id)) as TBaseItemType;
}
}
@ -83,7 +83,7 @@ namespace MediaBrowser.Api.HttpHandlers
if (string.IsNullOrEmpty(val))
{
return 44100;
return null;
}
return int.Parse(val);
@ -119,19 +119,19 @@ namespace MediaBrowser.Api.HttpHandlers
/// <summary>
/// Gets the format we'll be converting to
/// </summary>
protected virtual string GetConversionOutputFormat()
protected virtual TOutputType GetConversionOutputFormat()
{
return OutputFormats.First(f => !UnsupportedOutputEncodingFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase)));
return OutputFormats.First(f => !UnsupportedOutputEncodingFormats.Any(s => s.ToString().Equals(f.ToString(), StringComparison.OrdinalIgnoreCase)));
}
protected virtual bool RequiresConversion()
{
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
if (OutputFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
if (OutputFormats.Any(f => currentFormat.EndsWith(f.ToString(), StringComparison.OrdinalIgnoreCase)))
{
// We can output these files directly, but we can't encode them
if (UnsupportedOutputEncodingFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
if (UnsupportedOutputEncodingFormats.Any(f => currentFormat.EndsWith(f.ToString(), StringComparison.OrdinalIgnoreCase)))
{
return false;
}

View File

@ -15,13 +15,13 @@ namespace MediaBrowser.Api.HttpHandlers
/// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
/// </summary>
[Export(typeof(BaseHandler))]
class VideoHandler : BaseMediaHandler<Video>
class VideoHandler : BaseMediaHandler<Video, string>
{
public override bool HandlesRequest(HttpListenerRequest request)
{
return ApiService.IsApiUrlMatch("video", request);
}
/// <summary>
/// We can output these files directly, but we can't encode them
/// </summary>

View File

@ -1,7 +1,9 @@
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.ApiInteraction
{
@ -349,6 +351,32 @@ namespace MediaBrowser.ApiInteraction
return null;
}
/// <summary>
/// Gets the url needed to stream an audio file
/// </summary>
/// <param name="itemId">The id of the item</param>
/// <param name="supportedOutputFormats">List all the output formats the decice is capable of playing. The more, the better, as it will decrease the likelyhood of having to encode, which will put a load on the server.</param>
/// <param name="maxChannels">The maximum number of channels that the device can play. Omit this if it doesn't matter. Phones and tablets should generally specify 2.</param>
/// <param name="maxSampleRate">The maximum sample rate that the device can play. This should generally be omitted. If there's a problem, try 44100.</param>
public string GetAudioStreamUrl(Guid itemId, IEnumerable<AudioOutputFormats> supportedOutputFormats, int? maxChannels = null, int? maxSampleRate = null)
{
string url = ApiUrl + "/audio";
url += "?outputformats=" + string.Join(",", supportedOutputFormats.Select(s => s.ToString()).ToArray());
if (maxChannels.HasValue)
{
url += "&audiochannels=" + maxChannels.Value;
}
if (maxSampleRate.HasValue)
{
url += "&audiosamplerate=" + maxSampleRate.Value;
}
return url;
}
protected T DeserializeFromStream<T>(Stream stream)
where T : class
{

View File

@ -0,0 +1,15 @@

namespace MediaBrowser.Model.DTO
{
/// <summary>
/// These are the audio output formats that the api is cabaple of streaming
/// </summary>
public enum AudioOutputFormats
{
Aac,
Flac,
Mp3,
Wav,
Wma
}
}

View File

@ -35,6 +35,7 @@
<Compile Include="Configuration\BaseApplicationConfiguration.cs" />
<Compile Include="Configuration\ServerConfiguration.cs" />
<Compile Include="DTO\AudioInfo.cs" />
<Compile Include="DTO\AudioOutputFormats.cs" />
<Compile Include="DTO\SeriesInfo.cs" />
<Compile Include="Authentication\AuthenticationResult.cs" />
<Compile Include="DTO\DTOBaseItem.cs" />