AudioService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Channels;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Dlna;
  6. using MediaBrowser.Controller.Drawing;
  7. using MediaBrowser.Controller.Dto;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.LiveTv;
  10. using MediaBrowser.Controller.MediaEncoding;
  11. using MediaBrowser.Controller.Persistence;
  12. using MediaBrowser.Model.IO;
  13. using ServiceStack;
  14. using System.Collections.Generic;
  15. namespace MediaBrowser.Api.Playback.Progressive
  16. {
  17. /// <summary>
  18. /// Class GetAudioStream
  19. /// </summary>
  20. [Route("/Audio/{Id}/stream.mp3", "GET", Summary = "Gets an audio stream")]
  21. [Route("/Audio/{Id}/stream.wma", "GET", Summary = "Gets an audio stream")]
  22. [Route("/Audio/{Id}/stream.aac", "GET", Summary = "Gets an audio stream")]
  23. [Route("/Audio/{Id}/stream.flac", "GET", Summary = "Gets an audio stream")]
  24. [Route("/Audio/{Id}/stream.ogg", "GET", Summary = "Gets an audio stream")]
  25. [Route("/Audio/{Id}/stream.oga", "GET", Summary = "Gets an audio stream")]
  26. [Route("/Audio/{Id}/stream.webm", "GET", Summary = "Gets an audio stream")]
  27. [Route("/Audio/{Id}/stream", "GET", Summary = "Gets an audio stream")]
  28. [Route("/Audio/{Id}/stream.mp3", "HEAD", Summary = "Gets an audio stream")]
  29. [Route("/Audio/{Id}/stream.wma", "HEAD", Summary = "Gets an audio stream")]
  30. [Route("/Audio/{Id}/stream.aac", "HEAD", Summary = "Gets an audio stream")]
  31. [Route("/Audio/{Id}/stream.flac", "HEAD", Summary = "Gets an audio stream")]
  32. [Route("/Audio/{Id}/stream.ogg", "HEAD", Summary = "Gets an audio stream")]
  33. [Route("/Audio/{Id}/stream.oga", "HEAD", Summary = "Gets an audio stream")]
  34. [Route("/Audio/{Id}/stream.webm", "HEAD", Summary = "Gets an audio stream")]
  35. [Route("/Audio/{Id}/stream", "HEAD", Summary = "Gets an audio stream")]
  36. public class GetAudioStream : StreamRequest
  37. {
  38. }
  39. /// <summary>
  40. /// Class AudioService
  41. /// </summary>
  42. public class AudioService : BaseProgressiveStreamingService
  43. {
  44. public AudioService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IEncodingManager encodingManager, IDlnaManager dlnaManager, IChannelManager channelManager, IHttpClient httpClient, IImageProcessor imageProcessor) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager, encodingManager, dlnaManager, channelManager, httpClient, imageProcessor)
  45. {
  46. }
  47. /// <summary>
  48. /// Gets the specified request.
  49. /// </summary>
  50. /// <param name="request">The request.</param>
  51. /// <returns>System.Object.</returns>
  52. public object Get(GetAudioStream request)
  53. {
  54. return ProcessRequest(request, false);
  55. }
  56. /// <summary>
  57. /// Gets the specified request.
  58. /// </summary>
  59. /// <param name="request">The request.</param>
  60. /// <returns>System.Object.</returns>
  61. public object Head(GetAudioStream request)
  62. {
  63. return ProcessRequest(request, true);
  64. }
  65. /// <summary>
  66. /// Gets the command line arguments.
  67. /// </summary>
  68. /// <param name="outputPath">The output path.</param>
  69. /// <param name="state">The state.</param>
  70. /// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
  71. /// <returns>System.String.</returns>
  72. /// <exception cref="System.InvalidOperationException">Only aac and mp3 audio codecs are supported.</exception>
  73. protected override string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions)
  74. {
  75. var audioTranscodeParams = new List<string>();
  76. var bitrate = state.OutputAudioBitrate;
  77. if (bitrate.HasValue)
  78. {
  79. audioTranscodeParams.Add("-ab " + bitrate.Value.ToString(UsCulture));
  80. }
  81. if (state.OutputAudioChannels.HasValue)
  82. {
  83. audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(UsCulture));
  84. }
  85. if (state.OutputAudioSampleRate.HasValue)
  86. {
  87. audioTranscodeParams.Add("-ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture));
  88. }
  89. const string vn = " -vn";
  90. var threads = GetNumberOfThreads(state, false);
  91. var inputModifier = GetInputModifier(state);
  92. return string.Format("{0} -i {1} -threads {2}{3} {4} -id3v2_version 3 -write_id3v1 1 -y \"{5}\"",
  93. inputModifier,
  94. GetInputArgument(state),
  95. threads,
  96. vn,
  97. string.Join(" ", audioTranscodeParams.ToArray()),
  98. outputPath).Trim();
  99. }
  100. }
  101. }