Audio.cs 7.7 KB

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