Audio.cs 7.9 KB

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