Audio.cs 8.4 KB

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