Coverage Report

Created: 2026-03-04 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/video/client/video_main.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Video Optimized Remoting Virtual Channel Extension
4
 *
5
 * Copyright 2017 David Fort <contact@hardening-consulting.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <freerdp/config.h>
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include <winpr/crt.h>
27
#include <winpr/assert.h>
28
#include <winpr/cast.h>
29
#include <winpr/synch.h>
30
#include <winpr/print.h>
31
#include <winpr/stream.h>
32
#include <winpr/cmdline.h>
33
#include <winpr/collections.h>
34
#include <winpr/interlocked.h>
35
#include <winpr/sysinfo.h>
36
37
#include <freerdp/addin.h>
38
#include <freerdp/primitives.h>
39
#include <freerdp/client/channels.h>
40
#include <freerdp/client/geometry.h>
41
#include <freerdp/client/video.h>
42
#include <freerdp/channels/log.h>
43
#include <freerdp/codec/h264.h>
44
#include <freerdp/codec/yuv.h>
45
#include <freerdp/timer.h>
46
47
#define TAG CHANNELS_TAG("video")
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 = nullptr;
140
141
0
  WINPR_ASSERT(video);
142
0
  ret = calloc(1, sizeof(*ret));
143
0
  if (!ret)
144
0
    return nullptr;
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 nullptr;
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 = nullptr;
192
193
0
  WINPR_ASSERT(video);
194
195
0
  if (s > INT32_MAX)
196
0
    return nullptr;
197
198
0
  ret = calloc(1, sizeof(*ret));
199
0
  if (!ret)
200
0
    return nullptr;
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(nullptr, 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 nullptr;
236
0
}
237
238
static void PresentationContext_unref(PresentationContext** ppresentation)
239
0
{
240
0
  PresentationContext* presentation = nullptr;
241
0
  MAPPED_GEOMETRY* geometry = nullptr;
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 = nullptr;
256
0
    geometry->MappedGeometryClear = nullptr;
257
0
    geometry->custom = nullptr;
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 = nullptr;
266
0
}
267
268
static void VideoFrame_free(VideoFrame** pframe)
269
0
{
270
0
  VideoFrame* frame = nullptr;
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 = nullptr;
286
0
}
287
288
static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
289
                                  MAPPED_GEOMETRY* geom)
290
0
{
291
0
  VideoFrame* frame = nullptr;
292
0
  const VideoSurface* surface = nullptr;
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 nullptr;
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] = WINPR_C_ARRAY_INIT;
361
0
  wStream* s = nullptr;
362
0
  VIDEO_PLUGIN* video = nullptr;
363
0
  IWTSVirtualChannel* channel = nullptr;
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, nullptr);
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 = nullptr;
392
0
  RDP_RECT* r = nullptr;
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 = nullptr;
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 = nullptr;
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 = nullptr;
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 = WINPR_C_ARRAY_INIT;
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 = nullptr;
580
0
  VideoClientContext* context = nullptr;
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 = nullptr;
626
0
  VIDEO_PLUGIN* video = nullptr;
627
0
  IWTSVirtualChannel* channel = nullptr;
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_ResetPosition(s);
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, nullptr);
676
677
0
  return ret;
