Coverage Report

Created: 2026-04-12 07:03

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