Skip to content

Commit

Permalink
improvements: adding more spaces related exports (#51)
Browse files Browse the repository at this point in the history
* fix: adding missing exports + format

* info: replacing broadcasts with spaces for clickables spaces link

* info: moving guestReaction events from info to debug
  • Loading branch information
slkzgm authored Jan 15, 2025
1 parent 6678581 commit c15e68d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 39 deletions.
22 changes: 13 additions & 9 deletions src/_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ export { SearchMode } from './search';
export type { QueryProfilesResponse, QueryTweetsResponse } from './timeline-v1';
export type { Tweet } from './tweets';

export { Space } from './spaces/core/Space'
export { SpaceParticipant } from './spaces/core/SpaceParticipant'
export { Logger } from './spaces/logger'
export { SttTtsPlugin } from './spaces/plugins/SttTtsPlugin'
export { RecordToDiskPlugin } from './spaces/plugins/RecordToDiskPlugin'
export { MonitorAudioPlugin } from './spaces/plugins/MonitorAudioPlugin'
export { IdleMonitorPlugin } from './spaces/plugins/IdleMonitorPlugin'
export { HlsRecordPlugin } from './spaces/plugins/HlsRecordPlugin'
export { Space } from './spaces/core/Space';
export { SpaceParticipant } from './spaces/core/SpaceParticipant';
export { JanusClient } from './spaces/core/JanusClient';
export { JanusAudioSink, JanusAudioSource } from './spaces/core/JanusAudio';
export { ChatClient } from './spaces/core/ChatClient';
export { Logger } from './spaces/logger';
export { SttTtsPlugin } from './spaces/plugins/SttTtsPlugin';
export { RecordToDiskPlugin } from './spaces/plugins/RecordToDiskPlugin';
export { MonitorAudioPlugin } from './spaces/plugins/MonitorAudioPlugin';
export { IdleMonitorPlugin } from './spaces/plugins/IdleMonitorPlugin';
export { HlsRecordPlugin } from './spaces/plugins/HlsRecordPlugin';

export * from './types/spaces'
export * from './types/spaces';
export * from './spaces/types';
2 changes: 1 addition & 1 deletion src/spaces/core/ChatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class ChatClient extends EventEmitter {

// 5) Reaction => body.type=2
if (body?.type === 2) {
this.logger.info('[ChatClient] Emitting guestReaction =>', body);
this.logger.debug('[ChatClient] Emitting guestReaction =>', body);
this.emit('guestReaction', {
displayName: body.displayName,
emoji: body.body,
Expand Down
5 changes: 4 additions & 1 deletion src/spaces/core/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ export class Space extends EventEmitter {
this.setupChatEvents();
}

this.logger.info('[Space] Initialized =>', broadcast.share_url);
this.logger.info(
'[Space] Initialized =>',
broadcast.share_url.replace('broadcasts', 'spaces'),
);
this.isInitialized = true;

// Call plugin.init(...) and onJanusReady(...) for all plugins now that we're set
Expand Down
32 changes: 17 additions & 15 deletions src/spaces/plugins/HlsRecordPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export class HlsRecordPlugin implements Plugin {
// Use the same logger from onAttach
const broadcastInfo = (this.space as any)?.broadcastInfo;
if (!broadcastInfo || !broadcastInfo.broadcast?.media_key) {
this.logger?.warn('[HlsRecordPlugin] No media_key found in broadcastInfo');
this.logger?.warn(
'[HlsRecordPlugin] No media_key found in broadcastInfo',
);
return;
}
this.mediaKey = broadcastInfo.broadcast.media_key;
Expand All @@ -80,7 +82,7 @@ export class HlsRecordPlugin implements Plugin {
}

this.logger?.info(
`[HlsRecordPlugin] init => ready to record. Output path="${this.outputPath}"`,
`[HlsRecordPlugin] init => ready to record. Output path="${this.outputPath}"`,
);

// Listen for occupancy updates
Expand All @@ -104,7 +106,7 @@ export class HlsRecordPlugin implements Plugin {
}

this.logger?.debug(
`[HlsRecordPlugin] occupancy=${update.occupancy} => trying to fetch HLS URL...`,
`[HlsRecordPlugin] occupancy=${update.occupancy} => trying to fetch HLS URL...`,
);

const scraper = (this.space as any).scraper;
Expand All @@ -117,7 +119,7 @@ export class HlsRecordPlugin implements Plugin {
const status = await scraper.getAudioSpaceStreamStatus(this.mediaKey);
if (!status?.source?.location) {
this.logger?.debug(
'[HlsRecordPlugin] occupancy>0 but no HLS URL => wait next update',
'[HlsRecordPlugin] occupancy>0 but no HLS URL => wait next update',
);
return;
}
Expand All @@ -126,7 +128,7 @@ export class HlsRecordPlugin implements Plugin {
const isReady = await this.waitForHlsReady(hlsUrl, 1);
if (!isReady) {
this.logger?.debug(
'[HlsRecordPlugin] HLS URL 404 => waiting next occupancy update...',
'[HlsRecordPlugin] HLS URL 404 => waiting next occupancy update...',
);
return;
}
Expand All @@ -141,27 +143,27 @@ export class HlsRecordPlugin implements Plugin {
* maxRetries=1 => only try once here; rely on occupancy re-calls otherwise.
*/
private async waitForHlsReady(
hlsUrl: string,
maxRetries: number,
hlsUrl: string,
maxRetries: number,
): Promise<boolean> {
let attempt = 0;
while (attempt < maxRetries) {
try {
const resp = await fetch(hlsUrl, { method: 'HEAD' });
if (resp.ok) {
this.logger?.debug(
`[HlsRecordPlugin] HLS is ready (attempt #${attempt + 1})`,
`[HlsRecordPlugin] HLS is ready (attempt #${attempt + 1})`,
);
return true;
} else {
this.logger?.debug(
`[HlsRecordPlugin] HLS status=${resp.status}, retrying...`,
`[HlsRecordPlugin] HLS status=${resp.status}, retrying...`,
);
}
} catch (error) {
this.logger?.debug(
'[HlsRecordPlugin] HLS fetch error =>',
(error as Error).message,
'[HlsRecordPlugin] HLS fetch error =>',
(error as Error).message,
);
}
attempt++;
Expand All @@ -182,7 +184,7 @@ export class HlsRecordPlugin implements Plugin {

if (!this.outputPath) {
this.logger?.warn(
'[HlsRecordPlugin] No output path set, using /tmp/space_record.ts',
'[HlsRecordPlugin] No output path set, using /tmp/space_record.ts',
);
this.outputPath = '/tmp/space_record.ts';
}
Expand Down Expand Up @@ -211,8 +213,8 @@ export class HlsRecordPlugin implements Plugin {
this.recordingProcess.on('close', (code) => {
this.isRecording = false;
this.logger?.info(
'[HlsRecordPlugin] Recording process closed => code=',
code,
'[HlsRecordPlugin] Recording process closed => code=',
code,
);
});

Expand All @@ -233,4 +235,4 @@ export class HlsRecordPlugin implements Plugin {
this.isRecording = false;
}
}
}
}
8 changes: 4 additions & 4 deletions src/spaces/plugins/IdleMonitorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class IdleMonitorPlugin implements Plugin {
* @param checkEveryMs How frequently (in ms) to check for silence. (Default: 10s)
*/
constructor(
private idleTimeoutMs: number = 60_000,
private checkEveryMs: number = 10_000,
private idleTimeoutMs: number = 60_000,
private checkEveryMs: number = 10_000,
) {}

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ export class IdleMonitorPlugin implements Plugin {

if (idleMs >= this.idleTimeoutMs) {
this.logger?.warn(
`[IdleMonitorPlugin] idleTimeout => no audio for ${idleMs}ms`,
`[IdleMonitorPlugin] idleTimeout => no audio for ${idleMs}ms`,
);
this.space?.emit('idleTimeout', { idleMs });
}
Expand All @@ -98,4 +98,4 @@ export class IdleMonitorPlugin implements Plugin {
this.checkInterval = undefined;
}
}
}
}
11 changes: 4 additions & 7 deletions src/spaces/plugins/MonitorAudioPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Logger } from '../logger';
* Usage:
* const plugin = new MonitorAudioPlugin(48000, /* debug= *\/ true);
* space.use(plugin);
*/
*/
export class MonitorAudioPlugin implements Plugin {
private ffplay?: ChildProcessWithoutNullStreams;
private logger: Logger;
Expand All @@ -20,10 +20,7 @@ export class MonitorAudioPlugin implements Plugin {
* @param sampleRate The expected PCM sample rate (e.g. 16000 or 48000).
* @param debug If true, enables debug logging via Logger.
*/
constructor(
private readonly sampleRate = 48000,
debug = false,
) {
constructor(private readonly sampleRate = 48000, debug = false) {
this.logger = new Logger(debug);

// Spawn ffplay to read raw PCM (s16le) on stdin
Expand Down Expand Up @@ -51,7 +48,7 @@ export class MonitorAudioPlugin implements Plugin {
});

this.logger.info(
`[MonitorAudioPlugin] Started ffplay for real-time monitoring (sampleRate=${this.sampleRate})`
`[MonitorAudioPlugin] Started ffplay for real-time monitoring (sampleRate=${this.sampleRate})`,
);
}

Expand All @@ -62,7 +59,7 @@ export class MonitorAudioPlugin implements Plugin {
onAudioData(data: AudioDataWithUser): void {
// Log debug info
this.logger.debug(
`[MonitorAudioPlugin] onAudioData => userId=${data.userId}, samples=${data.samples.length}, sampleRate=${data.sampleRate}`,
`[MonitorAudioPlugin] onAudioData => userId=${data.userId}, samples=${data.samples.length}, sampleRate=${data.sampleRate}`,
);

if (!this.ffplay?.stdin.writable) {
Expand Down
4 changes: 2 additions & 2 deletions src/spaces/plugins/RecordToDiskPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class RecordToDiskPlugin implements Plugin {
const buf = Buffer.from(data.samples.buffer);
this.outStream.write(buf);
this.logger?.debug(
`[RecordToDiskPlugin] Wrote ${buf.byteLength} bytes from userId=${data.userId} to disk`,
`[RecordToDiskPlugin] Wrote ${buf.byteLength} bytes from userId=${data.userId} to disk`,
);
}

Expand All @@ -88,4 +88,4 @@ export class RecordToDiskPlugin implements Plugin {
this.outStream = undefined;
}
}
}
}

0 comments on commit c15e68d

Please sign in to comment.