678
0
}
679
680
static void video_timer(VideoClientContext* video, UINT64 now)
681
0
{
682
0
  PresentationContext* presentation = nullptr;
683
0
  VideoFrame* frame = nullptr;
684
685
0
  WINPR_ASSERT(video);
686
687
0
  VideoClientContextPriv* priv = video->priv;
688
0
  WINPR_ASSERT(priv);
689
690
0
  EnterCriticalSection(&priv->framesLock);
691
0
  do
692
0
  {
693
0
    const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
694
0
    if (!peekFrame)
695
0
      break;
696
697
0
    if (peekFrame->publishTime > now)
698
0
      break;
699
700
0
    if (frame)
701
0
    {
702
0
      WLog_DBG(TAG, "dropping frame @%" PRIu64, frame->publishTime);
703
0
      priv->droppedFrames++;
704
0
      VideoFrame_free(&frame);
705
0
    }
706
0
    frame = Queue_Dequeue(priv->frames);
707
0
  } while (1);
708
0
  LeaveCriticalSection(&priv->framesLock);
709
710
0
  if (frame)
711
0
  {
712
0
    presentation = frame->presentation;
713
714
0
    priv->publishedFrames++;
715
0
    memcpy(presentation->surface->data, frame->surfaceData, 1ull * frame->scanline * frame->h);
716
717
0
    WINPR_ASSERT(video->showSurface);
718
0
    if (!video->showSurface(video, presentation->surface, presentation->ScaledWidth,
719
0
                            presentation->ScaledHeight))
720
0
      WLog_WARN(TAG, "showSurface failed");
721
722
0
    VideoFrame_free(&frame);
723
0
  }
724
725
0
  if (priv->nextFeedbackTime < now)
726
0
  {
727
    /* we can compute some feedback only if we have some published frames and
728
     * a current presentation
729
     */
730
0
    if (priv->publishedFrames && priv->currentPresentation)
731
0
    {
732
0
      UINT32 computedRate = 0;
733
734
0
      PresentationContext_ref(priv->currentPresentation);
735
736
0
      if (priv->droppedFrames)
737
0
      {
738
        /**
739
         * some dropped frames, looks like we're asking too many frames per seconds,
740
         * try lowering rate. We go directly from unlimited rate to 24 frames/seconds
741
         * otherwise we lower rate by 2 frames by seconds
742
         */
743
0
        if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
744
0
          computedRate = 24;
745
0
        else
746
0
        {
747
0
          computedRate = priv->lastSentRate - 2;
748
0
          if (!computedRate)
749
0
            computedRate = 2;
750
0
        }
751
0
      }
752
0
      else
753
0
      {
754
        /**
755
         * we treat all frames ok, so either ask the server to send more,
756
         * or stay unlimited
757
         */
758
0
        if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
759
0
          computedRate = XF_VIDEO_UNLIMITED_RATE; /* stay unlimited */
760
0
        else
761
0
        {
762
0
          computedRate = priv->lastSentRate + 2;
763
0
          if (computedRate > XF_VIDEO_UNLIMITED_RATE)
764
0
            computedRate = XF_VIDEO_UNLIMITED_RATE;
765
0
        }
766
0
      }
767
768
0
      if (computedRate != priv->lastSentRate)
769
0
      {
770
0
        TSMM_CLIENT_NOTIFICATION notif;
771
772
0
        WINPR_ASSERT(priv->currentPresentation);
773
0
        notif.PresentationId = priv->currentPresentation->PresentationId;
774
0
        notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
775
0
        if (computedRate == XF_VIDEO_UNLIMITED_RATE)
776
0
        {
777
0
          notif.FramerateOverride.Flags = 0x01;
778
0
          notif.FramerateOverride.DesiredFrameRate = 0x00;
779
0
        }
780
0
        else
781
0
        {
782
0
          notif.FramerateOverride.Flags = 0x02;
783
0
          notif.FramerateOverride.DesiredFrameRate = computedRate;
784
0
        }
785
786
0
        video_control_send_client_notification(video, &notif);
787
0
        priv->lastSentRate = computedRate;
788
789
0
        WLog_VRB(TAG,
790
0
                 "server notified with rate %" PRIu32 " published=%" PRIu32
791
0
                 " dropped=%" PRIu32,
792
0
                 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
793
0
      }
794
795
0
      PresentationContext_unref(&priv->currentPresentation);
796
0
    }
797
798
0
    priv->droppedFrames = 0;
799
0
    priv->publishedFrames = 0;
800
0
    priv->nextFeedbackTime = now + 1000;
801
0
  }
802
0
}
803
804
static UINT video_VideoData(VideoClientContext* context, const TSMM_VIDEO_DATA* data)
805
0
{
806
0
  VideoClientContextPriv* priv = nullptr;
807
0
  PresentationContext* presentation = nullptr;
808
0
  int status = 0;
809
810
0
  WINPR_ASSERT(context);
811
0
  WINPR_ASSERT(data);
812
813
0
  priv = context->priv;
814
0
  WINPR_ASSERT(priv);
815
816
0
  presentation = priv->currentPresentation;
817
0
  if (!presentation)
818
0
  {
819
0
    WLog_ERR(TAG, "no current presentation");
820
0
    return CHANNEL_RC_OK;
821
0
  }
822
823
0
  if (presentation->PresentationId != data->PresentationId)
824
0
  {
825
0
    WLog_ERR(TAG, "current presentation id=%" PRIu8 " doesn't match data id=%" PRIu8,
826
0
             presentation->PresentationId, data->PresentationId);
827
0
    return CHANNEL_RC_OK;
828
0
  }
829
830
0
  if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
831
0
  {
832
0
    WLog_ERR(TAG, "unable to expand the current packet");
833
0
    return CHANNEL_RC_NO_MEMORY;
834
0
  }
835
836
0
  Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
837
838
0
  if (data->CurrentPacketIndex == data->PacketsInSample)
839
0
  {
840
0
    VideoSurface* surface = presentation->surface;
841
0
    H264_CONTEXT* h264 = presentation->h264;
842
0
    const UINT64 startTime = winpr_GetTickCount64NS();
843
0
    MAPPED_GEOMETRY* geom = presentation->geometry;
844
845
0
    const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
846
0
                              WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
847
0
    Stream_SealLength(presentation->currentSample);
848
0
    Stream_ResetPosition(presentation->currentSample);
849
850
0
    const UINT64 timeAfterH264 = winpr_GetTickCount64NS();
851
0
    if (data->SampleNumber == 1)
852
0
    {
853
0
      presentation->lastPublishTime = startTime;
854
0
    }
855
856
0
    presentation->lastPublishTime += 100ull * data->hnsDuration;
857
0
    if (presentation->lastPublishTime <= (10000000ull + timeAfterH264))
858
0
    {
859
0
      int dropped = 0;
860
861
0
      const size_t len = Stream_Length(presentation->currentSample);
862
0
      if (len > UINT32_MAX)
863
0
        return CHANNEL_RC_OK;
864
865
      /* if the frame is to be published in less than 10 ms, let's consider it's now */
866
0
      status =
867
0
          avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
868
0
                            surface->data, surface->format, surface->scanline,
869
0
                            surface->alignedWidth, surface->alignedHeight, &rect, 1);
870
871
0
      if (status < 0)
872
0
        return CHANNEL_RC_OK;
873
874
0
      WINPR_ASSERT(context->showSurface);
875
0
      if (!context->showSurface(context, presentation->surface, presentation->ScaledWidth,
876
0
                                presentation->ScaledHeight))
877
0
        return CHANNEL_RC_NOT_INITIALIZED;
878
879
0
      priv->publishedFrames++;
880
881
      /* cleanup previously scheduled frames */
882
0
      EnterCriticalSection(&priv->framesLock);
883
0
      while (Queue_Count(priv->frames) > 0)
884
0
      {
885
0
        VideoFrame* frame = Queue_Dequeue(priv->frames);
886
0
        if (frame)
887
0
        {
888
0
          priv->droppedFrames++;
889
0
          VideoFrame_free(&frame);
890
0
          dropped++;
891
0
        }
892
0
      }
893
0
      LeaveCriticalSection(&priv->framesLock);
894
895
0
      if (dropped)
896
0
        WLog_DBG(TAG, "showing frame (%d dropped)", dropped);
897
0
    }
898
0
    else
899
0
    {
900
0
      const size_t len = Stream_Length(presentation->currentSample);
901
0
      if (len > UINT32_MAX)
902
0
        return CHANNEL_RC_OK;
903
904
0
      BOOL enqueueResult = 0;
905
0
      VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
906
0
      if (!frame)
907
0
      {
908
0
        WLog_ERR(TAG, "unable to create frame");
909
0
        return CHANNEL_RC_NO_MEMORY;
910
0
      }
911
912
0
      status =
913
0
          avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
914
0
                            frame->surfaceData, surface->format, surface->scanline,
915
0
                            surface->alignedWidth, surface->alignedHeight, &rect, 1);
916
0
      if (status < 0)
917
0
      {
918
0
        VideoFrame_free(&frame);
919
0
        return CHANNEL_RC_OK;
920
0
      }
921
922
0
      EnterCriticalSection(&priv->framesLock);
923
0
      enqueueResult = Queue_Enqueue(priv->frames, frame);
924
0
      LeaveCriticalSection(&priv->framesLock);
925
926
0
      if (!enqueueResult)
927
0
      {
928
0
        WLog_ERR(TAG, "unable to enqueue frame");
929
0
        VideoFrame_free(&frame);
930
0
        return CHANNEL_RC_NO_MEMORY;
931
0
      }
932
933
      // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue owns frame
934
0
      WLog_DBG(TAG, "scheduling frame in %" PRIu64 " ms", (frame->publishTime - startTime));
935
0
    }
936
0
  }
