Coverage Report

Created: 2026-02-26 06:54

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 = 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] = WINPR_C_ARRAY_INIT;
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 = 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 = 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
  VideoFrame* frame = NULL;
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
    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 = peekFrame;
707
0
    Queue_Dequeue(priv->frames);
708
0
  } while (1);
709
0
  LeaveCriticalSection(&priv->framesLock);
710
711
0
  if (frame)
712
0
  {
713
0
    presentation = frame->presentation;
714
715
0
    priv->publishedFrames++;
716
0
    memcpy(presentation->surface->data, frame->surfaceData, 1ull * frame->scanline * frame->h);
717
718
0
    WINPR_ASSERT(video->showSurface);
719
0
    if (!video->showSurface(video, presentation->surface, presentation->ScaledWidth,
720
0
                            presentation->ScaledHeight))
721
0
      WLog_WARN(TAG, "showSurface failed");
722
723
0
    VideoFrame_free(&frame);
724
0
  }
725
726
0
  if (priv->nextFeedbackTime < now)
727
0
  {
728
    /* we can compute some feedback only if we have some published frames and
729
     * a current presentation
730
     */
731
0
    if (priv->publishedFrames && priv->currentPresentation)
732
0
    {
733
0
      UINT32 computedRate = 0;
734
735
0
      PresentationContext_ref(priv->currentPresentation);
736
737
0
      if (priv->droppedFrames)
738
0
      {
739
        /**
740
         * some dropped frames, looks like we're asking too many frames per seconds,
741
         * try lowering rate. We go directly from unlimited rate to 24 frames/seconds
742
         * otherwise we lower rate by 2 frames by seconds
743
         */
744
0
        if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
745
0
          computedRate = 24;
746
0
        else
747
0
        {
748
0
          computedRate = priv->lastSentRate - 2;
749
0
          if (!computedRate)
750
0
            computedRate = 2;
751
0
        }
752
0
      }
753
0
      else
754
0
      {
755
        /**
756
         * we treat all frames ok, so either ask the server to send more,
757
         * or stay unlimited
758
         */
759
0
        if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
760
0
          computedRate = XF_VIDEO_UNLIMITED_RATE; /* stay unlimited */
761
0
        else
762
0
        {
763
0
          computedRate = priv->lastSentRate + 2;
764
0
          if (computedRate > XF_VIDEO_UNLIMITED_RATE)
765
0
            computedRate = XF_VIDEO_UNLIMITED_RATE;
766
0
        }
767
0
      }
768
769
0
      if (computedRate != priv->lastSentRate)
770
0
      {
771
0
        TSMM_CLIENT_NOTIFICATION notif;
772
773
0
        WINPR_ASSERT(priv->currentPresentation);
774
0
        notif.PresentationId = priv->currentPresentation->PresentationId;
775
0
        notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
776
0
        if (computedRate == XF_VIDEO_UNLIMITED_RATE)
777
0
        {
778
0
          notif.FramerateOverride.Flags = 0x01;
779
0
          notif.FramerateOverride.DesiredFrameRate = 0x00;
780
0
        }
781
0
        else
782
0
        {
783
0
          notif.FramerateOverride.Flags = 0x02;
784
0
          notif.FramerateOverride.DesiredFrameRate = computedRate;
785
0
        }
786
787
0
        video_control_send_client_notification(video, &notif);
788
0
        priv->lastSentRate = computedRate;
789
790
0
        WLog_VRB(TAG,
791
0
                 "server notified with rate %" PRIu32 " published=%" PRIu32
792
0
                 " dropped=%" PRIu32,
793
0
                 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
794
0
      }
795
796
0
      PresentationContext_unref(&priv->currentPresentation);
797
0
    }
798
799
0
    priv->droppedFrames = 0;
800
0
    priv->publishedFrames = 0;
801
0
    priv->nextFeedbackTime = now + 1000;
802
0
  }
803
0
}
804
805
static UINT video_VideoData(VideoClientContext* context, const TSMM_VIDEO_DATA* data)
806
0
{
807
0
  VideoClientContextPriv* priv = NULL;
808
0
  PresentationContext* presentation = NULL;
809
0
  int status = 0;
810
811
0
  WINPR_ASSERT(context);
812
0
  WINPR_ASSERT(data);
813
814
0
  priv = context->priv;
815
0
  WINPR_ASSERT(priv);
816
817
0
  presentation = priv->currentPresentation;
818
0
  if (!presentation)
819
0
  {
820
0
    WLog_ERR(TAG, "no current presentation");
821
0
    return CHANNEL_RC_OK;
822
0
  }
823
824
0
  if (presentation->PresentationId != data->PresentationId)
825
0
  {
826
0
    WLog_ERR(TAG, "current presentation id=%" PRIu8 " doesn't match data id=%" PRIu8,
827
0
             presentation->PresentationId, data->PresentationId);
828
0
    return CHANNEL_RC_OK;
829
0
  }
830
831
0
  if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
832
0
  {
833
0
    WLog_ERR(TAG, "unable to expand the current packet");
834
0
    return CHANNEL_RC_NO_MEMORY;
835
0
  }
836
837
0
  Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
838
839
0
  if (data->CurrentPacketIndex == data->PacketsInSample)
840
0
  {
841
0
    VideoSurface* surface = presentation->surface;
842
0
    H264_CONTEXT* h264 = presentation->h264;
843
0
    const UINT64 startTime = winpr_GetTickCount64NS();
844
0
    MAPPED_GEOMETRY* geom = presentation->geometry;
845
846
0
    const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
847
0
                              WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
848
0
    Stream_SealLength(presentation->currentSample);
849
0
    Stream_SetPosition(presentation->currentSample, 0);
850
851
0
    const UINT64 timeAfterH264 = winpr_GetTickCount64NS();
852
0
    if (data->SampleNumber == 1)
853
0
    {
854
0
      presentation->lastPublishTime = startTime;
855
0
    }
856
857
0
    presentation->lastPublishTime += 100ull * data->hnsDuration;
858
0
    if (presentation->lastPublishTime <= (10000000ull + timeAfterH264))
859
0
    {
860
0
      int dropped = 0;
861
862
0
      const size_t len = Stream_Length(presentation->currentSample);
863
0
      if (len > UINT32_MAX)
864
0
        return CHANNEL_RC_OK;
865
866
      /* if the frame is to be published in less than 10 ms, let's consider it's now */
867
0
      status =
868
0
          avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
869
0
                            surface->data, surface->format, surface->scanline,
870
0
                            surface->alignedWidth, surface->alignedHeight, &rect, 1);
871
872
0
      if (status < 0)
873
0
        return CHANNEL_RC_OK;
874
875
0
      WINPR_ASSERT(context->showSurface);
876
0
      if (!context->showSurface(context, presentation->surface, presentation->ScaledWidth,
877
0
                                presentation->ScaledHeight))
878
0
        return CHANNEL_RC_NOT_INITIALIZED;
879
880
0
      priv->publishedFrames++;
881
882
      /* cleanup previously scheduled frames */
883
0
      EnterCriticalSection(&priv->framesLock);
884
0
      while (Queue_Count(priv->frames) > 0)
885
0
      {
886
0
        VideoFrame* frame = Queue_Dequeue(priv->frames);
887
0
        if (frame)
888
0
        {
889
0
          priv->droppedFrames++;
890
0
          VideoFrame_free(&frame);
891
0
          dropped++;
892
0
        }
893
0
      }
894
0
      LeaveCriticalSection(&priv->framesLock);
895
896
0
      if (dropped)
897
0
        WLog_DBG(TAG, "showing frame (%d dropped)", dropped);
898
0
    }
899
0
    else
900
0
    {
901
0
      const size_t len = Stream_Length(presentation->currentSample);
902
0
      if (len > UINT32_MAX)
903
0
        return CHANNEL_RC_OK;
904
905
0
      BOOL enqueueResult = 0;
906
0
      VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
907
0
      if (!frame)
908
0
      {
909
0
        WLog_ERR(TAG, "unable to create frame");
910
0
        return CHANNEL_RC_NO_MEMORY;
911
0
      }
912
913
0
      status =
914
0
          avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
915
0
                            frame->surfaceData, surface->format, surface->scanline,
916
0
                            surface->alignedWidth, surface->alignedHeight, &rect, 1);
917
0
      if (status < 0)
918
0
      {
919
0
        VideoFrame_free(&frame);
920
0
        return CHANNEL_RC_OK;
921
0
      }
922
923
0
      EnterCriticalSection(&priv->framesLock);
924
0
      enqueueResult = Queue_Enqueue(priv->frames, frame);
925
0
      LeaveCriticalSection(&priv->framesLock);
926
927
0
      if (!enqueueResult)
928
0
      {
929
0
        WLog_ERR(TAG, "unable to enqueue frame");
930
0
        VideoFrame_free(&frame);
931
0
        return CHANNEL_RC_NO_MEMORY;
932
0
      }
933
934
      // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue owns frame
935
0
      WLog_DBG(TAG, "scheduling frame in %" PRIu64 " ms", (frame->publishTime - startTime));
936
0
    }
