2
0

Audio.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 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 ExtraType? ExtraType { get; set; }
  31. [IgnoreDataMember]
  32. public bool IsThemeMedia
  33. {
  34. get
  35. {
  36. return ExtraType.HasValue && ExtraType.Value == Model.Entities.ExtraType.ThemeSong;
  37. }
  38. }
  39. public Audio()
  40. {
  41. Artists = new List<string>();
  42. AlbumArtists = new List<string>();
  43. Tags = new List<string>();
  44. }
  45. [IgnoreDataMember]
  46. public override bool SupportsAddingToPlaylist
  47. {
  48. get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
  49. }
  50. [IgnoreDataMember]
  51. protected override bool SupportsOwnedItems
  52. {
  53. get
  54. {
  55. return false;
  56. }
  57. }
  58. [IgnoreDataMember]
  59. public override Folder LatestItemsIndexContainer
  60. {
  61. get
  62. {
  63. return AlbumEntity;
  64. }
  65. }
  66. [IgnoreDataMember]
  67. public bool IsArchive
  68. {
  69. get
  70. {
  71. if (string.IsNullOrWhiteSpace(Path))
  72. {
  73. return false;
  74. }
  75. var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
  76. return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
  77. }
  78. }
  79. public override bool CanDownload()
  80. {
  81. var locationType = LocationType;
  82. return locationType != LocationType.Remote &&
  83. locationType != LocationType.Virtual;
  84. }
  85. /// <summary>
  86. /// Gets or sets the artist.
  87. /// </summary>
  88. /// <value>The artist.</value>
  89. public List<string> Artists { get; set; }
  90. public List<string> AlbumArtists { get; set; }
  91. [IgnoreDataMember]
  92. public List<string> AllArtists
  93. {
  94. get
  95. {
  96. var list = AlbumArtists.ToList();
  97. list.AddRange(Artists);
  98. return list;
  99. }
  100. }
  101. /// <summary>
  102. /// Gets or sets the album.
  103. /// </summary>
  104. /// <value>The album.</value>
  105. public string Album { get; set; }
  106. [IgnoreDataMember]
  107. public MusicAlbum AlbumEntity
  108. {
  109. get { return FindParent<MusicAlbum>(); }
  110. }
  111. /// <summary>
  112. /// Gets the type of the media.
  113. /// </summary>
  114. /// <value>The type of the media.</value>
  115. [IgnoreDataMember]
  116. public override string MediaType
  117. {
  118. get
  119. {
  120. return Model.Entities.MediaType.Audio;
  121. }
  122. }
  123. /// <summary>
  124. /// Creates the name of the sort.
  125. /// </summary>
  126. /// <returns>System.String.</returns>
  127. protected override string CreateSortName()
  128. {
  129. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  130. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  131. }
  132. /// <summary>
  133. /// Gets the user data key.
  134. /// </summary>
  135. /// <returns>System.String.</returns>
  136. protected override string CreateUserDataKey()
  137. {
  138. var parent = AlbumEntity;
  139. if (parent != null)
  140. {
  141. var parentKey = parent.GetUserDataKey();
  142. if (IndexNumber.HasValue)
  143. {
  144. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  145. + (IndexNumber.Value.ToString("0000 - "));
  146. return parentKey + songKey;
  147. }
  148. }
  149. return base.CreateUserDataKey();
  150. }
  151. protected override bool GetBlockUnratedValue(UserPolicy config)
  152. {
  153. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  154. }
  155. public SongInfo GetLookupInfo()
  156. {
  157. var info = GetItemLookupInfo<SongInfo>();
  158. info.AlbumArtists = AlbumArtists;
  159. info.Album = Album;
  160. info.Artists = Artists;
  161. return info;
  162. }
  163. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  164. {
  165. var result = new List<MediaSourceInfo>
  166. {
  167. GetVersionInfo(this, enablePathSubstitution)
  168. };
  169. return result;
  170. }
  171. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  172. {
  173. var locationType = i.LocationType;
  174. var info = new MediaSourceInfo
  175. {
  176. Id = i.Id.ToString("N"),
  177. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  178. MediaStreams = MediaSourceManager.GetMediaStreams(i.Id).ToList(),
  179. Name = i.Name,
  180. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  181. RunTimeTicks = i.RunTimeTicks,
  182. Container = i.Container,
  183. Size = i.Size
  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. }