937
938
0
  return CHANNEL_RC_OK;
939
0
}
940
941
static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s)
942
0
{
943
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
944
0
  VIDEO_PLUGIN* video = nullptr;
945
0
  VideoClientContext* context = nullptr;
946
0
  UINT32 cbSize = 0;
947
0
  UINT32 packetType = 0;
948
0
  TSMM_VIDEO_DATA data;
949
950
0
  video = (VIDEO_PLUGIN*)callback->plugin;
951
0
  context = (VideoClientContext*)video->wtsPlugin.pInterface;
952
953
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
954
0
    return ERROR_INVALID_DATA;
955
956
0
  Stream_Read_UINT32(s, cbSize);
957
0
  if (cbSize < 8)
958
0
  {
959
0
    WLog_ERR(TAG, "invalid cbSize %" PRIu32 ", expected >= 8", cbSize);
960
0
    return ERROR_INVALID_DATA;
961
0
  }
962
963
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
964
0
    return ERROR_INVALID_DATA;
965
966
0
  Stream_Read_UINT32(s, packetType);
967
0
  if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
968
0
  {
969
0
    WLog_ERR(TAG, "only expecting VIDEO_DATA on the data channel");
970
0
    return ERROR_INVALID_DATA;
971
0
  }
972
973
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
974
0
    return ERROR_INVALID_DATA;
975
976
0
  Stream_Read_UINT8(s, data.PresentationId);
977
0
  Stream_Read_UINT8(s, data.Version);
978
0
  Stream_Read_UINT8(s, data.Flags);
979
0
  Stream_Seek_UINT8(s); /* reserved */
980
0
  Stream_Read_UINT64(s, data.hnsTimestamp);
981
0
  Stream_Read_UINT64(s, data.hnsDuration);
982
0
  Stream_Read_UINT16(s, data.CurrentPacketIndex);
983
0
  Stream_Read_UINT16(s, data.PacketsInSample);
984
0
  Stream_Read_UINT32(s, data.SampleNumber);
985
0
  Stream_Read_UINT32(s, data.cbSample);
986
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
987
0
    return ERROR_INVALID_DATA;
988
0
  data.pSample = Stream_Pointer(s);
989
990
  /*
991
      WLog_DBG(TAG, "videoData: id:%"PRIu8" version:%"PRIu8" flags:0x%"PRIx8" timestamp=%"PRIu64"
992
     duration=%"PRIu64 " curPacketIndex:%"PRIu16" packetInSample:%"PRIu16" sampleNumber:%"PRIu32"
993
     cbSample:%"PRIu32"", data.PresentationId, data.Version, data.Flags, data.hnsTimestamp,
994
     data.hnsDuration, data.CurrentPacketIndex, data.PacketsInSample, data.SampleNumber,
995
     data.cbSample);
996
  */
997
998
0
  return video_VideoData(context, &data);
999
0
}
1000
1001
/**
1002
 * Function description
1003
 *
1004
 * @return 0 on success, otherwise a Win32 error code
1005
 */
