Audio.cs 7.7 KB

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