/src/FreeRDP/channels/video/client/video_main.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Video Optimized Remoting Virtual Channel Extension |
4 | | * |
5 | | * Copyright 2017 David Fort <contact@hardening-consulting.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <freerdp/config.h> |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include <winpr/crt.h> |
27 | | #include <winpr/assert.h> |
28 | | #include <winpr/cast.h> |
29 | | #include <winpr/synch.h> |
30 | | #include <winpr/print.h> |
31 | | #include <winpr/stream.h> |
32 | | #include <winpr/cmdline.h> |
33 | | #include <winpr/collections.h> |
34 | | #include <winpr/interlocked.h> |
35 | | #include <winpr/sysinfo.h> |
36 | | |
37 | | #include <freerdp/addin.h> |
38 | | #include <freerdp/primitives.h> |
39 | | #include <freerdp/client/channels.h> |
40 | | #include <freerdp/client/geometry.h> |
41 | | #include <freerdp/client/video.h> |
42 | | #include <freerdp/channels/log.h> |
43 | | #include <freerdp/codec/h264.h> |
44 | | #include <freerdp/codec/yuv.h> |
45 | | #include <freerdp/timer.h> |
46 | | |
47 | | #define TAG CHANNELS_TAG("video.client") |
48 | | |
49 | | #include "video_main.h" |
50 | | |
51 | | typedef struct |
52 | | { |
53 | | IWTSPlugin wtsPlugin; |
54 | | |
55 | | IWTSListener* controlListener; |
56 | | IWTSListener* dataListener; |
57 | | GENERIC_LISTENER_CALLBACK* control_callback; |
58 | | GENERIC_LISTENER_CALLBACK* data_callback; |
59 | | |
60 | | VideoClientContext* context; |
61 | | BOOL initialized; |
62 | | rdpContext* rdpcontext; |
63 | | } VIDEO_PLUGIN; |
64 | | |
65 | 0 | #define XF_VIDEO_UNLIMITED_RATE 31 |
66 | | |
67 | | static const BYTE MFVideoFormat_H264[] = { 'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00, |
68 | | 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }; |
69 | | |
70 | | typedef struct |
71 | | { |
72 | | BYTE PresentationId; |
73 | | UINT32 ScaledWidth; |
74 | | UINT32 ScaledHeight; |
75 | | |
76 | | UINT64 startTimeStamp; |
77 | | UINT64 publishOffset; |
78 | | wStream* currentSample; |
79 | | UINT64 lastPublishTime; |
80 | | UINT64 nextPublishTime; |
81 | | volatile LONG refCounter; |
82 | | H264_CONTEXT* h264; |
83 | | VideoSurface* surface; |
84 | | MAPPED_GEOMETRY* geometry; |
85 | | VideoClientContext* video; |
86 | | } PresentationContext; |
87 | | |
88 | | typedef struct |
89 | | { |
90 | | BYTE PresentationId; |
91 | | UINT64 publishTime; |
92 | | UINT64 hnsDuration; |
93 | | MAPPED_GEOMETRY* geometry; |
94 | | UINT32 w, h; |
95 | | UINT32 scanline; |
96 | | BYTE* surfaceData; |
97 | | } VideoFrame; |
98 | | |
99 | | /** @brief private data for the channel */ |
100 | | struct s_VideoClientContextPriv |
101 | | { |
102 | | VideoClientContext* video; |
103 | | GeometryClientContext* geometry; |
104 | | wQueue* frames; |
105 | | CRITICAL_SECTION framesLock; |
106 | | wBufferPool* surfacePool; |
107 | | UINT32 publishedFrames; |
108 | | UINT32 droppedFrames; |
109 | | UINT32 lastSentRate; |
110 | | UINT64 nextFeedbackTime; |
111 | | PresentationContext* currentPresentation; |
112 | | FreeRDP_TimerID timerID; |
113 | | }; |
114 | | |
115 | | static void PresentationContext_unref(PresentationContext** presentation); |
116 | | static void VideoClientContextPriv_free(VideoClientContextPriv* priv); |
117 | | |
118 | | WINPR_ATTR_NODISCARD |
119 | | static const char* video_command_name(BYTE cmd) |
120 | 0 | { |
121 | 0 | switch (cmd) |
122 | 0 | { |
123 | 0 | case TSMM_START_PRESENTATION: |
124 | 0 | return "start"; |
125 | 0 | case TSMM_STOP_PRESENTATION: |
126 | 0 | return "stop"; |
127 | 0 | default: |
128 | 0 | return "<unknown>"; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | static void video_client_context_set_geometry(VideoClientContext* video, |
133 | | GeometryClientContext* geometry) |
134 | 0 | { |
135 | 0 | WINPR_ASSERT(video); |
136 | 0 | WINPR_ASSERT(video->priv); |
137 | 0 | video->priv->geometry = geometry; |
138 | 0 | } |
139 | | |
140 | | WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1) |
141 | | static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video) |
142 | 0 | { |
143 | 0 | VideoClientContextPriv* ret = nullptr; |
144 | |
|
145 | 0 | WINPR_ASSERT(video); |
146 | 0 | ret = calloc(1, sizeof(*ret)); |
147 | 0 | if (!ret) |
148 | 0 | return nullptr; |
149 | | |
150 | 0 | ret->frames = Queue_New(TRUE, 10, 2); |
151 | 0 | if (!ret->frames) |
152 | 0 | { |
153 | 0 | WLog_ERR(TAG, "unable to allocate frames queue"); |
154 | 0 | goto fail; |
155 | 0 | } |
156 | | |
157 | 0 | ret->surfacePool = BufferPool_New(FALSE, 0, 16); |
158 | 0 | if (!ret->surfacePool) |
159 | 0 | { |
160 | 0 | WLog_ERR(TAG, "unable to create surface pool"); |
161 | 0 | goto fail; |
162 | 0 | } |
163 | | |
164 | 0 | if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000)) |
165 | 0 | { |
166 | 0 | WLog_ERR(TAG, "unable to initialize frames lock"); |
167 | 0 | goto fail; |
168 | 0 | } |
169 | | |
170 | 0 | ret->video = video; |
171 | | |
172 | | /* don't set to unlimited so that we have the chance to send a feedback in |
173 | | * the first second (for servers that want feedback directly) |
174 | | */ |
175 | 0 | ret->lastSentRate = 30; |
176 | 0 | return ret; |
177 | | |
178 | 0 | fail: |
179 | 0 | VideoClientContextPriv_free(ret); |
180 | 0 | return nullptr; |
181 | 0 | } |
182 | | |
183 | | WINPR_ATTR_NODISCARD |
184 | | static BOOL PresentationContext_ref(PresentationContext* presentation) |
185 | 0 | { |
186 | 0 | WINPR_ASSERT(presentation); |
187 | |
|
188 | 0 | const LONG val = InterlockedIncrement(&presentation->refCounter); |
189 | 0 | return val > 0; |
190 | 0 | } |
191 | | |
192 | | static void PresentationContext_free(PresentationContext* presentation) |
193 | 0 | { |
194 | 0 | if (!presentation) |
195 | 0 | return; |
196 | | |
197 | 0 | MAPPED_GEOMETRY* geometry = presentation->geometry; |
198 | 0 | if (geometry) |
199 | 0 | { |
200 | 0 | geometry->MappedGeometryUpdate = nullptr; |
201 | 0 | geometry->MappedGeometryClear = nullptr; |
202 | 0 | geometry->custom = nullptr; |
203 | 0 | mappedGeometryUnref(geometry); |
204 | 0 | } |
205 | |
|
206 | 0 | h264_context_free(presentation->h264); |
207 | 0 | Stream_Free(presentation->currentSample, TRUE); |
208 | 0 | presentation->video->deleteSurface(presentation->video, presentation->surface); |
209 | 0 | free(presentation); |
210 | 0 | } |
211 | | |
212 | | WINPR_ATTR_MALLOC(PresentationContext_free, 1) |
213 | | static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId, |
214 | | UINT32 x, UINT32 y, UINT32 width, UINT32 height) |
215 | 0 | { |
216 | 0 | size_t s = 4ULL * width * height; |
217 | 0 | PresentationContext* ret = nullptr; |
218 | |
|
219 | 0 | WINPR_ASSERT(video); |
220 | |
|
221 | 0 | if (s > INT32_MAX) |
222 | 0 | return nullptr; |
223 | | |
224 | 0 | ret = calloc(1, sizeof(*ret)); |
225 | 0 | if (!ret) |
226 | 0 | return nullptr; |
227 | | |
228 | 0 | ret->video = video; |
229 | 0 | ret->PresentationId = PresentationId; |
230 | |
|
231 | 0 | ret->h264 = h264_context_new(FALSE); |
232 | 0 | if (!ret->h264) |
233 | 0 | { |
234 | 0 | WLog_ERR(TAG, "unable to create a h264 context"); |
235 | 0 | goto fail; |
236 | 0 | } |
237 | 0 | if (!h264_context_reset(ret->h264, width, height)) |
238 | 0 | goto fail; |
239 | | |
240 | 0 | ret->currentSample = Stream_New(nullptr, 4096); |
241 | 0 | if (!ret->currentSample) |
242 | 0 | { |
243 | 0 | WLog_ERR(TAG, "unable to create current packet stream"); |
244 | 0 | goto fail; |
245 | 0 | } |
246 | | |
247 | 0 | ret->surface = video->createSurface(video, x, y, width, height); |
248 | 0 | if (!ret->surface) |
249 | 0 | { |
250 | 0 | WLog_ERR(TAG, "unable to create surface"); |
251 | 0 | goto fail; |
252 | 0 | } |
253 | | |
254 | 0 | if (!PresentationContext_ref(ret)) |
255 | 0 | goto fail; |
256 | | |
257 | 0 | return ret; |
258 | | |
259 | 0 | fail: |
260 | 0 | PresentationContext_free(ret); |
261 | 0 | return nullptr; |
262 | 0 | } |
263 | | |
264 | | static void PresentationContext_unref(PresentationContext** ppresentation) |
265 | 0 | { |
266 | 0 | WINPR_ASSERT(ppresentation); |
267 | |
|
268 | 0 | PresentationContext* presentation = *ppresentation; |
269 | 0 | if (!presentation) |
270 | 0 | return; |
271 | | |
272 | 0 | if (InterlockedDecrement(&presentation->refCounter) > 0) |
273 | 0 | return; |
274 | 0 | *ppresentation = nullptr; |
275 | |
|
276 | 0 | PresentationContext_free(presentation); |
277 | 0 | } |
278 | | |
279 | | static void VideoFrame_free(VideoClientContextPriv* priv, VideoFrame* frame) |
280 | 0 | { |
281 | 0 | WINPR_ASSERT(priv); |
282 | 0 | if (!frame) |
283 | 0 | return; |
284 | | |
285 | 0 | mappedGeometryUnref(frame->geometry); |
286 | |
|
287 | 0 | BufferPool_Return(priv->surfacePool, frame->surfaceData); |
288 | 0 | free(frame); |
289 | 0 | } |
290 | | |
291 | | WINPR_ATTR_MALLOC(VideoFrame_free, 1) |
292 | | static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation, |
293 | | MAPPED_GEOMETRY* geom) |
294 | 0 | { |
295 | 0 | WINPR_ASSERT(priv); |
296 | 0 | WINPR_ASSERT(presentation); |
297 | 0 | WINPR_ASSERT(geom); |
298 | |
|
299 | 0 | const VideoSurface* surface = presentation->surface; |
300 | 0 | WINPR_ASSERT(surface); |
301 | |
|
302 | 0 | VideoFrame* frame = calloc(1, sizeof(VideoFrame)); |
303 | 0 | if (!frame) |
304 | 0 | goto fail; |
305 | 0 | frame->PresentationId = presentation->PresentationId; |
306 | |
|
307 | 0 | mappedGeometryRef(geom); |
308 | |
|
309 | 0 | frame->publishTime = presentation->lastPublishTime; |
310 | 0 | frame->geometry = geom; |
311 | 0 | frame->w = surface->alignedWidth; |
312 | 0 | frame->h = surface->alignedHeight; |
313 | 0 | frame->scanline = surface->scanline; |
314 | |
|
315 | 0 | frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h); |
316 | 0 | if (!frame->surfaceData) |
317 | 0 | goto fail; |
318 | | |
319 | 0 | return frame; |
320 | | |
321 | 0 | fail: |
322 | 0 | VideoFrame_free(priv, frame); |
323 | 0 | return nullptr; |
324 | 0 | } |
325 | | |
326 | | void VideoClientContextPriv_free(VideoClientContextPriv* priv) |
327 | 0 | { |
328 | 0 | if (!priv) |
329 | 0 | return; |
330 | | |
331 | 0 | EnterCriticalSection(&priv->framesLock); |
332 | |
|
333 | 0 | if (priv->frames) |
334 | 0 | { |
335 | 0 | while (Queue_Count(priv->frames)) |
336 | 0 | { |
337 | 0 | VideoFrame* frame = Queue_Dequeue(priv->frames); |
338 | 0 | if (frame) |
339 | 0 | VideoFrame_free(priv, frame); |
340 | 0 | } |
341 | 0 | } |
342 | |
|
343 | 0 | Queue_Free(priv->frames); |
344 | 0 | LeaveCriticalSection(&priv->framesLock); |
345 | |
|
346 | 0 | DeleteCriticalSection(&priv->framesLock); |
347 | |
|
348 | 0 | if (priv->currentPresentation) |
349 | 0 | PresentationContext_unref(&priv->currentPresentation); |
350 | |
|
351 | 0 | BufferPool_Free(priv->surfacePool); |
352 | 0 | free(priv); |
353 | 0 | } |
354 | | |
355 | | WINPR_ATTR_NODISCARD |
356 | | static UINT video_channel_write(VIDEO_PLUGIN* video, const BYTE* data, UINT32 length) |
357 | 0 | { |
358 | 0 | WINPR_ASSERT(video); |
359 | |
|
360 | 0 | if (!video->control_callback || !video->control_callback->channel_callback) |
361 | 0 | return ERROR_BAD_CONFIGURATION; |
362 | 0 | IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel; |
363 | 0 | if (!channel || !channel->Write) |
364 | 0 | return ERROR_BAD_CONFIGURATION; |
365 | 0 | return channel->Write(channel, length, data, nullptr); |
366 | 0 | } |
367 | | |
368 | | WINPR_ATTR_NODISCARD |
369 | | static UINT video_control_send_presentation_response(VideoClientContext* context, |
370 | | TSMM_PRESENTATION_RESPONSE* resp) |
371 | 0 | { |
372 | 0 | BYTE buf[12] = WINPR_C_ARRAY_INIT; |
373 | |
|
374 | 0 | WINPR_ASSERT(context); |
375 | 0 | WINPR_ASSERT(resp); |
376 | |
|
377 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle; |
378 | 0 | WINPR_ASSERT(video); |
379 | |
|
380 | 0 | wStream* s = Stream_New(buf, 12); |
381 | 0 | if (!s) |
382 | 0 | return CHANNEL_RC_NO_MEMORY; |
383 | | |
384 | 0 | Stream_Write_UINT32(s, 12); /* cbSize */ |
385 | 0 | Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE); /* PacketType */ |
386 | 0 | Stream_Write_UINT8(s, resp->PresentationId); |
387 | 0 | Stream_Zero(s, 3); |
388 | 0 | Stream_SealLength(s); |
389 | 0 | Stream_Free(s, FALSE); |
390 | |
|
391 | 0 | return video_channel_write(video, buf, sizeof(buf)); |
392 | 0 | } |
393 | | |
394 | | WINPR_ATTR_NODISCARD |
395 | | static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry) |
396 | 0 | { |
397 | 0 | WINPR_ASSERT(geometry); |
398 | |
|
399 | 0 | PresentationContext* presentation = (PresentationContext*)geometry->custom; |
400 | 0 | WINPR_ASSERT(presentation); |
401 | |
|
402 | 0 | RDP_RECT* r = &geometry->geometry.boundingRect; |
403 | 0 | WLog_DBG(TAG, |
404 | 0 | "geometry updated topGeom=(%" PRId32 ",%" PRId32 "-%" PRId32 "x%" PRId32 |
405 | 0 | ") geom=(%" PRId32 ",%" PRId32 "-%" PRId32 "x%" PRId32 ") rects=(%" PRId16 ",%" PRId16 |
406 | 0 | "-%" PRId16 "x%" PRId16 ")", |
407 | 0 | geometry->topLevelLeft, geometry->topLevelTop, |
408 | 0 | geometry->topLevelRight - geometry->topLevelLeft, |
409 | 0 | geometry->topLevelBottom - geometry->topLevelTop, |
410 | |
|
411 | 0 | geometry->left, geometry->top, geometry->right - geometry->left, |
412 | 0 | geometry->bottom - geometry->top, |
413 | |
|
414 | 0 | r->x, r->y, r->width, r->height); |
415 | |
|
416 | 0 | WINPR_ASSERT(presentation->surface); |
417 | 0 | presentation->surface->x = |
418 | 0 | WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + geometry->left); |
419 | 0 | presentation->surface->y = |
420 | 0 | WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + geometry->top); |
421 | |
|
422 | 0 | return TRUE; |
423 | 0 | } |
424 | | |
425 | | WINPR_ATTR_NODISCARD |
426 | | static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry) |
427 | 0 | { |
428 | 0 | WINPR_ASSERT(geometry); |
429 | |
|
430 | 0 | PresentationContext* presentation = (PresentationContext*)geometry->custom; |
431 | 0 | WINPR_ASSERT(presentation); |
432 | |
|
433 | 0 | mappedGeometryUnref(presentation->geometry); |
434 | 0 | presentation->geometry = nullptr; |
435 | 0 | return TRUE; |
436 | 0 | } |
437 | | |
438 | | WINPR_ATTR_NODISCARD |
439 | | static UINT video_PresentationRequest(VideoClientContext* video, |
440 | | const TSMM_PRESENTATION_REQUEST* req) |
441 | 0 | { |
442 | 0 | UINT ret = CHANNEL_RC_OK; |
443 | |
|
444 | 0 | WINPR_ASSERT(video); |
445 | 0 | WINPR_ASSERT(req); |
446 | |
|
447 | 0 | VideoClientContextPriv* priv = video->priv; |
448 | 0 | WINPR_ASSERT(priv); |
449 | |
|
450 | 0 | EnterCriticalSection(&priv->framesLock); |
451 | 0 | if (req->Command == TSMM_START_PRESENTATION) |
452 | 0 | { |
453 | 0 | MAPPED_GEOMETRY* geom = nullptr; |
454 | 0 | TSMM_PRESENTATION_RESPONSE resp = WINPR_C_ARRAY_INIT; |
455 | |
|
456 | 0 | if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0) |
457 | 0 | { |
458 | 0 | WLog_ERR(TAG, "not a H264 video, ignoring request"); |
459 | 0 | goto fail; |
460 | 0 | } |
461 | | |
462 | 0 | if (priv->currentPresentation) |
463 | 0 | { |
464 | 0 | if (priv->currentPresentation->PresentationId == req->PresentationId) |
465 | 0 | { |
466 | 0 | WLog_ERR(TAG, "ignoring start request for existing presentation %" PRIu8, |
467 | 0 | req->PresentationId); |
468 | 0 | goto fail; |
469 | 0 | } |
470 | | |
471 | 0 | WLog_ERR(TAG, "releasing current presentation %" PRIu8, req->PresentationId); |
472 | 0 | PresentationContext_unref(&priv->currentPresentation); |
473 | 0 | } |
474 | | |
475 | 0 | if (!priv->geometry) |
476 | 0 | { |
477 | 0 | WLog_ERR(TAG, "geometry channel not ready, ignoring request"); |
478 | 0 | goto fail; |
479 | 0 | } |
480 | | |
481 | 0 | geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId)); |
482 | 0 | if (!geom) |
483 | 0 | { |
484 | 0 | WLog_ERR(TAG, "geometry mapping 0x%" PRIx64 " not registered", req->GeometryMappingId); |
485 | 0 | goto fail; |
486 | 0 | } |
487 | | |
488 | 0 | WLog_DBG(TAG, "creating presentation 0x%x", req->PresentationId); |
489 | 0 | priv->currentPresentation = PresentationContext_new( |
490 | 0 | video, req->PresentationId, |
491 | 0 | WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left), |
492 | 0 | WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth, |
493 | 0 | req->SourceHeight); |
494 | 0 | if (!priv->currentPresentation) |
495 | 0 | { |
496 | 0 | WLog_ERR(TAG, "unable to create presentation video"); |
497 | 0 | ret = CHANNEL_RC_NO_MEMORY; |
498 | 0 | goto fail; |
499 | 0 | } |
500 | | |
501 | 0 | mappedGeometryRef(geom); |
502 | 0 | priv->currentPresentation->geometry = geom; |
503 | |
|
504 | 0 | priv->currentPresentation->video = video; |
505 | 0 | priv->currentPresentation->ScaledWidth = req->ScaledWidth; |
506 | 0 | priv->currentPresentation->ScaledHeight = req->ScaledHeight; |
507 | |
|
508 | 0 | geom->custom = priv->currentPresentation; |
509 | 0 | geom->MappedGeometryUpdate = video_onMappedGeometryUpdate; |
510 | 0 | geom->MappedGeometryClear = video_onMappedGeometryClear; |
511 | | |
512 | | /* send back response */ |
513 | 0 | resp.PresentationId = req->PresentationId; |
514 | 0 | ret = video_control_send_presentation_response(video, &resp); |
515 | 0 | } |
516 | 0 | else if (req->Command == TSMM_STOP_PRESENTATION) |
517 | 0 | { |
518 | 0 | WLog_DBG(TAG, "stopping presentation 0x%x", req->PresentationId); |
519 | 0 | if (!priv->currentPresentation) |
520 | 0 | { |
521 | 0 | WLog_ERR(TAG, "unknown presentation to stop %" PRIu8, req->PresentationId); |
522 | 0 | goto fail; |
523 | 0 | } |
524 | | |
525 | 0 | priv->droppedFrames = 0; |
526 | 0 | priv->publishedFrames = 0; |
527 | 0 | PresentationContext_unref(&priv->currentPresentation); |
528 | 0 | } |
529 | | |
530 | 0 | fail: |
531 | 0 | LeaveCriticalSection(&priv->framesLock); |
532 | 0 | return ret; |
533 | 0 | } |
534 | | |
535 | | WINPR_ATTR_NODISCARD |
536 | | static UINT video_read_tsmm_presentation_req(VideoClientContext* context, wStream* s) |
537 | 0 | { |
538 | 0 | TSMM_PRESENTATION_REQUEST req = WINPR_C_ARRAY_INIT; |
539 | |
|
540 | 0 | WINPR_ASSERT(context); |
541 | 0 | WINPR_ASSERT(s); |
542 | |
|
543 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 60)) |
544 | 0 | return ERROR_INVALID_DATA; |
545 | | |
546 | 0 | Stream_Read_UINT8(s, req.PresentationId); |
547 | 0 | Stream_Read_UINT8(s, req.Version); |
548 | 0 | Stream_Read_UINT8(s, req.Command); |
549 | 0 | Stream_Read_UINT8(s, req.FrameRate); /* FrameRate - reserved and ignored */ |
550 | |
|
551 | 0 | Stream_Seek_UINT16(s); /* AverageBitrateKbps reserved and ignored */ |
552 | 0 | Stream_Seek_UINT16(s); /* reserved */ |
553 | |
|
554 | 0 | Stream_Read_UINT32(s, req.SourceWidth); |
555 | 0 | Stream_Read_UINT32(s, req.SourceHeight); |
556 | 0 | Stream_Read_UINT32(s, req.ScaledWidth); |
557 | 0 | Stream_Read_UINT32(s, req.ScaledHeight); |
558 | 0 | Stream_Read_UINT64(s, req.hnsTimestampOffset); |
559 | 0 | Stream_Read_UINT64(s, req.GeometryMappingId); |
560 | 0 | Stream_Read(s, req.VideoSubtypeId, 16); |
561 | |
|
562 | 0 | Stream_Read_UINT32(s, req.cbExtra); |
563 | |
|
564 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra)) |
565 | 0 | return ERROR_INVALID_DATA; |
566 | | |
567 | 0 | req.pExtraData = Stream_Pointer(s); |
568 | |
|
569 | 0 | WLog_DBG(TAG, |
570 | 0 | "presentationReq: id:%" PRIu8 " version:%" PRIu8 |
571 | 0 | " command:%s srcWidth/srcHeight=%" PRIu32 "x%" PRIu32 " scaled Width/Height=%" PRIu32 |
572 | 0 | "x%" PRIu32 " timestamp=%" PRIu64 " mappingId=%" PRIx64 "", |
573 | 0 | req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth, |
574 | 0 | req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset, |
575 | 0 | req.GeometryMappingId); |
576 | |
|
577 | 0 | return video_PresentationRequest(context, &req); |
578 | 0 | } |
579 | | |
580 | | /** |
581 | | * Function description |
582 | | * |
583 | | * @return 0 on success, otherwise a Win32 error code |
584 | | */ |
585 | | WINPR_ATTR_NODISCARD |
586 | | static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s) |
587 | 0 | { |
588 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
589 | 0 | UINT ret = CHANNEL_RC_OK; |
590 | 0 | UINT32 cbSize = 0; |
591 | 0 | UINT32 packetType = 0; |
592 | |
|
593 | 0 | WINPR_ASSERT(callback); |
594 | 0 | WINPR_ASSERT(s); |
595 | |
|
596 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin; |
597 | 0 | WINPR_ASSERT(video); |
598 | |
|
599 | 0 | VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface; |
600 | 0 | WINPR_ASSERT(context); |
601 | |
|
602 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
603 | 0 | return ERROR_INVALID_DATA; |
604 | | |
605 | 0 | Stream_Read_UINT32(s, cbSize); |
606 | 0 | if (cbSize < 8) |
607 | 0 | { |
608 | 0 | WLog_ERR(TAG, "invalid cbSize %" PRIu32 ", expected 8", cbSize); |
609 | 0 | return ERROR_INVALID_DATA; |
610 | 0 | } |
611 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4)) |
612 | 0 | return ERROR_INVALID_DATA; |
613 | | |
614 | 0 | Stream_Read_UINT32(s, packetType); |
615 | 0 | switch (packetType) |
616 | 0 | { |
617 | 0 | case TSMM_PACKET_TYPE_PRESENTATION_REQUEST: |
618 | 0 | ret = video_read_tsmm_presentation_req(context, s); |
619 | 0 | break; |
620 | 0 | default: |
621 | 0 | WLog_ERR(TAG, "not expecting packet type %" PRIu32 "", packetType); |
622 | 0 | ret = ERROR_UNSUPPORTED_TYPE; |
623 | 0 | break; |
624 | 0 | } |
625 | | |
626 | 0 | return ret; |
627 | 0 | } |
628 | | |
629 | | static UINT video_control_send_client_notification(VideoClientContext* context, |
630 | | const TSMM_CLIENT_NOTIFICATION* notif) |
631 | 0 | { |
632 | 0 | BYTE buf[100] = WINPR_C_ARRAY_INIT; |
633 | |
|
634 | 0 | WINPR_ASSERT(context); |
635 | 0 | WINPR_ASSERT(notif); |
636 | |
|
637 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle; |
638 | 0 | WINPR_ASSERT(video); |
639 | |
|
640 | 0 | wStream* s = Stream_New(buf, 32); |
641 | 0 | if (!s) |
642 | 0 | return CHANNEL_RC_NO_MEMORY; |
643 | | |
644 | 0 | UINT32 cbSize = 16; |
645 | 0 | Stream_Seek_UINT32(s); /* cbSize */ |
646 | 0 | Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION); /* PacketType */ |
647 | 0 | Stream_Write_UINT8(s, notif->PresentationId); |
648 | 0 | Stream_Write_UINT8(s, notif->NotificationType); |
649 | 0 | Stream_Zero(s, 2); |
650 | 0 | if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE) |
651 | 0 | { |
652 | 0 | Stream_Write_UINT32(s, 16); /* cbData */ |
653 | | |
654 | | /* TSMM_CLIENT_NOTIFICATION_FRAMERATE_OVERRIDE */ |
655 | 0 | Stream_Write_UINT32(s, notif->FramerateOverride.Flags); |
656 | 0 | Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate); |
657 | 0 | Stream_Zero(s, 4ULL * 2ULL); |
658 | |
|
659 | 0 | cbSize += 4UL * 4UL; |
660 | 0 | } |
661 | 0 | else |
662 | 0 | { |
663 | 0 | Stream_Write_UINT32(s, 0); /* cbData */ |
664 | 0 | } |
665 | |
|
666 | 0 | Stream_SealLength(s); |
667 | 0 | Stream_ResetPosition(s); |
668 | 0 | Stream_Write_UINT32(s, cbSize); |
669 | 0 | Stream_Free(s, FALSE); |
670 | |
|
671 | 0 | return video_channel_write(video, buf, cbSize); |
672 | 0 | } |
673 | | |
674 | | static void video_timer(VideoClientContext* video, UINT64 now) |
675 | 0 | { |
676 | 0 | VideoFrame* frame = nullptr; |
677 | |
|
678 | 0 | WINPR_ASSERT(video); |
679 | |
|
680 | 0 | VideoClientContextPriv* priv = video->priv; |
681 | 0 | WINPR_ASSERT(priv); |
682 | |
|
683 | 0 | EnterCriticalSection(&priv->framesLock); |
684 | 0 | PresentationContext* presentation = video->priv->currentPresentation; |
685 | 0 | do |
686 | 0 | { |
687 | 0 | const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames); |
688 | 0 | if (!peekFrame) |
689 | 0 | break; |
690 | | |
691 | 0 | if (peekFrame->publishTime > now) |
692 | 0 | break; |
693 | | |
694 | 0 | if (frame) |
695 | 0 | { |
696 | 0 | WLog_DBG(TAG, "dropping frame @%" PRIu64, frame->publishTime); |
697 | 0 | priv->droppedFrames++; |
698 | 0 | VideoFrame_free(priv, frame); |
699 | 0 | } |
700 | 0 | frame = Queue_Dequeue(priv->frames); |
701 | 0 | } while (1); |
702 | |
|
703 | 0 | if (frame) |
704 | 0 | { |
705 | 0 | if (presentation && (presentation->PresentationId == frame->PresentationId)) |
706 | 0 | { |
707 | 0 | priv->publishedFrames++; |
708 | 0 | memcpy(presentation->surface->data, frame->surfaceData, |
709 | 0 | 1ull * frame->scanline * frame->h); |
710 | |
|
711 | 0 | WINPR_ASSERT(video->showSurface); |
712 | 0 | if (!video->showSurface(video, presentation->surface, presentation->ScaledWidth, |
713 | 0 | presentation->ScaledHeight)) |
714 | 0 | WLog_WARN(TAG, "showSurface failed"); |
715 | 0 | } |
716 | 0 | VideoFrame_free(priv, frame); |
717 | 0 | } |
718 | |
|
719 | 0 | if (priv->nextFeedbackTime < now) |
720 | 0 | { |
721 | | /* we can compute some feedback only if we have some published frames and |
722 | | * a current presentation |
723 | | */ |
724 | 0 | if (priv->publishedFrames && priv->currentPresentation) |
725 | 0 | { |
726 | 0 | UINT32 computedRate = 0; |
727 | |
|
728 | 0 | if (!PresentationContext_ref(priv->currentPresentation)) |
729 | 0 | WLog_WARN(TAG, "PresentationContext_ref(priv->currentPresentation) failed"); |
730 | |
|
731 | 0 | if (priv->droppedFrames) |
732 | 0 | { |
733 | | /** |
734 | | * some dropped frames, looks like we're asking too many frames per seconds, |
735 | | * try lowering rate. We go directly from unlimited rate to 24 frames/seconds |
736 | | * otherwise we lower rate by 2 frames by seconds |
737 | | */ |
738 | 0 | if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE) |
739 | 0 | computedRate = 24; |
740 | 0 | else |
741 | 0 | { |
742 | 0 | computedRate = priv->lastSentRate - 2; |
743 | 0 | if (!computedRate) |
744 | 0 | computedRate = 2; |
745 | 0 | } |
746 | 0 | } |
747 | 0 | else |
748 | 0 | { |
749 | | /** |
750 | | * we treat all frames ok, so either ask the server to send more, |
751 | | * or stay unlimited |
752 | | */ |
753 | 0 | if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE) |
754 | 0 | computedRate = XF_VIDEO_UNLIMITED_RATE; /* stay unlimited */ |
755 | 0 | else |
756 | 0 | { |
757 | 0 | computedRate = priv->lastSentRate + 2; |
758 | 0 | if (computedRate > XF_VIDEO_UNLIMITED_RATE) |
759 | 0 | computedRate = XF_VIDEO_UNLIMITED_RATE; |
760 | 0 | } |
761 | 0 | } |
762 | |
|
763 | 0 | if (computedRate != priv->lastSentRate) |
764 | 0 | { |
765 | 0 | TSMM_CLIENT_NOTIFICATION notif = WINPR_C_ARRAY_INIT; |
766 | |
|
767 | 0 | WINPR_ASSERT(priv->currentPresentation); |
768 | 0 | notif.PresentationId = priv->currentPresentation->PresentationId; |
769 | 0 | notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE; |
770 | 0 | if (computedRate == XF_VIDEO_UNLIMITED_RATE) |
771 | 0 | { |
772 | 0 | notif.FramerateOverride.Flags = 0x01; |
773 | 0 | notif.FramerateOverride.DesiredFrameRate = 0x00; |
774 | 0 | } |
775 | 0 | else |
776 | 0 | { |
777 | 0 | notif.FramerateOverride.Flags = 0x02; |
778 | 0 | notif.FramerateOverride.DesiredFrameRate = computedRate; |
779 | 0 | } |
780 | |
|
781 | 0 | video_control_send_client_notification(video, ¬if); |
782 | 0 | priv->lastSentRate = computedRate; |
783 | |
|
784 | 0 | WLog_VRB(TAG, |
785 | 0 | "server notified with rate %" PRIu32 " published=%" PRIu32 |
786 | 0 | " dropped=%" PRIu32, |
787 | 0 | priv->lastSentRate, priv->publishedFrames, priv->droppedFrames); |
788 | 0 | } |
789 | |
|
790 | 0 | PresentationContext_unref(&priv->currentPresentation); |
791 | 0 | } |
792 | |
|
793 | 0 | priv->droppedFrames = 0; |
794 | 0 | priv->publishedFrames = 0; |
795 | 0 | priv->nextFeedbackTime = now + 1000; |
796 | 0 | } |
797 | 0 | LeaveCriticalSection(&priv->framesLock); |
798 | 0 | } |
799 | | |
800 | | WINPR_ATTR_NODISCARD |
801 | | static UINT video_VideoData(VideoClientContext* context, const TSMM_VIDEO_DATA* data) |
802 | 0 | { |
803 | 0 | int status = 0; |
804 | 0 | UINT res = CHANNEL_RC_OK; |
805 | |
|
806 | 0 | WINPR_ASSERT(context); |
807 | 0 | WINPR_ASSERT(data); |
808 | |
|
809 | 0 | VideoClientContextPriv* priv = context->priv; |
810 | 0 | WINPR_ASSERT(priv); |
811 | |
|
812 | 0 | PresentationContext* presentation = priv->currentPresentation; |
813 | 0 | if (!presentation) |
814 | 0 | { |
815 | 0 | WLog_ERR(TAG, "no current presentation"); |
816 | 0 | return CHANNEL_RC_OK; |
817 | 0 | } |
818 | | |
819 | 0 | if (!PresentationContext_ref(presentation)) |
820 | 0 | return ERROR_INTERNAL_ERROR; |
821 | | |
822 | 0 | EnterCriticalSection(&priv->framesLock); |
823 | 0 | if (presentation->PresentationId != data->PresentationId) |
824 | 0 | { |
825 | 0 | WLog_ERR(TAG, "current presentation id=%" PRIu8 " doesn't match data id=%" PRIu8, |
826 | 0 | presentation->PresentationId, data->PresentationId); |
827 | 0 | goto out; |
828 | 0 | } |
829 | | |
830 | 0 | if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample)) |
831 | 0 | { |
832 | 0 | WLog_ERR(TAG, "unable to expand the current packet"); |
833 | 0 | res = CHANNEL_RC_NO_MEMORY; |
834 | 0 | goto out; |
835 | 0 | } |
836 | | |
837 | 0 | Stream_Write(presentation->currentSample, data->pSample, data->cbSample); |
838 | |
|
839 | 0 | if (data->CurrentPacketIndex == data->PacketsInSample) |
840 | 0 | { |
841 | 0 | VideoSurface* surface = presentation->surface; |
842 | 0 | H264_CONTEXT* h264 = presentation->h264; |
843 | 0 | const UINT64 startTime = winpr_GetTickCount64NS(); |
844 | 0 | MAPPED_GEOMETRY* geom = presentation->geometry; |
845 | |
|
846 | 0 | const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth), |
847 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) }; |
848 | 0 | Stream_SealLength(presentation->currentSample); |
849 | 0 | Stream_ResetPosition(presentation->currentSample); |
850 | |
|
851 | 0 | if (data->SampleNumber == 1) |
852 | 0 | { |
853 | 0 | presentation->lastPublishTime = startTime; |
854 | 0 | } |
855 | |
|
856 | 0 | presentation->lastPublishTime += 100ull * data->hnsDuration; |
857 | 0 | const size_t len = Stream_Length(presentation->currentSample); |
858 | 0 | if (len > UINT32_MAX) |
859 | 0 | goto out; |
860 | | |
861 | 0 | BOOL enqueueResult = 0; |
862 | 0 | VideoFrame* frame = VideoFrame_new(priv, presentation, geom); |
863 | 0 | if (!frame) |
864 | 0 | { |
865 | 0 | WLog_ERR(TAG, "unable to create frame"); |
866 | 0 | res = CHANNEL_RC_NO_MEMORY; |
867 | 0 | goto out; |
868 | 0 | } |
869 | | |
870 | 0 | status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len, |
871 | 0 | frame->surfaceData, surface->format, surface->scanline, |
872 | 0 | surface->alignedWidth, surface->alignedHeight, &rect, 1); |
873 | 0 | if (status < 0) |
874 | 0 | { |
875 | 0 | VideoFrame_free(priv, frame); |
876 | 0 | goto out; |
877 | 0 | } |
878 | | |
879 | 0 | enqueueResult = Queue_Enqueue(priv->frames, frame); |
880 | |
|
881 | 0 | if (!enqueueResult) |
882 | 0 | { |
883 | 0 | WLog_ERR(TAG, "unable to enqueue frame"); |
884 | 0 | VideoFrame_free(priv, frame); |
885 | 0 | res = CHANNEL_RC_NO_MEMORY; |
886 | 0 | goto out; |
887 | 0 | } |
888 | | |
889 | | // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue owns frame |
890 | 0 | WLog_DBG(TAG, "scheduling frame in %" PRIu64 " ns", (frame->publishTime - startTime)); |
891 | 0 | } |
892 | | |
893 | 0 | out: |
894 | 0 | PresentationContext_unref(&priv->currentPresentation); |
895 | 0 | LeaveCriticalSection(&priv->framesLock); |
896 | |
|
897 | 0 | return res; |
898 | 0 | } |
899 | | |
900 | | WINPR_ATTR_NODISCARD |
901 | | static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s) |
902 | 0 | { |
903 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
904 | 0 | UINT32 cbSize = 0; |
905 | 0 | UINT32 packetType = 0; |
906 | 0 | TSMM_VIDEO_DATA data; |
907 | |
|
908 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin; |
909 | 0 | WINPR_ASSERT(video); |
910 | |
|
911 | 0 | VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface; |
912 | |
|
913 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
914 | 0 | return ERROR_INVALID_DATA; |
915 | | |
916 | 0 | Stream_Read_UINT32(s, cbSize); |
917 | 0 | if (cbSize < 8) |
918 | 0 | { |
919 | 0 | WLog_ERR(TAG, "invalid cbSize %" PRIu32 ", expected >= 8", cbSize); |
920 | 0 | return ERROR_INVALID_DATA; |
921 | 0 | } |
922 | | |
923 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4)) |
924 | 0 | return ERROR_INVALID_DATA; |
925 | | |
926 | 0 | Stream_Read_UINT32(s, packetType); |
927 | 0 | if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA) |
928 | 0 | { |
929 | 0 | WLog_ERR(TAG, "only expecting VIDEO_DATA on the data channel"); |
930 | 0 | return ERROR_INVALID_DATA; |
931 | 0 | } |
932 | | |
933 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 32)) |
934 | 0 | return ERROR_INVALID_DATA; |
935 | | |
936 | 0 | Stream_Read_UINT8(s, data.PresentationId); |
937 | 0 | Stream_Read_UINT8(s, data.Version); |
938 | 0 | Stream_Read_UINT8(s, data.Flags); |
939 | 0 | Stream_Seek_UINT8(s); /* reserved */ |
940 | 0 | Stream_Read_UINT64(s, data.hnsTimestamp); |
941 | 0 | Stream_Read_UINT64(s, data.hnsDuration); |
942 | 0 | Stream_Read_UINT16(s, data.CurrentPacketIndex); |
943 | 0 | Stream_Read_UINT16(s, data.PacketsInSample); |
944 | 0 | Stream_Read_UINT32(s, data.SampleNumber); |
945 | 0 | Stream_Read_UINT32(s, data.cbSample); |
946 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample)) |
947 | 0 | return ERROR_INVALID_DATA; |
948 | 0 | data.pSample = Stream_Pointer(s); |
949 | | |
950 | | /* |
951 | | WLog_DBG(TAG, "videoData: id:%"PRIu8" version:%"PRIu8" flags:0x%"PRIx8" timestamp=%"PRIu64" |
952 | | duration=%"PRIu64 " curPacketIndex:%"PRIu16" packetInSample:%"PRIu16" sampleNumber:%"PRIu32" |
953 | | cbSample:%"PRIu32"", data.PresentationId, data.Version, data.Flags, data.hnsTimestamp, |
954 | | data.hnsDuration, data.CurrentPacketIndex, data.PacketsInSample, data.SampleNumber, |
955 | | data.cbSample); |
956 | | */ |
957 | |
|
958 | 0 | return video_VideoData(context, &data); |
959 | 0 | } |
960 | | |
961 | | /** |
962 | | * Function description |
963 | | * |
964 | | * @return 0 on success, otherwise a Win32 error code |
965 | | */ |
966 | | WINPR_ATTR_NODISCARD |
967 | | static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback) |
968 | 0 | { |
969 | 0 | if (pChannelCallback) |
970 | 0 | { |
971 | 0 | GENERIC_CHANNEL_CALLBACK* listener_callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
972 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin; |
973 | 0 | if (video && video->control_callback) |
974 | 0 | { |
975 | 0 | video->control_callback->channel_callback = nullptr; |
976 | 0 | } |
977 | 0 | } |
978 | 0 | free(pChannelCallback); |
979 | 0 | return CHANNEL_RC_OK; |
980 | 0 | } |
981 | | |
982 | | WINPR_ATTR_NODISCARD |
983 | | static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback) |
984 | 0 | { |
985 | 0 | if (pChannelCallback) |
986 | 0 | { |
987 | 0 | GENERIC_CHANNEL_CALLBACK* listener_callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
988 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin; |
989 | 0 | if (video && video->data_callback) |
990 | 0 | { |
991 | 0 | video->data_callback->channel_callback = nullptr; |
992 | 0 | } |
993 | 0 | } |
994 | 0 | free(pChannelCallback); |
995 | 0 | return CHANNEL_RC_OK; |
996 | 0 | } |
997 | | |
998 | | /** |
999 | | * Function description |
1000 | | * |
1001 | | * @return 0 on success, otherwise a Win32 error code |
1002 | | */ |
1003 | | // NOLINTBEGIN(readability-non-const-parameter) |
1004 | | WINPR_ATTR_NODISCARD |
1005 | | static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback, |
1006 | | IWTSVirtualChannel* channel, |
1007 | | WINPR_ATTR_UNUSED BYTE* Data, |
1008 | | WINPR_ATTR_UNUSED BOOL* pbAccept, |
1009 | | IWTSVirtualChannelCallback** ppCallback) |
1010 | | // NOLINTEND(readability-non-const-parameter) |
1011 | 0 | { |
1012 | 0 | GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)listenerCallback; |
1013 | |
|
1014 | 0 | GENERIC_CHANNEL_CALLBACK* callback = |
1015 | 0 | (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK)); |
1016 | 0 | if (!callback) |
1017 | 0 | { |
1018 | 0 | WLog_ERR(TAG, "calloc failed!"); |
1019 | 0 | return CHANNEL_RC_NO_MEMORY; |
1020 | 0 | } |
1021 | | |
1022 | 0 | callback->iface.OnDataReceived = video_control_on_data_received; |
1023 | 0 | callback->iface.OnClose = video_control_on_close; |
1024 | 0 | callback->plugin = listener_callback->plugin; |
1025 | 0 | callback->channel_mgr = listener_callback->channel_mgr; |
1026 | 0 | callback->channel = channel; |
1027 | 0 | listener_callback->channel_callback = callback; |
1028 | |
|
1029 | 0 | *ppCallback = &callback->iface; |
1030 | |
|
1031 | 0 | return CHANNEL_RC_OK; |
1032 | 0 | } |
1033 | | |
1034 | | // NOLINTBEGIN(readability-non-const-parameter) |
1035 | | WINPR_ATTR_NODISCARD |
1036 | | static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback, |
1037 | | IWTSVirtualChannel* pChannel, |
1038 | | WINPR_ATTR_UNUSED BYTE* Data, |
1039 | | WINPR_ATTR_UNUSED BOOL* pbAccept, |
1040 | | IWTSVirtualChannelCallback** ppCallback) |
1041 | | // NOLINTEND(readability-non-const-parameter) |
1042 | 0 | { |
1043 | 0 | GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback; |
1044 | |
|
1045 | 0 | GENERIC_CHANNEL_CALLBACK* callback = |
1046 | 0 | (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK)); |
1047 | 0 | if (!callback) |
1048 | 0 | { |
1049 | 0 | WLog_ERR(TAG, "calloc failed!"); |
1050 | 0 | return CHANNEL_RC_NO_MEMORY; |
1051 | 0 | } |
1052 | | |
1053 | 0 | callback->iface.OnDataReceived = video_data_on_data_received; |
1054 | 0 | callback->iface.OnClose = video_data_on_close; |
1055 | 0 | callback->plugin = listener_callback->plugin; |
1056 | 0 | callback->channel_mgr = listener_callback->channel_mgr; |
1057 | 0 | callback->channel = pChannel; |
1058 | 0 | listener_callback->channel_callback = callback; |
1059 | |
|
1060 | 0 | *ppCallback = &callback->iface; |
1061 | |
|
1062 | 0 | return CHANNEL_RC_OK; |
1063 | 0 | } |
1064 | | |
1065 | | WINPR_ATTR_NODISCARD |
1066 | | static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context, void* userdata, |
1067 | | WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp, |
1068 | | uint64_t interval) |
1069 | 0 | { |
1070 | 0 | VideoClientContext* video = userdata; |
1071 | 0 | if (!video) |
1072 | 0 | return 0; |
1073 | 0 | if (!video->timer) |
1074 | 0 | return 0; |
1075 | | |
1076 | 0 | video->timer(video, timestamp); |
1077 | |
|
1078 | 0 | return interval; |
1079 | 0 | } |
1080 | | |
1081 | | /** |
1082 | | * Function description |
1083 | | * |
1084 | | * @return 0 on success, otherwise a Win32 error code |
1085 | | */ |
1086 | | WINPR_ATTR_NODISCARD |
1087 | | static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr) |
1088 | 0 | { |
1089 | 0 | UINT status = 0; |
1090 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin; |
1091 | |
|
1092 | 0 | if (video->initialized) |
1093 | 0 | { |
1094 | 0 | WLog_ERR(TAG, "[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME); |
1095 | 0 | return ERROR_INVALID_DATA; |
1096 | 0 | } |
1097 | | |
1098 | 0 | { |
1099 | 0 | GENERIC_LISTENER_CALLBACK* callback = |
1100 | 0 | (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK)); |
1101 | 0 | if (!callback) |
1102 | 0 | { |
1103 | 0 | WLog_ERR(TAG, "calloc for control callback failed!"); |
1104 | 0 | return CHANNEL_RC_NO_MEMORY; |
1105 | 0 | } |
1106 | | |
1107 | 0 | callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection; |
1108 | 0 | callback->plugin = plugin; |
1109 | 0 | callback->channel_mgr = channelMgr; |
1110 | |
|
1111 | 0 | status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0, |
1112 | 0 | &callback->iface, &(video->controlListener)); |
1113 | 0 | video->control_callback = callback; |
1114 | 0 | if (status != CHANNEL_RC_OK) |
1115 | 0 | return status; |
1116 | 0 | } |
1117 | 0 | video->controlListener->pInterface = video->wtsPlugin.pInterface; |
1118 | |
|
1119 | 0 | { |
1120 | 0 | GENERIC_LISTENER_CALLBACK* callback = |
1121 | 0 | (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK)); |
1122 | 0 | if (!callback) |
1123 | 0 | { |
1124 | 0 | WLog_ERR(TAG, "calloc for data callback failed!"); |
1125 | 0 | return CHANNEL_RC_NO_MEMORY; |
1126 | 0 | } |
1127 | | |
1128 | 0 | callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection; |
1129 | 0 | callback->plugin = plugin; |
1130 | 0 | callback->channel_mgr = channelMgr; |
1131 | |
|
1132 | 0 | status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0, |
1133 | 0 | &callback->iface, &(video->dataListener)); |
1134 | 0 | video->data_callback = callback; |
1135 | 0 | if (status == CHANNEL_RC_OK) |
1136 | 0 | video->dataListener->pInterface = video->wtsPlugin.pInterface; |
1137 | 0 | } |
1138 | | |
1139 | 0 | if (status == CHANNEL_RC_OK) |
1140 | 0 | video->context->priv->timerID = |
1141 | 0 | freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context, true); |
1142 | 0 | video->initialized = video->context->priv->timerID != 0; |
1143 | 0 | if (!video->initialized) |
1144 | 0 | status = ERROR_INTERNAL_ERROR; |
1145 | 0 | return status; |
1146 | 0 | } |
1147 | | |
1148 | | /** |
1149 | | * Function description |
1150 | | * |
1151 | | * @return 0 on success, otherwise a Win32 error code |
1152 | | */ |
1153 | | WINPR_ATTR_NODISCARD |
1154 | | static UINT video_plugin_terminated(IWTSPlugin* pPlugin) |
1155 | 0 | { |
1156 | 0 | VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin; |
1157 | 0 | if (!video) |
1158 | 0 | return CHANNEL_RC_INVALID_INSTANCE; |
1159 | | |
1160 | 0 | if (video->context && video->context->priv) |
1161 | 0 | freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID); |
1162 | |
|
1163 | 0 | if (video->control_callback) |
1164 | 0 | { |
1165 | 0 | IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr; |
1166 | 0 | if (mgr) |
1167 | 0 | IFCALL(mgr->DestroyListener, mgr, video->controlListener); |
1168 | 0 | } |
1169 | 0 | if (video->data_callback) |
1170 | 0 | { |
1171 | 0 | IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr; |
1172 | 0 | if (mgr) |
1173 | 0 | IFCALL(mgr->DestroyListener, mgr, video->dataListener); |
1174 | 0 | } |
1175 | |
|
1176 | 0 | if (video->context) |
1177 | 0 | VideoClientContextPriv_free(video->context->priv); |
1178 | |
|
1179 | 0 | free(video->control_callback); |
1180 | 0 | free(video->data_callback); |
1181 | 0 | free(video->wtsPlugin.pInterface); |
1182 | 0 | free(pPlugin); |
1183 | 0 | return CHANNEL_RC_OK; |
1184 | 0 | } |
1185 | | |
1186 | | /** |
1187 | | * Channel Client Interface |
1188 | | */ |
1189 | | /** |
1190 | | * Function description |
1191 | | * |
1192 | | * @return 0 on success, otherwise a Win32 error code |
1193 | | */ |
1194 | | WINPR_ATTR_NODISCARD |
1195 | | FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints)) |
1196 | 0 | { |
1197 | 0 | UINT error = ERROR_INTERNAL_ERROR; |
1198 | |
|
1199 | 0 | VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, "video"); |
1200 | 0 | if (!videoPlugin) |
1201 | 0 | { |
1202 | 0 | videoPlugin = (VIDEO_PLUGIN*)calloc(1, sizeof(VIDEO_PLUGIN)); |
1203 | 0 | if (!videoPlugin) |
1204 | 0 | { |
1205 | 0 | WLog_ERR(TAG, "calloc failed!"); |
1206 | 0 | return CHANNEL_RC_NO_MEMORY; |
1207 | 0 | } |
1208 | | |
1209 | 0 | videoPlugin->wtsPlugin.Initialize = video_plugin_initialize; |
1210 | 0 | videoPlugin->wtsPlugin.Connected = nullptr; |
1211 | 0 | videoPlugin->wtsPlugin.Disconnected = nullptr; |
1212 | 0 | videoPlugin->wtsPlugin.Terminated = video_plugin_terminated; |
1213 | |
|
1214 | 0 | VideoClientContext* videoContext = |
1215 | 0 | (VideoClientContext*)calloc(1, sizeof(VideoClientContext)); |
1216 | 0 | if (!videoContext) |
1217 | 0 | { |
1218 | 0 | WLog_ERR(TAG, "calloc failed!"); |
1219 | 0 | free(videoPlugin); |
1220 | 0 | return CHANNEL_RC_NO_MEMORY; |
1221 | 0 | } |
1222 | | |
1223 | 0 | VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext); |
1224 | 0 | if (!priv) |
1225 | 0 | { |
1226 | 0 | WLog_ERR(TAG, "VideoClientContextPriv_new failed!"); |
1227 | 0 | free(videoContext); |
1228 | 0 | free(videoPlugin); |
1229 | 0 | return CHANNEL_RC_NO_MEMORY; |
1230 | 0 | } |
1231 | | |
1232 | 0 | videoContext->handle = (void*)videoPlugin; |
1233 | 0 | videoContext->priv = priv; |
1234 | 0 | videoContext->timer = video_timer; |
1235 | 0 | videoContext->setGeometry = video_client_context_set_geometry; |
1236 | |
|
1237 | 0 | videoPlugin->wtsPlugin.pInterface = (void*)videoContext; |
1238 | 0 | videoPlugin->context = videoContext; |
1239 | 0 | videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints); |
1240 | 0 | if (videoPlugin->rdpcontext) |
1241 | 0 | error = pEntryPoints->RegisterPlugin(pEntryPoints, "video", &videoPlugin->wtsPlugin); |
1242 | 0 | } |
1243 | 0 | else |
1244 | 0 | { |
1245 | 0 | WLog_ERR(TAG, "could not get video Plugin."); |
1246 | 0 | return CHANNEL_RC_BAD_CHANNEL; |
1247 | 0 | } |
1248 | | |
1249 | 0 | return error; |
1250 | 0 | } |