mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-24 04:27:27 +08:00
feat(electron): create recording through tray (#10526)
- added tray menu for controlling recording status - recording watcher for monitoring system audio input events
This commit is contained in:
@@ -28,12 +28,13 @@ console.log(`📁 Ensuring recordings directory exists at ${RECORDING_DIR}`);
|
||||
|
||||
// Types
|
||||
interface Recording {
|
||||
app: TappableApplication;
|
||||
app: TappableApplication | null;
|
||||
appGroup: Application | null;
|
||||
buffers: Float32Array[];
|
||||
stream: AudioTapStream;
|
||||
startTime: number;
|
||||
isWriting: boolean;
|
||||
isGlobal?: boolean;
|
||||
}
|
||||
|
||||
interface RecordingStatus {
|
||||
@@ -54,6 +55,7 @@ interface RecordingMetadata {
|
||||
sampleRate: number;
|
||||
channels: number;
|
||||
totalSamples: number;
|
||||
isGlobal?: boolean;
|
||||
}
|
||||
|
||||
interface AppInfo {
|
||||
@@ -118,7 +120,7 @@ app.use(
|
||||
async function saveRecording(recording: Recording): Promise<string | null> {
|
||||
try {
|
||||
recording.isWriting = true;
|
||||
const app = recording.appGroup || recording.app;
|
||||
const app = recording.isGlobal ? null : recording.appGroup || recording.app;
|
||||
|
||||
const totalSamples = recording.buffers.reduce(
|
||||
(acc, buf) => acc + buf.length,
|
||||
@@ -133,9 +135,19 @@ async function saveRecording(recording: Recording): Promise<string | null> {
|
||||
const channelCount = recording.stream.channels;
|
||||
const expectedSamples = recordingDuration * actualSampleRate;
|
||||
|
||||
console.log(`💾 Saving recording for ${app.name}:`);
|
||||
console.log(`- Process ID: ${app.processId}`);
|
||||
console.log(`- Bundle ID: ${app.bundleIdentifier}`);
|
||||
if (recording.isGlobal) {
|
||||
console.log('💾 Saving global recording:');
|
||||
} else {
|
||||
const appName = app?.name ?? 'Unknown App';
|
||||
const processId = app?.processId ?? 0;
|
||||
const bundleId = app?.bundleIdentifier ?? 'unknown';
|
||||
console.log(`💾 Saving recording for ${appName}:`);
|
||||
if (app) {
|
||||
console.log(`- Process ID: ${processId}`);
|
||||
console.log(`- Bundle ID: ${bundleId}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`- Actual duration: ${recordingDuration.toFixed(2)}s`);
|
||||
console.log(`- Sample rate: ${actualSampleRate}Hz`);
|
||||
console.log(`- Channels: ${channelCount}`);
|
||||
@@ -156,7 +168,9 @@ async function saveRecording(recording: Recording): Promise<string | null> {
|
||||
await fs.ensureDir(RECORDING_DIR);
|
||||
|
||||
const timestamp = Date.now();
|
||||
const baseFilename = `${recording.app.bundleIdentifier}-${recording.app.processId}-${timestamp}`;
|
||||
const baseFilename = recording.isGlobal
|
||||
? `global-recording-${timestamp}`
|
||||
: `${app?.bundleIdentifier ?? 'unknown'}-${app?.processId ?? 0}-${timestamp}`;
|
||||
const recordingDir = `${RECORDING_DIR}/${baseFilename}`;
|
||||
await fs.ensureDir(recordingDir);
|
||||
|
||||
@@ -189,7 +203,7 @@ async function saveRecording(recording: Recording): Promise<string | null> {
|
||||
console.log('✅ Transcription MP3 file written successfully');
|
||||
|
||||
// Save app icon if available
|
||||
if (app.icon) {
|
||||
if (app?.icon) {
|
||||
console.log(`📝 Writing app icon to ${iconFilename}`);
|
||||
await fs.writeFile(iconFilename, app.icon);
|
||||
console.log('✅ App icon written successfully');
|
||||
@@ -198,15 +212,16 @@ async function saveRecording(recording: Recording): Promise<string | null> {
|
||||
console.log(`📝 Writing metadata to ${metadataFilename}`);
|
||||
// Save metadata with the actual sample rate from the stream
|
||||
const metadata: RecordingMetadata = {
|
||||
appName: app.name,
|
||||
bundleIdentifier: app.bundleIdentifier,
|
||||
processId: app.processId,
|
||||
appName: app?.name ?? 'Global Recording',
|
||||
bundleIdentifier: app?.bundleIdentifier ?? 'system.global',
|
||||
processId: app?.processId ?? -1,
|
||||
recordingStartTime: recording.startTime,
|
||||
recordingEndTime,
|
||||
recordingDuration,
|
||||
sampleRate: actualSampleRate,
|
||||
channels: channelCount,
|
||||
totalSamples,
|
||||
isGlobal: recording.isGlobal,
|
||||
};
|
||||
|
||||
await fs.writeJson(metadataFilename, metadata, { spaces: 2 });
|
||||
@@ -222,8 +237,8 @@ async function saveRecording(recording: Recording): Promise<string | null> {
|
||||
function getRecordingStatus(): RecordingStatus[] {
|
||||
return Array.from(recordingMap.entries()).map(([processId, recording]) => ({
|
||||
processId,
|
||||
bundleIdentifier: recording.app.bundleIdentifier,
|
||||
name: recording.app.name,
|
||||
bundleIdentifier: recording.app?.bundleIdentifier ?? 'system.global',
|
||||
name: recording.app?.name ?? 'Global Recording',
|
||||
startTime: recording.startTime,
|
||||
duration: Date.now() - recording.startTime,
|
||||
}));
|
||||
@@ -289,8 +304,11 @@ async function stopRecording(processId: number) {
|
||||
}
|
||||
|
||||
const app = recording.appGroup || recording.app;
|
||||
const appName =
|
||||
app?.name ?? (recording.isGlobal ? 'Global Recording' : 'Unknown App');
|
||||
const appPid = app?.processId ?? processId;
|
||||
|
||||
console.log(`⏹️ Stopping recording for ${app.name} (PID: ${app.processId})`);
|
||||
console.log(`⏹️ Stopping recording for ${appName} (PID: ${appPid})`);
|
||||
console.log(
|
||||
`⏱️ Recording duration: ${((Date.now() - recording.startTime) / 1000).toFixed(2)}s`
|
||||
);
|
||||
@@ -302,7 +320,7 @@ async function stopRecording(processId: number) {
|
||||
if (filename) {
|
||||
console.log(`✅ Recording saved successfully to ${filename}`);
|
||||
} else {
|
||||
console.error(`❌ Failed to save recording for ${app.name}`);
|
||||
console.error(`❌ Failed to save recording for ${appName}`);
|
||||
}
|
||||
|
||||
emitRecordingStatus();
|
||||
@@ -541,7 +559,13 @@ function listenToAppStateChanges(apps: AppInfo[]) {
|
||||
|
||||
appsSubscriber();
|
||||
appsSubscriber = () => {
|
||||
subscribers.forEach(subscriber => subscriber.unsubscribe());
|
||||
subscribers.forEach(subscriber => {
|
||||
try {
|
||||
subscriber.unsubscribe();
|
||||
} catch {
|
||||
// ignore unsubscribe error
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@@ -606,8 +630,8 @@ app.get('/apps/saved', rateLimiter, async (_req, res) => {
|
||||
// Utility function to validate and sanitize folder name
|
||||
function validateAndSanitizeFolderName(folderName: string): string | null {
|
||||
// Allow alphanumeric characters, hyphens, dots (for bundle IDs)
|
||||
// Format: bundleId-processId-timestamp
|
||||
if (!/^[\w.-]+-\d+-\d+$/.test(folderName)) {
|
||||
// Format: bundleId-processId-timestamp OR global-recording-timestamp
|
||||
if (!/^([\w.-]+-\d+-\d+|global-recording-\d+)$/.test(folderName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -788,6 +812,65 @@ app.post(
|
||||
}
|
||||
);
|
||||
|
||||
async function startGlobalRecording() {
|
||||
const GLOBAL_RECORDING_ID = -1;
|
||||
if (recordingMap.has(GLOBAL_RECORDING_ID)) {
|
||||
console.log('⚠️ Global recording already in progress');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('🎙️ Starting global recording');
|
||||
|
||||
const buffers: Float32Array[] = [];
|
||||
const stream = ShareableContent.tapGlobalAudio(
|
||||
null,
|
||||
(err: Error | null, samples: Float32Array) => {
|
||||
if (err) {
|
||||
console.error('❌ Global audio stream error:', err);
|
||||
return;
|
||||
}
|
||||
const recording = recordingMap.get(GLOBAL_RECORDING_ID);
|
||||
if (recording && !recording.isWriting) {
|
||||
buffers.push(new Float32Array(samples));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
recordingMap.set(GLOBAL_RECORDING_ID, {
|
||||
app: null,
|
||||
appGroup: null,
|
||||
buffers,
|
||||
stream,
|
||||
startTime: Date.now(),
|
||||
isWriting: false,
|
||||
isGlobal: true,
|
||||
});
|
||||
|
||||
console.log('✅ Global recording started successfully');
|
||||
emitRecordingStatus();
|
||||
} catch (error) {
|
||||
console.error('❌ Error starting global recording:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Add API endpoint for global recording
|
||||
app.post('/global/record', async (_req, res) => {
|
||||
try {
|
||||
await startGlobalRecording();
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('❌ Error starting global recording:', error);
|
||||
res.status(500).json({ error: 'Failed to start global recording' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/global/stop', async (_req, res) => {
|
||||
const GLOBAL_RECORDING_ID = -1;
|
||||
await stopRecording(GLOBAL_RECORDING_ID);
|
||||
res.json({ success: true });
|
||||
});
|
||||
|
||||
// Start server
|
||||
httpServer.listen(PORT, () => {
|
||||
console.log(`
|
||||
|
||||
Reference in New Issue
Block a user