|
| 1 | +import { |
| 2 | + IPendingSession, |
| 3 | + IExtendedMediaSession, |
| 4 | + IAcceptSessionRequest, |
| 5 | + ScreenRecordingMetadata, |
| 6 | + IUpdateOutgoingMedia, IStartScreenConferenceSessionParams, |
| 7 | +} from '../types/interfaces'; |
| 8 | +import BaseSessionHandler from './base-session-handler'; |
| 9 | +import { SessionTypes, SdkErrorTypes } from '../types/enums'; |
| 10 | +import {createAndEmitSdkError, isMonitorJid, requestApi} from '../utils'; |
| 11 | + |
| 12 | +export class LiveMonitoringSessionHandler extends BaseSessionHandler { |
| 13 | + sessionType = SessionTypes.collaborateVideo; |
| 14 | + private primaryScreenMediaStream?: MediaStream; |
| 15 | + |
| 16 | + shouldHandleSessionByJid(jid: string): boolean { |
| 17 | + return isMonitorJid(jid); |
| 18 | + } |
| 19 | + |
| 20 | + handleConversationUpdate(): void { |
| 21 | + /* no-op */ |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + async handlePropose (pendingSession: IPendingSession): Promise<void> { |
| 26 | + return this.proceedWithSession(pendingSession); |
| 27 | + } |
| 28 | + |
| 29 | + async startSession(params: IStartScreenConferenceSessionParams): Promise<{ conversationId: string }> { |
| 30 | + const primaryScreen = this.identifyPrimaryScreen(params.screenRecordingMetadatas); |
| 31 | + |
| 32 | + if (!primaryScreen) { |
| 33 | + throw createAndEmitSdkError.call(this.sdk, SdkErrorTypes.invalid_options, 'No primary screen found in metadata'); |
| 34 | + } |
| 35 | + |
| 36 | + const screenStream = await this.getScreenMediaForPrimary(primaryScreen); |
| 37 | + |
| 38 | + return this.joinConferenceWithScreen(params.conferenceJid, screenStream); |
| 39 | + } |
| 40 | + |
| 41 | + private identifyPrimaryScreen(metadatas: ScreenRecordingMetadata[]): ScreenRecordingMetadata | null { |
| 42 | + return metadatas.find(metadata => metadata.primary) || null; |
| 43 | + } |
| 44 | + |
| 45 | + private async getScreenMediaForPrimary(primaryScreen: ScreenRecordingMetadata): Promise<MediaStream> { |
| 46 | + try { |
| 47 | + const constraints = { |
| 48 | + video: { |
| 49 | + deviceId: primaryScreen.screenId |
| 50 | + } |
| 51 | + } as DisplayMediaStreamOptions; |
| 52 | + |
| 53 | + return await window.navigator.mediaDevices.getDisplayMedia(constraints); |
| 54 | + } catch (error) { |
| 55 | + throw createAndEmitSdkError.call(this.sdk, SdkErrorTypes.session, 'Failed to get screen media', { error }); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private async joinConferenceWithScreen(conferenceJid: string, screenStream: MediaStream): Promise<{ conversationId: string }> { |
| 60 | + const data = JSON.stringify({ |
| 61 | + roomId: conferenceJid, |
| 62 | + participant: { address: this.sdk._personDetails.chat.jabberId } |
| 63 | + }); |
| 64 | + |
| 65 | + try { |
| 66 | + const response = await requestApi.call(this.sdk, '/conversations/videos', { |
| 67 | + method: 'post', |
| 68 | + data |
| 69 | + }); |
| 70 | + |
| 71 | + // Store screen stream for later use in acceptSession |
| 72 | + this.primaryScreenMediaStream = screenStream; |
| 73 | + |
| 74 | + return { conversationId: response.data.conversationId }; |
| 75 | + } catch (error) { |
| 76 | + screenStream.getTracks().forEach(track => track.stop()); |
| 77 | + throw createAndEmitSdkError.call(this.sdk, SdkErrorTypes.session, 'Failed to join conference', { error }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async acceptSession(session: IExtendedMediaSession, params: IAcceptSessionRequest): Promise<any> { |
| 82 | + if (!params.screenRecordingMetadatas?.length) { |
| 83 | + throw createAndEmitSdkError.call(this.sdk, SdkErrorTypes.not_supported, 'acceptSession must be called with a `screenRecordingMetadatas` property for live monitoring sessions'); |
| 84 | + } |
| 85 | + |
| 86 | + // Set outbound stream to primary video media stream |
| 87 | + session._outboundStream = this.primaryScreenMediaStream; |
| 88 | + |
| 89 | + // Add primary screen tracks to session |
| 90 | + for (const track of this.primaryScreenMediaStream.getTracks()) { |
| 91 | + session.pc.addTrack(track); |
| 92 | + } |
| 93 | + |
| 94 | + this.primaryScreenMediaStream = undefined; |
| 95 | + |
| 96 | + return super.acceptSession(session, params); |
| 97 | + } |
| 98 | + |
| 99 | + updateOutgoingMedia(session: IExtendedMediaSession, options: IUpdateOutgoingMedia): never { |
| 100 | + this.log('warn', 'Cannot update outgoing media for live monitoring sessions', { sessionId: session.id, sessionType: session.sessionType }); |
| 101 | + throw createAndEmitSdkError.call(this.sdk, SdkErrorTypes.not_supported, 'Cannot update outgoing media for live monitoring sessions'); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export default LiveMonitoringSessionHandler; |
0 commit comments