Audio.cs 6.9 KB

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