Audio.cs 8.5 KB

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