Audio.cs 8.0 KB

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