Audio.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Dto;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.MediaInfo;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Threading;
  11. using MediaBrowser.Common.Extensions;
  12. using MediaBrowser.Controller.Channels;
  13. using MediaBrowser.Model.Serialization;
  14. namespace MediaBrowser.Controller.Entities.Audio
  15. {
  16. /// <summary>
  17. /// Class Audio
  18. /// </summary>
  19. public class Audio : BaseItem,
  20. IHasAlbumArtist,
  21. IHasArtist,
  22. IHasMusicGenres,
  23. IHasLookupInfo<SongInfo>,
  24. IHasMediaSources
  25. {
  26. public List<ChannelMediaInfo> ChannelMediaSources { get; set; }
  27. /// <summary>
  28. /// Gets or sets the artist.
  29. /// </summary>
  30. /// <value>The artist.</value>
  31. public List<string> Artists { get; set; }
  32. public List<string> AlbumArtists { get; set; }
  33. [IgnoreDataMember]
  34. public override bool EnableRefreshOnDateModifiedChange
  35. {
  36. get { return true; }
  37. }
  38. public Audio()
  39. {
  40. Artists = new List<string>();
  41. AlbumArtists = new List<string>();
  42. }
  43. public override double? GetDefaultPrimaryImageAspectRatio()
  44. {
  45. return 1;
  46. }
  47. [IgnoreDataMember]
  48. public override bool SupportsPlayedStatus
  49. {
  50. get
  51. {
  52. return true;
  53. }
  54. }
  55. [IgnoreDataMember]
  56. public override bool SupportsAddingToPlaylist
  57. {
  58. get { return true; }
  59. }
  60. [IgnoreDataMember]
  61. public override bool SupportsInheritedParentImages
  62. {
  63. get { return true; }
  64. }
  65. [IgnoreDataMember]
  66. protected override bool SupportsOwnedItems
  67. {
  68. get
  69. {
  70. return false;
  71. }
  72. }
  73. [IgnoreDataMember]
  74. public override Folder LatestItemsIndexContainer
  75. {
  76. get
  77. {
  78. return AlbumEntity;
  79. }
  80. }
  81. public override bool CanDownload()
  82. {
  83. var locationType = LocationType;
  84. return locationType != LocationType.Remote &&
  85. locationType != LocationType.Virtual;
  86. }
  87. [IgnoreDataMember]
  88. public List<string> AllArtists
  89. {
  90. get
  91. {
  92. var list = AlbumArtists.ToList();
  93. list.AddRange(Artists);
  94. return list;
  95. }
  96. }
  97. [IgnoreDataMember]
  98. public MusicAlbum AlbumEntity
  99. {
  100. get { return FindParent<MusicAlbum>(); }
  101. }
  102. /// <summary>
  103. /// Gets the type of the media.
  104. /// </summary>
  105. /// <value>The type of the media.</value>
  106. [IgnoreDataMember]
  107. public override string MediaType
  108. {
  109. get
  110. {
  111. return Model.Entities.MediaType.Audio;
  112. }
  113. }
  114. /// <summary>
  115. /// Creates the name of the sort.
  116. /// </summary>
  117. /// <returns>System.String.</returns>
  118. protected override string CreateSortName()
  119. {
  120. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  121. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  122. }
  123. public override List<string> GetUserDataKeys()
  124. {
  125. var list = base.GetUserDataKeys();
  126. if (ConfigurationManager.Configuration.EnableStandaloneMusicKeys)
  127. {
  128. var songKey = IndexNumber.HasValue ? IndexNumber.Value.ToString("0000") : string.Empty;
  129. if (ParentIndexNumber.HasValue)
  130. {
  131. songKey = ParentIndexNumber.Value.ToString("0000") + "-" + songKey;
  132. }
  133. songKey += Name;
  134. if (!string.IsNullOrWhiteSpace(Album))
  135. {
  136. songKey = Album + "-" + songKey;
  137. }
  138. var albumArtist = AlbumArtists.FirstOrDefault();
  139. if (!string.IsNullOrWhiteSpace(albumArtist))
  140. {
  141. songKey = albumArtist + "-" + songKey;
  142. }
  143. list.Insert(0, songKey);
  144. }
  145. else
  146. {
  147. var parent = AlbumEntity;
  148. if (parent != null && IndexNumber.HasValue)
  149. {
  150. list.InsertRange(0, parent.GetUserDataKeys().Select(i =>
  151. {
  152. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  153. + IndexNumber.Value.ToString("0000 - ");
  154. return i + songKey;
  155. }));
  156. }
  157. }
  158. return list;
  159. }
  160. public override UnratedItem GetBlockUnratedType()
  161. {
  162. if (SourceType == SourceType.Library)
  163. {
  164. return UnratedItem.Music;
  165. }
  166. return base.GetBlockUnratedType();
  167. }
  168. public SongInfo GetLookupInfo()
  169. {
  170. var info = GetItemLookupInfo<SongInfo>();
  171. info.AlbumArtists = AlbumArtists;
  172. info.Album = Album;
  173. info.Artists = Artists;
  174. return info;
  175. }
  176. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  177. {
  178. if (SourceType == SourceType.Channel)
  179. {
  180. var sources = ChannelManager.GetStaticMediaSources(this, CancellationToken.None)
  181. .Result.ToList();
  182. if (sources.Count > 0)
  183. {
  184. return sources;
  185. }
  186. var list = new List<MediaSourceInfo>
  187. {
  188. GetVersionInfo(this, enablePathSubstitution)
  189. };
  190. foreach (var mediaSource in list)
  191. {
  192. if (string.IsNullOrWhiteSpace(mediaSource.Path))
  193. {
  194. mediaSource.Type = MediaSourceType.Placeholder;
  195. }
  196. }
  197. return list;
  198. }
  199. var result = new List<MediaSourceInfo>
  200. {
  201. GetVersionInfo(this, enablePathSubstitution)
  202. };
  203. return result;
  204. }
  205. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  206. {
  207. var locationType = i.LocationType;
  208. var info = new MediaSourceInfo
  209. {
  210. Id = i.Id.ToString("N"),
  211. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  212. MediaStreams = MediaSourceManager.GetMediaStreams(i.Id).ToList(),
  213. Name = i.Name,
  214. Path = enablePathSubstituion ? GetMappedPath(i, i.Path, locationType) : i.Path,
  215. RunTimeTicks = i.RunTimeTicks,
  216. Container = i.Container,
  217. Size = i.Size
  218. };
  219. if (info.Protocol == MediaProtocol.File)
  220. {
  221. info.ETag = i.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
  222. }
  223. if (string.IsNullOrEmpty(info.Container))
  224. {
  225. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  226. {
  227. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  228. }
  229. }
  230. info.Bitrate = i.TotalBitrate;
  231. info.InferTotalBitrate();
  232. return info;
  233. }
  234. }
  235. }