Audio.cs 7.7 KB

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