Audio.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /// <summary>
  133. /// Gets the user data key.
  134. /// </summary>
  135. /// <returns>System.String.</returns>
  136. protected override string CreateUserDataKey()
  137. {
  138. if (ConfigurationManager.Configuration.EnableStandaloneMusicKeys)
  139. {
  140. var songKey = IndexNumber.HasValue ? IndexNumber.Value.ToString("0000") : string.Empty;
  141. if (ParentIndexNumber.HasValue)
  142. {
  143. songKey = ParentIndexNumber.Value.ToString("0000") + "-" + songKey;
  144. }
  145. songKey+= Name;
  146. if (!string.IsNullOrWhiteSpace(Album))
  147. {
  148. songKey = Album + "-" + songKey;
  149. }
  150. var albumArtist = AlbumArtists.FirstOrDefault();
  151. if (!string.IsNullOrWhiteSpace(albumArtist))
  152. {
  153. songKey = albumArtist + "-" + songKey;
  154. }
  155. return songKey;
  156. }
  157. var parent = AlbumEntity;
  158. if (parent != null)
  159. {
  160. var parentKey = parent.GetUserDataKey();
  161. if (IndexNumber.HasValue)
  162. {
  163. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  164. + IndexNumber.Value.ToString("0000 - ");
  165. return parentKey + songKey;
  166. }
  167. }
  168. return base.CreateUserDataKey();
  169. }
  170. public override UnratedItem GetBlockUnratedType()
  171. {
  172. if (SourceType == SourceType.Library)
  173. {
  174. return UnratedItem.Music;
  175. }
  176. return base.GetBlockUnratedType();
  177. }
  178. public SongInfo GetLookupInfo()
  179. {
  180. var info = GetItemLookupInfo<SongInfo>();
  181. info.AlbumArtists = AlbumArtists;
  182. info.Album = Album;
  183. info.Artists = Artists;
  184. return info;
  185. }
  186. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  187. {
  188. if (SourceType == SourceType.Channel)
  189. {
  190. var sources = ChannelManager.GetStaticMediaSources(this, false, CancellationToken.None)
  191. .Result.ToList();
  192. if (sources.Count > 0)
  193. {
  194. return sources;
  195. }
  196. var list = new List<MediaSourceInfo>
  197. {
  198. GetVersionInfo(this, enablePathSubstitution)
  199. };
  200. foreach (var mediaSource in list)
  201. {
  202. if (string.IsNullOrWhiteSpace(mediaSource.Path))
  203. {
  204. mediaSource.Type = MediaSourceType.Placeholder;
  205. }
  206. }
  207. return list;
  208. }
  209. var result = new List<MediaSourceInfo>
  210. {
  211. GetVersionInfo(this, enablePathSubstitution)
  212. };
  213. return result;
  214. }
  215. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  216. {
  217. var locationType = i.LocationType;
  218. var info = new MediaSourceInfo
  219. {
  220. Id = i.Id.ToString("N"),
  221. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  222. MediaStreams = MediaSourceManager.GetMediaStreams(i.Id).ToList(),
  223. Name = i.Name,
  224. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  225. RunTimeTicks = i.RunTimeTicks,
  226. Container = i.Container,
  227. Size = i.Size
  228. };
  229. if (string.IsNullOrEmpty(info.Container))
  230. {
  231. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  232. {
  233. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  234. }
  235. }
  236. var bitrate = i.TotalBitrate ??
  237. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  238. .Select(m => m.BitRate ?? 0)
  239. .Sum();
  240. if (bitrate > 0)
  241. {
  242. info.Bitrate = bitrate;
  243. }
  244. return info;
  245. }
  246. }
  247. }