937
0
  }
938
939
0
  return CHANNEL_RC_OK;
940
0
}
941
942
static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s)
943
0
{
944
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
945
0
  VIDEO_PLUGIN* video = NULL;
946
0
  VideoClientContext* context = NULL;
947
0
  UINT32 cbSize = 0;
948
0
  UINT32 packetType = 0;
949
0
  TSMM_VIDEO_DATA data;
950
951
0
  video = (VIDEO_PLUGIN*)callback->plugin;
952
0
  context = (VideoClientContext*)video->wtsPlugin.pInterface;
953
954
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
955
0
    return ERROR_INVALID_DATA;
956
957
0
  Stream_Read_UINT32(s, cbSize);
958
0
  if (cbSize < 8)
959
0
  {
960
0
    WLog_ERR(TAG, "invalid cbSize %" PRIu32 ", expected >= 8", cbSize);
961
0
    return ERROR_INVALID_DATA;
962
0
  }
963
964
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
965
0
    return ERROR_INVALID_DATA;
966
967
0
  Stream_Read_UINT32(s, packetType);
968
0
  if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
969
0
  {
970
0
    WLog_ERR(TAG, "only expecting VIDEO_DATA on the data channel");
971
0
    return ERROR_INVALID_DATA;
972
0
  }
973
974
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
975
0
    return ERROR_INVALID_DATA;
976
977
0
  Stream_Read_UINT8(s, data.PresentationId);
978
0
  Stream_Read_UINT8(s, data.Version);
979
0
  Stream_Read_UINT8(s, data.Flags);
980
0
  Stream_Seek_UINT8(s); /* reserved */
981
0
  Stream_Read_UINT64(s, data.hnsTimestamp);
982
0
  Stream_Read_UINT64(s, data.hnsDuration);
983
0
  Stream_Read_UINT16(s, data.CurrentPacketIndex);
984
0
  Stream_Read_UINT16(s, data.PacketsInSample);
985
0
  Stream_Read_UINT32(s, data.SampleNumber);
986
0
  Stream_Read_UINT32(s, data.cbSample);
987
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
988
0
    return ERROR_INVALID_DATA;
989
0
  data.pSample = Stream_Pointer(s);
990
991
  /*
992
      WLog_DBG(TAG, "videoData: id:%"PRIu8" version:%"PRIu8" flags:0x%"PRIx8" timestamp=%"PRIu64"
993
     duration=%"PRIu64 " curPacketIndex:%"PRIu16" packetInSample:%"PRIu16" sampleNumber:%"PRIu32"
994
     cbSample:%"PRIu32"", data.PresentationId, data.Version, data.Flags, data.hnsTimestamp,
995
     data.hnsDuration, data.CurrentPacketIndex, data.PacketsInSample, data.SampleNumber,
996
     data.cbSample);
997
  */
998
999
0
  return video_VideoData(context, &data);
1000
0
}
1001
1002
/**
1003
 * Function description
1004
 *
1005
 * @return 0 on success, otherwise a Win32 error code
1006
 */
1007
static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1008
0
{
1009
0
  free(pChannelCallback);
1010
0
  return CHANNEL_RC_OK;
1011
0
}
1012
1013
static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
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
static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1026
                                                    IWTSVirtualChannel* channel, BYTE* Data,
1027
                                                    BOOL* pbAccept,
1028
                                                    IWTSVirtualChannelCallback** ppCallback)
1029
// NOLINTEND(readability-non-const-parameter)
1030
0
{
1031
0
  GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)listenerCallback;
1032
1033
0
  WINPR_UNUSED(Data);
1034
0
  WINPR_UNUSED(pbAccept);
1035
1036
0
  GENERIC_CHANNEL_CALLBACK* callback =
1037
0
      (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK));
1038
0
  if (!callback)
1039
0
  {
1040
0
    WLog_ERR(TAG, "calloc failed!");
1041
0
    return CHANNEL_RC_NO_MEMORY;
1042
0
  }
1043
1044
0
  callback->iface.OnDataReceived = video_control_on_data_received;
1045
0
  callback->iface.OnClose = video_control_on_close;
1046
0
  callback->plugin = listener_callback->plugin;
1047
0
  callback->channel_mgr = listener_callback->channel_mgr;
1048
0
  callback->channel = channel;
1049
0
  listener_callback->channel_callback = callback;
