Audio.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. using System.Threading;
  11. using MediaBrowser.Controller.Channels;
  12. namespace MediaBrowser.Controller.Entities.Audio
  13. {
  14. /// <summary>
  15. /// Class Audio
  16. /// </summary>
  17. public class Audio : BaseItem,
  18. IHasAlbumArtist,
  19. IHasArtist,
  20. IHasMusicGenres,
  21. IHasLookupInfo<SongInfo>,
  22. IHasMediaSources,
  23. IThemeMedia,
  24. IArchivable
  25. {
  26. public List<ChannelMediaInfo> ChannelMediaSources { get; set; }
  27. public int? TotalBitrate { get; set; }
  28. public ExtraType? ExtraType { get; set; }
  29. /// <summary>
  30. /// Gets or sets the artist.
  31. /// </summary>
  32. /// <value>The artist.</value>
  33. public List<string> Artists { get; set; }
  34. public List<string> AlbumArtists { get; set; }
  35. [IgnoreDataMember]
  36. public bool IsThemeMedia
  37. {
  38. get
  39. {
  40. return ExtraType.HasValue && ExtraType.Value == Model.Entities.ExtraType.ThemeSong;
  41. }
  42. }
  43. [IgnoreDataMember]
  44. public override bool EnableForceSaveOnDateModifiedChange
  45. {
  46. get { return true; }
  47. }
  48. public Audio()
  49. {
  50. Artists = new List<string>();
  51. AlbumArtists = new List<string>();
  52. }
  53. [IgnoreDataMember]
  54. public override bool SupportsAddingToPlaylist
  55. {
  56. get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
  57. }
  58. [IgnoreDataMember]
  59. protected override bool SupportsOwnedItems
  60. {
  61. get
  62. {
  63. return false;
  64. }
  65. }
  66. [IgnoreDataMember]
  67. public override Folder LatestItemsIndexContainer
  68. {
  69. get
  70. {
  71. return AlbumEntity;
  72. }
  73. }
  74. [IgnoreDataMember]
  75. public bool IsArchive
  76. {
  77. get
  78. {
  79. if (string.IsNullOrWhiteSpace(Path))
  80. {
  81. return false;
  82. }
  83. var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
  84. return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
  85. }
  86. }
  87. public override bool CanDownload()
  88. {
  89. var locationType = LocationType;
  90. return locationType != LocationType.Remote &&
  91. locationType != LocationType.Virtual;
  92. }
  93. [IgnoreDataMember]
  94. public List<string> AllArtists
  95. {
  96. get
  97. {
  98. var list = AlbumArtists.ToList();
  99. list.AddRange(Artists);
  100. return list;
  101. }
  102. }
  103. [IgnoreDataMember]
  104. public MusicAlbum AlbumEntity
  105. {
  106. get { return FindParent<MusicAlbum>(); }
  107. }
  108. /// <summary>
  109. /// Gets the type of the media.
  110. /// </summary>
  111. /// <value>The type of the media.</value>
  112. [IgnoreDataMember]
  113. public override string MediaType
  114. {
  115. get
  116. {
  117. return Model.Entities.MediaType.Audio;
  118. }
  119. }
  120. /// <summary>
  121. /// Creates the name of the sort.
  122. /// </summary>
  123. /// <returns>System.String.</returns>
  124. protected override string CreateSortName()
  125. {
  126. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  127. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  128. }
  129. public override List<string> GetUserDataKeys()
  130. {
  131. var list = base.GetUserDataKeys();
  132. if (ConfigurationManager.Configuration.EnableStandaloneMusicKeys)
  133. {
  134. var songKey = IndexNumber.HasValue ? IndexNumber.Value.ToString("0000") : string.Empty;
  135. if (ParentIndexNumber.HasValue)
  136. {
  137. songKey = ParentIndexNumber.Value.ToString("0000") + "-" + songKey;
  138. }
  139. songKey += Name;
  140. if (!string.IsNullOrWhiteSpace(Album))
  141. {
  142. songKey = Album + "-" + songKey;
  143. }
  144. var albumArtist = AlbumArtists.FirstOrDefault();
  145. if (!string.IsNullOrWhiteSpace(albumArtist))
  146. {
  147. songKey = albumArtist + "-" + songKey;
  148. }
  149. list.Insert(0, songKey);
  150. }
  151. else
  152. {
  153. var parent = AlbumEntity;
  154. if (parent != null && IndexNumber.HasValue)
  155. {
  156. list.InsertRange(0, parent.GetUserDataKeys().Select(i =>
  157. {
  158. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  159. + IndexNumber.Value.ToString("0000 - ");
  160. return i + songKey;
  161. }));
  162. }
  163. }
  164. return list;
  165. }
  166. public override UnratedItem GetBlockUnratedType()
  167. {
  168. if (SourceType == SourceType.Library)
  169. {
  170. return UnratedItem.Music;
  171. }
  172. return base.GetBlockUnratedType();
  173. }
  174. public SongInfo GetLookupInfo()
  175. {
  176. var info = GetItemLookupInfo<SongInfo>();
  177. info.AlbumArtists = AlbumArtists;
  178. info.Album = Album;
  179. info.Artists = Artists;
  180. return info;
  181. }
  182. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  183. {
  184. if (SourceType == SourceType.Channel)
  185. {
  186. var sources = ChannelManager.GetStaticMediaSources(this, false, CancellationToken.None)
  187. .Result.ToList();
  188. if (sources.Count > 0)
  189. {
  190. return sources;
  191. }
  192. var list = new List<MediaSourceInfo>
  193. {
  194. GetVersionInfo(this, enablePathSubstitution)
  195. };
  196. foreach (var mediaSource in list)
  197. {
  198. if (string.IsNullOrWhiteSpace(mediaSource.Path))
  199. {
  200. mediaSource.Type = MediaSourceType.Placeholder;
  201. }
  202. }
  203. return list;
  204. }
  205. var result = new List<MediaSourceInfo>
  206. {
  207. GetVersionInfo(this, enablePathSubstitution)
  208. };
  209. return result;
  210. }
  211. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  212. {
  213. var locationType = i.LocationType;
  214. var info = new MediaSourceInfo
  215. {
  216. Id = i.Id.ToString("N"),
  217. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  218. MediaStreams = MediaSourceManager.GetMediaStreams(i.Id).ToList(),
  219. Name = i.Name,
  220. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  221. RunTimeTicks = i.RunTimeTicks,
  222. Container = i.Container,
  223. Size = i.Size
  224. };
  225. if (string.IsNullOrEmpty(info.Container))
  226. {
  227. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  228. {
  229. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  230. }
  231. }
  232. var bitrate = i.TotalBitrate ??
  233. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  234. .Select(m => m.BitRate ?? 0)
  235. .Sum();
  236. if (bitrate > 0)
  237. {
  238. info.Bitrate = bitrate;
  239. }
  240. return info;
  241. }
  242. }
  243. }