Audio.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Runtime.Serialization;
  11. using MediaBrowser.Model.Users;
  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. public override Folder LatestItemsIndexContainer
  77. {
  78. get
  79. {
  80. return Parents.OfType<MusicAlbum>().FirstOrDefault();
  81. }
  82. }
  83. [IgnoreDataMember]
  84. public bool IsArchive
  85. {
  86. get
  87. {
  88. if (string.IsNullOrWhiteSpace(Path))
  89. {
  90. return false;
  91. }
  92. var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
  93. return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
  94. }
  95. }
  96. /// <summary>
  97. /// Gets or sets the artist.
  98. /// </summary>
  99. /// <value>The artist.</value>
  100. public List<string> Artists { get; set; }
  101. public List<string> AlbumArtists { get; set; }
  102. [IgnoreDataMember]
  103. public List<string> AllArtists
  104. {
  105. get
  106. {
  107. var list = AlbumArtists.ToList();
  108. list.AddRange(Artists);
  109. return list;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets or sets the album.
  114. /// </summary>
  115. /// <value>The album.</value>
  116. public string Album { get; set; }
  117. /// <summary>
  118. /// Gets the type of the media.
  119. /// </summary>
  120. /// <value>The type of the media.</value>
  121. [IgnoreDataMember]
  122. public override string MediaType
  123. {
  124. get
  125. {
  126. return Model.Entities.MediaType.Audio;
  127. }
  128. }
  129. /// <summary>
  130. /// Creates the name of the sort.
  131. /// </summary>
  132. /// <returns>System.String.</returns>
  133. protected override string CreateSortName()
  134. {
  135. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  136. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  137. }
  138. /// <summary>
  139. /// Determines whether the specified name has artist.
  140. /// </summary>
  141. /// <param name="name">The name.</param>
  142. /// <returns><c>true</c> if the specified name has artist; otherwise, <c>false</c>.</returns>
  143. public bool HasArtist(string name)
  144. {
  145. return AllArtists.Contains(name, StringComparer.OrdinalIgnoreCase);
  146. }
  147. /// <summary>
  148. /// Gets the user data key.
  149. /// </summary>
  150. /// <returns>System.String.</returns>
  151. public override string GetUserDataKey()
  152. {
  153. var parent = FindParent<MusicAlbum>();
  154. if (parent != null)
  155. {
  156. var parentKey = parent.GetUserDataKey();
  157. if (IndexNumber.HasValue)
  158. {
  159. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  160. + (IndexNumber.Value.ToString("0000 - "));
  161. return parentKey + songKey;
  162. }
  163. }
  164. return base.GetUserDataKey();
  165. }
  166. protected override bool GetBlockUnratedValue(UserPolicy config)
  167. {
  168. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  169. }
  170. public SongInfo GetLookupInfo()
  171. {
  172. var info = GetItemLookupInfo<SongInfo>();
  173. info.AlbumArtists = AlbumArtists;
  174. info.Album = Album;
  175. info.Artists = Artists;
  176. return info;
  177. }
  178. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  179. {
  180. var result = new List<MediaSourceInfo>
  181. {
  182. GetVersionInfo(this, enablePathSubstitution)
  183. };
  184. return result;
  185. }
  186. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  187. {
  188. var locationType = i.LocationType;
  189. var info = new MediaSourceInfo
  190. {
  191. Id = i.Id.ToString("N"),
  192. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  193. MediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery { ItemId = i.Id }).ToList(),
  194. Name = i.Name,
  195. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  196. RunTimeTicks = i.RunTimeTicks,
  197. Container = i.Container,
  198. Size = i.Size,
  199. Formats = (i.FormatName ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()
  200. };
  201. if (string.IsNullOrEmpty(info.Container))
  202. {
  203. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  204. {
  205. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  206. }
  207. }
  208. var bitrate = i.TotalBitrate ??
  209. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  210. .Select(m => m.BitRate ?? 0)
  211. .Sum();
  212. if (bitrate > 0)
  213. {
  214. info.Bitrate = bitrate;
  215. }
  216. return info;
  217. }
  218. }
  219. }