2019-01-13 15:01:16 -05:00
using System ;
2019-01-13 14:24:58 -05:00
using System.Collections.Generic ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
using MediaBrowser.Common.Net ;
2018-12-14 04:40:55 -05:00
using MediaBrowser.Controller.Configuration ;
using MediaBrowser.Controller.Devices ;
using MediaBrowser.Controller.Dlna ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.MediaEncoding ;
using MediaBrowser.Controller.Net ;
2019-01-13 14:24:58 -05:00
using MediaBrowser.Model.Configuration ;
2018-12-14 04:40:55 -05:00
using MediaBrowser.Model.Dlna ;
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.IO ;
using MediaBrowser.Model.Serialization ;
using MediaBrowser.Model.Services ;
2018-12-14 18:48:06 -05:00
using Microsoft.Extensions.Logging ;
2019-01-13 14:24:58 -05:00
using MimeTypes = MediaBrowser . Model . Net . MimeTypes ;
2018-12-14 04:40:55 -05:00
namespace MediaBrowser.Api.Playback.Hls
{
/// <summary>
/// Options is needed for chromecast. Threw Head in there since it's related
/// </summary>
[Route("/Videos/{Id}/master.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
[Route("/Videos/{Id}/master.m3u8", "HEAD", Summary = "Gets a video stream using HTTP live streaming.")]
public class GetMasterHlsVideoPlaylist : VideoStreamRequest , IMasterHlsRequest
{
public bool EnableAdaptiveBitrateStreaming { get ; set ; }
public GetMasterHlsVideoPlaylist ( )
{
EnableAdaptiveBitrateStreaming = true ;
}
}
[Route("/Audio/{Id}/master.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")]
[Route("/Audio/{Id}/master.m3u8", "HEAD", Summary = "Gets an audio stream using HTTP live streaming.")]
public class GetMasterHlsAudioPlaylist : StreamRequest , IMasterHlsRequest
{
public bool EnableAdaptiveBitrateStreaming { get ; set ; }
public GetMasterHlsAudioPlaylist ( )
{
EnableAdaptiveBitrateStreaming = true ;
}
}
public interface IMasterHlsRequest
{
bool EnableAdaptiveBitrateStreaming { get ; set ; }
}
[Route("/Videos/{Id}/main.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
public class GetVariantHlsVideoPlaylist : VideoStreamRequest
{
}
[Route("/Audio/{Id}/main.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")]
public class GetVariantHlsAudioPlaylist : StreamRequest
{
}
[Route("/Videos/{Id}/hls1/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")]
public class GetHlsVideoSegment : VideoStreamRequest
{
public string PlaylistId { get ; set ; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get ; set ; }
}
[Route("/Audio/{Id}/hls1/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")]
public class GetHlsAudioSegment : StreamRequest
{
public string PlaylistId { get ; set ; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get ; set ; }
}
[Authenticated]
public class DynamicHlsService : BaseHlsService
{
2019-02-02 17:55:47 -05:00
public DynamicHlsService (
2020-05-14 20:11:34 -04:00
ILogger < DynamicHlsService > logger ,
2019-11-17 17:05:39 -05:00
IServerConfigurationManager serverConfigurationManager ,
IHttpResultFactory httpResultFactory ,
2019-02-02 17:55:47 -05:00
IUserManager userManager ,
ILibraryManager libraryManager ,
IIsoManager isoManager ,
IMediaEncoder mediaEncoder ,
IFileSystem fileSystem ,
IDlnaManager dlnaManager ,
IDeviceManager deviceManager ,
IMediaSourceManager mediaSourceManager ,
IJsonSerializer jsonSerializer ,
IAuthorizationContext authorizationContext ,
2019-10-26 16:53:53 -04:00
INetworkManager networkManager ,
EncodingHelper encodingHelper )
2019-11-17 17:05:39 -05:00
: base (
logger ,
serverConfigurationManager ,
httpResultFactory ,
2019-02-02 17:55:47 -05:00
userManager ,
libraryManager ,
isoManager ,
mediaEncoder ,
fileSystem ,
dlnaManager ,
deviceManager ,
mediaSourceManager ,
jsonSerializer ,
2019-10-26 16:53:53 -04:00
authorizationContext ,
encodingHelper )
2018-12-14 04:40:55 -05:00
{
NetworkManager = networkManager ;
}
protected INetworkManager NetworkManager { get ; private set ; }
public Task < object > Get ( GetMasterHlsVideoPlaylist request )
{
return GetMasterPlaylistInternal ( request , "GET" ) ;
}
public Task < object > Head ( GetMasterHlsVideoPlaylist request )
{
return GetMasterPlaylistInternal ( request , "HEAD" ) ;
}
public Task < object > Get ( GetMasterHlsAudioPlaylist request )
{
return GetMasterPlaylistInternal ( request , "GET" ) ;
}
public Task < object > Head ( GetMasterHlsAudioPlaylist request )
{
return GetMasterPlaylistInternal ( request , "HEAD" ) ;
}
public Task < object > Get ( GetVariantHlsVideoPlaylist request )
{
return GetVariantPlaylistInternal ( request , true , "main" ) ;
}
public Task < object > Get ( GetVariantHlsAudioPlaylist request )
{
return GetVariantPlaylistInternal ( request , false , "main" ) ;
}
public Task < object > Get ( GetHlsVideoSegment request )
{
return GetDynamicSegment ( request , request . SegmentId ) ;
}
public Task < object > Get ( GetHlsAudioSegment request )
{
return GetDynamicSegment ( request , request . SegmentId ) ;
}
private async Task < object > GetDynamicSegment ( StreamRequest request , string segmentId )
{
if ( ( request . StartTimeTicks ? ? 0 ) > 0 )
{
throw new ArgumentException ( "StartTimeTicks is not allowed." ) ;
}
var cancellationTokenSource = new CancellationTokenSource ( ) ;
var cancellationToken = cancellationTokenSource . Token ;
2019-06-05 14:59:06 -04:00
var requestedIndex = int . Parse ( segmentId , NumberStyles . Integer , CultureInfo . InvariantCulture ) ;
2018-12-14 04:40:55 -05:00
var state = await GetState ( request , cancellationToken ) . ConfigureAwait ( false ) ;
var playlistPath = Path . ChangeExtension ( state . OutputFilePath , ".m3u8" ) ;
var segmentPath = GetSegmentPath ( state , playlistPath , requestedIndex ) ;
var segmentExtension = GetSegmentFileExtension ( state . Request ) ;
TranscodingJob job = null ;
2019-01-26 16:59:53 -05:00
if ( File . Exists ( segmentPath ) )
2018-12-14 04:40:55 -05:00
{
job = ApiEntryPoint . Instance . OnTranscodeBeginRequest ( playlistPath , TranscodingJobType ) ;
2019-10-08 09:34:43 -04:00
Logger . LogDebug ( "returning {0} [it exists, try 1]" , segmentPath ) ;
2018-12-14 04:40:55 -05:00
return await GetSegmentResult ( state , playlistPath , segmentPath , segmentExtension , requestedIndex , job , cancellationToken ) . ConfigureAwait ( false ) ;
}
var transcodingLock = ApiEntryPoint . Instance . GetTranscodingLock ( playlistPath ) ;
await transcodingLock . WaitAsync ( cancellationTokenSource . Token ) . ConfigureAwait ( false ) ;
var released = false ;
var startTranscoding = false ;
try
{
2019-01-26 16:59:53 -05:00
if ( File . Exists ( segmentPath ) )
2018-12-14 04:40:55 -05:00
{
job = ApiEntryPoint . Instance . OnTranscodeBeginRequest ( playlistPath , TranscodingJobType ) ;
transcodingLock . Release ( ) ;
released = true ;
2019-10-08 09:34:43 -04:00
Logger . LogDebug ( "returning {0} [it exists, try 2]" , segmentPath ) ;
2018-12-14 04:40:55 -05:00
return await GetSegmentResult ( state , playlistPath , segmentPath , segmentExtension , requestedIndex , job , cancellationToken ) . ConfigureAwait ( false ) ;
}
else
{
var currentTranscodingIndex = GetCurrentTranscodingIndex ( playlistPath , segmentExtension ) ;
var segmentGapRequiringTranscodingChange = 24 / state . SegmentLength ;
if ( currentTranscodingIndex = = null )
{
2018-12-14 18:48:06 -05:00
Logger . LogDebug ( "Starting transcoding because currentTranscodingIndex=null" ) ;
2018-12-14 04:40:55 -05:00
startTranscoding = true ;
}
else if ( requestedIndex < currentTranscodingIndex . Value )
{
2018-12-14 18:48:06 -05:00
Logger . LogDebug ( "Starting transcoding because requestedIndex={0} and currentTranscodingIndex={1}" , requestedIndex , currentTranscodingIndex ) ;
2018-12-14 04:40:55 -05:00
startTranscoding = true ;
}
else if ( requestedIndex - currentTranscodingIndex . Value > segmentGapRequiringTranscodingChange )
{
2018-12-14 18:48:06 -05:00
Logger . LogDebug ( "Starting transcoding because segmentGap is {0} and max allowed gap is {1}. requestedIndex={2}" , requestedIndex - currentTranscodingIndex . Value , segmentGapRequiringTranscodingChange , requestedIndex ) ;
2018-12-14 04:40:55 -05:00
startTranscoding = true ;
}
if ( startTranscoding )
{
// If the playlist doesn't already exist, startup ffmpeg
try
{
2019-02-02 18:03:07 -05:00
await ApiEntryPoint . Instance . KillTranscodingJobs ( request . DeviceId , request . PlaySessionId , p = > false ) ;
2018-12-14 04:40:55 -05:00
if ( currentTranscodingIndex . HasValue )
{
DeleteLastFile ( playlistPath , segmentExtension , 0 ) ;
}
request . StartTimeTicks = GetStartPositionTicks ( state , requestedIndex ) ;
2019-10-03 11:50:39 -04:00
state . WaitForPath = segmentPath ;
2018-12-14 04:40:55 -05:00
job = await StartFfMpeg ( state , playlistPath , cancellationTokenSource ) . ConfigureAwait ( false ) ;
}
catch
{
state . Dispose ( ) ;
throw ;
}
//await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false);
}
else
{
job = ApiEntryPoint . Instance . OnTranscodeBeginRequest ( playlistPath , TranscodingJobType ) ;
if ( job . TranscodingThrottler ! = null )
{
2019-02-02 18:03:07 -05:00
await job . TranscodingThrottler . UnpauseTranscoding ( ) ;
2018-12-14 04:40:55 -05:00
}
}
}
}
finally
{
if ( ! released )
{
transcodingLock . Release ( ) ;
}
}
2018-12-14 18:48:06 -05:00
//Logger.LogInformation("waiting for {0}", segmentPath);
2018-12-14 04:40:55 -05:00
//while (!File.Exists(segmentPath))
//{
// await Task.Delay(50, cancellationToken).ConfigureAwait(false);
//}
2019-10-08 09:34:43 -04:00
Logger . LogDebug ( "returning {0} [general case]" , segmentPath ) ;
2020-04-05 12:45:01 -04:00
job ? ? = ApiEntryPoint . Instance . OnTranscodeBeginRequest ( playlistPath , TranscodingJobType ) ;
2018-12-14 04:40:55 -05:00
return await GetSegmentResult ( state , playlistPath , segmentPath , segmentExtension , requestedIndex , job , cancellationToken ) . ConfigureAwait ( false ) ;
}
private const int BufferSize = 81920 ;
private long GetStartPositionTicks ( StreamState state , int requestedIndex )
{
double startSeconds = 0 ;
var lengths = GetSegmentLengths ( state ) ;
if ( requestedIndex > = lengths . Length )
{
var msg = string . Format ( "Invalid segment index requested: {0} - Segment count: {1}" , requestedIndex , lengths . Length ) ;
throw new ArgumentException ( msg ) ;
}
for ( var i = 0 ; i < requestedIndex ; i + + )
{
startSeconds + = lengths [ i ] ;
}
var position = TimeSpan . FromSeconds ( startSeconds ) . Ticks ;
return position ;
}
private long GetEndPositionTicks ( StreamState state , int requestedIndex )
{
double startSeconds = 0 ;
var lengths = GetSegmentLengths ( state ) ;
if ( requestedIndex > = lengths . Length )
{
var msg = string . Format ( "Invalid segment index requested: {0} - Segment count: {1}" , requestedIndex , lengths . Length ) ;
throw new ArgumentException ( msg ) ;
}
for ( var i = 0 ; i < = requestedIndex ; i + + )
{
startSeconds + = lengths [ i ] ;
}
var position = TimeSpan . FromSeconds ( startSeconds ) . Ticks ;
return position ;
}
private double [ ] GetSegmentLengths ( StreamState state )
{
var result = new List < double > ( ) ;
var ticks = state . RunTimeTicks ? ? 0 ;
var segmentLengthTicks = TimeSpan . FromSeconds ( state . SegmentLength ) . Ticks ;
while ( ticks > 0 )
{
var length = ticks > = segmentLengthTicks ? segmentLengthTicks : ticks ;
result . Add ( TimeSpan . FromTicks ( length ) . TotalSeconds ) ;
ticks - = length ;
}
return result . ToArray ( ) ;
}
public int? GetCurrentTranscodingIndex ( string playlist , string segmentExtension )
{
var job = ApiEntryPoint . Instance . GetTranscodingJob ( playlist , TranscodingJobType ) ;
if ( job = = null | | job . HasExited )
{
return null ;
}
var file = GetLastTranscodingFile ( playlist , segmentExtension , FileSystem ) ;
if ( file = = null )
{
return null ;
}
var playlistFilename = Path . GetFileNameWithoutExtension ( playlist ) ;
var indexString = Path . GetFileNameWithoutExtension ( file . Name ) . Substring ( playlistFilename . Length ) ;
2019-06-05 14:59:06 -04:00
return int . Parse ( indexString , NumberStyles . Integer , CultureInfo . InvariantCulture ) ;
2018-12-14 04:40:55 -05:00
}
private void DeleteLastFile ( string playlistPath , string segmentExtension , int retryCount )
{
var file = GetLastTranscodingFile ( playlistPath , segmentExtension , FileSystem ) ;
if ( file ! = null )
{
DeleteFile ( file . FullName , retryCount ) ;
}
}
private void DeleteFile ( string path , int retryCount )
{
if ( retryCount > = 5 )
{
return ;
}
2018-12-14 18:48:06 -05:00
Logger . LogDebug ( "Deleting partial HLS file {path}" , path ) ;
2018-12-14 04:40:55 -05:00
try
{
FileSystem . DeleteFile ( path ) ;
}
catch ( IOException ex )
{
2018-12-20 07:11:26 -05:00
Logger . LogError ( ex , "Error deleting partial stream file(s) {path}" , path ) ;
2018-12-14 04:40:55 -05:00
var task = Task . Delay ( 100 ) ;
Task . WaitAll ( task ) ;
DeleteFile ( path , retryCount + 1 ) ;
}
catch ( Exception ex )
{
2018-12-20 07:11:26 -05:00
Logger . LogError ( ex , "Error deleting partial stream file(s) {path}" , path ) ;
2018-12-14 04:40:55 -05:00
}
}
private static FileSystemMetadata GetLastTranscodingFile ( string playlist , string segmentExtension , IFileSystem fileSystem )
{
2019-01-26 15:47:11 -05:00
var folder = Path . GetDirectoryName ( playlist ) ;
2018-12-14 04:40:55 -05:00
var filePrefix = Path . GetFileNameWithoutExtension ( playlist ) ? ? string . Empty ;
try
{
return fileSystem . GetFiles ( folder , new [ ] { segmentExtension } , true , false )
. Where ( i = > Path . GetFileNameWithoutExtension ( i . Name ) . StartsWith ( filePrefix , StringComparison . OrdinalIgnoreCase ) )
. OrderByDescending ( fileSystem . GetLastWriteTimeUtc )
. FirstOrDefault ( ) ;
}
catch ( IOException )
{
return null ;
}
}
protected override int GetStartNumber ( StreamState state )
{
return GetStartNumber ( state . VideoRequest ) ;
}
private int GetStartNumber ( VideoStreamRequest request )
{
var segmentId = "0" ;
2020-04-05 12:07:43 -04:00
if ( request is GetHlsVideoSegment segmentRequest )
2018-12-14 04:40:55 -05:00
{
segmentId = segmentRequest . SegmentId ;
}
2019-06-05 14:59:06 -04:00
return int . Parse ( segmentId , NumberStyles . Integer , CultureInfo . InvariantCulture ) ;
2018-12-14 04:40:55 -05:00
}
private string GetSegmentPath ( StreamState state , string playlist , int index )
{
2019-01-26 15:47:11 -05:00
var folder = Path . GetDirectoryName ( playlist ) ;
2018-12-14 04:40:55 -05:00
var filename = Path . GetFileNameWithoutExtension ( playlist ) ;
2019-06-05 14:59:06 -04:00
return Path . Combine ( folder , filename + index . ToString ( CultureInfo . InvariantCulture ) + GetSegmentFileExtension ( state . Request ) ) ;
2018-12-14 04:40:55 -05:00
}
private async Task < object > GetSegmentResult ( StreamState state ,
string playlistPath ,
string segmentPath ,
string segmentExtension ,
int segmentIndex ,
TranscodingJob transcodingJob ,
CancellationToken cancellationToken )
{
2019-10-03 11:50:39 -04:00
var segmentExists = File . Exists ( segmentPath ) ;
if ( segmentExists )
2018-12-14 04:40:55 -05:00
{
2019-10-03 11:50:39 -04:00
if ( transcodingJob ! = null & & transcodingJob . HasExited )
{
// Transcoding job is over, so assume all existing files are ready
2019-10-08 09:34:43 -04:00
Logger . LogDebug ( "serving up {0} as transcode is over" , segmentPath ) ;
2019-10-03 11:50:39 -04:00
return await GetSegmentResult ( state , segmentPath , segmentIndex , transcodingJob ) . ConfigureAwait ( false ) ;
}
2018-12-14 04:40:55 -05:00
var currentTranscodingIndex = GetCurrentTranscodingIndex ( playlistPath , segmentExtension ) ;
// If requested segment is less than transcoding position, we can't transcode backwards, so assume it's ready
if ( segmentIndex < currentTranscodingIndex )
{
2019-10-09 05:55:09 -04:00
Logger . LogDebug ( "serving up {0} as transcode index {1} is past requested point {2}" , segmentPath , currentTranscodingIndex , segmentIndex ) ;
2018-12-14 04:40:55 -05:00
return await GetSegmentResult ( state , segmentPath , segmentIndex , transcodingJob ) . ConfigureAwait ( false ) ;
}
}
2019-10-03 11:50:39 -04:00
var nextSegmentPath = GetSegmentPath ( state , playlistPath , segmentIndex + 1 ) ;
2019-10-08 10:00:16 -04:00
if ( transcodingJob ! = null )
2018-12-14 04:40:55 -05:00
{
2019-10-08 10:00:16 -04:00
while ( ! cancellationToken . IsCancellationRequested & & ! transcodingJob . HasExited )
2018-12-14 04:40:55 -05:00
{
2019-10-08 10:00:16 -04:00
// To be considered ready, the segment file has to exist AND
// either the transcoding job should be done or next segment should also exist
if ( segmentExists )
2018-12-14 04:40:55 -05:00
{
2019-10-08 10:00:16 -04:00
if ( transcodingJob . HasExited | | File . Exists ( nextSegmentPath ) )
{
Logger . LogDebug ( "serving up {0} as it deemed ready" , segmentPath ) ;
return await GetSegmentResult ( state , segmentPath , segmentIndex , transcodingJob ) . ConfigureAwait ( false ) ;
}
2018-12-14 04:40:55 -05:00
}
2019-10-08 10:00:16 -04:00
else
2019-10-03 11:50:39 -04:00
{
2019-10-08 10:00:16 -04:00
segmentExists = File . Exists ( segmentPath ) ;
if ( segmentExists )
{
continue ; // avoid unnecessary waiting if segment just became available
}
2019-10-03 11:50:39 -04:00
}
2019-10-27 09:47:00 -04:00
2019-10-08 10:00:16 -04:00
await Task . Delay ( 100 , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
}
2019-10-08 10:00:16 -04:00
if ( ! File . Exists ( segmentPath ) )
{
Logger . LogWarning ( "cannot serve {0} as transcoding quit before we got there" , segmentPath ) ;
}
else
{
Logger . LogDebug ( "serving {0} as it's on disk and transcoding stopped" , segmentPath ) ;
}
2019-10-27 09:48:42 -04:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2019-10-08 09:34:43 -04:00
}
else
{
2019-10-08 10:00:16 -04:00
Logger . LogWarning ( "cannot serve {0} as it doesn't exist and no transcode is running" , segmentPath ) ;
2019-10-08 09:34:43 -04:00
}
2019-10-08 10:00:16 -04:00
2018-12-14 04:40:55 -05:00
return await GetSegmentResult ( state , segmentPath , segmentIndex , transcodingJob ) . ConfigureAwait ( false ) ;
}
private Task < object > GetSegmentResult ( StreamState state , string segmentPath , int index , TranscodingJob transcodingJob )
{
var segmentEndingPositionTicks = GetEndPositionTicks ( state , index ) ;
return ResultFactory . GetStaticFileResult ( Request , new StaticFileResultOptions
{
Path = segmentPath ,
2020-01-08 11:52:50 -05:00
FileShare = FileShare . ReadWrite ,
2018-12-14 04:40:55 -05:00
OnComplete = ( ) = >
{
2019-10-08 09:34:43 -04:00
Logger . LogDebug ( "finished serving {0}" , segmentPath ) ;
2018-12-14 04:40:55 -05:00
if ( transcodingJob ! = null )
{
transcodingJob . DownloadPositionTicks = Math . Max ( transcodingJob . DownloadPositionTicks ? ? segmentEndingPositionTicks , segmentEndingPositionTicks ) ;
ApiEntryPoint . Instance . OnTranscodeEndRequest ( transcodingJob ) ;
}
}
} ) ;
}
private async Task < object > GetMasterPlaylistInternal ( StreamRequest request , string method )
{
var state = await GetState ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
if ( string . IsNullOrEmpty ( request . MediaSourceId ) )
{
throw new ArgumentException ( "MediaSourceId is required" ) ;
}
var playlistText = string . Empty ;
if ( string . Equals ( method , "GET" , StringComparison . OrdinalIgnoreCase ) )
{
var audioBitrate = state . OutputAudioBitrate ? ? 0 ;
var videoBitrate = state . OutputVideoBitrate ? ? 0 ;
playlistText = GetMasterPlaylistFileText ( state , videoBitrate + audioBitrate ) ;
}
return ResultFactory . GetResult ( playlistText , MimeTypes . GetMimeType ( "playlist.m3u8" ) , new Dictionary < string , string > ( ) ) ;
}
private string GetMasterPlaylistFileText ( StreamState state , int totalBitrate )
{
var builder = new StringBuilder ( ) ;
builder . AppendLine ( "#EXTM3U" ) ;
var isLiveStream = state . IsSegmentedLiveStream ;
var queryStringIndex = Request . RawUrl . IndexOf ( '?' ) ;
var queryString = queryStringIndex = = - 1 ? string . Empty : Request . RawUrl . Substring ( queryStringIndex ) ;
// from universal audio service
if ( queryString . IndexOf ( "SegmentContainer" , StringComparison . OrdinalIgnoreCase ) = = - 1 & & ! string . IsNullOrWhiteSpace ( state . Request . SegmentContainer ) )
{
queryString + = "&SegmentContainer=" + state . Request . SegmentContainer ;
}
// from universal audio service
if ( ! string . IsNullOrWhiteSpace ( state . Request . TranscodeReasons ) & & queryString . IndexOf ( "TranscodeReasons=" , StringComparison . OrdinalIgnoreCase ) = = - 1 )
{
queryString + = "&TranscodeReasons=" + state . Request . TranscodeReasons ;
}
// Main stream
var playlistUrl = isLiveStream ? "live.m3u8" : "main.m3u8" ;
playlistUrl + = queryString ;
var request = state . Request ;
var subtitleStreams = state . MediaSource
. MediaStreams
. Where ( i = > i . IsTextSubtitleStream )
. ToList ( ) ;
var subtitleGroup = subtitleStreams . Count > 0 & &
request is GetMasterHlsVideoPlaylist & &
( state . SubtitleDeliveryMethod = = SubtitleDeliveryMethod . Hls | | state . VideoRequest . EnableSubtitlesInManifest ) ?
"subs" :
null ;
// If we're burning in subtitles then don't add additional subs to the manifest
if ( state . SubtitleStream ! = null & & state . SubtitleDeliveryMethod = = SubtitleDeliveryMethod . Encode )
{
subtitleGroup = null ;
}
if ( ! string . IsNullOrWhiteSpace ( subtitleGroup ) )
{
AddSubtitles ( state , subtitleStreams , builder ) ;
}
AppendPlaylist ( builder , state , playlistUrl , totalBitrate , subtitleGroup ) ;
if ( EnableAdaptiveBitrateStreaming ( state , isLiveStream ) )
{
var requestedVideoBitrate = state . VideoRequest = = null ? 0 : state . VideoRequest . VideoBitRate ? ? 0 ;
// By default, vary by just 200k
var variation = GetBitrateVariation ( totalBitrate ) ;
var newBitrate = totalBitrate - variation ;
var variantUrl = ReplaceBitrate ( playlistUrl , requestedVideoBitrate , requestedVideoBitrate - variation ) ;
AppendPlaylist ( builder , state , variantUrl , newBitrate , subtitleGroup ) ;
variation * = 2 ;
newBitrate = totalBitrate - variation ;
variantUrl = ReplaceBitrate ( playlistUrl , requestedVideoBitrate , requestedVideoBitrate - variation ) ;
AppendPlaylist ( builder , state , variantUrl , newBitrate , subtitleGroup ) ;
}
return builder . ToString ( ) ;
}
private string ReplaceBitrate ( string url , int oldValue , int newValue )
{
return url . Replace (
2019-06-05 14:59:06 -04:00
"videobitrate=" + oldValue . ToString ( CultureInfo . InvariantCulture ) ,
"videobitrate=" + newValue . ToString ( CultureInfo . InvariantCulture ) ,
2018-12-14 04:40:55 -05:00
StringComparison . OrdinalIgnoreCase ) ;
}
private void AddSubtitles ( StreamState state , IEnumerable < MediaStream > subtitles , StringBuilder builder )
{
var selectedIndex = state . SubtitleStream = = null | | state . SubtitleDeliveryMethod ! = SubtitleDeliveryMethod . Hls ? ( int? ) null : state . SubtitleStream . Index ;
foreach ( var stream in subtitles )
{
const string format = "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"subs\",NAME=\"{0}\",DEFAULT={1},FORCED={2},AUTOSELECT=YES,URI=\"{3}\",LANGUAGE=\"{4}\"" ;
var name = stream . DisplayTitle ;
var isDefault = selectedIndex . HasValue & & selectedIndex . Value = = stream . Index ;
var isForced = stream . IsForced ;
var url = string . Format ( "{0}/Subtitles/{1}/subtitles.m3u8?SegmentLength={2}&api_key={3}" ,
state . Request . MediaSourceId ,
2019-06-05 14:59:06 -04:00
stream . Index . ToString ( CultureInfo . InvariantCulture ) ,
30. ToString ( CultureInfo . InvariantCulture ) ,
2018-12-14 04:40:55 -05:00
AuthorizationContext . GetAuthorizationInfo ( Request ) . Token ) ;
var line = string . Format ( format ,
name ,
isDefault ? "YES" : "NO" ,
isForced ? "YES" : "NO" ,
url ,
stream . Language ? ? "Unknown" ) ;
builder . AppendLine ( line ) ;
}
}
private bool EnableAdaptiveBitrateStreaming ( StreamState state , bool isLiveStream )
{
// Within the local network this will likely do more harm than good.
if ( Request . IsLocal | | NetworkManager . IsInLocalNetwork ( Request . RemoteIp ) )
{
return false ;
}
2020-04-05 12:07:43 -04:00
if ( state . Request is IMasterHlsRequest request & & ! request . EnableAdaptiveBitrateStreaming )
2018-12-14 04:40:55 -05:00
{
return false ;
}
if ( isLiveStream | | string . IsNullOrWhiteSpace ( state . MediaPath ) )
{
// Opening live streams is so slow it's not even worth it
return false ;
}
2020-05-25 17:58:19 -04:00
if ( EncodingHelper . IsCopyCodec ( state . OutputVideoCodec ) )
2018-12-14 04:40:55 -05:00
{
return false ;
}
2020-05-25 17:58:19 -04:00
if ( EncodingHelper . IsCopyCodec ( state . OutputAudioCodec ) )
2018-12-14 04:40:55 -05:00
{
return false ;
}
if ( ! state . IsOutputVideo )
{
return false ;
}
// Having problems in android
return false ;
//return state.VideoRequest.VideoBitRate.HasValue;
}
2020-04-25 09:22:09 -04:00
/// <summary>
/// Get the H.26X level of the output video stream.
/// </summary>
/// <param name="state">StreamState of the current stream.</param>
/// <returns>H.26X level of the output video stream.</returns>
private int? GetOutputVideoCodecLevel ( StreamState state )
{
string levelString ;
2020-05-25 17:58:19 -04:00
if ( EncodingHelper . IsCopyCodec ( state . OutputVideoCodec )
2020-04-25 09:22:09 -04:00
& & state . VideoStream . Level . HasValue )
{
levelString = state . VideoStream ? . Level . ToString ( ) ;
}
else
{
levelString = state . GetRequestedLevel ( state . ActualOutputVideoCodec ) ;
}
if ( int . TryParse ( levelString , NumberStyles . Integer , CultureInfo . InvariantCulture , out var parsedLevel ) )
{
return parsedLevel ;
}
return null ;
}
2020-03-11 13:14:36 -04:00
/// <summary>
/// Gets a formatted string of the output audio codec, for use in the CODECS field.
/// </summary>
/// <seealso cref="AppendPlaylistCodecsField(StringBuilder, StreamState)"/>
2020-04-25 09:22:09 -04:00
/// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/>
2020-03-11 13:14:36 -04:00
/// <param name="state">StreamState of the current stream.</param>
/// <returns>Formatted audio codec string.</returns>
private string GetPlaylistAudioCodecs ( StreamState state )
{
if ( string . Equals ( state . ActualOutputAudioCodec , "aac" , StringComparison . OrdinalIgnoreCase ) )
{
string profile = state . GetRequestedProfiles ( "aac" ) . FirstOrDefault ( ) ;
return HlsCodecStringFactory . GetAACString ( profile ) ;
}
else if ( string . Equals ( state . ActualOutputAudioCodec , "mp3" , StringComparison . OrdinalIgnoreCase ) )
{
return HlsCodecStringFactory . GetMP3String ( ) ;
}
else if ( string . Equals ( state . ActualOutputAudioCodec , "ac3" , StringComparison . OrdinalIgnoreCase ) )
{
return HlsCodecStringFactory . GetAC3String ( ) ;
}
else if ( string . Equals ( state . ActualOutputAudioCodec , "eac3" , StringComparison . OrdinalIgnoreCase ) )
{
return HlsCodecStringFactory . GetEAC3String ( ) ;
}
return string . Empty ;
}
/// <summary>
/// Gets a formatted string of the output video codec, for use in the CODECS field.
/// </summary>
/// <seealso cref="AppendPlaylistCodecsField(StringBuilder, StreamState)"/>
/// <seealso cref="GetPlaylistAudioCodecs(StreamState)"/>
/// <param name="state">StreamState of the current stream.</param>
/// <returns>Formatted video codec string.</returns>
2020-04-25 09:22:09 -04:00
private string GetPlaylistVideoCodecs ( StreamState state , string codec , int level )
2020-03-11 13:14:36 -04:00
{
2020-04-25 09:22:09 -04:00
if ( level = = 0 )
{
// This is 0 when there's no requested H.26X level in the device profile
// and the source is not encoded in H.26X
Logger . LogError ( "Got invalid H.26X level when building CODECS field for HLS master playlist" ) ;
return string . Empty ;
}
2020-03-11 13:14:36 -04:00
2020-04-25 09:22:09 -04:00
if ( string . Equals ( codec , "h264" , StringComparison . OrdinalIgnoreCase ) )
2020-03-11 13:14:36 -04:00
{
string profile = state . GetRequestedProfiles ( "h264" ) . FirstOrDefault ( ) ;
return HlsCodecStringFactory . GetH264String ( profile , level ) ;
}
2020-04-25 09:22:09 -04:00
else if ( string . Equals ( codec , "h265" , StringComparison . OrdinalIgnoreCase )
| | string . Equals ( codec , "hevc" , StringComparison . OrdinalIgnoreCase ) )
2020-03-11 13:14:36 -04:00
{
string profile = state . GetRequestedProfiles ( "h265" ) . FirstOrDefault ( ) ;
return HlsCodecStringFactory . GetH265String ( profile , level ) ;
}
return string . Empty ;
}
/// <summary>
/// Appends a CODECS field containing formatted strings of
/// the active streams output video and audio codecs.
/// </summary>
/// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/>
2020-04-25 09:22:09 -04:00
/// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/>
2020-03-11 13:14:36 -04:00
/// <seealso cref="GetPlaylistAudioCodecs(StreamState)"/>
/// <param name="builder">StringBuilder to append the field to.</param>
/// <param name="state">StreamState of the current stream.</param>
private void AppendPlaylistCodecsField ( StringBuilder builder , StreamState state )
{
// Video
string videoCodecs = string . Empty ;
2020-04-25 09:22:09 -04:00
int? videoCodecLevel = GetOutputVideoCodecLevel ( state ) ;
if ( ! string . IsNullOrEmpty ( state . ActualOutputVideoCodec ) & & videoCodecLevel . HasValue )
2020-03-11 13:14:36 -04:00
{
2020-04-25 09:22:09 -04:00
videoCodecs = GetPlaylistVideoCodecs ( state , state . ActualOutputVideoCodec , videoCodecLevel . Value ) ;
2020-03-11 13:14:36 -04:00
}
// Audio
string audioCodecs = string . Empty ;
if ( ! string . IsNullOrEmpty ( state . ActualOutputAudioCodec ) )
{
audioCodecs = GetPlaylistAudioCodecs ( state ) ;
}
2020-04-25 09:22:09 -04:00
StringBuilder codecs = new StringBuilder ( ) ;
2020-03-11 13:14:36 -04:00
2020-04-25 09:22:09 -04:00
codecs . Append ( videoCodecs )
. Append ( ',' )
. Append ( audioCodecs ) ;
2020-03-11 13:14:36 -04:00
2020-04-25 09:22:09 -04:00
if ( codecs . Length > 1 )
{
builder . Append ( ",CODECS=\"" )
. Append ( codecs )
. Append ( '"' ) ;
2020-03-11 13:14:36 -04:00
}
}
2020-03-11 13:16:57 -04:00
/// <summary>
/// Appends a FRAME-RATE field containing the framerate of the output stream.
/// </summary>
/// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/>
/// <param name="builder">StringBuilder to append the field to.</param>
/// <param name="state">StreamState of the current stream.</param>
private void AppendPlaylistFramerateField ( StringBuilder builder , StreamState state )
{
double? framerate = null ;
if ( state . TargetFramerate . HasValue )
{
framerate = Math . Round ( state . TargetFramerate . GetValueOrDefault ( ) , 3 ) ;
}
2020-05-27 00:55:45 -04:00
else if ( state . VideoStream ? . RealFrameRate ! = null )
2020-03-11 13:16:57 -04:00
{
framerate = Math . Round ( state . VideoStream . RealFrameRate . GetValueOrDefault ( ) , 3 ) ;
}
if ( framerate . HasValue )
{
2020-06-05 16:05:33 -04:00
builder . Append ( ",FRAME-RATE=" )
. Append ( framerate . Value ) ;
2020-03-11 13:16:57 -04:00
}
}
2020-03-15 13:13:19 -04:00
/// <summary>
/// Appends a RESOLUTION field containing the resolution of the output stream.
/// </summary>
/// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/>
/// <param name="builder">StringBuilder to append the field to.</param>
/// <param name="state">StreamState of the current stream.</param>
private void AppendPlaylistResolutionField ( StringBuilder builder , StreamState state )
{
if ( state . OutputWidth . HasValue & & state . OutputHeight . HasValue )
{
2020-06-05 16:05:33 -04:00
builder . Append ( ",RESOLUTION=" )
2020-03-15 13:13:19 -04:00
. Append ( state . OutputWidth . GetValueOrDefault ( ) )
. Append ( 'x' )
2020-06-05 16:05:33 -04:00
. Append ( state . OutputHeight . GetValueOrDefault ( ) ) ;
2020-03-15 13:13:19 -04:00
}
}
2018-12-14 04:40:55 -05:00
private void AppendPlaylist ( StringBuilder builder , StreamState state , string url , int bitrate , string subtitleGroup )
{
2020-03-15 10:06:38 -04:00
builder . Append ( "#EXT-X-STREAM-INF:BANDWIDTH=" )
. Append ( bitrate . ToString ( CultureInfo . InvariantCulture ) )
. Append ( ",AVERAGE-BANDWIDTH=" )
. Append ( bitrate . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-12-14 04:40:55 -05:00
2020-03-11 13:14:36 -04:00
AppendPlaylistCodecsField ( builder , state ) ;
2020-03-15 13:13:19 -04:00
AppendPlaylistResolutionField ( builder , state ) ;
2020-03-11 13:16:57 -04:00
AppendPlaylistFramerateField ( builder , state ) ;
2018-12-14 04:40:55 -05:00
if ( ! string . IsNullOrWhiteSpace ( subtitleGroup ) )
{
2020-03-15 10:06:38 -04:00
builder . Append ( ",SUBTITLES=\"" )
. Append ( subtitleGroup )
. Append ( '"' ) ;
2018-12-14 04:40:55 -05:00
}
2020-03-15 10:06:38 -04:00
builder . Append ( Environment . NewLine ) ;
2018-12-14 04:40:55 -05:00
builder . AppendLine ( url ) ;
}
private int GetBitrateVariation ( int bitrate )
{
// By default, vary by just 50k
var variation = 50000 ;
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 ;
}
else if ( bitrate > = 400000 )
{
variation = 100000 ;
}
return variation ;
}
private async Task < object > GetVariantPlaylistInternal ( StreamRequest request , bool isOutputVideo , string name )
{
var state = await GetState ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
var segmentLengths = GetSegmentLengths ( state ) ;
var builder = new StringBuilder ( ) ;
builder . AppendLine ( "#EXTM3U" ) ;
builder . AppendLine ( "#EXT-X-PLAYLIST-TYPE:VOD" ) ;
builder . AppendLine ( "#EXT-X-VERSION:3" ) ;
2019-06-05 14:59:06 -04:00
builder . AppendLine ( "#EXT-X-TARGETDURATION:" + Math . Ceiling ( segmentLengths . Length > 0 ? segmentLengths . Max ( ) : state . SegmentLength ) . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-12-14 04:40:55 -05:00
builder . AppendLine ( "#EXT-X-MEDIA-SEQUENCE:0" ) ;
var queryStringIndex = Request . RawUrl . IndexOf ( '?' ) ;
var queryString = queryStringIndex = = - 1 ? string . Empty : Request . RawUrl . Substring ( queryStringIndex ) ;
//if ((Request.UserAgent ?? string.Empty).IndexOf("roku", StringComparison.OrdinalIgnoreCase) != -1)
//{
// queryString = string.Empty;
//}
var index = 0 ;
foreach ( var length in segmentLengths )
{
2019-06-05 14:59:06 -04:00
builder . AppendLine ( "#EXTINF:" + length . ToString ( "0.0000" , CultureInfo . InvariantCulture ) + ", nodesc" ) ;
2018-12-14 04:40:55 -05:00
builder . AppendLine ( string . Format ( "hls1/{0}/{1}{2}{3}" ,
name ,
2019-06-05 14:59:06 -04:00
index . ToString ( CultureInfo . InvariantCulture ) ,
2018-12-14 04:40:55 -05:00
GetSegmentFileExtension ( request ) ,
queryString ) ) ;
index + + ;
}
builder . AppendLine ( "#EXT-X-ENDLIST" ) ;
var playlistText = builder . ToString ( ) ;
return ResultFactory . GetResult ( playlistText , MimeTypes . GetMimeType ( "playlist.m3u8" ) , new Dictionary < string , string > ( ) ) ;
}
protected override string GetAudioArguments ( StreamState state , EncodingOptions encodingOptions )
{
var audioCodec = EncodingHelper . GetAudioEncoder ( state ) ;
if ( ! state . IsOutputVideo )
{
2020-05-25 17:58:19 -04:00
if ( EncodingHelper . IsCopyCodec ( audioCodec ) )
2018-12-14 04:40:55 -05:00
{
return "-acodec copy" ;
}
var audioTranscodeParams = new List < string > ( ) ;
audioTranscodeParams . Add ( "-acodec " + audioCodec ) ;
if ( state . OutputAudioBitrate . HasValue )
{
2019-06-05 14:59:06 -04:00
audioTranscodeParams . Add ( "-ab " + state . OutputAudioBitrate . Value . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-12-14 04:40:55 -05:00
}
if ( state . OutputAudioChannels . HasValue )
{
2019-06-05 14:59:06 -04:00
audioTranscodeParams . Add ( "-ac " + state . OutputAudioChannels . Value . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-12-14 04:40:55 -05:00
}
if ( state . OutputAudioSampleRate . HasValue )
{
2019-06-05 14:59:06 -04:00
audioTranscodeParams . Add ( "-ar " + state . OutputAudioSampleRate . Value . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-12-14 04:40:55 -05:00
}
audioTranscodeParams . Add ( "-vn" ) ;
return string . Join ( " " , audioTranscodeParams . ToArray ( ) ) ;
}
2020-05-25 17:58:19 -04:00
if ( EncodingHelper . IsCopyCodec ( audioCodec ) )
2018-12-14 04:40:55 -05:00
{
var videoCodec = EncodingHelper . GetVideoEncoder ( state , encodingOptions ) ;
2020-05-25 17:58:19 -04:00
if ( EncodingHelper . IsCopyCodec ( videoCodec ) & & state . EnableBreakOnNonKeyFrames ( videoCodec ) )
2018-12-14 04:40:55 -05:00
{
return "-codec:a:0 copy -copypriorss:a:0 0" ;
}
return "-codec:a:0 copy" ;
}
var args = "-codec:a:0 " + audioCodec ;
var channels = state . OutputAudioChannels ;
if ( channels . HasValue )
{
args + = " -ac " + channels . Value ;
}
var bitrate = state . OutputAudioBitrate ;
if ( bitrate . HasValue )
{
2019-06-05 14:59:06 -04:00
args + = " -ab " + bitrate . Value . ToString ( CultureInfo . InvariantCulture ) ;
2018-12-14 04:40:55 -05:00
}
if ( state . OutputAudioSampleRate . HasValue )
{
2019-06-05 14:59:06 -04:00
args + = " -ar " + state . OutputAudioSampleRate . Value . ToString ( CultureInfo . InvariantCulture ) ;
2018-12-14 04:40:55 -05:00
}
args + = " " + EncodingHelper . GetAudioFilterParam ( state , encodingOptions , true ) ;
return args ;
}
protected override string GetVideoArguments ( StreamState state , EncodingOptions encodingOptions )
{
if ( ! state . IsOutputVideo )
{
return string . Empty ;
}
var codec = EncodingHelper . GetVideoEncoder ( state , encodingOptions ) ;
var args = "-codec:v:0 " + codec ;
// if (state.EnableMpegtsM2TsMode)
// {
// args += " -mpegts_m2ts_mode 1";
// }
// See if we can save come cpu cycles by avoiding encoding
2020-05-25 17:58:19 -04:00
if ( EncodingHelper . IsCopyCodec ( codec ) )
2018-12-14 04:40:55 -05:00
{
2019-06-01 18:03:48 -04:00
if ( state . VideoStream ! = null & & ! string . Equals ( state . VideoStream . NalLengthSize , "0" , StringComparison . OrdinalIgnoreCase ) )
2018-12-14 04:40:55 -05:00
{
2019-06-01 18:03:48 -04:00
string bitStreamArgs = EncodingHelper . GetBitStreamArgs ( state . VideoStream ) ;
if ( ! string . IsNullOrEmpty ( bitStreamArgs ) )
{
args + = " " + bitStreamArgs ;
}
2018-12-14 04:40:55 -05:00
}
//args += " -flags -global_header";
}
else
{
2020-03-28 16:27:49 -04:00
var gopArg = string . Empty ;
2019-10-27 09:47:00 -04:00
var keyFrameArg = string . Format (
CultureInfo . InvariantCulture ,
2019-08-24 05:38:33 -04:00
" -force_key_frames:0 \"expr:gte(t,{0}+n_forced*{1})\"" ,
2019-10-21 05:04:57 -04:00
GetStartNumber ( state ) * state . SegmentLength ,
state . SegmentLength ) ;
2020-03-28 16:27:49 -04:00
var framerate = state . VideoStream ? . RealFrameRate ;
2020-04-05 12:45:01 -04:00
if ( framerate . HasValue )
2019-10-09 06:46:01 -04:00
{
// This is to make sure keyframe interval is limited to our segment,
// as forcing keyframes is not enough.
2019-10-14 06:15:41 -04:00
// Example: we encoded half of desired length, then codec detected
2019-10-09 06:46:01 -04:00
// scene cut and inserted a keyframe; next forced keyframe would
2020-03-30 16:04:55 -04:00
// be created outside of segment, which breaks seeking
// -sc_threshold 0 is used to prevent the hardware encoder from post processing to break the set keyframe
2020-03-28 16:27:49 -04:00
gopArg = string . Format (
2019-10-27 09:47:00 -04:00
CultureInfo . InvariantCulture ,
2020-03-28 16:27:49 -04:00
" -g {0} -keyint_min {0} -sc_threshold 0" ,
2020-03-30 02:46:05 -04:00
Math . Ceiling ( state . SegmentLength * framerate . Value )
2019-10-09 06:46:01 -04:00
) ;
}
2018-12-14 04:40:55 -05:00
2019-12-04 03:16:19 -05:00
args + = " " + EncodingHelper . GetVideoQualityParam ( state , codec , encodingOptions , GetDefaultEncoderPreset ( ) ) ;
2020-03-28 16:27:49 -04:00
// Unable to force key frames using these hw encoders, set key frames by GOP
if ( string . Equals ( codec , "h264_qsv" , StringComparison . OrdinalIgnoreCase )
| | string . Equals ( codec , "h264_nvenc" , StringComparison . OrdinalIgnoreCase )
| | string . Equals ( codec , "h264_amf" , StringComparison . OrdinalIgnoreCase ) )
2019-12-13 19:45:30 -05:00
{
2020-03-28 16:27:49 -04:00
args + = " " + gopArg ;
2020-01-08 11:52:50 -05:00
}
2019-12-06 01:51:24 -05:00
else
{
2020-03-28 16:27:49 -04:00
args + = " " + keyFrameArg + gopArg ;
2020-01-08 11:52:50 -05:00
}
2018-12-14 04:40:55 -05:00
//args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0";
2020-03-03 10:56:50 -05:00
var hasGraphicalSubs = state . SubtitleStream ! = null & & ! state . SubtitleStream . IsTextSubtitleStream & & state . SubtitleDeliveryMethod = = SubtitleDeliveryMethod . Encode ;
// This is for graphical subs
2018-12-14 04:40:55 -05:00
if ( hasGraphicalSubs )
{
args + = EncodingHelper . GetGraphicalSubtitleParam ( state , encodingOptions , codec ) ;
}
// Add resolution params, if specified
2020-03-30 16:04:55 -04:00
else
2018-12-14 04:40:55 -05:00
{
2020-03-30 16:04:55 -04:00
args + = EncodingHelper . GetOutputSizeParam ( state , encodingOptions , codec ) ;
2018-12-14 04:40:55 -05:00
}
2020-03-30 02:46:05 -04:00
// -start_at_zero is necessary to use with -ss when seeking,
// otherwise the target position cannot be determined.
2020-03-03 10:56:50 -05:00
if ( ! ( state . SubtitleStream ! = null & & state . SubtitleStream . IsExternal & & ! state . SubtitleStream . IsTextSubtitleStream ) )
2018-12-14 04:40:55 -05:00
{
2020-03-03 10:56:50 -05:00
args + = " -start_at_zero" ;
2018-12-14 04:40:55 -05:00
}
//args += " -flags -global_header";
}
if ( ! string . IsNullOrEmpty ( state . OutputVideoSync ) )
{
args + = " -vsync " + state . OutputVideoSync ;
}
args + = EncodingHelper . GetOutputFFlags ( state ) ;
return args ;
}
protected override string GetCommandLineArguments ( string outputPath , EncodingOptions encodingOptions , StreamState state , bool isEncoding )
{
var videoCodec = EncodingHelper . GetVideoEncoder ( state , encodingOptions ) ;
var threads = EncodingHelper . GetNumberOfThreads ( state , encodingOptions , videoCodec ) ;
2019-10-14 06:13:04 -04:00
if ( state . BaseRequest . BreakOnNonKeyFrames )
{
// FIXME: this is actually a workaround, as ideally it really should be the client which decides whether non-keyframe
// breakpoints are supported; but current implementation always uses "ffmpeg input seeking" which is liable
// to produce a missing part of video stream before first keyframe is encountered, which may lead to
// awkward cases like a few starting HLS segments having no video whatsoever, which breaks hls.js
Logger . LogInformation ( "Current HLS implementation doesn't support non-keyframe breaks but one is requested, ignoring that request" ) ;
state . BaseRequest . BreakOnNonKeyFrames = false ;
}
2020-03-11 11:30:22 -04:00
2018-12-14 04:40:55 -05:00
var inputModifier = EncodingHelper . GetInputModifier ( state , encodingOptions ) ;
// If isEncoding is true we're actually starting ffmpeg
var startNumber = GetStartNumber ( state ) ;
2019-06-05 14:59:06 -04:00
var startNumberParam = isEncoding ? startNumber . ToString ( CultureInfo . InvariantCulture ) : "0" ;
2018-12-14 04:40:55 -05:00
var mapArgs = state . IsOutputVideo ? EncodingHelper . GetMapArgs ( state ) : string . Empty ;
2019-01-26 15:47:11 -05:00
var outputTsArg = Path . Combine ( Path . GetDirectoryName ( outputPath ) , Path . GetFileNameWithoutExtension ( outputPath ) ) + "%d" + GetSegmentFileExtension ( state . Request ) ;
2018-12-14 04:40:55 -05:00
var segmentFormat = GetSegmentFileExtension ( state . Request ) . TrimStart ( '.' ) ;
if ( string . Equals ( segmentFormat , "ts" , StringComparison . OrdinalIgnoreCase ) )
{
segmentFormat = "mpegts" ;
}
2019-08-24 05:17:17 -04:00
return string . Format (
2020-03-03 10:56:50 -05:00
"{0} {1} -map_metadata -1 -map_chapters -1 -threads {2} {3} {4} {5} -copyts -avoid_negative_ts disabled -f hls -max_delay 5000000 -hls_time {6} -individual_header_trailer 0 -hls_segment_type {7} -start_number {8} -hls_segment_filename \"{9}\" -hls_playlist_type vod -hls_list_size 0 -y \"{10}\"" ,
2018-12-14 04:40:55 -05:00
inputModifier ,
EncodingHelper . GetInputArgument ( state , encodingOptions ) ,
threads ,
mapArgs ,
GetVideoArguments ( state , encodingOptions ) ,
GetAudioArguments ( state , encodingOptions ) ,
2019-06-05 14:59:06 -04:00
state . SegmentLength . ToString ( CultureInfo . InvariantCulture ) ,
2019-10-03 09:17:35 -04:00
segmentFormat ,
2018-12-14 04:40:55 -05:00
startNumberParam ,
outputTsArg ,
2019-10-03 09:17:35 -04:00
outputPath
2018-12-14 04:40:55 -05:00
) . Trim ( ) ;
}
}
2018-12-14 18:48:06 -05:00
}