@@ -27,6 +27,8 @@ import {PayloadEvent, WORKLET_NAME} from "./consts";
27
27
import { UPDATE_EVENT } from "../stores/AsyncStore" ;
28
28
import { Playback } from "./Playback" ;
29
29
import { createAudioContext } from "./compat" ;
30
+ import { IEncryptedFile } from "matrix-js-sdk/src/@types/event" ;
31
+ import { uploadFile } from "../ContentMessages" ;
30
32
31
33
const CHANNELS = 1 ; // stereo isn't important
32
34
export const SAMPLE_RATE = 48000 ; // 48khz is what WebRTC uses. 12khz is where we lose quality.
@@ -49,6 +51,11 @@ export enum RecordingState {
49
51
Uploaded = "uploaded" ,
50
52
}
51
53
54
+ export interface IUpload {
55
+ mxc ?: string ; // for unencrypted uploads
56
+ encrypted ?: IEncryptedFile ;
57
+ }
58
+
52
59
export class VoiceRecording extends EventEmitter implements IDestroyable {
53
60
private recorder : Recorder ;
54
61
private recorderContext : AudioContext ;
@@ -58,7 +65,7 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
58
65
private recorderWorklet : AudioWorkletNode ;
59
66
private recorderProcessor : ScriptProcessorNode ;
60
67
private buffer = new Uint8Array ( 0 ) ; // use this.audioBuffer to access
61
- private mxc : string ;
68
+ private lastUpload : IUpload ;
62
69
private recording = false ;
63
70
private observable : SimpleObservable < IRecordingUpdate > ;
64
71
private amplitudes : number [ ] = [ ] ; // at each second mark, generated
@@ -214,13 +221,6 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
214
221
return this . buffer . length > 0 ;
215
222
}
216
223
217
- public get mxcUri ( ) : string {
218
- if ( ! this . mxc ) {
219
- throw new Error ( "Recording has not been uploaded yet" ) ;
220
- }
221
- return this . mxc ;
222
- }
223
-
224
224
private onAudioProcess = ( ev : AudioProcessingEvent ) => {
225
225
this . processAudioUpdate ( ev . playbackTime ) ;
226
226
@@ -290,7 +290,7 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
290
290
} ;
291
291
292
292
public async start ( ) : Promise < void > {
293
- if ( this . mxc || this . hasRecording ) {
293
+ if ( this . lastUpload || this . hasRecording ) {
294
294
throw new Error ( "Recording already prepared" ) ;
295
295
}
296
296
if ( this . recording ) {
@@ -362,20 +362,19 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
362
362
this . observable . close ( ) ;
363
363
}
364
364
365
- public async upload ( ) : Promise < string > {
365
+ public async upload ( inRoomId : string ) : Promise < IUpload > {
366
366
if ( ! this . hasRecording ) {
367
367
throw new Error ( "No recording available to upload" ) ;
368
368
}
369
369
370
- if ( this . mxc ) return this . mxc ;
370
+ if ( this . lastUpload ) return this . lastUpload ;
371
371
372
372
this . emit ( RecordingState . Uploading ) ;
373
- this . mxc = await this . client . uploadContent ( new Blob ( [ this . audioBuffer ] , {
373
+ const { url : mxc , file : encrypted } = await uploadFile ( this . client , inRoomId , new Blob ( [ this . audioBuffer ] , {
374
374
type : this . contentType ,
375
- } ) , {
376
- onlyContentUri : false , // to stop the warnings in the console
377
- } ) . then ( r => r [ 'content_uri' ] ) ;
375
+ } ) ) ;
376
+ this . lastUpload = { mxc, encrypted } ;
378
377
this . emit ( RecordingState . Uploaded ) ;
379
- return this . mxc ;
378
+ return this . lastUpload ;
380
379
}
381
380
}
0 commit comments