1050
1051
0
  *ppCallback = &callback->iface;
1052
1053
0
  return CHANNEL_RC_OK;
1054
0
}
1055
1056
// NOLINTBEGIN(readability-non-const-parameter)
1057
static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1058
                                                 IWTSVirtualChannel* pChannel, BYTE* Data,
1059
                                                 BOOL* pbAccept,
1060
                                                 IWTSVirtualChannelCallback** ppCallback)
1061
// NOLINTEND(readability-non-const-parameter)
1062
0
{
1063
0
  GENERIC_CHANNEL_CALLBACK* callback = NULL;
1064
0
  GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
1065
1066
0
  WINPR_UNUSED(Data);
1067
0
  WINPR_UNUSED(pbAccept);
1068
1069
0
  callback = (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK));
1070
0
  if (!callback)
1071
0
  {
1072
0
    WLog_ERR(TAG, "calloc failed!");
1073
0
    return CHANNEL_RC_NO_MEMORY;
1074
0
  }
1075
1076
0
  callback->iface.OnDataReceived = video_data_on_data_received;
1077
0
  callback->iface.OnClose = video_data_on_close;
1078
0
  callback->plugin = listener_callback->plugin;
1079
0
  callback->channel_mgr = listener_callback->channel_mgr;
1080
0
  callback->channel = pChannel;
1081
0
  listener_callback->channel_callback = callback;
1082
1083
0
  *ppCallback = &callback->iface;
1084
1085
0
  return CHANNEL_RC_OK;
1086
0
}
1087
1088
static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context, void* userdata,
1089
                         WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1090
                         uint64_t interval)
1091
0
{
1092
0
  VideoClientContext* video = userdata;
1093
0
  if (!video)
1094
0
    return 0;
1095
0
  if (!video->timer)
1096
0
    return 0;
1097
1098
0
  video->timer(video, timestamp);
1099
1100
0
  return interval;
1101
0
}
1102
1103
/**
1104
 * Function description
1105
 *
1106
 * @return 0 on success, otherwise a Win32 error code
1107
 */
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
0
  GENERIC_LISTENER_CALLBACK* callback = NULL;
1113
1114
0
  if (video->initialized)
1115
0
  {
1116
0
    WLog_ERR(TAG, "[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1117
0
    return ERROR_INVALID_DATA;
1118
0
  }
1119
0
  video->control_callback = callback =
1120
0
      (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK));
1121
0
  if (!callback)
1122
0
  {
1123
0
    WLog_ERR(TAG, "calloc for control callback failed!");
1124
0
    return CHANNEL_RC_NO_MEMORY;
1125
0
  }
1126
1127
0
  callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1128
0
  callback->plugin = plugin;
1129
0
  callback->channel_mgr = channelMgr;
1130
1131
0
  status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1132
0
                                      &callback->iface, &(video->controlListener));
1133
1134
0
  if (status != CHANNEL_RC_OK)
1135
0
    return status;
1136
0
  video->controlListener->pInterface = video->wtsPlugin.pInterface;
1137
1138
0
  video->data_callback = callback =
1139
0
      (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK));
1140
0
  if (!callback)
1141
0
  {
1142
0
    WLog_ERR(TAG, "calloc for data callback failed!");
1143
0
    return CHANNEL_RC_NO_MEMORY;
1144
0
  }
1145
1146
0
  callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1147
0
  callback->plugin = plugin;
1148
0
  callback->channel_mgr = channelMgr;
1149
1150
0
  status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1151
0
                                      &callback->iface, &(video->dataListener));
1152
1153
0
  if (status == CHANNEL_RC_OK)