1006
static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1007
0
{
1008
0
  free(pChannelCallback);
1009
0
  return CHANNEL_RC_OK;
1010
0
}
1011
1012
static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1013
0
{
1014
0
  free(pChannelCallback);
1015
0
  return CHANNEL_RC_OK;
1016
0
}
1017
1018
/**
1019
 * Function description
1020
 *
1021
 * @return 0 on success, otherwise a Win32 error code
1022
 */
1023
// NOLINTBEGIN(readability-non-const-parameter)
1024
static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1025
                                                    IWTSVirtualChannel* channel, BYTE* Data,
1026
                                                    BOOL* pbAccept,
1027
                                                    IWTSVirtualChannelCallback** ppCallback)
1028
// NOLINTEND(readability-non-const-parameter)
1029
0
{
1030
0
  GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)listenerCallback;
1031
1032
0
  WINPR_UNUSED(Data);
1033
0
  WINPR_UNUSED(pbAccept);
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
static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1057
                                                 IWTSVirtualChannel* pChannel, BYTE* Data,
1058
                                                 BOOL* pbAccept,
1059
                                                 IWTSVirtualChannelCallback** ppCallback)
1060
// NOLINTEND(readability-non-const-parameter)
1061
0
{
1062
0
  GENERIC_CHANNEL_CALLBACK* callback = nullptr;
1063
0
  GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
1064
1065
0
  WINPR_UNUSED(Data);
1066
0
  WINPR_UNUSED(pbAccept);
1067
1068
0
  callback = (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK));
