Audio.cs 7.5 KB

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