spot: fixing inconsistent recording

This commit is contained in:
nick-delirium 2024-10-08 17:48:45 +02:00
parent 4cfc5d2517
commit fca752526d
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
5 changed files with 50 additions and 32 deletions

View file

@ -5,7 +5,7 @@ let checkBusy = false;
export default defineBackground(() => { export default defineBackground(() => {
const CHECK_INT = 60 * 1000; const CHECK_INT = 60 * 1000;
const PING_INT = 30 * 1000; const PING_INT = 30 * 1000;
const VER = "1.0.7"; const VER = "1.0.10";
const messages = { const messages = {
popup: { popup: {
@ -42,6 +42,7 @@ export default defineBackground(() => {
checkNewTab: "ort:check-new-tab", checkNewTab: "ort:check-new-tab",
started: "ort:started", started: "ort:started",
stopped: "ort:stopped", stopped: "ort:stopped",
toStop: "ort:stop",
restart: "ort:restart", restart: "ort:restart",
getErrorEvents: "ort:get-error-events", getErrorEvents: "ort:get-error-events",
}, },
@ -63,6 +64,7 @@ export default defineBackground(() => {
to: { to: {
checkRecStatus: "offscr:check-status", checkRecStatus: "offscr:check-status",
startRecording: "offscr:start-recording", startRecording: "offscr:start-recording",
stopRecording: "offscr:stop-recording",
}, },
}, },
}; };
@ -661,10 +663,14 @@ export default defineBackground(() => {
errorData, errorData,
}); });
} }
if (request.type === "ort:stop") { if (request.type === messages.content.from.toStop) {
if (recordingState.recording === REC_STATE.stopped) {
return console.error('Calling stopped recording?')
}
console.log('calling stop')
browser.runtime browser.runtime
.sendMessage({ .sendMessage({
type: "offscr:stop-recording", type: messages.offscreen.to.stopRecording,
target: "offscreen", target: "offscreen",
}) })
.then((r) => { .then((r) => {
@ -999,6 +1005,7 @@ export default defineBackground(() => {
let activeTab = activeTabs[0]; let activeTab = activeTabs[0];
const sendTo = message.activeTabId || activeTab.id!; const sendTo = message.activeTabId || activeTab.id!;
let attempts = 0; let attempts = 0;
// 10 seconds;
while (!contentArmy[sendTo] && attempts < 100) { while (!contentArmy[sendTo] && attempts < 100) {
await new Promise((resolve) => setTimeout(resolve, 100)); await new Promise((resolve) => setTimeout(resolve, 100));
attempts++; attempts++;
@ -1142,18 +1149,16 @@ export default defineBackground(() => {
} }
const trackedTab: number | null = usedTab ?? null; const trackedTab: number | null = usedTab ?? null;
/** reloads ui on currently active tab once its reloads itself */ /** reloads ui on currently active tab once its reloads itself */
function tabUpdateListener(tabId: number, changeInfo: any) { function tabNavigatedListener(details: { tabId: number }) {
const state = getRecState(); const state = getRecState();
if (state === REC_STATE.stopped) { if (state === REC_STATE.stopped) {
return stopTabListening(); return stopNavListening();
} }
contentArmy[details.tabId] = false
if (changeInfo.status !== "complete") { if (area === "tab" && (!trackedTab || details.tabId !== trackedTab)) {
return (contentArmy[tabId] = false);
}
if (area === "tab" && (!trackedTab || tabId !== trackedTab)) {
return; return;
} }
@ -1174,6 +1179,13 @@ export default defineBackground(() => {
}); });
} }
function startNavListening() {
browser.webNavigation.onCompleted.addListener(tabNavigatedListener)
}
function stopNavListening() {
browser.webNavigation.onCompleted.removeListener(tabNavigatedListener)
}
/** discards recording if was recording single tab and its now closed */ /** discards recording if was recording single tab and its now closed */
function tabRemovedListener(tabId: number) { function tabRemovedListener(tabId: number) {
if (tabId === trackedTab) { if (tabId === trackedTab) {
@ -1200,15 +1212,7 @@ export default defineBackground(() => {
browser.tabs.onRemoved.removeListener(tabRemovedListener); browser.tabs.onRemoved.removeListener(tabRemovedListener);
} }
function startTabListening() { startNavListening();
browser.tabs.onUpdated.addListener(tabUpdateListener);
}
function stopTabListening() {
browser.tabs.onUpdated.removeListener(tabUpdateListener);
}
startTabListening();
if (area === "desktop") { if (area === "desktop") {
// if desktop, watch for tab change events // if desktop, watch for tab change events
startTabActivationListening(); startTabActivationListening();
@ -1218,7 +1222,7 @@ export default defineBackground(() => {
startRemovedListening(); startRemovedListening();
} }
setOnStop(() => { setOnStop(() => {
stopTabListening(); stopNavListening();
if (area === "desktop") { if (area === "desktop") {
stopTabActivationListening(); stopTabActivationListening();
} }

View file

@ -125,6 +125,7 @@ export default defineContentScript({
stopClickRecording(); stopClickRecording();
stopLocationRecording(); stopLocationRecording();
const result = await browser.runtime.sendMessage({ type: "ort:stop" }); const result = await browser.runtime.sendMessage({ type: "ort:stop" });
console.log('Spot getting video:', result)
if (result.status === "full") { if (result.status === "full") {
chunksReady = true; chunksReady = true;
data = result; data = result;
@ -323,7 +324,9 @@ export default defineContentScript({
}); });
} }
void browser.runtime.sendMessage({ type: "ort:content-ready" }); setInterval(() => {
void browser.runtime.sendMessage({ type: "ort:content-ready" });
}, 500)
// @ts-ignore false positive // @ts-ignore false positive
browser.runtime.onMessage.addListener((message: any, resp) => { browser.runtime.onMessage.addListener((message: any, resp) => {
if (message.type === "content:mount") { if (message.type === "content:mount") {

View file

@ -1,3 +1,6 @@
/**
* 24 MB; hardlimit for video chunk
* */
const hardLimit = 24 * 1024 * 1024; // 24 MB const hardLimit = 24 * 1024 * 1024; // 24 MB
function getRecordingSettings(qualityValue) { function getRecordingSettings(qualityValue) {
@ -124,6 +127,7 @@ class ScreenRecorder {
this.mRecorder.start(); this.mRecorder.start();
this.isRecording = true; this.isRecording = true;
this.trackDuration(); this.trackDuration();
console.log('started recording inside startRecording');
} }
stop() { stop() {
@ -217,20 +221,24 @@ class ScreenRecorder {
}, },
})); }));
} catch (e) { } catch (e) {
console.error(e); console.error('get stream error:', e);
throw e; throw e;
} }
// try {
// microphoneStream = await navigator.mediaDevices.getUserMedia({
// audio: { echoCancellation: false, deviceId: audioId },
// });
// this.audioTrack = microphoneStream.getAudioTracks()[0];
// if (!useMicrophone) {
// this.audioTrack.enabled = false;
// }
// } catch (e) {
// console.error('get audio error', e);
// }
try { try {
microphoneStream = await navigator.mediaDevices.getUserMedia({
audio: { echoCancellation: false, deviceId: audioId },
});
this.audioTrack = microphoneStream.getAudioTracks()[0];
if (!useMicrophone) {
this.audioTrack.enabled = false;
}
} catch (e) {
console.error(e);
this.audioTrack = this.createPlaceholderAudioTrack(); this.audioTrack = this.createPlaceholderAudioTrack();
} catch (e) {
console.error('get audio error', e);
} }
const existingAudioTracks = this.videoStream.getAudioTracks(); const existingAudioTracks = this.videoStream.getAudioTracks();
@ -312,6 +320,7 @@ browser.runtime.onMessage.addListener((message, _, respond) => {
message.audioId, message.audioId,
) )
.then(() => { .then(() => {
console.log('started recording');
respond({ success: true, time: Date.now() }); respond({ success: true, time: Date.now() });
}) })
.catch(e => { .catch(e => {
@ -336,6 +345,7 @@ browser.runtime.onMessage.addListener((message, _, respond) => {
recorder.stop(); recorder.stop();
const duration = recorder.duration; const duration = recorder.duration;
recorder.getVideoData().then((data) => { recorder.getVideoData().then((data) => {
console.log(data)
if (!data.blob) { if (!data.blob) {
respond({ status: "empty" }); respond({ status: "empty" });
} }

View file

@ -2,7 +2,7 @@
"name": "wxt-starter", "name": "wxt-starter",
"description": "manifest.json description", "description": "manifest.json description",
"private": true, "private": true,
"version": "1.0.9", "version": "1.0.10",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "wxt", "dev": "wxt",

View file

@ -25,6 +25,7 @@ export default defineConfig({
"tabCapture", "tabCapture",
"offscreen", "offscreen",
"unlimitedStorage", "unlimitedStorage",
"webNavigation",
], ],
}, },
}); });