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