Audio.cs 8.1 KB

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