MediaInfoService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using MediaBrowser.Controller.Devices;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Net;
  5. using MediaBrowser.Model.Dlna;
  6. using MediaBrowser.Model.Dto;
  7. using MediaBrowser.Model.MediaInfo;
  8. using MediaBrowser.Model.Session;
  9. using ServiceStack;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Api.Playback
  16. {
  17. [Route("/Items/{Id}/MediaInfo", "GET", Summary = "Gets live playback media info for an item")]
  18. public class GetLiveMediaInfo : IReturn<PlaybackInfoResponse>
  19. {
  20. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  21. public string Id { get; set; }
  22. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  23. public string UserId { get; set; }
  24. }
  25. [Route("/Items/{Id}/PlaybackInfo", "GET", Summary = "Gets live playback media info for an item")]
  26. public class GetPlaybackInfo : IReturn<PlaybackInfoResponse>
  27. {
  28. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  29. public string Id { get; set; }
  30. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  31. public string UserId { get; set; }
  32. }
  33. [Route("/Items/{Id}/PlaybackInfo", "POST", Summary = "Gets live playback media info for an item")]
  34. public class GetPostedPlaybackInfo : PlaybackInfoRequest, IReturn<PlaybackInfoResponse>
  35. {
  36. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  37. public string Id { get; set; }
  38. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  39. public string UserId { get; set; }
  40. [ApiMember(Name = "StartTimeTicks", Description = "Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  41. public long? StartTimeTicks { get; set; }
  42. [ApiMember(Name = "AudioStreamIndex", Description = "Optional. The index of the audio stream to use. If omitted the first audio stream will be used.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  43. public int? AudioStreamIndex { get; set; }
  44. [ApiMember(Name = "SubtitleStreamIndex", Description = "Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  45. public int? SubtitleStreamIndex { get; set; }
  46. }
  47. [Authenticated]
  48. public class MediaInfoService : BaseApiService
  49. {
  50. private readonly IMediaSourceManager _mediaSourceManager;
  51. private readonly IDeviceManager _deviceManager;
  52. private readonly ILibraryManager _libraryManager;
  53. public MediaInfoService(IMediaSourceManager mediaSourceManager, IDeviceManager deviceManager, ILibraryManager libraryManager)
  54. {
  55. _mediaSourceManager = mediaSourceManager;
  56. _deviceManager = deviceManager;
  57. _libraryManager = libraryManager;
  58. }
  59. public async Task<object> Get(GetPlaybackInfo request)
  60. {
  61. var result = await GetPlaybackInfo(request.Id, request.UserId).ConfigureAwait(false);
  62. return ToOptimizedResult(result);
  63. }
  64. public async Task<object> Get(GetLiveMediaInfo request)
  65. {
  66. var result = await GetPlaybackInfo(request.Id, request.UserId).ConfigureAwait(false);
  67. return ToOptimizedResult(result);
  68. }
  69. public async Task<object> Post(GetPostedPlaybackInfo request)
  70. {
  71. var info = await GetPlaybackInfo(request.Id, request.UserId, request.MediaSource).ConfigureAwait(false);
  72. var authInfo = AuthorizationContext.GetAuthorizationInfo(Request);
  73. var profile = request.DeviceProfile;
  74. //if (profile == null)
  75. //{
  76. // var caps = _deviceManager.GetCapabilities(authInfo.DeviceId);
  77. // if (caps != null)
  78. // {
  79. // profile = caps.DeviceProfile;
  80. // }
  81. //}
  82. if (profile != null)
  83. {
  84. var mediaSourceId = request.MediaSource == null ? null : request.MediaSource.Id;
  85. SetDeviceSpecificData(request.Id, info, profile, authInfo, null, request.StartTimeTicks ?? 0, mediaSourceId, request.AudioStreamIndex, request.SubtitleStreamIndex);
  86. }
  87. return ToOptimizedResult(info);
  88. }
  89. private async Task<PlaybackInfoResponse> GetPlaybackInfo(string id, string userId, MediaSourceInfo mediaSource = null)
  90. {
  91. var result = new PlaybackInfoResponse();
  92. if (mediaSource == null)
  93. {
  94. IEnumerable<MediaSourceInfo> mediaSources;
  95. try
  96. {
  97. mediaSources = await _mediaSourceManager.GetPlayackMediaSources(id, userId, true, CancellationToken.None).ConfigureAwait(false);
  98. }
  99. catch (PlaybackException ex)
  100. {
  101. mediaSources = new List<MediaSourceInfo>();
  102. result.ErrorCode = ex.ErrorCode;
  103. }
  104. result.MediaSources = mediaSources.ToList();
  105. }
  106. else
  107. {
  108. result.MediaSources = new List<MediaSourceInfo> { mediaSource };
  109. }
  110. if (result.MediaSources.Count == 0)
  111. {
  112. if (!result.ErrorCode.HasValue)
  113. {
  114. result.ErrorCode = PlaybackErrorCode.NoCompatibleStream;
  115. }
  116. }
  117. else
  118. {
  119. result.StreamId = Guid.NewGuid().ToString("N");
  120. }
  121. return result;
  122. }
  123. private void SetDeviceSpecificData(string itemId,
  124. PlaybackInfoResponse result,
  125. DeviceProfile profile,
  126. AuthorizationInfo auth,
  127. int? maxBitrate,
  128. long startTimeTicks,
  129. string mediaSourceId,
  130. int? audioStreamIndex,
  131. int? subtitleStreamIndex)
  132. {
  133. var streamBuilder = new StreamBuilder();
  134. var item = _libraryManager.GetItemById(itemId);
  135. foreach (var mediaSource in result.MediaSources)
  136. {
  137. var options = new VideoOptions
  138. {
  139. MediaSources = new List<MediaSourceInfo> { mediaSource },
  140. Context = EncodingContext.Streaming,
  141. DeviceId = auth.DeviceId,
  142. ItemId = item.Id.ToString("N"),
  143. Profile = profile,
  144. MaxBitrate = maxBitrate
  145. };
  146. if (string.Equals(mediaSourceId, mediaSource.Id, StringComparison.OrdinalIgnoreCase))
  147. {
  148. options.MediaSourceId = mediaSourceId;
  149. options.AudioStreamIndex = audioStreamIndex;
  150. options.SubtitleStreamIndex = subtitleStreamIndex;
  151. }
  152. if (mediaSource.SupportsDirectPlay)
  153. {
  154. var supportsDirectStream = mediaSource.SupportsDirectStream;
  155. // Dummy this up to fool StreamBuilder
  156. mediaSource.SupportsDirectStream = true;
  157. // The MediaSource supports direct stream, now test to see if the client supports it
  158. var streamInfo = item is Video ?
  159. streamBuilder.BuildVideoItem(options) :
  160. streamBuilder.BuildAudioItem(options);
  161. if (streamInfo == null || !streamInfo.IsDirectStream)
  162. {
  163. mediaSource.SupportsDirectPlay = false;
  164. }
  165. // Set this back to what it was
  166. mediaSource.SupportsDirectStream = supportsDirectStream;
  167. }
  168. if (mediaSource.SupportsDirectStream)
  169. {
  170. // The MediaSource supports direct stream, now test to see if the client supports it
  171. var streamInfo = item is Video ?
  172. streamBuilder.BuildVideoItem(options) :
  173. streamBuilder.BuildAudioItem(options);
  174. if (streamInfo == null || !streamInfo.IsDirectStream)
  175. {
  176. mediaSource.SupportsDirectStream = false;
  177. }
  178. }
  179. if (mediaSource.SupportsTranscoding)
  180. {
  181. // The MediaSource supports direct stream, now test to see if the client supports it
  182. var streamInfo = item is Video ?
  183. streamBuilder.BuildVideoItem(options) :
  184. streamBuilder.BuildAudioItem(options);
  185. if (streamInfo != null && streamInfo.PlayMethod == PlayMethod.Transcode)
  186. {
  187. streamInfo.StartPositionTicks = startTimeTicks;
  188. mediaSource.TranscodingUrl = streamInfo.ToUrl("-", auth.Token).Substring(1);
  189. mediaSource.TranscodingContainer = streamInfo.Container;
  190. mediaSource.TranscodingSubProtocol = streamInfo.SubProtocol;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }