@@ -30,7 +30,7 @@ import { PlaybackManager } from "../../audio/PlaybackManager";
3030import { UPDATE_EVENT } from "../../stores/AsyncStore" ;
3131import { MediaEventHelper } from "../../utils/MediaEventHelper" ;
3232import { IDestroyable } from "../../utils/IDestroyable" ;
33- import { VoiceBroadcastInfoEventType , VoiceBroadcastInfoState } from ".." ;
33+ import { VoiceBroadcastLiveness , VoiceBroadcastInfoEventType , VoiceBroadcastInfoState } from ".." ;
3434import { RelationsHelper , RelationsHelperEvent } from "../../events/RelationsHelper" ;
3535import { VoiceBroadcastChunkEvents } from "../utils/VoiceBroadcastChunkEvents" ;
3636
@@ -44,13 +44,15 @@ export enum VoiceBroadcastPlaybackState {
4444export enum VoiceBroadcastPlaybackEvent {
4545 PositionChanged = "position_changed" ,
4646 LengthChanged = "length_changed" ,
47+ LivenessChanged = "liveness_changed" ,
4748 StateChanged = "state_changed" ,
4849 InfoStateChanged = "info_state_changed" ,
4950}
5051
5152interface EventMap {
5253 [ VoiceBroadcastPlaybackEvent . PositionChanged ] : ( position : number ) => void ;
5354 [ VoiceBroadcastPlaybackEvent . LengthChanged ] : ( length : number ) => void ;
55+ [ VoiceBroadcastPlaybackEvent . LivenessChanged ] : ( liveness : VoiceBroadcastLiveness ) => void ;
5456 [ VoiceBroadcastPlaybackEvent . StateChanged ] : (
5557 state : VoiceBroadcastPlaybackState ,
5658 playback : VoiceBroadcastPlayback
@@ -70,6 +72,7 @@ export class VoiceBroadcastPlayback
7072 /** @var current playback position in milliseconds */
7173 private position = 0 ;
7274 public readonly liveData = new SimpleObservable < number [ ] > ( ) ;
75+ private liveness : VoiceBroadcastLiveness = "not-live" ;
7376
7477 // set vial addInfoEvent() in constructor
7578 private infoState ! : VoiceBroadcastInfoState ;
@@ -143,6 +146,7 @@ export class VoiceBroadcastPlayback
143146
144147 if ( this . getState ( ) === VoiceBroadcastPlaybackState . Buffering ) {
145148 await this . start ( ) ;
149+ this . updateLiveness ( ) ;
146150 }
147151
148152 return true ;
@@ -212,23 +216,19 @@ export class VoiceBroadcastPlayback
212216 } ;
213217
214218 private setDuration ( duration : number ) : void {
215- const shouldEmit = this . duration !== duration ;
216- this . duration = duration ;
219+ if ( this . duration === duration ) return ;
217220
218- if ( shouldEmit ) {
219- this . emit ( VoiceBroadcastPlaybackEvent . LengthChanged , this . duration ) ;
220- this . liveData . update ( [ this . timeSeconds , this . durationSeconds ] ) ;
221- }
221+ this . duration = duration ;
222+ this . emit ( VoiceBroadcastPlaybackEvent . LengthChanged , this . duration ) ;
223+ this . liveData . update ( [ this . timeSeconds , this . durationSeconds ] ) ;
222224 }
223225
224226 private setPosition ( position : number ) : void {
225- const shouldEmit = this . position !== position ;
226- this . position = position ;
227+ if ( this . position === position ) return ;
227228
228- if ( shouldEmit ) {
229- this . emit ( VoiceBroadcastPlaybackEvent . PositionChanged , this . position ) ;
230- this . liveData . update ( [ this . timeSeconds , this . durationSeconds ] ) ;
231- }
229+ this . position = position ;
230+ this . emit ( VoiceBroadcastPlaybackEvent . PositionChanged , this . position ) ;
231+ this . liveData . update ( [ this . timeSeconds , this . durationSeconds ] ) ;
232232 }
233233
234234 private onPlaybackStateChange = async ( event : MatrixEvent , newState : PlaybackState ) : Promise < void > => {
@@ -279,6 +279,42 @@ export class VoiceBroadcastPlayback
279279 return playback ;
280280 }
281281
282+ public getLiveness ( ) : VoiceBroadcastLiveness {
283+ return this . liveness ;
284+ }
285+
286+ private setLiveness ( liveness : VoiceBroadcastLiveness ) : void {
287+ if ( this . liveness === liveness ) return ;
288+
289+ this . liveness = liveness ;
290+ this . emit ( VoiceBroadcastPlaybackEvent . LivenessChanged , liveness ) ;
291+ }
292+
293+ private updateLiveness ( ) : void {
294+ if ( this . infoState === VoiceBroadcastInfoState . Stopped ) {
295+ this . setLiveness ( "not-live" ) ;
296+ return ;
297+ }
298+
299+ if ( this . infoState === VoiceBroadcastInfoState . Paused ) {
300+ this . setLiveness ( "grey" ) ;
301+ return ;
302+ }
303+
304+ if ( [ VoiceBroadcastPlaybackState . Stopped , VoiceBroadcastPlaybackState . Paused ] . includes ( this . state ) ) {
305+ this . setLiveness ( "grey" ) ;
306+ return ;
307+ }
308+
309+ if ( this . currentlyPlaying && this . chunkEvents . isLast ( this . currentlyPlaying ) ) {
310+ this . setLiveness ( "live" ) ;
311+ return ;
312+ }
313+
314+ this . setLiveness ( "grey" ) ;
315+ return ;
316+ }
317+
282318 public get currentState ( ) : PlaybackState {
283319 return PlaybackState . Playing ;
284320 }
@@ -295,7 +331,10 @@ export class VoiceBroadcastPlayback
295331 const time = timeSeconds * 1000 ;
296332 const event = this . chunkEvents . findByTime ( time ) ;
297333
298- if ( ! event ) return ;
334+ if ( ! event ) {
335+ logger . warn ( "voice broadcast chunk event to skip to not found" ) ;
336+ return ;
337+ }
299338
300339 const currentPlayback = this . currentlyPlaying
301340 ? this . getPlaybackForEvent ( this . currentlyPlaying )
@@ -304,7 +343,7 @@ export class VoiceBroadcastPlayback
304343 const skipToPlayback = this . getPlaybackForEvent ( event ) ;
305344
306345 if ( ! skipToPlayback ) {
307- logger . error ( "voice broadcast chunk to skip to not found" , event ) ;
346+ logger . warn ( "voice broadcast chunk to skip to not found" , event ) ;
308347 return ;
309348 }
310349
@@ -324,6 +363,7 @@ export class VoiceBroadcastPlayback
324363 }
325364
326365 this . setPosition ( time ) ;
366+ this . updateLiveness ( ) ;
327367 }
328368
329369 public async start ( ) : Promise < void > {
@@ -398,6 +438,7 @@ export class VoiceBroadcastPlayback
398438
399439 this . state = state ;
400440 this . emit ( VoiceBroadcastPlaybackEvent . StateChanged , state , this ) ;
441+ this . updateLiveness ( ) ;
401442 }
402443
403444 public getInfoState ( ) : VoiceBroadcastInfoState {
@@ -411,6 +452,7 @@ export class VoiceBroadcastPlayback
411452
412453 this . infoState = state ;
413454 this . emit ( VoiceBroadcastPlaybackEvent . InfoStateChanged , state ) ;
455+ this . updateLiveness ( ) ;
414456 }
415457
416458 public destroy ( ) : void {
0 commit comments