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