Audio.cs 7.4 KB

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