Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
fix return audio distortion
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandawg93 committed Oct 1, 2020
1 parent 4b93c39 commit c9f4626
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/nest/streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class NexusStreamer {
private ffmpegAudio: FfmpegProcess | undefined;
private ffmpegReturnAudio: FfmpegProcess | undefined;
private authorized = false;
private videoStarted = false;
private returnAudioStarted = false;
private readonly log: Logging;
private readonly config: NestConfig;
private sessionID: number = Math.floor(Math.random() * 100);
Expand All @@ -32,7 +34,6 @@ export class NexusStreamer {
private videoChannelID = -1;
private audioChannelID = -1;
private returnAudioTimeout: NodeJS.Timeout | undefined;
private started = false;

constructor(
cameraInfo: CameraInfo,
Expand All @@ -57,7 +58,7 @@ export class NexusStreamer {
* Close the socket and stop playback
*/
stopPlayback(): void {
this.started = false;
this.videoStarted = false;
if (this.socket) {
this.sendStopPlayback();
this.socket.close();
Expand Down Expand Up @@ -92,20 +93,24 @@ export class NexusStreamer {
}
this.returnAudioTimeout = setTimeout(() => {
self.sendAudioPayload(Buffer.from([]));
}, 1000);
}, 500);
});
this.returnAudioStarted = true;
}
}

/**
* Setup socket communication and send hello packet
* @param {string} host The websocket server address
*/
private setupConnection(host: string): void {
const self = this;
let pingInterval: NodeJS.Timeout;

this.stopPlayback();
this.createReturnAudioServer();
if (!this.returnAudioStarted) {
this.createReturnAudioServer();
}
this.socket = new WebSocket(`wss://${host}/nexustalk`);
this.socket.on('open', () => {
self.log.info('[NexusStreamer] Connected');
Expand Down Expand Up @@ -247,7 +252,7 @@ export class NexusStreamer {
* Request that playback start with specific params
*/
startPlayback(): void {
this.started = true;
this.videoStarted = true;
// Attempt to use camera's stream profile or use default
let primaryProfile = StreamProfile.VIDEO_H264_2MBIT_L40;
const otherProfiles: Array<StreamProfile> = [];
Expand Down Expand Up @@ -373,15 +378,15 @@ export class NexusStreamer {
// H264 NAL Units require 0001 added to beginning
const startCode = Buffer.from([0x00, 0x00, 0x00, 0x01]);
const stdin = this.ffmpegVideo.getStdin();
if (this.started) {
if (this.videoStarted) {
stdin?.write(Buffer.concat([startCode, Buffer.from(packet.payload)]), () => {
// Do nothing
});
}
}
if (packet.channel_id === this.audioChannelID) {
const stdin = this.ffmpegAudio?.getStdin();
if (this.started) {
if (this.videoStarted) {
stdin?.write(Buffer.from(packet.payload), () => {
// Do nothing
});
Expand Down
6 changes: 1 addition & 5 deletions src/streaming-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,8 @@ export class StreamingDelegate implements CameraStreamingDelegate {
'0:0',
'-c:a',
'libspeex',
'-af',
'atempo=2.0,asetrate=16000/0.95',
'-frames_per_packet',
'2',
'-cbr_quality',
'10',
'4',
'-ac',
'1',
'-ar',
Expand Down

0 comments on commit c9f4626

Please sign in to comment.