Audio.cs 8.3 KB

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