Coverage Report

Created: 2026-07-16 07:09

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