1154
0
    video->dataListener->pInterface = video->wtsPlugin.pInterface;
1155
1156
0
  if (status == CHANNEL_RC_OK)
1157
0
    video->context->priv->timerID =
1158
0
        freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context, true);
1159
0
  video->initialized = video->context->priv->timerID != 0;
1160
0
  if (!video->initialized)
1161
0
    status = ERROR_INTERNAL_ERROR;
1162
0
  return status;
1163
0
}
1164
1165
/**
1166
 * Function description
1167
 *
1168
 * @return 0 on success, otherwise a Win32 error code
1169
 */
1170
static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1171
0
{
1172
0
  VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1173
0
  if (!video)
1174
0
    return CHANNEL_RC_INVALID_INSTANCE;
1175
1176
0
  if (video->context && video->context->priv)
1177
0
    freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1178
1179
0
  if (video->control_callback)
1180
0
  {
1181
0
    IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1182
0
    if (mgr)
1183
0
      IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1184
0
  }
1185
0
  if (video->data_callback)
1186
0
  {
1187
0
    IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1188
0
    if (mgr)
1189
0
      IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1190
0
  }
1191
1192
0
  if (video->context)
1193
0
    VideoClientContextPriv_free(video->context->priv);
1194
1195
0
  free(video->control_callback);
1196
0
  free(video->data_callback);
1197
0
  free(video->wtsPlugin.pInterface);
1198
0
  free(pPlugin);
1199
0
  return CHANNEL_RC_OK;
1200
0
}
1201
1202
/**
1203
 * Channel Client Interface
1204
 */
1205
/**
1206
 * Function description
1207
 *
1208
 * @return 0 on success, otherwise a Win32 error code
1209
 */
1210
FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1211
0
{
1212
0
  UINT error = ERROR_INTERNAL_ERROR;
1213
0
  VIDEO_PLUGIN* videoPlugin = NULL;
1214
0
  VideoClientContext* videoContext = NULL;
1215
0
  VideoClientContextPriv* priv = NULL;
1216
1217
0
  videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, "video");
1218
0
  if (!videoPlugin)
1219
0
  {
1220
0
    videoPlugin = (VIDEO_PLUGIN*)calloc(1, sizeof(VIDEO_PLUGIN));
1221
0
    if (!videoPlugin)
1222
0
    {
1223
0
      WLog_ERR(TAG, "calloc failed!");
1224
0
      return CHANNEL_RC_NO_MEMORY;
1225
0
    }
1226
1227
0
    videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1228
0
    videoPlugin->wtsPlugin.Connected = NULL;
1229
0
    videoPlugin->wtsPlugin.Disconnected = NULL;
1230
0
    videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1231
1232
0
    videoContext = (VideoClientContext*)calloc(1, sizeof(VideoClientContext));
1233
0
    if (!videoContext)
1234
0
    {
1235
0
      WLog_ERR(TAG, "calloc failed!");
1236
0
      free(videoPlugin);
1237
0
      return CHANNEL_RC_NO_MEMORY;
1238
0
    }
1239
1240
0
    priv = VideoClientContextPriv_new(videoContext);
1241
0
    if (!priv)
1242
0
    {
1243
0
      WLog_ERR(TAG, "VideoClientContextPriv_new failed!");
1244
0
      free(videoContext);
1245
0
      free(videoPlugin);
1246
0
      return CHANNEL_RC_NO_MEMORY;
1247
0
    }
1248
1249
0
    videoContext->handle = (void*)videoPlugin;
1250
0
    videoContext->priv = priv;
1251
0
    videoContext->timer = video_timer;
1252
0
    videoContext->setGeometry = video_client_context_set_geometry;
1253
1254
0
    videoPlugin->wtsPlugin.pInterface = (void*)videoContext;
1255
0
    videoPlugin->context = videoContext;
1256
0
    videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1257
0
    if (videoPlugin->rdpcontext)
1258
0
      error = pEntryPoints->RegisterPlugin(pEntryPoints, "video", &videoPlugin->wtsPlugin);
1259
0
  }
1260
0
  else
1261
0
  {
1262
0
    WLog_ERR(TAG, "could not get video Plugin.");
1263
0
    return CHANNEL_RC_BAD_CHANNEL;
1264
0
  }
1265
1266
0
  return error;
1267
0
}