using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Net;
namespace MediaBrowser.Api.Streaming
{
///
/// Providers a progressive streaming audio api
///
[Export(typeof(IHttpServerHandler))]
public class AudioHandler : BaseProgressiveStreamingHandler
{
///
/// Handleses the request.
///
/// The request.
/// true if XXXX, false otherwise
public override bool HandlesRequest(HttpListenerRequest request)
{
return EntityResolutionHelper.AudioFileExtensions.Any(a => ApiService.IsApiUrlMatch("audio" + a, request));
}
///
/// Gets the audio codec.
///
/// The audio codec.
///
protected override AudioCodecs? AudioCodec
{
get
{
var ext = OutputFileExtension;
if (ext.Equals(".aac", StringComparison.OrdinalIgnoreCase) || ext.Equals(".m4a", StringComparison.OrdinalIgnoreCase))
{
return AudioCodecs.Aac;
}
if (ext.Equals(".mp3", StringComparison.OrdinalIgnoreCase))
{
return AudioCodecs.Mp3;
}
if (ext.Equals(".wma", StringComparison.OrdinalIgnoreCase))
{
return AudioCodecs.Wma;
}
if (ext.Equals(".oga", StringComparison.OrdinalIgnoreCase) || ext.Equals(".ogg", StringComparison.OrdinalIgnoreCase))
{
return AudioCodecs.Vorbis;
}
throw new InvalidOperationException();
}
}
///
/// Creates arguments to pass to ffmpeg
///
/// The output path.
/// The iso mount.
/// System.String.
/// Only aac and mp3 audio codecs are supported.
/// Only aac and mp3 audio codecs are supported.
/// Only aac and mp3 audio codecs are supported.
protected override string GetCommandLineArguments(string outputPath, IIsoMount isoMount)
{
var audioTranscodeParams = new List();
var outputFormat = AudioCodec;
if (outputFormat != AudioCodecs.Aac && outputFormat != AudioCodecs.Mp3)
{
throw new InvalidOperationException("Only aac and mp3 audio codecs are supported.");
}
if (AudioBitRate.HasValue)
{
audioTranscodeParams.Add("-ab " + AudioBitRate.Value);
}
var channels = GetNumAudioChannelsParam();
if (channels.HasValue)
{
audioTranscodeParams.Add("-ac " + channels.Value);
}
var sampleRate = GetSampleRateParam();
if (sampleRate.HasValue)
{
audioTranscodeParams.Add("-ar " + sampleRate.Value);
}
const string vn = " -vn";
return string.Format("{0} -i {1}{2} -threads 0{5} {3} -id3v2_version 3 -write_id3v1 1 \"{4}\"",
FastSeekCommandLineParameter,
GetInputArgument(isoMount),
SlowSeekCommandLineParameter,
string.Join(" ", audioTranscodeParams.ToArray()),
outputPath,
vn).Trim();
}
}
}