Audio.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using MediaBrowser.Controller.Persistence;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Configuration;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.MediaInfo;
  7. using MediaBrowser.Model.Users;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Runtime.Serialization;
  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. {
  26. public string FormatName { get; set; }
  27. public long? Size { get; set; }
  28. public string Container { get; set; }
  29. public int? TotalBitrate { get; set; }
  30. public List<string> Tags { get; set; }
  31. public ExtraType ExtraType { get; set; }
  32. public bool IsThemeMedia { get; set; }
  33. public Audio()
  34. {
  35. Artists = new List<string>();
  36. AlbumArtists = new List<string>();
  37. Tags = new List<string>();
  38. }
  39. [IgnoreDataMember]
  40. public override bool SupportsAddingToPlaylist
  41. {
  42. get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
  43. }
  44. /// <summary>
  45. /// Gets or sets a value indicating whether this instance has embedded image.
  46. /// </summary>
  47. /// <value><c>true</c> if this instance has embedded image; otherwise, <c>false</c>.</value>
  48. public bool HasEmbeddedImage { get; set; }
  49. /// <summary>
  50. /// Override this to true if class should be grouped under a container in indicies
  51. /// The container class should be defined via IndexContainer
  52. /// </summary>
  53. /// <value><c>true</c> if [group in index]; otherwise, <c>false</c>.</value>
  54. [IgnoreDataMember]
  55. public override bool GroupInIndex
  56. {
  57. get
  58. {
  59. return true;
  60. }
  61. }
  62. /// <summary>
  63. /// Override this to return the folder that should be used to construct a container
  64. /// for this item in an index. GroupInIndex should be true as well.
  65. /// </summary>
  66. /// <value>The index container.</value>
  67. [IgnoreDataMember]
  68. public override Folder IndexContainer
  69. {
  70. get
  71. {
  72. return LatestItemsIndexContainer ?? new MusicAlbum { Name = "Unknown Album" };
  73. }
  74. }
  75. [IgnoreDataMember]
  76. protected override bool SupportsOwnedItems
  77. {
  78. get
  79. {
  80. return false;
  81. }
  82. }
  83. [IgnoreDataMember]
  84. public override Folder LatestItemsIndexContainer
  85. {
  86. get
  87. {
  88. return Parents.OfType<MusicAlbum>().FirstOrDefault();
  89. }
  90. }
  91. [IgnoreDataMember]
  92. public bool IsArchive
  93. {
  94. get
  95. {
  96. if (string.IsNullOrWhiteSpace(Path))
  97. {
  98. return false;
  99. }
  100. var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
  101. return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
  102. }
  103. }
  104. public override bool CanDownload()
  105. {
  106. var locationType = LocationType;
  107. return locationType != LocationType.Remote &&
  108. locationType != LocationType.Virtual;
  109. }
  110. /// <summary>
  111. /// Gets or sets the artist.
  112. /// </summary>
  113. /// <value>The artist.</value>
  114. public List<string> Artists { get; set; }
  115. public List<string> AlbumArtists { get; set; }
  116. [IgnoreDataMember]
  117. public List<string> AllArtists
  118. {
  119. get
  120. {
  121. var list = AlbumArtists.ToList();
  122. list.AddRange(Artists);
  123. return list;
  124. }
  125. }
  126. /// <summary>
  127. /// Gets or sets the album.
  128. /// </summary>
  129. /// <value>The album.</value>
  130. public string Album { get; set; }
  131. /// <summary>
  132. /// Gets the type of the media.
  133. /// </summary>
  134. /// <value>The type of the media.</value>
  135. [IgnoreDataMember]
  136. public override string MediaType
  137. {
  138. get
  139. {
  140. return Model.Entities.MediaType.Audio;
  141. }
  142. }
  143. /// <summary>
  144. /// Creates the name of the sort.
  145. /// </summary>
  146. /// <returns>System.String.</returns>
  147. protected override string CreateSortName()
  148. {
  149. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  150. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  151. }
  152. /// <summary>
  153. /// Determines whether the specified name has artist.
  154. /// </summary>
  155. /// <param name="name">The name.</param>
  156. /// <returns><c>true</c> if the specified name has artist; otherwise, <c>false</c>.</returns>
  157. public bool HasArtist(string name)
  158. {
  159. return AllArtists.Contains(name, StringComparer.OrdinalIgnoreCase);
  160. }
  161. /// <summary>
  162. /// Gets the user data key.
  163. /// </summary>
  164. /// <returns>System.String.</returns>
  165. protected override string CreateUserDataKey()
  166. {
  167. var parent = FindParent<MusicAlbum>();
  168. if (parent != null)
  169. {
  170. var parentKey = parent.GetUserDataKey();
  171. if (IndexNumber.HasValue)
  172. {
  173. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  174. + (IndexNumber.Value.ToString("0000 - "));
  175. return parentKey + songKey;
  176. }
  177. }
  178. return base.CreateUserDataKey();
  179. }
  180. protected override bool GetBlockUnratedValue(UserPolicy config)
  181. {
  182. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  183. }
  184. public SongInfo GetLookupInfo()
  185. {
  186. var info = GetItemLookupInfo<SongInfo>();
  187. info.AlbumArtists = AlbumArtists;
  188. info.Album = Album;
  189. info.Artists = Artists;
  190. return info;
  191. }
  192. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  193. {
  194. var result = new List<MediaSourceInfo>
  195. {
  196. GetVersionInfo(this, enablePathSubstitution)
  197. };
  198. return result;
  199. }
  200. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  201. {
  202. var locationType = i.LocationType;
  203. var info = new MediaSourceInfo
  204. {
  205. Id = i.Id.ToString("N"),
  206. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  207. MediaStreams = MediaSourceManager.GetMediaStreams(new MediaStreamQuery { ItemId = i.Id }).ToList(),
  208. Name = i.Name,
  209. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  210. RunTimeTicks = i.RunTimeTicks,
  211. Container = i.Container,
  212. Size = i.Size,
  213. Formats = (i.FormatName ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()
  214. };
  215. if (string.IsNullOrEmpty(info.Container))
  216. {
  217. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  218. {
  219. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  220. }
  221. }
  222. var bitrate = i.TotalBitrate ??
  223. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  224. .Select(m => m.BitRate ?? 0)
  225. .Sum();
  226. if (bitrate > 0)
  227. {
  228. info.Bitrate = bitrate;
  229. }
  230. return info;
  231. }
  232. }
  233. }