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