Audio.cs 7.8 KB

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