Skip to content

Commit 98c96e5

Browse files
Added screensharing to stream-chat.service.ts
1 parent eef4126 commit 98c96e5

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

src/app/app.component.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {MockWebSocketService} from './core/services/testing/mock-websocket.servi
5454
import {TRACE_SERVICE, TraceService} from './core/services/trace.service';
5555
import {VIDEO_SERVICE, VideoService} from './core/services/video.service';
5656
import {WEBSOCKET_SERVICE, WebSocketService,} from './core/services/websocket.service';
57+
import { SCREEN_SHARING_SERVICE } from './core/services/screensharing.service';
5758

5859
describe('AppComponent', () => {
5960
beforeEach(async () => {
@@ -65,6 +66,7 @@ describe('AppComponent', () => {
6566
const audioService = new MockAudioService();
6667
const webSocketService = new MockWebSocketService();
6768
const videoService = new MockVideoService();
69+
const screenSharingService = new MockScreenSharingService();
6870
const streamChatService = new MockStreamChatService();
6971
const eventService = new MockEventService();
7072
const downloadService = new MockDownloadService();
@@ -115,6 +117,10 @@ describe('AppComponent', () => {
115117
provide: VIDEO_SERVICE,
116118
useValue: videoService,
117119
},
120+
{
121+
provide: SCREEN_SHARING_SERVICE,
122+
useValue: screenSharingService,
123+
},
118124
{
119125
provide: STREAM_CHAT_SERVICE,
120126
useValue: streamChatService,

src/app/components/chat/chat.component.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ describe('ChatComponent', () => {
112112
let mockAudioService: MockAudioService;
113113
let mockWebSocketService: MockWebSocketService;
114114
let mockVideoService: MockVideoService;
115+
let mockScreenSharingService: MockScreenSharingService;
115116
let mockStreamChatService: MockStreamChatService;
116117
let mockEventService: MockEventService;
117118
let mockDownloadService: MockDownloadService;
@@ -134,6 +135,7 @@ describe('ChatComponent', () => {
134135
mockAudioService = new MockAudioService();
135136
mockWebSocketService = new MockWebSocketService();
136137
mockVideoService = new MockVideoService();
138+
mockScreenSharingService = new MockScreenSharingService();
137139
mockStreamChatService = new MockStreamChatService();
138140
mockEventService = new MockEventService();
139141
mockDownloadService = new MockDownloadService();

src/app/components/chat/chat.component.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,11 +1048,12 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
10481048
return;
10491049
}
10501050
this.isScreenSharing = true;
1051-
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
1052-
this.webSocketService.connect(
1053-
`${protocol}://${URLUtil.getWSServerUrl()}/run_live?app_name=${this.appName}&user_id=${this.userId}&session_id=${this.sessionId}`,
1054-
);
1055-
this.screenSharingService.startScreenSharing(screenSharingContainer);
1051+
this.streamChatService.startScreenSharingChat({
1052+
appName: this.appName,
1053+
userId: this.userId,
1054+
sessionId: this.sessionId,
1055+
screenSharingContainer,
1056+
});
10561057
this.messages.update(
10571058
messages => [...messages, {role: 'user', text: 'Sharing Screen...'}]);
10581059
this.sessionHasUsedBidi.add(this.sessionId);
@@ -1064,8 +1065,7 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
10641065
return;
10651066
}
10661067

1067-
this.screenSharingService.stopScreenSharing(screenSharingContainer);
1068-
this.webSocketService.closeConnection();
1068+
this.streamChatService.stopScreenSharingChat(screenSharingContainer);
10691069
this.isScreenSharing = false;
10701070
}
10711071

src/app/core/services/stream-chat.service.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {LiveRequest} from '../models/LiveRequest';
2323
import {AUDIO_SERVICE, AudioService} from './audio.service';
2424
import {VIDEO_SERVICE, VideoService} from './video.service';
2525
import {WEBSOCKET_SERVICE, WebSocketService} from './websocket.service';
26+
import { SCREEN_SHARING_SERVICE, ScreenSharingService } from './screensharing.service';
2627

2728
export const STREAM_CHAT_SERVICE =
2829
new InjectionToken<StreamChatService>('StreamChatService');
@@ -36,10 +37,12 @@ export const STREAM_CHAT_SERVICE =
3637
export class StreamChatService {
3738
private audioIntervalId: number|undefined = undefined;
3839
private videoIntervalId: number|undefined = undefined;
40+
private screenSharingIntervalId: number|undefined = undefined;
3941

4042
constructor(
4143
@Inject(AUDIO_SERVICE) private readonly audioService: AudioService,
4244
@Inject(VIDEO_SERVICE) private readonly videoService: VideoService,
45+
@Inject(SCREEN_SHARING_SERVICE) private readonly screenSharingService: ScreenSharingService,
4346
@Inject(WEBSOCKET_SERVICE) private readonly webSocketService:
4447
WebSocketService,
4548
) {}
@@ -111,12 +114,38 @@ export class StreamChatService {
111114
await this.startVideoStreaming(videoContainer);
112115
}
113116

117+
async startScreenSharingChat({
118+
appName,
119+
userId,
120+
sessionId,
121+
screenSharingContainer,
122+
}: {
123+
appName: string; userId: string; sessionId: string;
124+
screenSharingContainer: ElementRef
125+
}){
126+
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
127+
this.webSocketService.connect(
128+
`${protocol}://${URLUtil.getWSServerUrl()}/run_live?app_name=${
129+
appName}&user_id=${userId}&session_id=${sessionId}`,
130+
);
131+
132+
await this.startAudioStreaming();
133+
await this.startScreenSharing(screenSharingContainer);
134+
}
135+
114136
stopVideoChat(videoContainer: ElementRef) {
115137
this.stopAudioStreaming();
116138
this.stopVideoStreaming(videoContainer);
117139
this.webSocketService.closeConnection();
118140
}
119141

142+
stopScreenSharingChat(screenSharingContainer: ElementRef) {
143+
this.stopAudioStreaming();
144+
this.stopScreenSharing(screenSharingContainer);
145+
this.webSocketService.closeConnection();
146+
}
147+
148+
120149
private async startVideoStreaming(videoContainer: ElementRef) {
121150
try {
122151
await this.videoService.startRecording(videoContainer);
@@ -129,6 +158,18 @@ export class StreamChatService {
129158
}
130159
}
131160

161+
private async startScreenSharing(screenSharingContainer: ElementRef) {
162+
try {
163+
await this.screenSharingService.startScreenSharing(screenSharingContainer);
164+
this.screenSharingIntervalId = setInterval(
165+
async () => await this.sendCapturedFrame(),
166+
1000,
167+
);
168+
} catch (error) {
169+
console.error('Error accessing camera:', error);
170+
}
171+
}
172+
132173
private async sendCapturedFrame() {
133174
const capturedFrame = await this.videoService.getCapturedFrame();
134175
if (!capturedFrame) return;
@@ -148,6 +189,12 @@ export class StreamChatService {
148189
this.videoService.stopRecording(videoContainer);
149190
}
150191

192+
private stopScreenSharing(screenSharingContainer: ElementRef) {
193+
clearInterval(this.screenSharingIntervalId);
194+
this.screenSharingIntervalId = undefined;
195+
this.screenSharingService.stopScreenSharing(screenSharingContainer);
196+
}
197+
151198
onStreamClose() {
152199
return this.webSocketService.onCloseReason();
153200
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"backendUrl": "http://localhost:8000"
3-
}
3+
}

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fetch('./assets/config/runtime-config.json')
7070
},
7171
{provide: AUDIO_SERVICE, useClass: AudioService},
7272
{provide: VIDEO_SERVICE, useClass: VideoService},
73+
{provide: SCREEN_SHARING_SERVICE, useClass: ScreenSharingService},
7374
{provide: STREAM_CHAT_SERVICE, useClass: StreamChatService},
7475
{provide: EVENT_SERVICE, useClass: EventService},
7576
{provide: EVAL_SERVICE, useClass: EvalService},

0 commit comments

Comments
 (0)