2015-03-16 03:41:12 +00:00
using MediaBrowser.Api.Playback.Hls ;
using MediaBrowser.Common.IO ;
2014-10-12 17:31:41 +00:00
using MediaBrowser.Common.Net ;
using MediaBrowser.Controller.Configuration ;
2015-01-20 05:19:13 +00:00
using MediaBrowser.Controller.Devices ;
2014-10-12 17:31:41 +00:00
using MediaBrowser.Controller.Dlna ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.MediaEncoding ;
2014-10-16 03:26:39 +00:00
using MediaBrowser.Controller.Net ;
2014-10-12 17:31:41 +00:00
using MediaBrowser.Model.IO ;
using ServiceStack ;
using System ;
using System.Collections.Generic ;
using System.Globalization ;
2014-10-16 03:26:39 +00:00
using System.IO ;
using System.Linq ;
2014-10-12 17:31:41 +00:00
using System.Threading ;
using System.Threading.Tasks ;
2014-12-26 17:45:06 +00:00
using MimeTypes = MediaBrowser . Model . Net . MimeTypes ;
2014-10-12 17:31:41 +00:00
2015-03-16 03:41:12 +00:00
namespace MediaBrowser.Api.Playback.Dash
2014-10-12 17:31:41 +00:00
{
/// <summary>
/// Options is needed for chromecast. Threw Head in there since it's related
/// </summary>
[Route("/Videos/{Id}/master.mpd", "GET", Summary = "Gets a video stream using Mpeg dash.")]
[Route("/Videos/{Id}/master.mpd", "HEAD", Summary = "Gets a video stream using Mpeg dash.")]
public class GetMasterManifest : VideoStreamRequest
{
public bool EnableAdaptiveBitrateStreaming { get ; set ; }
public GetMasterManifest ( )
{
EnableAdaptiveBitrateStreaming = true ;
}
}
2015-03-16 04:07:46 +00:00
[Route("/Videos/{Id}/dash/{RepresentationId}/{SegmentId}.m4s", "GET")]
2014-10-12 17:31:41 +00:00
public class GetDashSegment : VideoStreamRequest
{
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get ; set ; }
2015-03-10 04:22:42 +00:00
/// <summary>
2015-03-16 04:07:46 +00:00
/// Gets or sets the representation identifier.
2015-03-10 04:22:42 +00:00
/// </summary>
2015-03-16 04:07:46 +00:00
/// <value>The representation identifier.</value>
public string RepresentationId { get ; set ; }
2014-10-12 17:31:41 +00:00
}
2014-10-15 00:04:44 +00:00
2014-10-12 17:31:41 +00:00
public class MpegDashService : BaseHlsService
{
2015-03-28 20:22:27 +00:00
public MpegDashService ( IServerConfigurationManager serverConfig , IUserManager userManager , ILibraryManager libraryManager , IIsoManager isoManager , IMediaEncoder mediaEncoder , IFileSystem fileSystem , IDlnaManager dlnaManager , ISubtitleEncoder subtitleEncoder , IDeviceManager deviceManager , IMediaSourceManager mediaSourceManager , IZipClient zipClient , INetworkManager networkManager ) : base ( serverConfig , userManager , libraryManager , isoManager , mediaEncoder , fileSystem , dlnaManager , subtitleEncoder , deviceManager , mediaSourceManager , zipClient )
2014-10-12 17:31:41 +00:00
{
NetworkManager = networkManager ;
}
2015-01-20 05:19:13 +00:00
protected INetworkManager NetworkManager { get ; private set ; }
2014-10-12 17:31:41 +00:00
public object Get ( GetMasterManifest request )
{
var result = GetAsync ( request , "GET" ) . Result ;
return result ;
}
public object Head ( GetMasterManifest request )
{
var result = GetAsync ( request , "HEAD" ) . Result ;
return result ;
}
2015-03-10 04:22:42 +00:00
protected override bool EnableOutputInSubFolder
{
get
{
return true ;
}
}
2014-10-12 17:31:41 +00:00
private async Task < object > GetAsync ( GetMasterManifest request , string method )
{
if ( string . IsNullOrEmpty ( request . MediaSourceId ) )
{
throw new ArgumentException ( "MediaSourceId is required" ) ;
}
var state = await GetState ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
var playlistText = string . Empty ;
if ( string . Equals ( method , "GET" , StringComparison . OrdinalIgnoreCase ) )
{
2015-03-16 03:41:12 +00:00
playlistText = new ManifestBuilder ( ) . GetManifestText ( state , Request . RawUrl ) ;
2014-10-12 17:31:41 +00:00
}
2014-12-26 17:45:06 +00:00
return ResultFactory . GetResult ( playlistText , MimeTypes . GetMimeType ( "playlist.mpd" ) , new Dictionary < string , string > ( ) ) ;
2014-10-12 17:31:41 +00:00
}
public object Get ( GetDashSegment request )
{
2015-03-16 04:07:46 +00:00
return GetDynamicSegment ( request , request . SegmentId , request . RepresentationId ) . Result ;
2014-10-12 17:31:41 +00:00
}
2015-03-16 04:07:46 +00:00
private async Task < object > GetDynamicSegment ( VideoStreamRequest request , string segmentId , string representationId )
2014-10-16 03:26:39 +00:00
{
if ( ( request . StartTimeTicks ? ? 0 ) > 0 )
{
throw new ArgumentException ( "StartTimeTicks is not allowed." ) ;
}
var cancellationTokenSource = new CancellationTokenSource ( ) ;
var cancellationToken = cancellationTokenSource . Token ;
2015-03-18 06:02:22 +00:00
var requestedIndex = string . Equals ( segmentId , "init" , StringComparison . OrdinalIgnoreCase ) ?
2015-03-16 14:40:44 +00:00
- 1 :
int . Parse ( segmentId , NumberStyles . Integer , UsCulture ) ;
2014-10-16 03:26:39 +00:00
var state = await GetState ( request , cancellationToken ) . ConfigureAwait ( false ) ;
2015-03-10 02:37:24 +00:00
var playlistPath = Path . ChangeExtension ( state . OutputFilePath , ".mpd" ) ;
2014-10-16 03:26:39 +00:00
var segmentExtension = GetSegmentFileExtension ( state ) ;
2015-03-18 06:02:22 +00:00
var segmentPath = FindSegment ( playlistPath , representationId , segmentExtension , requestedIndex ) ;
2014-10-16 03:26:39 +00:00
var segmentLength = state . SegmentLength ;
TranscodingJob job = null ;
2015-03-18 06:02:22 +00:00
if ( ! string . IsNullOrWhiteSpace ( segmentPath ) )
2014-10-16 03:26:39 +00:00
{
2015-03-10 02:37:24 +00:00
job = ApiEntryPoint . Instance . GetTranscodingJob ( playlistPath , TranscodingJobType ) ;
2015-03-18 06:02:22 +00:00
return await GetSegmentResult ( playlistPath , segmentPath , requestedIndex , segmentLength , job , cancellationToken ) . ConfigureAwait ( false ) ;
2014-10-16 03:26:39 +00:00
}
await ApiEntryPoint . Instance . TranscodingStartLock . WaitAsync ( cancellationTokenSource . Token ) . ConfigureAwait ( false ) ;
try
{
2015-03-18 06:02:22 +00:00
segmentPath = FindSegment ( playlistPath , representationId , segmentExtension , requestedIndex ) ;
if ( ! string . IsNullOrWhiteSpace ( segmentPath ) )
2014-10-16 03:26:39 +00:00
{
2015-03-10 02:37:24 +00:00
job = ApiEntryPoint . Instance . GetTranscodingJob ( playlistPath , TranscodingJobType ) ;
2015-03-18 06:02:22 +00:00
return await GetSegmentResult ( playlistPath , segmentPath , requestedIndex , segmentLength , job , cancellationToken ) . ConfigureAwait ( false ) ;
2014-10-16 03:26:39 +00:00
}
else
{
2015-03-16 14:40:44 +00:00
if ( string . Equals ( representationId , "0" , StringComparison . OrdinalIgnoreCase ) )
2014-10-16 03:26:39 +00:00
{
2015-03-18 06:02:22 +00:00
job = ApiEntryPoint . Instance . GetTranscodingJob ( playlistPath , TranscodingJobType ) ;
2015-03-16 14:40:44 +00:00
var currentTranscodingIndex = GetCurrentTranscodingIndex ( playlistPath , segmentExtension ) ;
2015-03-18 06:02:22 +00:00
var segmentGapRequiringTranscodingChange = 24 / state . SegmentLength ;
Logger . Debug ( "Current transcoding index is {0}. requestedIndex={1}. segmentGapRequiringTranscodingChange={2}" , currentTranscodingIndex ? ? - 2 , requestedIndex , segmentGapRequiringTranscodingChange ) ;
if ( currentTranscodingIndex = = null | | requestedIndex < currentTranscodingIndex . Value | | ( requestedIndex - currentTranscodingIndex . Value ) > segmentGapRequiringTranscodingChange )
2014-10-16 03:26:39 +00:00
{
2015-03-16 14:40:44 +00:00
// If the playlist doesn't already exist, startup ffmpeg
try
{
2015-03-29 18:31:28 +00:00
ApiEntryPoint . Instance . KillTranscodingJobs ( request . DeviceId , request . PlaySessionId , p = > false ) ;
2015-03-16 14:40:44 +00:00
if ( currentTranscodingIndex . HasValue )
{
2015-03-18 06:02:22 +00:00
DeleteLastTranscodedFiles ( playlistPath , 0 ) ;
2015-03-16 14:40:44 +00:00
}
2014-10-16 03:26:39 +00:00
2015-03-18 06:02:22 +00:00
var positionTicks = GetPositionTicks ( state , requestedIndex ) ;
2015-03-16 14:40:44 +00:00
request . StartTimeTicks = positionTicks ;
2015-03-18 06:02:22 +00:00
var startNumber = GetStartNumber ( state ) ;
var workingDirectory = Path . Combine ( Path . GetDirectoryName ( playlistPath ) , ( startNumber = = - 1 ? 0 : startNumber ) . ToString ( CultureInfo . InvariantCulture ) ) ;
state . WaitForPath = Path . Combine ( workingDirectory , Path . GetFileName ( playlistPath ) ) ;
Directory . CreateDirectory ( workingDirectory ) ;
job = await StartFfMpeg ( state , playlistPath , cancellationTokenSource , workingDirectory ) . ConfigureAwait ( false ) ;
await WaitForMinimumDashSegmentCount ( Path . Combine ( workingDirectory , Path . GetFileName ( playlistPath ) ) , 1 , cancellationTokenSource . Token ) . ConfigureAwait ( false ) ;
2015-03-16 14:40:44 +00:00
}
catch
2014-10-16 03:26:39 +00:00
{
2015-03-16 14:40:44 +00:00
state . Dispose ( ) ;
throw ;
2014-10-16 03:26:39 +00:00
}
}
}
}
}
finally
{
ApiEntryPoint . Instance . TranscodingStartLock . Release ( ) ;
}
2015-03-18 06:02:22 +00:00
while ( string . IsNullOrWhiteSpace ( segmentPath ) )
2014-10-16 03:26:39 +00:00
{
2015-03-18 06:02:22 +00:00
segmentPath = FindSegment ( playlistPath , representationId , segmentExtension , requestedIndex ) ;
2014-10-16 03:26:39 +00:00
await Task . Delay ( 50 , cancellationToken ) . ConfigureAwait ( false ) ;
}
Logger . Info ( "returning {0}" , segmentPath ) ;
2015-03-18 06:02:22 +00:00
return await GetSegmentResult ( playlistPath , segmentPath , requestedIndex , segmentLength , job ? ? ApiEntryPoint . Instance . GetTranscodingJob ( playlistPath , TranscodingJobType ) , cancellationToken ) . ConfigureAwait ( false ) ;
2015-03-16 14:40:44 +00:00
}
2015-03-18 16:40:16 +00:00
private long GetPositionTicks ( StreamState state , int requestedIndex )
2015-03-16 04:39:55 +00:00
{
2015-03-18 16:40:16 +00:00
if ( requestedIndex < = 0 )
2015-03-16 14:40:44 +00:00
{
return 0 ;
}
2015-03-18 16:40:16 +00:00
var startSeconds = requestedIndex * state . SegmentLength ;
2015-03-16 04:39:55 +00:00
return TimeSpan . FromSeconds ( startSeconds ) . Ticks ;
}
2015-03-18 06:02:22 +00:00
protected Task WaitForMinimumDashSegmentCount ( string playlist , int segmentCount , CancellationToken cancellationToken )
2015-03-10 04:33:51 +00:00
{
2015-03-18 06:02:22 +00:00
return WaitForSegment ( playlist , "stream0-" + segmentCount . ToString ( "00000" , CultureInfo . InvariantCulture ) + ".m4s" , cancellationToken ) ;
2015-03-10 04:33:51 +00:00
}
2014-10-16 03:26:39 +00:00
private async Task < object > GetSegmentResult ( string playlistPath ,
string segmentPath ,
int segmentIndex ,
int segmentLength ,
TranscodingJob transcodingJob ,
CancellationToken cancellationToken )
{
// If all transcoding has completed, just return immediately
2015-03-16 04:07:46 +00:00
if ( transcodingJob ! = null & & transcodingJob . HasExited )
2014-10-16 03:26:39 +00:00
{
return GetSegmentResult ( segmentPath , segmentIndex , segmentLength , transcodingJob ) ;
}
// 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 ) ;
}
2015-03-18 06:02:22 +00:00
2014-10-16 03:26:39 +00:00
return GetSegmentResult ( segmentPath , segmentIndex , segmentLength , transcodingJob ) ;
}
private object GetSegmentResult ( string segmentPath , int index , int segmentLength , TranscodingJob transcodingJob )
{
var segmentEndingSeconds = ( 1 + index ) * segmentLength ;
var segmentEndingPositionTicks = TimeSpan . FromSeconds ( segmentEndingSeconds ) . Ticks ;
return ResultFactory . GetStaticFileResult ( Request , new StaticFileResultOptions
{
Path = segmentPath ,
FileShare = FileShare . ReadWrite ,
OnComplete = ( ) = >
{
if ( transcodingJob ! = null )
{
transcodingJob . DownloadPositionTicks = Math . Max ( transcodingJob . DownloadPositionTicks ? ? segmentEndingPositionTicks , segmentEndingPositionTicks ) ;
}
}
} ) ;
}
public int? GetCurrentTranscodingIndex ( string playlist , string segmentExtension )
{
2015-03-18 16:40:16 +00:00
var job = ApiEntryPoint . Instance . GetTranscodingJob ( playlist , TranscodingJobType ) ;
if ( job = = null | | job . HasExited )
{
return null ;
}
2015-03-16 04:39:55 +00:00
var file = GetLastTranscodingFiles ( playlist , segmentExtension , FileSystem , 1 ) . FirstOrDefault ( ) ;
2014-10-16 03:26:39 +00:00
if ( file = = null )
{
return null ;
}
2015-03-18 06:02:22 +00:00
return GetIndex ( file . FullName ) ;
2014-10-16 03:26:39 +00:00
}
2015-03-18 06:02:22 +00:00
public int GetIndex ( string segmentPath )
2014-10-16 03:26:39 +00:00
{
2015-03-18 06:02:22 +00:00
var indexString = Path . GetFileNameWithoutExtension ( segmentPath ) . Split ( '-' ) . LastOrDefault ( ) ;
2015-03-16 04:39:55 +00:00
2015-03-16 14:40:44 +00:00
if ( string . Equals ( indexString , "init" , StringComparison . OrdinalIgnoreCase ) )
2014-10-16 03:26:39 +00:00
{
2015-03-16 14:40:44 +00:00
return - 1 ;
2014-10-16 03:26:39 +00:00
}
2015-03-18 06:02:22 +00:00
var startNumber = int . Parse ( Path . GetFileNameWithoutExtension ( Path . GetDirectoryName ( segmentPath ) ) , NumberStyles . Integer , UsCulture ) ;
return startNumber + int . Parse ( indexString , NumberStyles . Integer , UsCulture ) - 1 ;
2015-03-16 14:40:44 +00:00
}
2014-10-16 03:26:39 +00:00
2015-03-18 06:02:22 +00:00
private void DeleteLastTranscodedFiles ( string playlistPath , int retryCount )
2015-03-16 14:40:44 +00:00
{
if ( retryCount > = 5 )
2015-03-16 04:39:55 +00:00
{
return ;
}
2014-10-16 03:26:39 +00:00
}
2015-03-16 04:39:55 +00:00
private static List < FileInfo > GetLastTranscodingFiles ( string playlist , string segmentExtension , IFileSystem fileSystem , int count )
2014-10-16 03:26:39 +00:00
{
var folder = Path . GetDirectoryName ( playlist ) ;
try
{
return new DirectoryInfo ( folder )
2015-03-18 06:02:22 +00:00
. EnumerateFiles ( "*" , SearchOption . AllDirectories )
2014-10-16 03:26:39 +00:00
. Where ( i = > string . Equals ( i . Extension , segmentExtension , StringComparison . OrdinalIgnoreCase ) )
. OrderByDescending ( fileSystem . GetLastWriteTimeUtc )
2015-03-16 04:39:55 +00:00
. Take ( count )
. ToList ( ) ;
2014-10-16 03:26:39 +00:00
}
catch ( DirectoryNotFoundException )
{
2015-03-16 04:39:55 +00:00
return new List < FileInfo > ( ) ;
2014-10-16 03:26:39 +00:00
}
}
2015-03-18 16:40:16 +00:00
private string FindSegment ( string playlist , string representationId , string segmentExtension , int requestedIndex )
2014-10-16 03:26:39 +00:00
{
var folder = Path . GetDirectoryName ( playlist ) ;
2015-03-18 16:40:16 +00:00
if ( requestedIndex = = - 1 )
2015-03-18 06:02:22 +00:00
{
var path = Path . Combine ( folder , "0" , "stream" + representationId + "-" + "init" + segmentExtension ) ;
return File . Exists ( path ) ? path : null ;
}
2015-03-16 14:40:44 +00:00
2015-03-18 06:02:22 +00:00
try
{
foreach ( var subfolder in new DirectoryInfo ( folder ) . EnumerateDirectories ( ) . ToList ( ) )
{
2015-03-18 16:40:16 +00:00
var subfolderName = Path . GetFileNameWithoutExtension ( subfolder . FullName ) ;
2015-03-18 06:02:22 +00:00
int startNumber ;
2015-03-18 16:40:16 +00:00
if ( int . TryParse ( subfolderName , NumberStyles . Any , UsCulture , out startNumber ) )
2015-03-18 06:02:22 +00:00
{
2015-03-18 16:40:16 +00:00
var segmentIndex = requestedIndex - startNumber + 1 ;
var path = Path . Combine ( folder , subfolderName , "stream" + representationId + "-" + segmentIndex . ToString ( "00000" , CultureInfo . InvariantCulture ) + segmentExtension ) ;
if ( File . Exists ( path ) )
{
return path ;
}
2015-03-18 06:02:22 +00:00
}
}
}
catch ( DirectoryNotFoundException )
{
}
2014-10-16 03:26:39 +00:00
2015-03-18 06:02:22 +00:00
return null ;
2014-10-16 03:26:39 +00:00
}
2015-03-10 04:22:42 +00:00
2014-10-12 17:31:41 +00:00
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 ;
2014-12-26 17:45:06 +00:00
var args = "-codec:v:0 " + codec ;
if ( state . EnableMpegtsM2TsMode )
{
args + = " -mpegts_m2ts_mode 1" ;
}
2014-10-12 17:31:41 +00:00
// See if we can save come cpu cycles by avoiding encoding
if ( codec . Equals ( "copy" , StringComparison . OrdinalIgnoreCase ) )
{
2014-12-26 17:45:06 +00:00
return state . VideoStream ! = null & & IsH264 ( state . VideoStream ) ?
args + " -bsf:v h264_mp4toannexb" :
args ;
2014-10-12 17:31:41 +00:00
}
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 ;
2015-03-10 04:22:42 +00:00
args + = " " + GetVideoQualityParam ( state , H264Encoder , true ) + keyFrameArg ;
2014-10-12 17:31:41 +00:00
// Add resolution params, if specified
if ( ! hasGraphicalSubs )
{
args + = GetOutputSizeParam ( state , codec , false ) ;
}
// This is for internal graphical subs
if ( hasGraphicalSubs )
{
args + = GetGraphicalSubtitleParam ( state , codec ) ;
}
return args ;
}
2015-03-28 20:22:27 +00:00
protected override string GetCommandLineArguments ( string outputPath , StreamState state , bool isEncoding )
2014-10-12 17:31:41 +00:00
{
2015-01-17 19:30:23 +00:00
// test url http://192.168.1.2:8096/videos/233e8905d559a8f230db9bffd2ac9d6d/master.mpd?mediasourceid=233e8905d559a8f230db9bffd2ac9d6d&videocodec=h264&audiocodec=aac&maxwidth=1280&videobitrate=500000&audiobitrate=128000&profile=baseline&level=3
2014-11-09 03:18:14 +00:00
// Good info on i-frames http://blog.streamroot.io/encode-multi-bitrate-videos-mpeg-dash-mse-based-media-players/
2014-10-12 17:31:41 +00:00
var threads = GetNumberOfThreads ( state , false ) ;
var inputModifier = GetInputModifier ( state ) ;
2015-03-16 04:39:55 +00:00
2015-03-16 14:40:44 +00:00
var initSegmentName = "stream$RepresentationID$-init.m4s" ;
2015-03-16 04:39:55 +00:00
var segmentName = "stream$RepresentationID$-$Number%05d$.m4s" ;
2014-10-12 17:31:41 +00:00
2015-03-16 04:39:55 +00:00
var args = string . Format ( "{0} {1} -map_metadata -1 -threads {2} {3} {4} -copyts {5} -f dash -init_seg_name \"{6}\" -media_seg_name \"{7}\" -use_template 0 -use_timeline 1 -min_seg_duration {8} -y \"{9}\"" ,
2014-10-12 17:31:41 +00:00
inputModifier ,
2015-03-28 20:22:27 +00:00
GetInputArgument ( state ) ,
2014-10-12 17:31:41 +00:00
threads ,
GetMapArgs ( state ) ,
GetVideoArguments ( state ) ,
GetAudioArguments ( state ) ,
2015-03-16 04:39:55 +00:00
initSegmentName ,
segmentName ,
2015-03-10 04:03:37 +00:00
( state . SegmentLength * 1000000 ) . ToString ( CultureInfo . InvariantCulture ) ,
2015-03-18 06:02:22 +00:00
state . WaitForPath
2014-10-12 17:31:41 +00:00
) . Trim ( ) ;
return args ;
}
2015-03-16 04:07:46 +00:00
protected override int GetStartNumber ( StreamState state )
{
return GetStartNumber ( state . VideoRequest ) ;
}
2015-03-16 03:41:12 +00:00
2015-03-16 04:07:46 +00:00
private int GetStartNumber ( VideoStreamRequest request )
{
var segmentId = "0" ;
2015-03-16 03:41:12 +00:00
2015-03-16 04:07:46 +00:00
var segmentRequest = request as GetDashSegment ;
if ( segmentRequest ! = null )
{
segmentId = segmentRequest . SegmentId ;
}
2015-03-16 03:41:12 +00:00
2015-03-16 14:40:44 +00:00
if ( string . Equals ( segmentId , "init" , StringComparison . OrdinalIgnoreCase ) )
{
return - 1 ;
}
2015-03-16 04:07:46 +00:00
return int . Parse ( segmentId , NumberStyles . Integer , UsCulture ) ;
}
2015-03-16 03:41:12 +00:00
2014-10-12 17:31:41 +00:00
/// <summary>
/// Gets the segment file extension.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
protected override string GetSegmentFileExtension ( StreamState state )
{
2015-03-10 02:37:24 +00:00
return ".m4s" ;
2014-10-12 17:31:41 +00:00
}
protected override TranscodingJobType TranscodingJobType
{
get
{
return TranscodingJobType . Dash ;
}
}
2015-03-16 14:40:44 +00:00
2015-03-18 06:02:22 +00:00
private async Task WaitForSegment ( string playlist , string segment , CancellationToken cancellationToken )
2015-03-16 14:40:44 +00:00
{
2015-03-18 06:02:22 +00:00
var segmentFilename = Path . GetFileName ( segment ) ;
2015-03-16 14:40:44 +00:00
2015-03-18 06:02:22 +00:00
Logger . Debug ( "Waiting for {0} in {1}" , segmentFilename , playlist ) ;
2015-03-16 14:40:44 +00:00
2015-03-18 06:02:22 +00:00
while ( true )
{
// Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written
2015-04-02 16:26:42 +00:00
using ( var fileStream = GetPlaylistFileStream ( playlist ) )
2015-03-16 14:40:44 +00:00
{
2015-03-18 06:02:22 +00:00
using ( var reader = new StreamReader ( fileStream ) )
2015-03-16 14:40:44 +00:00
{
2015-03-18 06:02:22 +00:00
while ( ! reader . EndOfStream )
{
var line = await reader . ReadLineAsync ( ) . ConfigureAwait ( false ) ;
2015-03-16 14:40:44 +00:00
2015-03-18 06:02:22 +00:00
if ( line . IndexOf ( segmentFilename , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
Logger . Debug ( "Finished waiting for {0} in {1}" , segmentFilename , playlist ) ;
return ;
}
}
await Task . Delay ( 100 , cancellationToken ) . ConfigureAwait ( false ) ;
}
2015-03-16 14:40:44 +00:00
}
}
}
2014-10-12 17:31:41 +00:00
}
}