Repository navigation
feat(media): implement video transcoding and HLS chunking pipeline with signed CloudFront cookies - #1977
Open
Alhaandr wants to merge 1 commit into
Conversation
…th signed CloudFront cookies
|
@Alhaandr is attempting to deploy a commit to the Ayomide Adeniran's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@Alhaandr Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This pull request implements an automated video transcoding and HTTP Live Streaming (HLS) chunking pipeline along with AWS CloudFront signed cookies validated against student enrollment records, resolving issue #1844 ([BE-097]).
Raw MP4 video lectures cause excessive buffering for students with fluctuating bandwidth and permit unauthorized downloads that bypass course enrollment paywalls. This solution converts uploaded MP4 videos into adaptive multi-bitrate HLS streams (1080p, 720p, 480p) using FFmpeg, generates standard variant and master playlists (
master.m3u8), and restricts access to HLS manifests and segment chunks (.ts) via cryptographic AWS CloudFront signed cookies tied to active student enrollment records.What changed
backend/src/workers/videoTranscodingWorker.ts):transcodeVideoToHlsusing FFmpeg CLI arguments for HLS chunking (6s target duration,mpegtschunks,vodplaylist type) and generated themaster.m3u8adaptive bitrate manifest.createVideoTranscodingProcessor) and integratedstartVideoTranscodingWorker/stopVideoTranscodingWorker/enqueueVideoTranscoding.backend/src/services/cloudfrontCookieService.ts):generateCloudFrontSignedCookiescreating minified custom policy statements with expiration and RSA-SHA1 signatures using CloudFront key pairs (CloudFront-Key-Pair-Id,CloudFront-Policy,CloudFront-Signature).verifyCloudFrontSignedCookiesfor signature validation, expiry checks, and wildcard resource pattern matching.backend/src/queues/queueManager.ts,backend/src/workers/queueWorkers.ts):VIDEO_TRANSCODING: 'video-transcoding'toQUEUE_NAMESand standard tier queue definitions.backend/src/controllers/mediaController.ts,backend/src/routes/media.routes.ts):POST&GET/media/courses/:courseId/videos/:videoId/session: Validates student enrollment against database records, returning HTTP 403 for unenrolled students, and sets signed CloudFront cookies on response for enrolled students.GET/media/courses/:courseId/videos/:videoId/hls/:file: Protects all manifests and.tsvideo chunks. Verifies CloudFront signed cookies (or authenticated student enrollment); rejects unauthorized/unenrolled requests with HTTP 403 Forbidden.POST/media/transcode: Protected route to enqueue video transcoding jobs.backend/src/middleware/auth.ts):optionalAuthenticateTokenhelper allowing routes (such as video segment streaming) to support either CloudFront cookies or Authorization headers.backend/tests/videoTranscodingWorker.test.ts: Unit tests covering HLS profiles, master playlist generation, FFmpeg argument construction, job validation, and BullMQ worker execution.backend/tests/cloudfrontCookies.test.ts: Unit tests for CloudFront base64 encoding, policy generation, signed cookie issuance, expiration, tampering, and signature verification.backend/tests/media.routes.test.ts: Integration tests verifying 401 unauthenticated, 403 for unenrolled users, 200 for enrolled students, and valid HLS chunk serving.Why
Course video lectures need to adapt dynamically to student connection speeds to prevent buffering while ensuring intellectual property protection and enrollment enforcement. Adaptive HLS streams switch bitrates seamlessly, and CloudFront signed cookies provide scalable edge-level tokenized authentication validated against enrollment records.
Testing performed
backend/tests/videoTranscodingWorker.test.ts(passed)backend/tests/cloudfrontCookies.test.ts(passed)backend/tests/media.routes.test.ts(passed)backend/tests/queueWorkers.test.ts(passed)npm --prefix backend run build):tsc) finished with zero errors.Risks considered
CLOUDFRONT_PRIVATE_KEYis not present, an in-memory keypair is automatically generated to allow local test execution and development without external dependencies. In production, keys are loaded from environment variables.concurrency: 2) and per-user rate limiting to prevent CPU exhaustion.Edge cases handled
.tsvideo segments with expired, tampered, or missing cookies receive HTTP 403.UnrecoverableErrorwithout burning retries.Closes #1844