Implement more robust broadcast chunk header detection (#10006)
This commit is contained in:
parent
e9d723269f
commit
269d1622b9
2 changed files with 43 additions and 13 deletions
|
@ -14,7 +14,9 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { isEqual } from "lodash";
|
||||
import { Optional } from "matrix-events-sdk";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
import { TypedEventEmitter } from "matrix-js-sdk/src/models/typed-event-emitter";
|
||||
|
||||
import { getChunkLength } from "..";
|
||||
|
@ -38,6 +40,12 @@ export interface ChunkRecordedPayload {
|
|||
length: number;
|
||||
}
|
||||
|
||||
// char sequence of "OpusHead"
|
||||
const OpusHead = [79, 112, 117, 115, 72, 101, 97, 100];
|
||||
|
||||
// char sequence of "OpusTags"
|
||||
const OpusTags = [79, 112, 117, 115, 84, 97, 103, 115];
|
||||
|
||||
/**
|
||||
* This class provides the function to seamlessly record fixed length chunks.
|
||||
* Subscribe with on(VoiceBroadcastRecordingEvents.ChunkRecorded, (payload: ChunkRecordedPayload) => {})
|
||||
|
@ -47,11 +55,11 @@ export class VoiceBroadcastRecorder
|
|||
extends TypedEventEmitter<VoiceBroadcastRecorderEvent, EventMap>
|
||||
implements IDestroyable
|
||||
{
|
||||
private headers = new Uint8Array(0);
|
||||
private opusHead?: Uint8Array;
|
||||
private opusTags?: Uint8Array;
|
||||
private chunkBuffer = new Uint8Array(0);
|
||||
// position of the previous chunk in seconds
|
||||
private previousChunkEndTimePosition = 0;
|
||||
private pagesFromRecorderCount = 0;
|
||||
// current chunk length in seconds
|
||||
private currentChunkLength = 0;
|
||||
|
||||
|
@ -73,7 +81,7 @@ export class VoiceBroadcastRecorder
|
|||
public async stop(): Promise<Optional<ChunkRecordedPayload>> {
|
||||
try {
|
||||
await this.voiceRecording.stop();
|
||||
} catch {
|
||||
} catch (e) {
|
||||
// Ignore if the recording raises any error.
|
||||
}
|
||||
|
||||
|
@ -82,7 +90,6 @@ export class VoiceBroadcastRecorder
|
|||
const chunk = this.extractChunk();
|
||||
this.currentChunkLength = 0;
|
||||
this.previousChunkEndTimePosition = 0;
|
||||
this.headers = new Uint8Array(0);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
|
@ -103,11 +110,19 @@ export class VoiceBroadcastRecorder
|
|||
|
||||
private onDataAvailable = (data: ArrayBuffer): void => {
|
||||
const dataArray = new Uint8Array(data);
|
||||
this.pagesFromRecorderCount++;
|
||||
|
||||
if (this.pagesFromRecorderCount <= 2) {
|
||||
// first two pages contain the headers
|
||||
this.headers = concat(this.headers, dataArray);
|
||||
// extract the part, that contains the header type info
|
||||
const headerType = Array.from(dataArray.slice(28, 36));
|
||||
|
||||
if (isEqual(OpusHead, headerType)) {
|
||||
// data seems to be an "OpusHead" header
|
||||
this.opusHead = dataArray;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEqual(OpusTags, headerType)) {
|
||||
// data seems to be an "OpusTags" header
|
||||
this.opusTags = dataArray;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -134,9 +149,14 @@ export class VoiceBroadcastRecorder
|
|||
return null;
|
||||
}
|
||||
|
||||
if (!this.opusHead || !this.opusTags) {
|
||||
logger.warn("Broadcast chunk cannot be extracted. OpusHead or OpusTags is missing.");
|
||||
return null;
|
||||
}
|
||||
|
||||
const currentRecorderTime = this.voiceRecording.recorderSeconds;
|
||||
const payload: ChunkRecordedPayload = {
|
||||
buffer: concat(this.headers, this.chunkBuffer),
|
||||
buffer: concat(this.opusHead!, this.opusTags!, this.chunkBuffer),
|
||||
length: this.getCurrentChunkLength(),
|
||||
};
|
||||
this.chunkBuffer = new Uint8Array(0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue