123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- using MediaBrowser.Common.IO;
- using MediaBrowser.Controller.Channels;
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Dlna;
- using MediaBrowser.Controller.Library;
- using MediaBrowser.Controller.LiveTv;
- using MediaBrowser.Controller.MediaEncoding;
- using MediaBrowser.Model.IO;
- using ServiceStack;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MediaBrowser.Api.Playback.Hls
- {
- [Route("/Videos/{Id}/master.m3u8", "GET")]
- [Api(Description = "Gets a video stream using HTTP live streaming.")]
- public class GetMasterHlsVideoStream : VideoStreamRequest
- {
- }
- [Route("/Videos/{Id}/main.m3u8", "GET")]
- [Api(Description = "Gets a video stream using HTTP live streaming.")]
- public class GetMainHlsVideoStream : VideoStreamRequest
- {
- }
- /// <summary>
- /// Class GetHlsVideoSegment
- /// </summary>
- [Route("/Videos/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.ts", "GET")]
- [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
- public class GetDynamicHlsVideoSegment : VideoStreamRequest
- {
- public string PlaylistId { get; set; }
- /// <summary>
- /// Gets or sets the segment id.
- /// </summary>
- /// <value>The segment id.</value>
- public string SegmentId { get; set; }
- }
- public class DynamicHlsService : BaseHlsService
- {
- public DynamicHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, ILiveTvManager liveTvManager, IDlnaManager dlnaManager, IChannelManager channelManager, ISubtitleEncoder subtitleEncoder)
- : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, liveTvManager, dlnaManager, channelManager, subtitleEncoder)
- {
- }
- public object Get(GetMasterHlsVideoStream request)
- {
- if (string.Equals(request.AudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
- {
- throw new ArgumentException("Audio codec copy is not allowed here.");
- }
- if (string.Equals(request.VideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
- {
- throw new ArgumentException("Video codec copy is not allowed here.");
- }
- var result = GetAsync(request).Result;
- return result;
- }
- public object Get(GetMainHlsVideoStream request)
- {
- var result = GetPlaylistAsync(request, "main").Result;
- // Get the transcoding started
- //var start = GetStartNumber(request);
- //var segment = GetDynamicSegment(request, start.ToString(UsCulture)).Result;
- return result;
- }
- public object Get(GetDynamicHlsVideoSegment request)
- {
- return GetDynamicSegment(request, request.SegmentId).Result;
- }
- private async Task<object> GetDynamicSegment(VideoStreamRequest request, string segmentId)
- {
- if ((request.StartTimeTicks ?? 0) > 0)
- {
- throw new ArgumentException("StartTimeTicks is not allowed.");
- }
- var cancellationTokenSource = new CancellationTokenSource();
- var cancellationToken = cancellationTokenSource.Token;
- var index = int.Parse(segmentId, NumberStyles.Integer, UsCulture);
- var state = await GetState(request, cancellationToken).ConfigureAwait(false);
- var playlistPath = Path.ChangeExtension(state.OutputFilePath, ".m3u8");
- var segmentPath = GetSegmentPath(playlistPath, index);
- if (File.Exists(segmentPath))
- {
- ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
- return await GetSegmentResult(playlistPath, segmentPath, index, cancellationToken).ConfigureAwait(false);
- }
- await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
- try
- {
- if (File.Exists(segmentPath))
- {
- ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
- return await GetSegmentResult(playlistPath, segmentPath, index, cancellationToken).ConfigureAwait(false);
- }
- else
- {
- var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath);
- if (currentTranscodingIndex == null || index < currentTranscodingIndex.Value || (index - currentTranscodingIndex.Value) > 4)
- {
- // If the playlist doesn't already exist, startup ffmpeg
- try
- {
- await ApiEntryPoint.Instance.KillTranscodingJobs(state.Request.DeviceId, TranscodingJobType.Hls, p => !string.Equals(p, playlistPath, StringComparison.OrdinalIgnoreCase), false).ConfigureAwait(false);
- if (currentTranscodingIndex.HasValue)
- {
- DeleteLastFile(playlistPath, 0);
- }
- var startSeconds = index * state.SegmentLength;
- request.StartTimeTicks = TimeSpan.FromSeconds(startSeconds).Ticks;
- await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false);
- }
- catch
- {
- state.Dispose();
- throw;
- }
- await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false);
- }
- }
- }
- finally
- {
- ApiEntryPoint.Instance.TranscodingStartLock.Release();
- }
- Logger.Info("waiting for {0}", segmentPath);
- while (!File.Exists(segmentPath))
- {
- await Task.Delay(50, cancellationToken).ConfigureAwait(false);
- }
- Logger.Info("returning {0}", segmentPath);
- return await GetSegmentResult(playlistPath, segmentPath, index, cancellationToken).ConfigureAwait(false);
- }
- public int? GetCurrentTranscodingIndex(string playlist)
- {
- var file = GetLastTranscodingFile(playlist, FileSystem);
- if (file == null)
- {
- return null;
- }
- var playlistFilename = Path.GetFileNameWithoutExtension(playlist);
- var indexString = Path.GetFileNameWithoutExtension(file.Name).Substring(playlistFilename.Length);
- return int.Parse(indexString, NumberStyles.Integer, UsCulture);
- }
- private void DeleteLastFile(string path, int retryCount)
- {
- if (retryCount >= 5)
- {
- return;
- }
- var file = GetLastTranscodingFile(path, FileSystem);
- if (file != null)
- {
- try
- {
- File.Delete(file.FullName);
- }
- catch (IOException ex)
- {
- Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, file.FullName);
- Thread.Sleep(100);
- DeleteLastFile(path, retryCount + 1);
- }
- catch (Exception ex)
- {
- Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, file.FullName);
- }
- }
- }
- private static FileInfo GetLastTranscodingFile(string playlist, IFileSystem fileSystem)
- {
- var folder = Path.GetDirectoryName(playlist);
- try
- {
- return new DirectoryInfo(folder)
- .EnumerateFiles("*", SearchOption.TopDirectoryOnly)
- .Where(i => string.Equals(i.Extension, ".ts", StringComparison.OrdinalIgnoreCase))
- .OrderByDescending(fileSystem.GetLastWriteTimeUtc)
- .FirstOrDefault();
- }
- catch (DirectoryNotFoundException)
- {
- return null;
- }
- }
- protected override int GetStartNumber(StreamState state)
- {
- return GetStartNumber(state.VideoRequest);
- }
- private int GetStartNumber(VideoStreamRequest request)
- {
- var segmentId = "0";
- var segmentRequest = request as GetDynamicHlsVideoSegment;
- if (segmentRequest != null)
- {
- segmentId = segmentRequest.SegmentId;
- }
- return int.Parse(segmentId, NumberStyles.Integer, UsCulture);
- }
- private string GetSegmentPath(string playlist, int index)
- {
- var folder = Path.GetDirectoryName(playlist);
- var filename = Path.GetFileNameWithoutExtension(playlist);
- return Path.Combine(folder, filename + index.ToString(UsCulture) + ".ts");
- }
- private async Task<object> GetSegmentResult(string playlistPath, string segmentPath, int segmentIndex, CancellationToken cancellationToken)
- {
- // If all transcoding has completed, just return immediately
- if (!IsTranscoding(playlistPath))
- {
- return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
- }
- var segmentFilename = Path.GetFileName(segmentPath);
- // If it appears in the playlist, it's done
- if (File.ReadAllText(playlistPath).IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
- {
- return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
- }
- // if a different file is encoding, it's done
- //var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath);
- //if (currentTranscodingIndex > segmentIndex)
- //{
- // return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
- //}
- // Wait for the file to stop being written to, then stream it
- var length = new FileInfo(segmentPath).Length;
- var eofCount = 0;
- while (eofCount < 10)
- {
- var info = new FileInfo(segmentPath);
- if (!info.Exists)
- {
- break;
- }
- var newLength = info.Length;
- if (newLength == length)
- {
- eofCount++;
- }
- else
- {
- eofCount = 0;
- }
- length = newLength;
- await Task.Delay(100, cancellationToken).ConfigureAwait(false);
- }
- return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
- }
- private bool IsTranscoding(string playlistPath)
- {
- var job = ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType);
- return job != null && !job.HasExited;
- }
- private async Task<object> GetAsync(GetMasterHlsVideoStream request)
- {
- var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
- var audioBitrate = state.OutputAudioBitrate ?? 0;
- var videoBitrate = state.OutputVideoBitrate ?? 0;
- var playlistText = GetMasterPlaylistFileText(state, videoBitrate + audioBitrate);
- return ResultFactory.GetResult(playlistText, Common.Net.MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
- }
- private string GetMasterPlaylistFileText(StreamState state, int totalBitrate)
- {
- var builder = new StringBuilder();
- builder.AppendLine("#EXTM3U");
- var queryStringIndex = Request.RawUrl.IndexOf('?');
- var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
- // Main stream
- var playlistUrl = (state.RunTimeTicks ?? 0) > 0 ? "main.m3u8" : "live.m3u8";
- playlistUrl += queryString;
- AppendPlaylist(builder, playlistUrl, totalBitrate);
- if (EnableAdaptiveBitrateStreaming(state))
- {
- var requestedVideoBitrate = state.VideoRequest.VideoBitRate.Value;
- // By default, vary by just 200k
- var variation = GetBitrateVariation(totalBitrate);
- var newBitrate = totalBitrate - variation;
- AppendPlaylist(builder, playlistUrl.Replace(requestedVideoBitrate.ToString(UsCulture), (requestedVideoBitrate - variation).ToString(UsCulture)), newBitrate);
- variation *= 2;
- newBitrate = totalBitrate - variation;
- AppendPlaylist(builder, playlistUrl.Replace(requestedVideoBitrate.ToString(UsCulture), (requestedVideoBitrate - variation).ToString(UsCulture)), newBitrate);
- }
- return builder.ToString();
- }
- private bool EnableAdaptiveBitrateStreaming(StreamState state)
- {
- if (string.IsNullOrWhiteSpace(state.MediaPath))
- {
- // Opening live streams is so slow it's not even worth it
- return false;
- }
- return state.VideoRequest.VideoBitRate.HasValue;
- }
- private void AppendPlaylist(StringBuilder builder, string url, int bitrate)
- {
- builder.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=" + bitrate.ToString(UsCulture));
- builder.AppendLine(url);
- }
- private int GetBitrateVariation(int bitrate)
- {
- // By default, vary by just 100k
- var variation = 100000;
- if (bitrate >= 10000000)
- {
- variation = 2000000;
- }
- else if (bitrate >= 5000000)
- {
- variation = 1500000;
- }
- else if (bitrate >= 3000000)
- {
- variation = 1000000;
- }
- else if (bitrate >= 2000000)
- {
- variation = 500000;
- }
- else if (bitrate >= 1000000)
- {
- variation = 300000;
- }
- else if (bitrate >= 600000)
- {
- variation = 200000;
- }
- return variation;
- }
- private async Task<object> GetPlaylistAsync(VideoStreamRequest request, string name)
- {
- var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
- var builder = new StringBuilder();
- builder.AppendLine("#EXTM3U");
- builder.AppendLine("#EXT-X-VERSION:3");
- builder.AppendLine("#EXT-X-TARGETDURATION:" + state.SegmentLength.ToString(UsCulture));
- builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
- builder.AppendLine("#EXT-X-ALLOW-CACHE:NO");
- var queryStringIndex = Request.RawUrl.IndexOf('?');
- var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
- var seconds = TimeSpan.FromTicks(state.RunTimeTicks ?? 0).TotalSeconds;
- var index = 0;
- while (seconds > 0)
- {
- var length = seconds >= state.SegmentLength ? state.SegmentLength : seconds;
- builder.AppendLine("#EXTINF:" + length.ToString(UsCulture) + ",");
- builder.AppendLine(string.Format("hlsdynamic/{0}/{1}.ts{2}",
- name,
- index.ToString(UsCulture),
- queryString));
- seconds -= state.SegmentLength;
- index++;
- }
- builder.AppendLine("#EXT-X-ENDLIST");
- var playlistText = builder.ToString();
- return ResultFactory.GetResult(playlistText, Common.Net.MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
- }
- protected override string GetAudioArguments(StreamState state)
- {
- var codec = state.OutputAudioCodec;
- if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
- {
- return "-codec:a:0 copy";
- }
- var args = "-codec:a:0 " + codec;
- var channels = state.OutputAudioChannels;
- if (channels.HasValue)
- {
- args += " -ac " + channels.Value;
- }
- var bitrate = state.OutputAudioBitrate;
- if (bitrate.HasValue)
- {
- args += " -ab " + bitrate.Value.ToString(UsCulture);
- }
- args += " " + GetAudioFilterParam(state, true);
- return args;
- }
- protected override string GetVideoArguments(StreamState state)
- {
- var codec = state.OutputVideoCodec;
- // See if we can save come cpu cycles by avoiding encoding
- if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
- {
- // TOOD: Switch to -bsf dump_extra?
- return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
- }
- var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
- state.SegmentLength.ToString(UsCulture));
- var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
- var args = "-codec:v:0 " + codec + " " + GetVideoQualityParam(state, "libx264", true) + keyFrameArg;
- // Add resolution params, if specified
- if (!hasGraphicalSubs)
- {
- args += GetOutputSizeParam(state, codec, CancellationToken.None, false);
- }
- // This is for internal graphical subs
- if (hasGraphicalSubs)
- {
- args += GetInternalGraphicalSubtitleParam(state, codec);
- }
- return args;
- }
- /// <summary>
- /// Gets the command line arguments.
- /// </summary>
- /// <param name="outputPath">The output path.</param>
- /// <param name="state">The state.</param>
- /// <param name="isEncoding">if set to <c>true</c> [is encoding].</param>
- /// <returns>System.String.</returns>
- protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
- {
- var threads = GetNumberOfThreads(state, false);
- var inputModifier = GetInputModifier(state);
- // If isEncoding is true we're actually starting ffmpeg
- var startNumberParam = isEncoding ? GetStartNumber(state).ToString(UsCulture) : "0";
- var args = string.Format("{0} -i {1} -map_metadata -1 -threads {2} {3} {4} -copyts -flags -global_header {5} -hls_time {6} -start_number {7} -hls_list_size {8} -y \"{9}\"",
- inputModifier,
- GetInputArgument(state),
- threads,
- GetMapArgs(state),
- GetVideoArguments(state),
- GetAudioArguments(state),
- state.SegmentLength.ToString(UsCulture),
- startNumberParam,
- state.HlsListSize.ToString(UsCulture),
- outputPath
- ).Trim();
- return args;
- }
- /// <summary>
- /// Gets the segment file extension.
- /// </summary>
- /// <param name="state">The state.</param>
- /// <returns>System.String.</returns>
- protected override string GetSegmentFileExtension(StreamState state)
- {
- return ".ts";
- }
- protected override TranscodingJobType TranscodingJobType
- {
- get
- {
- return TranscodingJobType.Hls;
- }
- }
- }
- }
|