DynamicHlsService.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Devices;
  5. using MediaBrowser.Controller.Dlna;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.MediaEncoding;
  8. using MediaBrowser.Controller.Net;
  9. using MediaBrowser.Model.Dlna;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Extensions;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.Serialization;
  14. using ServiceStack;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Globalization;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using MimeTypes = MediaBrowser.Model.Net.MimeTypes;
  24. namespace MediaBrowser.Api.Playback.Hls
  25. {
  26. /// <summary>
  27. /// Options is needed for chromecast. Threw Head in there since it's related
  28. /// </summary>
  29. [Route("/Videos/{Id}/master.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
  30. [Route("/Videos/{Id}/master.m3u8", "HEAD", Summary = "Gets a video stream using HTTP live streaming.")]
  31. public class GetMasterHlsVideoPlaylist : VideoStreamRequest, IMasterHlsRequest
  32. {
  33. public bool EnableAdaptiveBitrateStreaming { get; set; }
  34. public GetMasterHlsVideoPlaylist()
  35. {
  36. EnableAdaptiveBitrateStreaming = true;
  37. }
  38. }
  39. [Route("/Audio/{Id}/master.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")]
  40. [Route("/Audio/{Id}/master.m3u8", "HEAD", Summary = "Gets an audio stream using HTTP live streaming.")]
  41. public class GetMasterHlsAudioPlaylist : StreamRequest, IMasterHlsRequest
  42. {
  43. public bool EnableAdaptiveBitrateStreaming { get; set; }
  44. public GetMasterHlsAudioPlaylist()
  45. {
  46. EnableAdaptiveBitrateStreaming = true;
  47. }
  48. }
  49. public interface IMasterHlsRequest
  50. {
  51. bool EnableAdaptiveBitrateStreaming { get; set; }
  52. }
  53. [Route("/Videos/{Id}/main.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
  54. public class GetVariantHlsVideoPlaylist : VideoStreamRequest
  55. {
  56. }
  57. [Route("/Audio/{Id}/main.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")]
  58. public class GetVariantHlsAudioPlaylist : StreamRequest
  59. {
  60. }
  61. [Route("/Videos/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.ts", "GET")]
  62. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  63. public class GetHlsVideoSegment : VideoStreamRequest
  64. {
  65. public string PlaylistId { get; set; }
  66. /// <summary>
  67. /// Gets or sets the segment id.
  68. /// </summary>
  69. /// <value>The segment id.</value>
  70. public string SegmentId { get; set; }
  71. }
  72. [Route("/Audio/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.aac", "GET")]
  73. [Route("/Audio/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.ts", "GET")]
  74. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  75. public class GetHlsAudioSegment : StreamRequest
  76. {
  77. public string PlaylistId { get; set; }
  78. /// <summary>
  79. /// Gets or sets the segment id.
  80. /// </summary>
  81. /// <value>The segment id.</value>
  82. public string SegmentId { get; set; }
  83. }
  84. public class DynamicHlsService : BaseHlsService
  85. {
  86. public DynamicHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, INetworkManager networkManager)
  87. : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer)
  88. {
  89. NetworkManager = networkManager;
  90. }
  91. protected INetworkManager NetworkManager { get; private set; }
  92. public Task<object> Get(GetMasterHlsVideoPlaylist request)
  93. {
  94. return GetMasterPlaylistInternal(request, "GET");
  95. }
  96. public Task<object> Head(GetMasterHlsVideoPlaylist request)
  97. {
  98. return GetMasterPlaylistInternal(request, "HEAD");
  99. }
  100. public Task<object> Get(GetMasterHlsAudioPlaylist request)
  101. {
  102. return GetMasterPlaylistInternal(request, "GET");
  103. }
  104. public Task<object> Head(GetMasterHlsAudioPlaylist request)
  105. {
  106. return GetMasterPlaylistInternal(request, "HEAD");
  107. }
  108. public Task<object> Get(GetVariantHlsVideoPlaylist request)
  109. {
  110. return GetVariantPlaylistInternal(request, true, "main");
  111. }
  112. public Task<object> Get(GetVariantHlsAudioPlaylist request)
  113. {
  114. return GetVariantPlaylistInternal(request, false, "main");
  115. }
  116. public Task<object> Get(GetHlsVideoSegment request)
  117. {
  118. return GetDynamicSegment(request, request.SegmentId);
  119. }
  120. public Task<object> Get(GetHlsAudioSegment request)
  121. {
  122. return GetDynamicSegment(request, request.SegmentId);
  123. }
  124. private async Task<object> GetDynamicSegment(StreamRequest request, string segmentId)
  125. {
  126. if ((request.StartTimeTicks ?? 0) > 0)
  127. {
  128. throw new ArgumentException("StartTimeTicks is not allowed.");
  129. }
  130. var cancellationTokenSource = new CancellationTokenSource();
  131. var cancellationToken = cancellationTokenSource.Token;
  132. var requestedIndex = int.Parse(segmentId, NumberStyles.Integer, UsCulture);
  133. var state = await GetState(request, cancellationToken).ConfigureAwait(false);
  134. var playlistPath = Path.ChangeExtension(state.OutputFilePath, ".m3u8");
  135. var segmentPath = GetSegmentPath(state, playlistPath, requestedIndex);
  136. var segmentExtension = GetSegmentFileExtension(state);
  137. TranscodingJob job = null;
  138. if (FileSystem.FileExists(segmentPath))
  139. {
  140. job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
  141. return await GetSegmentResult(state, playlistPath, segmentPath, requestedIndex, job, cancellationToken).ConfigureAwait(false);
  142. }
  143. await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
  144. try
  145. {
  146. if (FileSystem.FileExists(segmentPath))
  147. {
  148. job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
  149. return await GetSegmentResult(state, playlistPath, segmentPath, requestedIndex, job, cancellationToken).ConfigureAwait(false);
  150. }
  151. else
  152. {
  153. var startTranscoding = false;
  154. var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension);
  155. var segmentGapRequiringTranscodingChange = 24 / state.SegmentLength;
  156. if (currentTranscodingIndex == null)
  157. {
  158. Logger.Debug("Starting transcoding because currentTranscodingIndex=null");
  159. startTranscoding = true;
  160. }
  161. else if (requestedIndex < currentTranscodingIndex.Value)
  162. {
  163. Logger.Debug("Starting transcoding because requestedIndex={0} and currentTranscodingIndex={1}", requestedIndex, currentTranscodingIndex);
  164. startTranscoding = true;
  165. }
  166. else if ((requestedIndex - currentTranscodingIndex.Value) > segmentGapRequiringTranscodingChange)
  167. {
  168. Logger.Debug("Starting transcoding because segmentGap is {0} and max allowed gap is {1}. requestedIndex={2}", (requestedIndex - currentTranscodingIndex.Value), segmentGapRequiringTranscodingChange, requestedIndex);
  169. startTranscoding = true;
  170. }
  171. if (startTranscoding)
  172. {
  173. // If the playlist doesn't already exist, startup ffmpeg
  174. try
  175. {
  176. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false);
  177. if (currentTranscodingIndex.HasValue)
  178. {
  179. DeleteLastFile(playlistPath, segmentExtension, 0);
  180. }
  181. request.StartTimeTicks = GetStartPositionTicks(state, requestedIndex);
  182. job = await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false);
  183. }
  184. catch
  185. {
  186. state.Dispose();
  187. throw;
  188. }
  189. //await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false);
  190. }
  191. else
  192. {
  193. job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
  194. if (job.TranscodingThrottler != null)
  195. {
  196. job.TranscodingThrottler.UnpauseTranscoding();
  197. }
  198. }
  199. }
  200. }
  201. finally
  202. {
  203. ApiEntryPoint.Instance.TranscodingStartLock.Release();
  204. }
  205. //Logger.Info("waiting for {0}", segmentPath);
  206. //while (!File.Exists(segmentPath))
  207. //{
  208. // await Task.Delay(50, cancellationToken).ConfigureAwait(false);
  209. //}
  210. Logger.Info("returning {0}", segmentPath);
  211. job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
  212. return await GetSegmentResult(state, playlistPath, segmentPath, requestedIndex, job, cancellationToken).ConfigureAwait(false);
  213. }
  214. // 256k
  215. private const int BufferSize = 262144;
  216. private long GetStartPositionTicks(StreamState state, int requestedIndex)
  217. {
  218. double startSeconds = 0;
  219. var lengths = GetSegmentLengths(state);
  220. for (var i = 0; i < requestedIndex; i++)
  221. {
  222. startSeconds += lengths[requestedIndex];
  223. }
  224. var position = TimeSpan.FromSeconds(startSeconds).Ticks;
  225. return position;
  226. }
  227. private long GetEndPositionTicks(StreamState state, int requestedIndex)
  228. {
  229. double startSeconds = 0;
  230. var lengths = GetSegmentLengths(state);
  231. for (var i = 0; i <= requestedIndex; i++)
  232. {
  233. startSeconds += lengths[requestedIndex];
  234. }
  235. var position = TimeSpan.FromSeconds(startSeconds).Ticks;
  236. return position;
  237. }
  238. private double[] GetSegmentLengths(StreamState state)
  239. {
  240. var result = new List<double>();
  241. if (state.VideoRequest != null)
  242. {
  243. var encoder = GetVideoEncoder(state);
  244. if (string.Equals(encoder, "copy", StringComparison.OrdinalIgnoreCase))
  245. {
  246. var videoStream = state.VideoStream;
  247. if (videoStream.KeyFrames != null && videoStream.KeyFrames.Count > 0)
  248. {
  249. foreach (var frame in videoStream.KeyFrames)
  250. {
  251. var seconds = TimeSpan.FromMilliseconds(frame).TotalSeconds;
  252. seconds -= result.Sum();
  253. result.Add(seconds);
  254. }
  255. return result.ToArray();
  256. }
  257. }
  258. }
  259. var ticks = state.RunTimeTicks ?? 0;
  260. var segmentLengthTicks = TimeSpan.FromSeconds(state.SegmentLength).Ticks;
  261. while (ticks > 0)
  262. {
  263. var length = ticks >= segmentLengthTicks ? segmentLengthTicks : ticks;
  264. result.Add(TimeSpan.FromTicks(length).TotalSeconds);
  265. ticks -= length;
  266. }
  267. return result.ToArray();
  268. }
  269. public int? GetCurrentTranscodingIndex(string playlist, string segmentExtension)
  270. {
  271. var job = ApiEntryPoint.Instance.GetTranscodingJob(playlist, TranscodingJobType);
  272. if (job == null || job.HasExited)
  273. {
  274. return null;
  275. }
  276. var file = GetLastTranscodingFile(playlist, segmentExtension, FileSystem);
  277. if (file == null)
  278. {
  279. return null;
  280. }
  281. var playlistFilename = Path.GetFileNameWithoutExtension(playlist);
  282. var indexString = Path.GetFileNameWithoutExtension(file.Name).Substring(playlistFilename.Length);
  283. return int.Parse(indexString, NumberStyles.Integer, UsCulture);
  284. }
  285. private void DeleteLastFile(string playlistPath, string segmentExtension, int retryCount)
  286. {
  287. var file = GetLastTranscodingFile(playlistPath, segmentExtension, FileSystem);
  288. if (file != null)
  289. {
  290. DeleteFile(file, retryCount);
  291. }
  292. }
  293. private void DeleteFile(FileInfo file, int retryCount)
  294. {
  295. if (retryCount >= 5)
  296. {
  297. return;
  298. }
  299. try
  300. {
  301. FileSystem.DeleteFile(file.FullName);
  302. }
  303. catch (IOException ex)
  304. {
  305. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, file.FullName);
  306. Thread.Sleep(100);
  307. DeleteFile(file, retryCount + 1);
  308. }
  309. catch (Exception ex)
  310. {
  311. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, file.FullName);
  312. }
  313. }
  314. private static FileInfo GetLastTranscodingFile(string playlist, string segmentExtension, IFileSystem fileSystem)
  315. {
  316. var folder = Path.GetDirectoryName(playlist);
  317. var filePrefix = Path.GetFileNameWithoutExtension(playlist) ?? string.Empty;
  318. try
  319. {
  320. return fileSystem.GetFiles(folder)
  321. .Where(i => string.Equals(i.Extension, segmentExtension, StringComparison.OrdinalIgnoreCase) && Path.GetFileNameWithoutExtension(i.Name).StartsWith(filePrefix, StringComparison.OrdinalIgnoreCase))
  322. .OrderByDescending(fileSystem.GetLastWriteTimeUtc)
  323. .FirstOrDefault();
  324. }
  325. catch (DirectoryNotFoundException)
  326. {
  327. return null;
  328. }
  329. }
  330. protected override int GetStartNumber(StreamState state)
  331. {
  332. return GetStartNumber(state.VideoRequest);
  333. }
  334. private int GetStartNumber(VideoStreamRequest request)
  335. {
  336. var segmentId = "0";
  337. var segmentRequest = request as GetHlsVideoSegment;
  338. if (segmentRequest != null)
  339. {
  340. segmentId = segmentRequest.SegmentId;
  341. }
  342. return int.Parse(segmentId, NumberStyles.Integer, UsCulture);
  343. }
  344. private string GetSegmentPath(StreamState state, string playlist, int index)
  345. {
  346. var folder = Path.GetDirectoryName(playlist);
  347. var filename = Path.GetFileNameWithoutExtension(playlist);
  348. return Path.Combine(folder, filename + index.ToString(UsCulture) + GetSegmentFileExtension(state));
  349. }
  350. private async Task<object> GetSegmentResult(StreamState state, string playlistPath,
  351. string segmentPath,
  352. int segmentIndex,
  353. TranscodingJob transcodingJob,
  354. CancellationToken cancellationToken)
  355. {
  356. // If all transcoding has completed, just return immediately
  357. if (transcodingJob != null && transcodingJob.HasExited && FileSystem.FileExists(segmentPath))
  358. {
  359. return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob);
  360. }
  361. var segmentFilename = Path.GetFileName(segmentPath);
  362. while (!cancellationToken.IsCancellationRequested)
  363. {
  364. try
  365. {
  366. using (var fileStream = GetPlaylistFileStream(playlistPath))
  367. {
  368. using (var reader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
  369. {
  370. var text = await reader.ReadToEndAsync().ConfigureAwait(false);
  371. // If it appears in the playlist, it's done
  372. if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
  373. {
  374. if (FileSystem.FileExists(segmentPath))
  375. {
  376. return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob);
  377. }
  378. //break;
  379. }
  380. }
  381. }
  382. }
  383. catch (IOException)
  384. {
  385. // May get an error if the file is locked
  386. }
  387. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  388. }
  389. // if a different file is encoding, it's done
  390. //var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath);
  391. //if (currentTranscodingIndex > segmentIndex)
  392. //{
  393. //return GetSegmentResult(segmentPath, segmentIndex);
  394. //}
  395. //// Wait for the file to stop being written to, then stream it
  396. //var length = new FileInfo(segmentPath).Length;
  397. //var eofCount = 0;
  398. //while (eofCount < 10)
  399. //{
  400. // var info = new FileInfo(segmentPath);
  401. // if (!info.Exists)
  402. // {
  403. // break;
  404. // }
  405. // var newLength = info.Length;
  406. // if (newLength == length)
  407. // {
  408. // eofCount++;
  409. // }
  410. // else
  411. // {
  412. // eofCount = 0;
  413. // }
  414. // length = newLength;
  415. // await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  416. //}
  417. cancellationToken.ThrowIfCancellationRequested();
  418. return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob);
  419. }
  420. private object GetSegmentResult(StreamState state, string segmentPath, int index, TranscodingJob transcodingJob)
  421. {
  422. var segmentEndingPositionTicks = GetEndPositionTicks(state, index);
  423. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  424. {
  425. Path = segmentPath,
  426. FileShare = FileShare.ReadWrite,
  427. OnComplete = () =>
  428. {
  429. if (transcodingJob != null)
  430. {
  431. transcodingJob.DownloadPositionTicks = Math.Max(transcodingJob.DownloadPositionTicks ?? segmentEndingPositionTicks, segmentEndingPositionTicks);
  432. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  433. }
  434. }
  435. });
  436. }
  437. private async Task<object> GetMasterPlaylistInternal(StreamRequest request, string method)
  438. {
  439. var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
  440. if (string.IsNullOrEmpty(request.MediaSourceId))
  441. {
  442. throw new ArgumentException("MediaSourceId is required");
  443. }
  444. var playlistText = string.Empty;
  445. if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase))
  446. {
  447. var audioBitrate = state.OutputAudioBitrate ?? 0;
  448. var videoBitrate = state.OutputVideoBitrate ?? 0;
  449. playlistText = GetMasterPlaylistFileText(state, videoBitrate + audioBitrate);
  450. }
  451. return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
  452. }
  453. private string GetMasterPlaylistFileText(StreamState state, int totalBitrate)
  454. {
  455. var builder = new StringBuilder();
  456. builder.AppendLine("#EXTM3U");
  457. var isLiveStream = (state.RunTimeTicks ?? 0) == 0;
  458. var queryStringIndex = Request.RawUrl.IndexOf('?');
  459. var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
  460. // Main stream
  461. var playlistUrl = isLiveStream ? "live.m3u8" : "main.m3u8";
  462. playlistUrl += queryString;
  463. var request = state.Request;
  464. var subtitleStreams = state.MediaSource
  465. .MediaStreams
  466. .Where(i => i.IsTextSubtitleStream)
  467. .ToList();
  468. var subtitleGroup = subtitleStreams.Count > 0 &&
  469. (request is GetMasterHlsVideoPlaylist) &&
  470. ((GetMasterHlsVideoPlaylist)request).SubtitleMethod == SubtitleDeliveryMethod.Hls ?
  471. "subs" :
  472. null;
  473. AppendPlaylist(builder, playlistUrl, totalBitrate, subtitleGroup);
  474. if (EnableAdaptiveBitrateStreaming(state, isLiveStream))
  475. {
  476. var requestedVideoBitrate = state.VideoRequest == null ? 0 : state.VideoRequest.VideoBitRate ?? 0;
  477. // By default, vary by just 200k
  478. var variation = GetBitrateVariation(totalBitrate);
  479. var newBitrate = totalBitrate - variation;
  480. var variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, (requestedVideoBitrate - variation));
  481. AppendPlaylist(builder, variantUrl, newBitrate, subtitleGroup);
  482. variation *= 2;
  483. newBitrate = totalBitrate - variation;
  484. variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, (requestedVideoBitrate - variation));
  485. AppendPlaylist(builder, variantUrl, newBitrate, subtitleGroup);
  486. }
  487. if (!string.IsNullOrWhiteSpace(subtitleGroup))
  488. {
  489. AddSubtitles(state, subtitleStreams, builder);
  490. }
  491. return builder.ToString();
  492. }
  493. private string ReplaceBitrate(string url, int oldValue, int newValue)
  494. {
  495. return url.Replace(
  496. "videobitrate=" + oldValue.ToString(UsCulture),
  497. "videobitrate=" + newValue.ToString(UsCulture),
  498. StringComparison.OrdinalIgnoreCase);
  499. }
  500. private void AddSubtitles(StreamState state, IEnumerable<MediaStream> subtitles, StringBuilder builder)
  501. {
  502. var selectedIndex = state.SubtitleStream == null ? (int?)null : state.SubtitleStream.Index;
  503. foreach (var stream in subtitles)
  504. {
  505. const string format = "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"subs\",NAME=\"{0}\",DEFAULT={1},FORCED={2},URI=\"{3}\",LANGUAGE=\"{4}\"";
  506. var name = stream.Language;
  507. var isDefault = selectedIndex.HasValue && selectedIndex.Value == stream.Index;
  508. var isForced = stream.IsForced;
  509. if (string.IsNullOrWhiteSpace(name)) name = stream.Codec ?? "Unknown";
  510. var url = string.Format("{0}/Subtitles/{1}/subtitles.m3u8?SegmentLength={2}",
  511. state.Request.MediaSourceId,
  512. stream.Index.ToString(UsCulture),
  513. 30.ToString(UsCulture));
  514. var line = string.Format(format,
  515. name,
  516. isDefault ? "YES" : "NO",
  517. isForced ? "YES" : "NO",
  518. url,
  519. stream.Language ?? "Unknown");
  520. builder.AppendLine(line);
  521. }
  522. }
  523. private bool EnableAdaptiveBitrateStreaming(StreamState state, bool isLiveStream)
  524. {
  525. // Within the local network this will likely do more harm than good.
  526. if (Request.IsLocal || NetworkManager.IsInLocalNetwork(Request.RemoteIp))
  527. {
  528. return false;
  529. }
  530. var request = state.Request as IMasterHlsRequest;
  531. if (request != null && !request.EnableAdaptiveBitrateStreaming)
  532. {
  533. return false;
  534. }
  535. if (isLiveStream || string.IsNullOrWhiteSpace(state.MediaPath))
  536. {
  537. // Opening live streams is so slow it's not even worth it
  538. return false;
  539. }
  540. if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  541. {
  542. return false;
  543. }
  544. if (string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
  545. {
  546. return false;
  547. }
  548. if (!state.IsOutputVideo)
  549. {
  550. return false;
  551. }
  552. // Having problems in android
  553. return false;
  554. //return state.VideoRequest.VideoBitRate.HasValue;
  555. }
  556. private void AppendPlaylist(StringBuilder builder, string url, int bitrate, string subtitleGroup)
  557. {
  558. var header = "#EXT-X-STREAM-INF:BANDWIDTH=" + bitrate.ToString(UsCulture);
  559. if (!string.IsNullOrWhiteSpace(subtitleGroup))
  560. {
  561. header += string.Format(",SUBTITLES=\"{0}\"", subtitleGroup);
  562. }
  563. builder.AppendLine(header);
  564. builder.AppendLine(url);
  565. }
  566. private int GetBitrateVariation(int bitrate)
  567. {
  568. // By default, vary by just 50k
  569. var variation = 50000;
  570. if (bitrate >= 10000000)
  571. {
  572. variation = 2000000;
  573. }
  574. else if (bitrate >= 5000000)
  575. {
  576. variation = 1500000;
  577. }
  578. else if (bitrate >= 3000000)
  579. {
  580. variation = 1000000;
  581. }
  582. else if (bitrate >= 2000000)
  583. {
  584. variation = 500000;
  585. }
  586. else if (bitrate >= 1000000)
  587. {
  588. variation = 300000;
  589. }
  590. else if (bitrate >= 600000)
  591. {
  592. variation = 200000;
  593. }
  594. else if (bitrate >= 400000)
  595. {
  596. variation = 100000;
  597. }
  598. return variation;
  599. }
  600. private async Task<object> GetVariantPlaylistInternal(StreamRequest request, bool isOutputVideo, string name)
  601. {
  602. var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
  603. var segmentLengths = GetSegmentLengths(state);
  604. var builder = new StringBuilder();
  605. builder.AppendLine("#EXTM3U");
  606. builder.AppendLine("#EXT-X-VERSION:3");
  607. builder.AppendLine("#EXT-X-TARGETDURATION:" + Math.Ceiling((segmentLengths.Length > 0 ? segmentLengths.Max() : state.SegmentLength)).ToString(UsCulture));
  608. builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
  609. var queryStringIndex = Request.RawUrl.IndexOf('?');
  610. var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
  611. var index = 0;
  612. foreach (var length in segmentLengths)
  613. {
  614. builder.AppendLine("#EXTINF:" + length.ToString("0.000000", UsCulture) + ",");
  615. builder.AppendLine(string.Format("hlsdynamic/{0}/{1}{2}{3}",
  616. name,
  617. index.ToString(UsCulture),
  618. GetSegmentFileExtension(isOutputVideo),
  619. queryString));
  620. index++;
  621. }
  622. builder.AppendLine("#EXT-X-ENDLIST");
  623. var playlistText = builder.ToString();
  624. return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
  625. }
  626. protected override string GetAudioArguments(StreamState state)
  627. {
  628. var codec = GetAudioEncoder(state);
  629. if (!state.IsOutputVideo)
  630. {
  631. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  632. {
  633. return "-acodec copy";
  634. }
  635. var audioTranscodeParams = new List<string>();
  636. audioTranscodeParams.Add("-acodec " + codec);
  637. if (state.OutputAudioBitrate.HasValue)
  638. {
  639. audioTranscodeParams.Add("-ab " + state.OutputAudioBitrate.Value.ToString(UsCulture));
  640. }
  641. if (state.OutputAudioChannels.HasValue)
  642. {
  643. audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(UsCulture));
  644. }
  645. if (state.OutputAudioSampleRate.HasValue)
  646. {
  647. audioTranscodeParams.Add("-ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture));
  648. }
  649. audioTranscodeParams.Add("-vn");
  650. return string.Join(" ", audioTranscodeParams.ToArray());
  651. }
  652. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  653. {
  654. return "-codec:a:0 copy";
  655. }
  656. var args = "-codec:a:0 " + codec;
  657. var channels = state.OutputAudioChannels;
  658. if (channels.HasValue)
  659. {
  660. args += " -ac " + channels.Value;
  661. }
  662. var bitrate = state.OutputAudioBitrate;
  663. if (bitrate.HasValue)
  664. {
  665. args += " -ab " + bitrate.Value.ToString(UsCulture);
  666. }
  667. args += " " + GetAudioFilterParam(state, true);
  668. return args;
  669. }
  670. protected override string GetVideoArguments(StreamState state)
  671. {
  672. if (!state.IsOutputVideo)
  673. {
  674. return string.Empty;
  675. }
  676. var codec = GetVideoEncoder(state);
  677. var args = "-codec:v:0 " + codec;
  678. if (state.EnableMpegtsM2TsMode)
  679. {
  680. args += " -mpegts_m2ts_mode 1";
  681. }
  682. // See if we can save come cpu cycles by avoiding encoding
  683. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  684. {
  685. if (state.VideoStream != null && IsH264(state.VideoStream))
  686. {
  687. args += " -bsf:v h264_mp4toannexb";
  688. }
  689. args += " -flags -global_header -sc_threshold 0";
  690. }
  691. else
  692. {
  693. var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
  694. state.SegmentLength.ToString(UsCulture));
  695. var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
  696. args += " " + GetVideoQualityParam(state, H264Encoder, true) + keyFrameArg;
  697. //args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0";
  698. // Add resolution params, if specified
  699. if (!hasGraphicalSubs)
  700. {
  701. args += GetOutputSizeParam(state, codec, EnableCopyTs(state));
  702. }
  703. // This is for internal graphical subs
  704. if (hasGraphicalSubs)
  705. {
  706. args += GetGraphicalSubtitleParam(state, codec);
  707. }
  708. args += " -flags -global_header -sc_threshold 0";
  709. }
  710. return args;
  711. }
  712. private bool EnableCopyTs(StreamState state)
  713. {
  714. return state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream;
  715. }
  716. protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
  717. {
  718. var threads = GetNumberOfThreads(state, false);
  719. var inputModifier = GetInputModifier(state, false);
  720. // If isEncoding is true we're actually starting ffmpeg
  721. var startNumberParam = isEncoding ? GetStartNumber(state).ToString(UsCulture) : "0";
  722. var toTimeParam = string.Empty;
  723. var timestampOffsetParam = string.Empty;
  724. if (state.IsOutputVideo && !EnableCopyTs(state) && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) && (state.Request.StartTimeTicks ?? 0) > 0)
  725. {
  726. timestampOffsetParam = " -output_ts_offset " + MediaEncoder.GetTimeParameter(state.Request.StartTimeTicks ?? 0).ToString(CultureInfo.InvariantCulture);
  727. }
  728. var mapArgs = state.IsOutputVideo ? GetMapArgs(state) : string.Empty;
  729. //var outputTsArg = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d" + GetSegmentFileExtension(state);
  730. //return string.Format("{0} {11} {1}{10} -map_metadata -1 -threads {2} {3} {4} {5} -f segment -segment_time {6} -segment_format mpegts -segment_list_type m3u8 -segment_start_number {7} -segment_list \"{8}\" -y \"{9}\"",
  731. // inputModifier,
  732. // GetInputArgument(state),
  733. // threads,
  734. // mapArgs,
  735. // GetVideoArguments(state),
  736. // GetAudioArguments(state),
  737. // state.SegmentLength.ToString(UsCulture),
  738. // startNumberParam,
  739. // outputPath,
  740. // outputTsArg,
  741. // slowSeekParam,
  742. // toTimeParam
  743. // ).Trim();
  744. return string.Format("{0}{11} {1} -map_metadata -1 -threads {2} {3} {4}{5} {6} -hls_time {7} -start_number {8} -hls_list_size {9} -y \"{10}\"",
  745. inputModifier,
  746. GetInputArgument(state),
  747. threads,
  748. mapArgs,
  749. GetVideoArguments(state),
  750. timestampOffsetParam,
  751. GetAudioArguments(state),
  752. state.SegmentLength.ToString(UsCulture),
  753. startNumberParam,
  754. state.HlsListSize.ToString(UsCulture),
  755. outputPath,
  756. toTimeParam
  757. ).Trim();
  758. }
  759. protected override bool EnableThrottling(StreamState state)
  760. {
  761. return true;
  762. }
  763. /// <summary>
  764. /// Gets the segment file extension.
  765. /// </summary>
  766. /// <param name="state">The state.</param>
  767. /// <returns>System.String.</returns>
  768. protected override string GetSegmentFileExtension(StreamState state)
  769. {
  770. return GetSegmentFileExtension(state.IsOutputVideo);
  771. }
  772. protected string GetSegmentFileExtension(bool isOutputVideo)
  773. {
  774. return isOutputVideo ? ".ts" : ".ts";
  775. }
  776. protected override bool CanStreamCopyVideo(VideoStreamRequest request, MediaStream videoStream)
  777. {
  778. if (videoStream.KeyFrames == null || videoStream.KeyFrames.Count == 0)
  779. {
  780. Logger.Debug("Cannot stream copy video due to missing keyframe info");
  781. return false;
  782. }
  783. var previousSegment = 0;
  784. foreach (var frame in videoStream.KeyFrames)
  785. {
  786. var length = frame - previousSegment;
  787. // Don't allow really long segments because this could result in long download times
  788. if (length > 10000)
  789. {
  790. Logger.Debug("Cannot stream copy video due to long segment length of {0}ms", length);
  791. return false;
  792. }
  793. previousSegment = frame;
  794. }
  795. return base.CanStreamCopyVideo(request, videoStream);
  796. }
  797. }
  798. }