Audio.cs 8.3 KB

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