Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fadf5a9a7 | ||
|
|
a6ae4994e9 | ||
|
|
12af9bddb1 | ||
|
|
92a840cf42 | ||
|
|
9210532cf1 | ||
|
|
66d5faead2 | ||
|
|
65c7a16cee | ||
|
|
46684402e6 | ||
|
|
d75e69249b |
+21
-20
@@ -15,47 +15,48 @@ on:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: linux/amd64
|
||||
container:
|
||||
image: gitea.tourolle.paris/dtourolle/srfplay-builder:latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Verify .NET installation
|
||||
run: dotnet --version
|
||||
with:
|
||||
path: build-${{ github.run_id }}
|
||||
|
||||
- name: Restore dependencies
|
||||
working-directory: build-${{ github.run_id }}
|
||||
run: dotnet restore Jellyfin.Plugin.SRFPlay.sln
|
||||
|
||||
- name: Build solution
|
||||
run: dotnet build Jellyfin.Plugin.SRFPlay.sln --configuration Release --no-restore --no-self-contained
|
||||
working-directory: build-${{ github.run_id }}
|
||||
run: dotnet build Jellyfin.Plugin.SRFPlay.sln --configuration Release --no-restore --no-self-contained /m:1
|
||||
|
||||
- name: Run tests
|
||||
working-directory: build-${{ github.run_id }}
|
||||
run: dotnet test Jellyfin.Plugin.SRFPlay.sln --no-build --configuration Release --verbosity normal
|
||||
|
||||
- name: Install JPRM
|
||||
run: |
|
||||
python3 -m venv /tmp/jprm-venv
|
||||
/tmp/jprm-venv/bin/pip install jprm
|
||||
|
||||
- name: Build Jellyfin Plugin
|
||||
id: jprm
|
||||
working-directory: build-${{ github.run_id }}
|
||||
run: |
|
||||
# Create artifacts directory for JPRM output
|
||||
mkdir -p artifacts
|
||||
|
||||
# Build plugin using JPRM
|
||||
/tmp/jprm-venv/bin/jprm --verbosity=debug plugin build .
|
||||
|
||||
# Find the generated zip file
|
||||
ARTIFACT=$(find . -name "*.zip" -type f -print -quit)
|
||||
echo "artifact=${ARTIFACT}" >> $GITHUB_OUTPUT
|
||||
echo "Found artifact: ${ARTIFACT}"
|
||||
jprm --verbosity=debug plugin build .
|
||||
ARTIFACT=$(find . -name "*.zip" -type f -print -quit | sed 's|^\./||')
|
||||
LATEST="artifacts/srfplay_latest.zip"
|
||||
cp "${ARTIFACT}" "${LATEST}"
|
||||
echo "artifact=${LATEST}" >> $GITHUB_OUTPUT
|
||||
echo "Found artifact: ${ARTIFACT} -> ${LATEST}"
|
||||
|
||||
- name: Upload build artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: jellyfin-srfplay-plugin
|
||||
path: ${{ steps.jprm.outputs.artifact }}
|
||||
path: build-${{ github.run_id }}/${{ steps.jprm.outputs.artifact }}
|
||||
retention-days: 30
|
||||
if-no-files-found: error
|
||||
|
||||
- name: Cleanup
|
||||
if: always()
|
||||
run: rm -rf build-${{ github.run_id }}
|
||||
|
||||
@@ -13,14 +13,15 @@ on:
|
||||
|
||||
jobs:
|
||||
build-and-release:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: linux/amd64
|
||||
container:
|
||||
image: gitea.tourolle.paris/dtourolle/srfplay-builder:latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Verify .NET installation
|
||||
run: dotnet --version
|
||||
with:
|
||||
path: release-${{ github.run_id }}
|
||||
|
||||
- name: Get version
|
||||
id: get_version
|
||||
@@ -35,36 +36,31 @@ jobs:
|
||||
echo "Building version: ${VERSION}"
|
||||
|
||||
- name: Update build.yaml with version
|
||||
working-directory: release-${{ github.run_id }}
|
||||
run: |
|
||||
VERSION="${{ steps.get_version.outputs.version_number }}"
|
||||
sed -i "s/^version:.*/version: \"${VERSION}\"/" build.yaml
|
||||
cat build.yaml
|
||||
|
||||
- name: Restore dependencies
|
||||
working-directory: release-${{ github.run_id }}
|
||||
run: dotnet restore Jellyfin.Plugin.SRFPlay.sln
|
||||
|
||||
- name: Build solution
|
||||
run: dotnet build Jellyfin.Plugin.SRFPlay.sln --configuration Release --no-restore --no-self-contained
|
||||
working-directory: release-${{ github.run_id }}
|
||||
run: dotnet build Jellyfin.Plugin.SRFPlay.sln --configuration Release --no-restore --no-self-contained /m:1
|
||||
|
||||
- name: Run tests
|
||||
working-directory: release-${{ github.run_id }}
|
||||
run: dotnet test Jellyfin.Plugin.SRFPlay.sln --no-build --configuration Release --verbosity normal
|
||||
|
||||
- name: Install JPRM
|
||||
run: |
|
||||
python3 -m venv /tmp/jprm-venv
|
||||
/tmp/jprm-venv/bin/pip install jprm
|
||||
|
||||
- name: Build Jellyfin Plugin
|
||||
id: jprm
|
||||
working-directory: release-${{ github.run_id }}
|
||||
run: |
|
||||
# Create artifacts directory for JPRM output
|
||||
mkdir -p artifacts
|
||||
|
||||
# Build plugin using JPRM
|
||||
/tmp/jprm-venv/bin/jprm --verbosity=debug plugin build ./
|
||||
|
||||
# Find the generated zip file
|
||||
ARTIFACT=$(find . -name "*.zip" -type f -print -quit)
|
||||
jprm --verbosity=debug plugin build ./
|
||||
ARTIFACT=$(find . -name "*.zip" -type f -print -quit | sed 's|^\./||')
|
||||
ARTIFACT_NAME=$(basename "${ARTIFACT}")
|
||||
echo "artifact=${ARTIFACT}" >> $GITHUB_OUTPUT
|
||||
echo "artifact_name=${ARTIFACT_NAME}" >> $GITHUB_OUTPUT
|
||||
@@ -72,12 +68,14 @@ jobs:
|
||||
|
||||
- name: Calculate checksum
|
||||
id: checksum
|
||||
working-directory: release-${{ github.run_id }}
|
||||
run: |
|
||||
CHECKSUM=$(md5sum "${{ steps.jprm.outputs.artifact }}" | awk '{print $1}')
|
||||
echo "checksum=${CHECKSUM}" >> $GITHUB_OUTPUT
|
||||
echo "Checksum: ${CHECKSUM}"
|
||||
|
||||
- name: Create Release
|
||||
working-directory: release-${{ github.run_id }}
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
@@ -86,22 +84,20 @@ jobs:
|
||||
REPO_NAME="${{ github.event.repository.name }}"
|
||||
GITEA_URL="${{ github.server_url }}"
|
||||
|
||||
# Prepare release body
|
||||
RELEASE_BODY="SRFPlay Jellyfin Plugin ${{ steps.get_version.outputs.version }}\n\nSee attached files for plugin installation."
|
||||
RELEASE_BODY_JSON=$(echo -n "${RELEASE_BODY}" | jq -Rs .)
|
||||
# Build JSON payload with jq to handle escaping properly
|
||||
VERSION="${{ steps.get_version.outputs.version }}"
|
||||
PAYLOAD=$(jq -n \
|
||||
--arg tag "${VERSION}" \
|
||||
--arg name "Release ${VERSION}" \
|
||||
--arg body "SRFPlay Jellyfin Plugin ${VERSION}\n\nSee attached files for plugin installation." \
|
||||
'{tag_name: $tag, name: $name, body: $body, draft: false, prerelease: false}')
|
||||
|
||||
# Create release using Gitea API
|
||||
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
"${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/releases" \
|
||||
-d "{
|
||||
\"tag_name\": \"${{ steps.get_version.outputs.version }}\",
|
||||
\"name\": \"Release ${{ steps.get_version.outputs.version }}\",
|
||||
\"body\": ${RELEASE_BODY_JSON},
|
||||
\"draft\": false,
|
||||
\"prerelease\": false
|
||||
}")
|
||||
-d "${PAYLOAD}")
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
BODY=$(echo "$RESPONSE" | sed '$d')
|
||||
@@ -135,6 +131,7 @@ jobs:
|
||||
echo "View at: ${GITEA_URL}/${REPO_OWNER}/${REPO_NAME}/releases/tag/${{ steps.get_version.outputs.version }}"
|
||||
|
||||
- name: Update manifest.json
|
||||
working-directory: release-${{ github.run_id }}
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
@@ -168,3 +165,7 @@ jobs:
|
||||
git add manifest.json
|
||||
git commit -m "Update manifest.json for version ${VERSION}"
|
||||
git push origin master
|
||||
|
||||
- name: Cleanup
|
||||
if: always()
|
||||
run: rm -rf release-${{ github.run_id }}
|
||||
|
||||
@@ -17,22 +17,26 @@ on:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: linux/amd64
|
||||
container:
|
||||
image: gitea.tourolle.paris/dtourolle/srfplay-builder:latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Verify .NET installation
|
||||
run: dotnet --version
|
||||
with:
|
||||
path: test-${{ github.run_id }}
|
||||
|
||||
- name: Restore dependencies
|
||||
working-directory: test-${{ github.run_id }}
|
||||
run: dotnet restore Jellyfin.Plugin.SRFPlay.sln
|
||||
|
||||
- name: Build solution
|
||||
run: dotnet build Jellyfin.Plugin.SRFPlay.sln --configuration Debug --no-restore --no-self-contained
|
||||
working-directory: test-${{ github.run_id }}
|
||||
run: dotnet build Jellyfin.Plugin.SRFPlay.sln --configuration Debug --no-restore --no-self-contained /m:1
|
||||
|
||||
- name: Run tests
|
||||
working-directory: test-${{ github.run_id }}
|
||||
run: dotnet test Jellyfin.Plugin.SRFPlay.sln --no-build --configuration Debug --verbosity normal --logger "trx;LogFileName=test-results.trx"
|
||||
|
||||
- name: Upload test results
|
||||
@@ -40,5 +44,9 @@ jobs:
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: test-results
|
||||
path: '**/test-results.trx'
|
||||
path: test-${{ github.run_id }}/**/test-results.trx
|
||||
retention-days: 7
|
||||
|
||||
- name: Cleanup
|
||||
if: always()
|
||||
run: rm -rf test-${{ github.run_id }}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# SRFPlay Builder Image
|
||||
# Pre-built image with .NET SDK and JPRM for building Jellyfin plugins
|
||||
# Build: docker build -f Dockerfile.builder -t gitea.tourolle.paris/dtourolle/srfplay-builder:latest .
|
||||
# Push: docker push gitea.tourolle.paris/dtourolle/srfplay-builder:latest
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
python3 \
|
||||
python3-pip \
|
||||
git \
|
||||
jq \
|
||||
nodejs \
|
||||
npm \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN pip install --break-system-packages jprm
|
||||
|
||||
WORKDIR /src
|
||||
@@ -58,16 +58,28 @@ public class StreamProxyController : ControllerBase
|
||||
/// Livestreams need frequent manifest refresh, VOD can be cached longer.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID to check.</param>
|
||||
private void AddManifestCacheHeaders(string itemId)
|
||||
/// <param name="isVariantManifest">Whether this is a variant (sub) manifest with segment lists.</param>
|
||||
private void AddManifestCacheHeaders(string itemId, bool isVariantManifest = false)
|
||||
{
|
||||
var metadata = _proxyService.GetStreamMetadata(itemId);
|
||||
var isLiveStream = metadata?.IsLiveStream ?? false;
|
||||
|
||||
if (isLiveStream)
|
||||
{
|
||||
// Livestreams need frequent manifest refresh (segments rotate every ~6-10s)
|
||||
if (isVariantManifest)
|
||||
{
|
||||
// Variant manifests contain the segment list which changes every target duration.
|
||||
// Use no-cache to ensure the player always gets the freshest segment list,
|
||||
// preventing requests for segments that have been rotated out of the sliding window.
|
||||
Response.Headers["Cache-Control"] = "no-cache, no-store";
|
||||
}
|
||||
else
|
||||
{
|
||||
// Master manifest is relatively stable (just lists quality variants)
|
||||
Response.Headers["Cache-Control"] = "max-age=2, must-revalidate";
|
||||
_logger.LogDebug("Setting livestream cache headers for {ItemId}", itemId);
|
||||
}
|
||||
|
||||
_logger.LogDebug("Setting livestream cache headers for {ItemId} (variant={IsVariant})", itemId, isVariantManifest);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -215,7 +227,8 @@ public class StreamProxyController : ControllerBase
|
||||
var rewrittenContent = _proxyService.RewriteVariantManifestUrls(manifestContent, baseProxyUrl, queryParams);
|
||||
|
||||
// Set cache headers based on stream type (live vs VOD)
|
||||
AddManifestCacheHeaders(actualItemId);
|
||||
// Variant manifests use stricter no-cache for live streams
|
||||
AddManifestCacheHeaders(actualItemId, isVariantManifest: true);
|
||||
|
||||
_logger.LogDebug("Returning variant manifest for item {ItemId} ({Length} bytes)", itemId, rewrittenContent.Length);
|
||||
return Content(rewrittenContent, "application/vnd.apple.mpegurl; charset=utf-8");
|
||||
@@ -253,9 +266,12 @@ public class StreamProxyController : ControllerBase
|
||||
{
|
||||
// Pass the original query string to preserve segment-specific parameters (e.g., ?m=timestamp)
|
||||
var queryString = Request.QueryString.HasValue ? Request.QueryString.Value : null;
|
||||
var segmentData = await _proxyService.GetSegmentAsync(actualItemId, segmentPath, queryString, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (segmentData == null)
|
||||
// Use streaming proxy: starts forwarding data to the client before the full segment
|
||||
// is downloaded from the CDN, reducing time-to-first-byte for live streams
|
||||
using var upstreamResponse = await _proxyService.GetSegmentStreamAsync(actualItemId, segmentPath, queryString, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (upstreamResponse == null)
|
||||
{
|
||||
_logger.LogWarning("Segment not found - ItemId: {ItemId}, Path: {SegmentPath}", itemId, segmentPath);
|
||||
return NotFound();
|
||||
@@ -263,9 +279,18 @@ public class StreamProxyController : ControllerBase
|
||||
|
||||
// Determine content type based on file extension
|
||||
var contentType = MimeTypeHelper.GetSegmentContentType(segmentPath);
|
||||
Response.ContentType = contentType;
|
||||
|
||||
_logger.LogDebug("Returning segment {SegmentPath} ({Length} bytes, {ContentType})", segmentPath, segmentData.Length, contentType);
|
||||
return File(segmentData, contentType);
|
||||
if (upstreamResponse.Content.Headers.ContentLength.HasValue)
|
||||
{
|
||||
Response.ContentLength = upstreamResponse.Content.Headers.ContentLength.Value;
|
||||
}
|
||||
|
||||
// Stream directly from CDN to client without buffering the entire segment in memory
|
||||
await upstreamResponse.Content.CopyToAsync(Response.Body, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_logger.LogDebug("Streamed segment {SegmentPath} ({ContentType})", segmentPath, contentType);
|
||||
return new EmptyResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -52,6 +53,18 @@ public interface IStreamProxyService
|
||||
/// <returns>The segment content as bytes.</returns>
|
||||
Task<byte[]?> GetSegmentAsync(string itemId, string segmentPath, string? queryString = null, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Fetches a segment from the original source as a streaming response.
|
||||
/// Returns the HttpResponseMessage for streaming directly to the client, reducing TTFB.
|
||||
/// The caller is responsible for disposing the response.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID.</param>
|
||||
/// <param name="segmentPath">The segment path.</param>
|
||||
/// <param name="queryString">The original query string from the request.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>The HTTP response for streaming, or null if not found.</returns>
|
||||
Task<HttpResponseMessage?> GetSegmentStreamAsync(string itemId, string segmentPath, string? queryString = null, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Rewrites URLs in a variant (sub) manifest to point to the proxy.
|
||||
/// </summary>
|
||||
|
||||
@@ -647,6 +647,21 @@ public class StreamProxyService : IStreamProxyService
|
||||
// Rewrite the manifest to replace Akamai URLs with proxy URLs
|
||||
var rewrittenContent = RewriteManifestUrls(manifestContent, authenticatedUrl, baseProxyUrl);
|
||||
|
||||
// For live streams, inject #EXT-X-START to tell the player to start near the live edge
|
||||
// Without this, players may start at the beginning of the sliding window and stutter
|
||||
// as old segments get rotated out by the CDN
|
||||
if (_streamMappings.TryGetValue(itemId, out var streamInfoForManifest) && streamInfoForManifest.IsLiveStream)
|
||||
{
|
||||
if (!rewrittenContent.Contains("#EXT-X-START", StringComparison.Ordinal))
|
||||
{
|
||||
rewrittenContent = rewrittenContent.Replace(
|
||||
"#EXTM3U",
|
||||
"#EXTM3U\n#EXT-X-START:TIME-OFFSET=-6,PRECISE=NO",
|
||||
StringComparison.Ordinal);
|
||||
_logger.LogDebug("Injected #EXT-X-START tag for live stream {ItemId}", itemId);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogDebug("Rewritten manifest for item {ItemId} ({Length} bytes):\n{Content}", itemId, rewrittenContent.Length, rewrittenContent);
|
||||
return rewrittenContent;
|
||||
}
|
||||
@@ -720,6 +735,67 @@ public class StreamProxyService : IStreamProxyService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches a segment as a streaming response for direct forwarding to the client.
|
||||
/// Uses HttpCompletionOption.ResponseHeadersRead to start streaming before the full
|
||||
/// segment is downloaded, reducing time-to-first-byte for live streams.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID.</param>
|
||||
/// <param name="segmentPath">The segment path.</param>
|
||||
/// <param name="queryString">The original query string from the request.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>The HTTP response for streaming, or null if not found.</returns>
|
||||
public async Task<HttpResponseMessage?> GetSegmentStreamAsync(
|
||||
string itemId,
|
||||
string segmentPath,
|
||||
string? queryString = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var authenticatedUrl = await GetAuthenticatedUrlAsync(itemId, cancellationToken).ConfigureAwait(false);
|
||||
if (authenticatedUrl == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var baseUri = new Uri(authenticatedUrl);
|
||||
var baseUrl = $"{baseUri.Scheme}://{baseUri.Host}{string.Join('/', baseUri.AbsolutePath.Split('/')[..^1])}";
|
||||
|
||||
var queryParams = string.Empty;
|
||||
if (!string.IsNullOrEmpty(queryString))
|
||||
{
|
||||
queryParams = queryString.StartsWith('?') ? queryString : $"?{queryString}";
|
||||
}
|
||||
else if (!segmentPath.Contains("hdntl=", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
queryParams = baseUri.Query;
|
||||
}
|
||||
|
||||
var segmentUrl = $"{baseUrl}/{segmentPath}{queryParams}";
|
||||
|
||||
_logger.LogDebug("Streaming segment - SegmentPath: {SegmentPath}, FullUrl: {FullUrl}", segmentPath, segmentUrl);
|
||||
|
||||
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, segmentUrl);
|
||||
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogWarning("Segment stream request failed with {StatusCode} for {SegmentPath}", response.StatusCode, segmentPath);
|
||||
response.Dispose();
|
||||
return null;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to stream segment {SegmentPath} for item {ItemId}", segmentPath, itemId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rewrites URLs in HLS manifest to point to proxy.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user