Audio.cs 7.3 KB

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