Audio.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. namespace MediaBrowser.Controller.Entities.Audio
  12. {
  13. /// <summary>
  14. /// Class Audio
  15. /// </summary>
  16. public class Audio : BaseItem,
  17. IHasAlbumArtist,
  18. IHasArtist,
  19. IHasMusicGenres,
  20. IHasLookupInfo<SongInfo>,
  21. IHasTags,
  22. IHasMediaSources,
  23. IThemeMedia
  24. {
  25. public string FormatName { get; set; }
  26. public long? Size { get; set; }
  27. public string Container { get; set; }
  28. public int? TotalBitrate { get; set; }
  29. public List<string> Tags { get; set; }
  30. public ExtraType ExtraType { get; set; }
  31. public bool IsThemeMedia { get; set; }
  32. public Audio()
  33. {
  34. Artists = new List<string>();
  35. AlbumArtists = new List<string>();
  36. Tags = new List<string>();
  37. }
  38. [IgnoreDataMember]
  39. public override bool SupportsAddingToPlaylist
  40. {
  41. get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
  42. }
  43. /// <summary>
  44. /// Gets or sets a value indicating whether this instance has embedded image.
  45. /// </summary>
  46. /// <value><c>true</c> if this instance has embedded image; otherwise, <c>false</c>.</value>
  47. public bool HasEmbeddedImage { get; set; }
  48. /// <summary>
  49. /// Override this to true if class should be grouped under a container in indicies
  50. /// The container class should be defined via IndexContainer
  51. /// </summary>
  52. /// <value><c>true</c> if [group in index]; otherwise, <c>false</c>.</value>
  53. [IgnoreDataMember]
  54. public override bool GroupInIndex
  55. {
  56. get
  57. {
  58. return true;
  59. }
  60. }
  61. /// <summary>
  62. /// Override this to return the folder that should be used to construct a container
  63. /// for this item in an index. GroupInIndex should be true as well.
  64. /// </summary>
  65. /// <value>The index container.</value>
  66. [IgnoreDataMember]
  67. public override Folder IndexContainer
  68. {
  69. get
  70. {
  71. return LatestItemsIndexContainer ?? new MusicAlbum { Name = "Unknown Album" };
  72. }
  73. }
  74. [IgnoreDataMember]
  75. public override Folder LatestItemsIndexContainer
  76. {
  77. get
  78. {
  79. return Parents.OfType<MusicAlbum>().FirstOrDefault();
  80. }
  81. }
  82. /// <summary>
  83. /// Gets or sets the artist.
  84. /// </summary>
  85. /// <value>The artist.</value>
  86. public List<string> Artists { get; set; }
  87. public List<string> AlbumArtists { get; set; }
  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. /// <summary>
  99. /// Gets or sets the album.
  100. /// </summary>
  101. /// <value>The album.</value>
  102. public string Album { get; set; }
  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. /// <summary>
  125. /// Determines whether the specified name has artist.
  126. /// </summary>
  127. /// <param name="name">The name.</param>
  128. /// <returns><c>true</c> if the specified name has artist; otherwise, <c>false</c>.</returns>
  129. public bool HasArtist(string name)
  130. {
  131. return AllArtists.Contains(name, StringComparer.OrdinalIgnoreCase);
  132. }
  133. /// <summary>
  134. /// Gets the user data key.
  135. /// </summary>
  136. /// <returns>System.String.</returns>
  137. public override string GetUserDataKey()
  138. {
  139. var parent = FindParent<MusicAlbum>();
  140. if (parent != null)
  141. {
  142. var parentKey = parent.GetUserDataKey();
  143. if (IndexNumber.HasValue)
  144. {
  145. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  146. + (IndexNumber.Value.ToString("0000 - "));
  147. return parentKey + songKey;
  148. }
  149. }
  150. return base.GetUserDataKey();
  151. }
  152. protected override bool GetBlockUnratedValue(UserConfiguration config)
  153. {
  154. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  155. }
  156. public SongInfo GetLookupInfo()
  157. {
  158. var info = GetItemLookupInfo<SongInfo>();
  159. info.AlbumArtists = AlbumArtists;
  160. info.Album = Album;
  161. info.Artists = Artists;
  162. return info;
  163. }
  164. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  165. {
  166. var result = new List<MediaSourceInfo>
  167. {
  168. GetVersionInfo(this, enablePathSubstitution)
  169. };
  170. return result;
  171. }
  172. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  173. {
  174. var locationType = i.LocationType;
  175. var info = new MediaSourceInfo
  176. {
  177. Id = i.Id.ToString("N"),
  178. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  179. MediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery { ItemId = i.Id }).ToList(),
  180. Name = i.Name,
  181. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  182. RunTimeTicks = i.RunTimeTicks,
  183. Container = i.Container,
  184. Size = i.Size,
  185. Formats = (i.FormatName ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()
  186. };
  187. if (string.IsNullOrEmpty(info.Container))
  188. {
  189. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  190. {
  191. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  192. }
  193. }
  194. var bitrate = i.TotalBitrate ??
  195. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  196. .Select(m => m.BitRate ?? 0)
  197. .Sum();
  198. if (bitrate > 0)
  199. {
  200. info.Bitrate = bitrate;
  201. }
  202. return info;
  203. }
  204. }
  205. }