1069
0
  if (!callback)
1070
0
  {
1071
0
    WLog_ERR(TAG, "calloc failed!");
1072
0
    return CHANNEL_RC_NO_MEMORY;
1073
0
  }
1074
1075
0
  callback->iface.OnDataReceived = video_data_on_data_received;
1076
0
  callback->iface.OnClose = video_data_on_close;
1077
0
  callback->plugin = listener_callback->plugin;
1078
0
  callback->channel_mgr = listener_callback->channel_mgr;
1079
0
  callback->channel = pChannel;
1080
0
  listener_callback->channel_callback = callback;
1081
1082
0
  *ppCallback = &callback->iface;
1083
1084
0
  return CHANNEL_RC_OK;
1085
0
}
1086
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
static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1108
0
{
1109
0
  UINT status = 0;
1110
0
  VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1111
0
  GENERIC_LISTENER_CALLBACK* callback = nullptr;
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
0
  video->control_callback = callback =
1119
0
      (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK));
1120
0
  if (!callback)
1121
0
  {
1122
0
    WLog_ERR(TAG, "calloc for control callback failed!");
1123
0
    return CHANNEL_RC_NO_MEMORY;
1124
0
  }
1125
1126
0
  callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1127
0
  callback->plugin = plugin;
1128
0
  callback->channel_mgr = channelMgr;
1129
1130
0
  status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1131
0
                                      &callback->iface, &(video->controlListener));
1132
1133
0
  if (status != CHANNEL_RC_OK)
1134
0
    return status;
1135
0
  video->controlListener->pInterface = video->wtsPlugin.pInterface;
1136
1137
0
  video->data_callback = callback =
1138
0
      (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK));
1139
0
  if (!callback)
1140
0
  {
1141
0
    WLog_ERR(TAG, "calloc for data callback failed!");
1142
0
    return CHANNEL_RC_NO_MEMORY;
1143
0
  }
1144
1145
0
  callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1146
0
  callback->plugin = plugin;
1147
0
  callback->channel_mgr = channelMgr;
1148
1149
0
  status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1150
0
                                      &callback->iface, &(video->dataListener));
1151
1152
0
  if (status == CHANNEL_RC_OK)
1153
0
    video->dataListener->pInterface = video->wtsPlugin.pInterface;
1154
1155
0
  if (status == CHANNEL_RC_OK)
1156
0
    video->context->priv->timerID =
1157
0
        freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context, true);
1158
0
  video->initialized = video->context->priv->timerID != 0;
