AudioService.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Library;
  3. using ServiceStack.ServiceHost;
  4. using System.Collections.Generic;
  5. namespace MediaBrowser.Api.Playback.Progressive
  6. {
  7. /// <summary>
  8. /// Class GetAudioStream
  9. /// </summary>
  10. [Route("/Audio/{Id}/stream.mp3", "GET")]
  11. [Route("/Audio/{Id}/stream.wma", "GET")]
  12. [Route("/Audio/{Id}/stream.aac", "GET")]
  13. [Route("/Audio/{Id}/stream.flac", "GET")]
  14. [Route("/Audio/{Id}/stream.ogg", "GET")]
  15. [Route("/Audio/{Id}/stream", "GET")]
  16. public class GetAudioStream : StreamRequest
  17. {
  18. }
  19. /// <summary>
  20. /// Class AudioService
  21. /// </summary>
  22. public class AudioService : BaseProgressiveStreamingService
  23. {
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="BaseProgressiveStreamingService" /> class.
  26. /// </summary>
  27. /// <param name="appPaths">The app paths.</param>
  28. public AudioService(IServerApplicationPaths appPaths, IUserManager userManager)
  29. : base(appPaths, userManager)
  30. {
  31. }
  32. /// <summary>
  33. /// Gets the specified request.
  34. /// </summary>
  35. /// <param name="request">The request.</param>
  36. /// <returns>System.Object.</returns>
  37. public object Get(GetAudioStream request)
  38. {
  39. return ProcessRequest(request);
  40. }
  41. /// <summary>
  42. /// Gets the command line arguments.
  43. /// </summary>
  44. /// <param name="outputPath">The output path.</param>
  45. /// <param name="state">The state.</param>
  46. /// <returns>System.String.</returns>
  47. /// <exception cref="System.InvalidOperationException">Only aac and mp3 audio codecs are supported.</exception>
  48. protected override string GetCommandLineArguments(string outputPath, StreamState state)
  49. {
  50. var request = state.Request;
  51. var audioTranscodeParams = new List<string>();
  52. if (request.AudioBitRate.HasValue)
  53. {
  54. audioTranscodeParams.Add("-ab " + request.AudioBitRate.Value);
  55. }
  56. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  57. if (channels.HasValue)
  58. {
  59. audioTranscodeParams.Add("-ac " + channels.Value);
  60. }
  61. if (request.AudioSampleRate.HasValue)
  62. {
  63. audioTranscodeParams.Add("-ar " + request.AudioSampleRate.Value);
  64. }
  65. const string vn = " -vn";
  66. return string.Format("{0} -i {1}{2} -threads 0{5} {3} -id3v2_version 3 -write_id3v1 1 \"{4}\"",
  67. GetFastSeekCommandLineParameter(request),
  68. GetInputArgument(state.Item, state.IsoMount),
  69. GetSlowSeekCommandLineParameter(request),
  70. string.Join(" ", audioTranscodeParams.ToArray()),
  71. outputPath,
  72. vn).Trim();
  73. }
  74. }
  75. }