-
Notifications
You must be signed in to change notification settings - Fork 221
Cache waveform peaks and use direct URLs for faster replay loads #1223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hilsonshrestha
wants to merge
8
commits into
dev
Choose a base branch
from
1174-replay-videos-should-load-faster
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,826
−32
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc67285
Cache waveform peaks and use direct URLs for faster replay loads
hilsonshrestha 5c20644
Display warning message for video replay when participant used a diff…
hilsonshrestha e2e25f8
Add Firebase Cloud Functions for screen recording conversion
hilsonshrestha a9e4017
Skip mp4 and save without extension but with 'converted' metadata
hilsonshrestha c1958fe
Check for webm compatible copy
hilsonshrestha 4340a31
add eslint-config-airbnb-base and fix eslint
hilsonshrestha e82df5a
don't mark waveform as loaded for screen-only recordings; fix message…
hilsonshrestha 1cb2444
Merge branch 'dev' into 1174-replay-videos-should-load-faster
hilsonshrestha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,4 +38,5 @@ supabase/volumes/* | |
| supabase/volumes/db/data | ||
| !supabase/volumes/api/ | ||
|
|
||
| .firebaserc | ||
| coverage/ | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "functions": [ | ||
| { | ||
| "source": "functions", | ||
| "codebase": "default", | ||
| "disallowLegacyRuntimeConfig": true, | ||
| "ignore": [ | ||
| "node_modules", | ||
| ".git", | ||
| "firebase-debug.log", | ||
| "firebase-debug.*.log", | ||
| "*.local" | ||
| ], | ||
| "predeploy": [ | ||
| "yarn --cwd \"$RESOURCE_DIR\" lint", | ||
| "yarn --cwd \"$RESOURCE_DIR\" build" | ||
| ] | ||
| } | ||
| ] | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| VITE_FIREBASE_CONFIG=' | ||
| { | ||
| apiKey: "AIzaSyAm9QtUgx1lYPDeE0vKLN-lK17WfUGVkLo", | ||
| authDomain: "revisit-utah.firebaseapp.com", | ||
| projectId: "revisit-utah", | ||
| storageBucket: "revisit-utah.appspot.com", | ||
| messagingSenderId: "811568460432", | ||
| appId: "1:811568460432:web:995f6b4f1fc8042b5dde15" | ||
| } | ||
| ' |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| module.exports = { | ||
| root: true, | ||
| env: { | ||
| es6: true, | ||
| node: true, | ||
| }, | ||
| extends: [ | ||
| 'airbnb-base', | ||
| 'eslint:recommended', | ||
| 'plugin:import/typescript', | ||
| 'plugin:@typescript-eslint/recommended', | ||
| ], | ||
| parser: '@typescript-eslint/parser', | ||
| parserOptions: { | ||
| project: ['tsconfig.json'], | ||
| sourceType: 'module', | ||
| }, | ||
| ignorePatterns: [ | ||
| '/lib/**/*', // Ignore built files. | ||
| '/generated/**/*', // Ignore generated files. | ||
| '.eslintrc.js', | ||
| ], | ||
| plugins: [ | ||
| '@typescript-eslint', | ||
| 'import', | ||
| ], | ||
| rules: { | ||
| quotes: ['error', 'single'], | ||
| 'import/no-unresolved': 0, | ||
| indent: ['error', 2], | ||
| 'valid-jsdoc': 'off', | ||
| 'require-jsdoc': 'off', | ||
| 'import/prefer-default-export': 'off', | ||
| 'no-restricted-syntax': 'off', | ||
| }, | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Compiled JavaScript files | ||
| lib/**/*.js | ||
| lib/**/*.js.map | ||
|
|
||
| # TypeScript v1 declaration files | ||
| typings/ | ||
|
|
||
| # Node.js dependency directory | ||
| node_modules/ | ||
| *.local |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Firebase Functions Setup | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [Firebase CLI](https://firebase.google.com/docs/cli) installed and authenticated | ||
| - [gcloud CLI](https://cloud.google.com/sdk/docs/install) installed and authenticated | ||
| - Access to your Firebase project | ||
|
|
||
| ## 1. Configure Environment Variables | ||
|
|
||
| Update to `.env` in the **functions** directory with your required values: | ||
|
|
||
| ``` | ||
| VITE_FIREBASE_CONFIG=' | ||
| { | ||
| apiKey: "YOUR_API_KEY", | ||
| authDomain: "YOUR_PROJECT_ID.firebaseapp.com", | ||
| projectId: "YOUR_PROJECT_ID", | ||
| storageBucket: "YOUR_PROJECT_ID.appspot.com", | ||
| messagingSenderId: "YOUR_MESSAGING_SENDER_ID", | ||
| appId: "YOUR_APP_ID" | ||
| } | ||
| ' | ||
| ``` | ||
|
|
||
| You can find these values in the Firebase console under **Project Settings > Your apps**. | ||
| They should be the same as the ones in your `.env` from your root folder. | ||
|
|
||
| ## 2. Grant IAM Permissions | ||
|
|
||
| These steps are required to allow Firebase/Eventarc to respond to Cloud Storage events. | ||
|
|
||
| ### 2a. Get your project number | ||
|
|
||
| ```bash | ||
| gcloud projects describe YOUR_PROJECT_ID --format="value(projectNumber)" | ||
| ``` | ||
|
|
||
| Replace `YOUR_PROJECT_ID` with your Firebase project ID. Save the output — you'll use it as `PROJECT_NUMBER` in the next steps. | ||
|
|
||
| ### 2b. Grant Storage Admin to the Eventarc service account | ||
|
|
||
| ```bash | ||
| gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ | ||
| --member="serviceAccount:[email protected]" \ | ||
| --role="roles/storage.admin" | ||
| ``` | ||
|
|
||
| ## 3. Deploy Functions | ||
|
|
||
| Install dependencies: | ||
|
|
||
| ```bash | ||
| yarn | ||
| ``` | ||
|
|
||
| Then deploy: | ||
|
|
||
| ```bash | ||
| yarn deploy | ||
| ``` |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "name": "functions", | ||
| "scripts": { | ||
| "lint": "ESLINT_USE_FLAT_CONFIG=false eslint --ext .js,.ts .", | ||
| "build": "tsc", | ||
| "build:watch": "tsc --watch", | ||
| "serve": "yarn build && firebase emulators:start --only functions", | ||
| "shell": "yarn build && firebase functions:shell", | ||
| "start": "npm run shell", | ||
| "deploy": "firebase deploy --only functions", | ||
| "logs": "firebase functions:log" | ||
| }, | ||
| "engines": { | ||
| "node": "22" | ||
| }, | ||
| "main": "lib/index.js", | ||
| "dependencies": { | ||
| "@ffmpeg-installer/ffmpeg": "^1.1.0", | ||
| "firebase-admin": "^13.6.0", | ||
| "firebase-functions": "^7.0.0", | ||
| "fluent-ffmpeg": "^2.1.3", | ||
| "hjson": "^3.2.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/fluent-ffmpeg": "^2.1.27", | ||
| "@types/hjson": "^2.4.6", | ||
| "@typescript-eslint/eslint-plugin": "^8.0.0", | ||
| "@typescript-eslint/parser": "^8.0.0", | ||
| "eslint": "^8.9.0", | ||
| "eslint-config-airbnb-base": "^15.0.0", | ||
| "eslint-plugin-import": "^2.25.4", | ||
| "firebase-functions-test": "^3.4.1", | ||
| "typescript": "^5.7.3" | ||
| }, | ||
| "private": true | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { setGlobalOptions } from 'firebase-functions'; | ||
| import { onObjectFinalized } from 'firebase-functions/v2/storage'; | ||
| import * as logger from 'firebase-functions/logger'; | ||
| import * as admin from 'firebase-admin'; | ||
| import * as path from 'path'; | ||
| import * as os from 'os'; | ||
| import * as fs from 'fs'; | ||
| import ffmpegInstaller from '@ffmpeg-installer/ffmpeg'; | ||
| import ffmpeg from 'fluent-ffmpeg'; | ||
| import { parse as hjsonParse } from 'hjson'; | ||
|
|
||
| const firebaseConfig = hjsonParse(process.env.VITE_FIREBASE_CONFIG ?? '{}'); | ||
| const BUCKET: string = firebaseConfig.storageBucket; | ||
|
|
||
| admin.initializeApp(); | ||
| setGlobalOptions({ maxInstances: 5 }); | ||
| ffmpeg.setFfmpegPath(ffmpegInstaller.path); | ||
|
|
||
| const SCREEN_RECORDING_PATH = /^[^/]+\/screenRecording\//; | ||
| const WEBM_COMPATIBLE_CODECS = new Set(['vp8', 'vp9', 'av1', 'opus', 'vorbis']); | ||
|
|
||
| function isWebmCopyCompatible(filePath: string): Promise<boolean> { | ||
| return new Promise((resolve) => { | ||
| ffmpeg.ffprobe(filePath, (err, metadata) => { | ||
| if (err) { | ||
| resolve(false); | ||
| return; | ||
| } | ||
| const streams = metadata.streams ?? []; | ||
| resolve(streams.every((s) => !s.codec_name || WEBM_COMPATIBLE_CODECS.has(s.codec_name))); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| export const convertScreenRecording = onObjectFinalized( | ||
| { | ||
| bucket: BUCKET, memory: '1GiB', timeoutSeconds: 60, maxInstances: 10, | ||
| }, | ||
| async (event) => { | ||
| const filePath = event.data.name; | ||
|
|
||
| if (!SCREEN_RECORDING_PATH.test(filePath)) return; | ||
|
|
||
| // Prevent re-trigger loop: uploading back to the same path fires onObjectFinalized again | ||
| if (event.data.metadata?.converted === 'true') { | ||
| logger.info(`Skipping already-converted: ${filePath}`); | ||
| return; | ||
| } | ||
|
|
||
| const fileName = path.basename(filePath); | ||
| const tmpInput = path.join(os.tmpdir(), fileName); | ||
| const tmpOutput = path.join(os.tmpdir(), `${fileName}.tmp`); | ||
| const bucket = admin.storage().bucket(BUCKET); | ||
|
|
||
| try { | ||
| logger.info(`Downloading ${filePath}`); | ||
| await bucket.file(filePath).download({ destination: tmpInput }); | ||
|
|
||
| if (!await isWebmCopyCompatible(tmpInput)) { | ||
| logger.info(`Skipping: codecs not compatible with WebM stream copy: ${filePath}`); | ||
| return; | ||
| } | ||
|
|
||
| logger.info('Converting to webm'); | ||
| await new Promise<void>((resolve, reject) => { | ||
| ffmpeg(tmpInput) | ||
| .outputOptions('-c', 'copy', '-f', 'webm') | ||
| .output(tmpOutput) | ||
| .on('end', () => resolve()) | ||
| .on('error', (err: Error) => reject(err)) | ||
| .run(); | ||
| }); | ||
|
|
||
| logger.info(`Uploading ${filePath}`); | ||
| await bucket.upload(tmpOutput, { | ||
| destination: filePath, | ||
| metadata: { contentType: 'video/webm', metadata: { converted: 'true' } }, | ||
| }); | ||
|
|
||
| logger.info(`Done: ${filePath}`); | ||
| } catch (err) { | ||
| logger.error(`Conversion failed for ${filePath}`, err); | ||
| throw err; | ||
| } finally { | ||
| for (const f of [tmpInput, tmpOutput]) { | ||
| try { | ||
| if (fs.existsSync(f)) fs.unlinkSync(f); | ||
| } catch { /* ignore */ } | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "include": [ | ||
| ".eslintrc.js" | ||
| ] | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "module": "NodeNext", | ||
| "esModuleInterop": true, | ||
| "moduleResolution": "nodenext", | ||
| "noImplicitReturns": true, | ||
| "noUnusedLocals": true, | ||
| "outDir": "lib", | ||
| "sourceMap": true, | ||
| "strict": true, | ||
| "target": "ESNext", | ||
| }, | ||
| "compileOnSave": true, | ||
| "include": ["src"], | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Second-generation functions can process events concurrently, but these temp paths use only the object basename. Identically named participant/task recordings from different studies can overwrite each other and corrupt the converted upload. Please create a unique temp directory per event (for example with
fs.mkdtemp) or include a full-path hash/event ID in both paths, then clean it up infinally.