Audio.cs 8.1 KB

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