Audio.cs 8.0 KB

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