Audio.cs 8.2 KB

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