1159
0
  if (!video->initialized)
1160
0
    status = ERROR_INTERNAL_ERROR;
1161
0
  return status;
1162
0
}
1163
1164
/**
1165
 * Function description
1166
 *
1167
 * @return 0 on success, otherwise a Win32 error code
1168
 */
1169
static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1170
0
{
1171
0
  VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1172
0
  if (!video)
1173
0
    return CHANNEL_RC_INVALID_INSTANCE;
1174
1175
0
  if (video->context && video->context->priv)
1176
0
    freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1177
1178
0
  if (video->control_callback)
1179
0
  {
1180
0
    IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1181
0
    if (mgr)
1182
0
      IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1183
0
  }
1184
0
  if (video->data_callback)
1185
0
  {
1186
0
    IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1187
0
    if (mgr)
1188
0
      IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1189
0
  }
1190
1191
0
  if (video->context)
1192
0
    VideoClientContextPriv_free(video->context->priv);
1193
1194
0
  free(video->control_callback);
1195
0
  free(video->data_callback);
1196
0
  free(video->wtsPlugin.pInterface);
1197
0
  free(pPlugin);
1198
0
  return CHANNEL_RC_OK;
1199
0
}
1200
1201
/**
1202
 * Channel Client Interface
1203
 */
1204
/**
1205
 * Function description
1206
 *
1207
 * @return 0 on success, otherwise a Win32 error code
1208
 */
1209
FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1210
0
{
1211
0
  UINT error = ERROR_INTERNAL_ERROR;
1212
0
  VIDEO_PLUGIN* videoPlugin = nullptr;
1213
0
  VideoClientContext* videoContext = nullptr;
1214
0
  VideoClientContextPriv* priv = nullptr;
1215
1216
0
  videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, "video");
1217
0
  if (!videoPlugin)
1218
0
  {
1219
0
    videoPlugin = (VIDEO_PLUGIN*)calloc(1, sizeof(VIDEO_PLUGIN));
1220
0
    if (!videoPlugin)
1221
0
    {
1222
0
      WLog_ERR(TAG, "calloc failed!");
1223
0
      return CHANNEL_RC_NO_MEMORY;
1224
0
    }
1225
1226
0
    videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1227
0
    videoPlugin->wtsPlugin.Connected = nullptr;
1228
0
    videoPlugin->wtsPlugin.Disconnected = nullptr;
1229
0
    videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1230
1231
0
    videoContext = (VideoClientContext*)calloc(1, sizeof(VideoClientContext));
1232
0
    if (!videoContext)
1233
0
    {
1234
0
      WLog_ERR(TAG, "calloc failed!");
1235
0
      free(videoPlugin);
1236
0
      return CHANNEL_RC_NO_MEMORY;
1237
0
    }
1238
1239
0
    priv = VideoClientContextPriv_new(videoContext);
1240
0
    if (!priv)
1241
0
    {
1242
0
      WLog_ERR(TAG, "VideoClientContextPriv_new failed!");
1243
0
      free(videoContext);
1244
0
      free(videoPlugin);
1245
0
      return CHANNEL_RC_NO_MEMORY;
1246
0
    }
1247
1248
0
    videoContext->handle = (void*)videoPlugin;
1249
0
    videoContext->priv = priv;
1250
0
    videoContext->timer = video_timer;
1251
0
    videoContext->setGeometry = video_client_context_set_geometry;
1252
1253
0
    videoPlugin->wtsPlugin.pInterface = (void*)videoContext;
1254
0
    videoPlugin->context = videoContext;
1255
0
    videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1256
0
    if (videoPlugin->rdpcontext)
1257
0
      error = pEntryPoints->RegisterPlugin(pEntryPoints, "video", &videoPlugin->wtsPlugin);
1258
0
  }
1259
0
  else
1260
0
  {
1261
0
    WLog_ERR(TAG, "could not get video Plugin.");
1262
0
    return CHANNEL_RC_BAD_CHANNEL;
1263
0
  }
1264
1265
0
  return error;
1266
0
}