Audio.cs 7.3 KB

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