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