1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Streaming
{
/// <summary>
/// Class BaseMediaHandler
/// </summary>
/// <typeparam name="TBaseItemType">The type of the T base item type.</typeparam>
public abstract class BaseProgressiveStreamingHandler<TBaseItemType> : BaseStreamingHandler<TBaseItemType>
where TBaseItemType : BaseItem, IHasMediaStreams, new()
{
/// <summary>
/// Gets the type of the transcoding job.
/// </summary>
/// <value>The type of the transcoding job.</value>
protected override TranscodingJobType TranscodingJobType
{
get { return TranscodingJobType.Progressive; }
}
/// <summary>
/// Processes the request.
/// </summary>
/// <param name="ctx">The CTX.</param>
/// <returns>Task.</returns>
public override Task ProcessRequest(HttpListenerContext ctx)
{
HttpListenerContext = ctx;
if (!RequiresConversion())
{
return new StaticFileHandler(Kernel)
{
Path = LibraryItem.Path
}.ProcessRequest(ctx);
}
var outputPath = OutputFilePath;
if (File.Exists(outputPath) && !Plugin.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
{
return new StaticFileHandler(Kernel)
{
Path = outputPath
}.ProcessRequest(ctx);
}
return base.ProcessRequest(ctx);
}
/// <summary>
/// Requireses the conversion.
/// </summary>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected bool RequiresConversion()
{
return !GetBoolQueryStringParam("static");
}
/// <summary>
/// Writes the response to output stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="responseInfo">The response info.</param>
/// <param name="contentLength">Length of the content.</param>
/// <returns>Task.</returns>
protected override async Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? contentLength)
{
// Use the command line args with a dummy playlist path
var outputPath = OutputFilePath;
if (!File.Exists(outputPath))
{
await StartFFMpeg(outputPath).ConfigureAwait(false);
}
else
{
Plugin.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
}
try
{
await StreamFile(outputPath, stream).ConfigureAwait(false);
}
catch (Exception ex)
{
//Logger.ErrorException("Error streaming media", ex);
}
finally
{
Plugin.Instance.OnTranscodeEndRequest(outputPath, TranscodingJobType.Progressive);
}
}
/// <summary>
/// Streams the file.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="outputStream">The output stream.</param>
/// <returns>Task{System.Boolean}.</returns>
private async Task StreamFile(string path, Stream outputStream)
{
var eofCount = 0;
long position = 0;
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
{
while (eofCount < 15)
{
await fs.CopyToAsync(outputStream).ConfigureAwait(false);
var fsPosition = fs.Position;
var bytesRead = fsPosition - position;
//Logger.LogInfo("Streamed {0} bytes from file {1}", bytesRead, path);
if (bytesRead == 0)
{
eofCount++;
await Task.Delay(100).ConfigureAwait(false);
}
else
{
eofCount = 0;
}
position = fsPosition;
}
}
}
/// <summary>
/// Deletes the partial stream files.
/// </summary>
/// <param name="outputFilePath">The output file path.</param>
protected override void DeletePartialStreamFiles(string outputFilePath)
{
File.Delete(outputFilePath);
}
}
}
|