Audio.cs 7.8 KB

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