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