1 /* GStreamer
2 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/base/base.h>
25 #include <gst/video/video.h>
26 #include "gstmfcapturewinrt.h"
27 #include "gstmfutils.h"
28 #include "mediacapturewrapper.h"
29 #include <memorybuffer.h>
30 #include <memory>
31 #include <algorithm>
32
33 /* *INDENT-OFF* */
34 using namespace Microsoft::WRL;
35 using namespace Microsoft::WRL::Wrappers;
36 using namespace ABI::Windows::Media::MediaProperties;
37 using namespace ABI::Windows::Graphics::Imaging;
38 using namespace ABI::Windows::Foundation;
39
40 G_BEGIN_DECLS
41
42 GST_DEBUG_CATEGORY_EXTERN (gst_mf_source_object_debug);
43 #define GST_CAT_DEFAULT gst_mf_source_object_debug
44
45 G_END_DECLS
46 /* *INDENT-ON* */
47
48 enum
49 {
50 PROP_0,
51 PROP_DISPATCHER,
52 };
53
54 struct _GstMFCaptureWinRT
55 {
56 GstMFSourceObject parent;
57
58 MediaCaptureWrapper *capture;
59
60 GThread *thread;
61 GMutex lock;
62 GCond cond;
63 GMainContext *context;
64 GMainLoop *loop;
65
66 /* protected by lock */
67 GstQueueArray *queue;
68
69 GstCaps *supported_caps;
70 GstVideoInfo info;
71 gboolean flushing;
72 gboolean got_error;
73
74 gpointer dispatcher;
75 };
76
77 typedef struct _GstMFCaptureWinRTFrame
78 {
79 IMediaFrameReference *frame;
80 GstClockTime clock_time;
81 } GstMFCaptureWinRTFrame;
82
83 static void gst_mf_capture_winrt_constructed (GObject * object);
84 static void gst_mf_capture_winrt_finalize (GObject * object);
85 static void gst_mf_capture_winrt_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
87 static void gst_mf_capture_winrt_set_property (GObject * object, guint prop_id,
88 const GValue * value, GParamSpec * pspec);
89
90 static gboolean gst_mf_capture_winrt_start (GstMFSourceObject * object);
91 static gboolean gst_mf_capture_winrt_stop (GstMFSourceObject * object);
92 static GstFlowReturn gst_mf_capture_winrt_fill (GstMFSourceObject * object,
93 GstBuffer * buffer);
94 static gboolean gst_mf_capture_winrt_unlock (GstMFSourceObject * object);
95 static gboolean gst_mf_capture_winrt_unlock_stop (GstMFSourceObject * object);
96 static GstCaps *gst_mf_capture_winrt_get_caps (GstMFSourceObject * object);
97 static gboolean gst_mf_capture_winrt_set_caps (GstMFSourceObject * object,
98 GstCaps * caps);
99 static HRESULT gst_mf_capture_winrt_on_frame (IMediaFrameReference * frame,
100 void *user_data);
101 static HRESULT gst_mf_capture_winrt_on_failed (const std::string & error,
102 UINT32 error_code, void *user_data);
103
104 static gpointer gst_mf_capture_winrt_thread_func (GstMFCaptureWinRT * self);
105 static void
106 gst_mf_capture_winrt_frame_clear (GstMFCaptureWinRTFrame * winrt_frame);
107
108 #define gst_mf_capture_winrt_parent_class parent_class
109 G_DEFINE_TYPE (GstMFCaptureWinRT, gst_mf_capture_winrt,
110 GST_TYPE_MF_SOURCE_OBJECT);
111
112 static void
gst_mf_capture_winrt_class_init(GstMFCaptureWinRTClass * klass)113 gst_mf_capture_winrt_class_init (GstMFCaptureWinRTClass * klass)
114 {
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 GstMFSourceObjectClass *source_class = GST_MF_SOURCE_OBJECT_CLASS (klass);
117
118 gobject_class->constructed = gst_mf_capture_winrt_constructed;
119 gobject_class->finalize = gst_mf_capture_winrt_finalize;
120 gobject_class->get_property = gst_mf_capture_winrt_get_property;
121 gobject_class->set_property = gst_mf_capture_winrt_set_property;
122
123 g_object_class_install_property (gobject_class, PROP_DISPATCHER,
124 g_param_spec_pointer ("dispatcher", "Dispatcher",
125 "ICoreDispatcher COM object to use",
126 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
127 G_PARAM_STATIC_STRINGS)));
128
129 source_class->start = GST_DEBUG_FUNCPTR (gst_mf_capture_winrt_start);
130 source_class->stop = GST_DEBUG_FUNCPTR (gst_mf_capture_winrt_stop);
131 source_class->fill = GST_DEBUG_FUNCPTR (gst_mf_capture_winrt_fill);
132 source_class->unlock = GST_DEBUG_FUNCPTR (gst_mf_capture_winrt_unlock);
133 source_class->unlock_stop =
134 GST_DEBUG_FUNCPTR (gst_mf_capture_winrt_unlock_stop);
135 source_class->get_caps = GST_DEBUG_FUNCPTR (gst_mf_capture_winrt_get_caps);
136 source_class->set_caps = GST_DEBUG_FUNCPTR (gst_mf_capture_winrt_set_caps);
137 }
138
139 static void
gst_mf_capture_winrt_init(GstMFCaptureWinRT * self)140 gst_mf_capture_winrt_init (GstMFCaptureWinRT * self)
141 {
142 self->queue =
143 gst_queue_array_new_for_struct (sizeof (GstMFCaptureWinRTFrame), 2);
144 gst_queue_array_set_clear_func (self->queue,
145 (GDestroyNotify) gst_mf_capture_winrt_frame_clear);
146 g_mutex_init (&self->lock);
147 g_cond_init (&self->cond);
148 }
149
150 static void
gst_mf_capture_winrt_constructed(GObject * object)151 gst_mf_capture_winrt_constructed (GObject * object)
152 {
153 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
154
155 self->context = g_main_context_new ();
156 self->loop = g_main_loop_new (self->context, FALSE);
157
158 /* Create a new thread to ensure that COM thread can be MTA thread */
159 g_mutex_lock (&self->lock);
160 self->thread = g_thread_new ("GstMFCaptureWinRT",
161 (GThreadFunc) gst_mf_capture_winrt_thread_func, self);
162 while (!g_main_loop_is_running (self->loop))
163 g_cond_wait (&self->cond, &self->lock);
164 g_mutex_unlock (&self->lock);
165
166 G_OBJECT_CLASS (parent_class)->constructed (object);
167 }
168
169 static void
gst_mf_capture_winrt_finalize(GObject * object)170 gst_mf_capture_winrt_finalize (GObject * object)
171 {
172 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
173
174 g_main_loop_quit (self->loop);
175 g_thread_join (self->thread);
176 g_main_loop_unref (self->loop);
177 g_main_context_unref (self->context);
178
179 gst_queue_array_free (self->queue);
180 gst_clear_caps (&self->supported_caps);
181 g_mutex_clear (&self->lock);
182 g_cond_clear (&self->cond);
183
184 G_OBJECT_CLASS (parent_class)->finalize (object);
185 }
186
187 static void
gst_mf_capture_winrt_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)188 gst_mf_capture_winrt_get_property (GObject * object, guint prop_id,
189 GValue * value, GParamSpec * pspec)
190 {
191 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
192
193 switch (prop_id) {
194 case PROP_DISPATCHER:
195 g_value_set_pointer (value, self->dispatcher);
196 break;
197 default:
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 break;
200 }
201 }
202
203 static void
gst_mf_capture_winrt_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)204 gst_mf_capture_winrt_set_property (GObject * object, guint prop_id,
205 const GValue * value, GParamSpec * pspec)
206 {
207 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
208
209 switch (prop_id) {
210 case PROP_DISPATCHER:
211 self->dispatcher = g_value_get_pointer (value);
212 break;
213 default:
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 break;
216 }
217 }
218
219 static gboolean
gst_mf_capture_winrt_main_loop_running_cb(GstMFCaptureWinRT * self)220 gst_mf_capture_winrt_main_loop_running_cb (GstMFCaptureWinRT * self)
221 {
222 GST_DEBUG_OBJECT (self, "Main loop running now");
223
224 g_mutex_lock (&self->lock);
225 g_cond_signal (&self->cond);
226 g_mutex_unlock (&self->lock);
227
228 return G_SOURCE_REMOVE;
229 }
230
231 static gpointer
gst_mf_capture_winrt_thread_func(GstMFCaptureWinRT * self)232 gst_mf_capture_winrt_thread_func (GstMFCaptureWinRT * self)
233 {
234 GstMFSourceObject *source = GST_MF_SOURCE_OBJECT (self);
235 HRESULT hr;
236 guint index;
237 GSource *idle_source;
238 std::shared_ptr < GstWinRTMediaFrameSourceGroup > target_group;
239 std::vector < GstWinRTMediaFrameSourceGroup > group_list;
240 MediaCaptureWrapperCallbacks callbacks;
241
242 RoInitializeWrapper init_wrapper (RO_INIT_MULTITHREADED);
243
244 self->capture = new MediaCaptureWrapper (self->dispatcher);
245 callbacks.frame_arrived = gst_mf_capture_winrt_on_frame;
246 callbacks.failed = gst_mf_capture_winrt_on_failed;
247 self->capture->RegisterCb (callbacks, self);
248
249 g_main_context_push_thread_default (self->context);
250
251 idle_source = g_idle_source_new ();
252 g_source_set_callback (idle_source,
253 (GSourceFunc) gst_mf_capture_winrt_main_loop_running_cb, self, NULL);
254 g_source_attach (idle_source, self->context);
255 g_source_unref (idle_source);
256
257 hr = self->capture->EnumrateFrameSourceGroup (group_list);
258
259 /* *INDENT-OFF* */
260 #ifndef GST_DISABLE_GST_DEBUG
261 index = 0;
262 for (const auto& iter: group_list) {
263 GST_DEBUG_OBJECT (self, "device %d, name: \"%s\", path: \"%s\"",
264 index, iter.display_name_.c_str(), iter.id_.c_str());
265 index++;
266 }
267 #endif
268
269 GST_DEBUG_OBJECT (self,
270 "Requested device index: %d, name: \"%s\", path \"%s\"",
271 source->device_index, GST_STR_NULL (source->device_name),
272 GST_STR_NULL (source->device_path));
273
274 index = 0;
275 for (const auto& iter: group_list) {
276 gboolean match;
277
278 if (source->device_path) {
279 match = g_ascii_strcasecmp (iter.id_.c_str(), source->device_path) == 0;
280 } else if (source->device_name) {
281 match = g_ascii_strcasecmp (iter.display_name_.c_str(),
282 source->device_name) == 0;
283 } else if (source->device_index >= 0) {
284 match = index == source->device_index;
285 } else {
286 /* pick the first entry */
287 match = TRUE;
288 }
289
290 if (match) {
291 target_group = std::make_shared<GstWinRTMediaFrameSourceGroup>(iter);
292 break;
293 }
294
295 index++;
296 }
297 /* *INDENT-ON* */
298
299 if (!target_group) {
300 GST_WARNING_OBJECT (self, "No matching device");
301 goto run_loop;
302 }
303
304 if (target_group->source_list_.empty ()) {
305 GST_WARNING_OBJECT (self, "No available source list");
306 goto run_loop;
307 }
308
309 self->capture->SetSourceGroup (*target_group);
310
311 std::sort (target_group->source_list_.begin (),
312 target_group->source_list_.end (), WinRTCapsCompareFunc);
313
314 self->supported_caps = gst_caps_new_empty ();
315
316 /* *INDENT-OFF* */
317 for (auto iter: target_group->source_list_)
318 gst_caps_append (self->supported_caps, gst_caps_copy (iter.caps_));
319 /* *INDENT-ON* */
320
321 GST_DEBUG_OBJECT (self, "Available output caps %" GST_PTR_FORMAT,
322 self->supported_caps);
323
324 source->opened = TRUE;
325
326 g_free (source->device_path);
327 source->device_path = g_strdup (target_group->id_.c_str ());
328
329 g_free (source->device_name);
330 source->device_name = g_strdup (target_group->display_name_.c_str ());
331
332 source->device_index = index;
333
334 run_loop:
335 GST_DEBUG_OBJECT (self, "Starting main loop");
336 g_main_loop_run (self->loop);
337 GST_DEBUG_OBJECT (self, "Stopped main loop");
338
339 g_main_context_pop_thread_default (self->context);
340
341 gst_mf_capture_winrt_stop (source);
342
343 delete self->capture;
344 self->capture = NULL;
345
346 return NULL;
347 }
348
349 static gboolean
gst_mf_capture_winrt_start(GstMFSourceObject * object)350 gst_mf_capture_winrt_start (GstMFSourceObject * object)
351 {
352 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
353 HRESULT hr;
354
355 if (!self->capture) {
356 GST_ERROR_OBJECT (self, "No capture object was configured");
357 return FALSE;
358 }
359
360 hr = self->capture->StartCapture ();
361 if (!gst_mf_result (hr)) {
362 GST_ERROR_OBJECT (self, "Capture object doesn't want to start capture");
363 return FALSE;
364 }
365
366 return TRUE;
367 }
368
369 static gboolean
gst_mf_capture_winrt_stop(GstMFSourceObject * object)370 gst_mf_capture_winrt_stop (GstMFSourceObject * object)
371 {
372 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
373 HRESULT hr;
374
375 if (!self->capture) {
376 GST_ERROR_OBJECT (self, "No capture object was configured");
377 return FALSE;
378 }
379
380 hr = self->capture->StopCapture ();
381
382 gst_queue_array_clear (self->queue);
383
384 if (!gst_mf_result (hr)) {
385 GST_ERROR_OBJECT (self, "Capture object doesn't want to stop capture");
386 return FALSE;
387 }
388
389 return TRUE;
390 }
391
392 static HRESULT
gst_mf_capture_winrt_on_frame(IMediaFrameReference * frame,void * user_data)393 gst_mf_capture_winrt_on_frame (IMediaFrameReference * frame, void *user_data)
394 {
395 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (user_data);
396 GstMFCaptureWinRTFrame winrt_frame;
397
398 g_mutex_lock (&self->lock);
399 if (self->flushing) {
400 g_mutex_unlock (&self->lock);
401 return S_OK;
402 }
403
404 winrt_frame.frame = frame;
405 winrt_frame.clock_time =
406 gst_mf_source_object_get_running_time (GST_MF_SOURCE_OBJECT (self));
407 gst_queue_array_push_tail_struct (self->queue, &winrt_frame);
408 frame->AddRef ();
409
410 g_cond_broadcast (&self->cond);
411 g_mutex_unlock (&self->lock);
412
413 return S_OK;
414 }
415
416 static HRESULT
gst_mf_capture_winrt_on_failed(const std::string & error,UINT32 error_code,void * user_data)417 gst_mf_capture_winrt_on_failed (const std::string & error,
418 UINT32 error_code, void *user_data)
419 {
420 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (user_data);
421
422 GST_DEBUG_OBJECT (self, "Have error %s (%d)", error.c_str (), error_code);
423
424 g_mutex_lock (&self->lock);
425 self->got_error = TRUE;
426 g_cond_broadcast (&self->cond);
427 g_mutex_unlock (&self->lock);
428
429 return S_OK;
430 }
431
432 static GstFlowReturn
gst_mf_capture_winrt_get_video_media_frame(GstMFCaptureWinRT * self,IVideoMediaFrame ** media_frame,GstClockTime * timestamp,GstClockTime * duration)433 gst_mf_capture_winrt_get_video_media_frame (GstMFCaptureWinRT * self,
434 IVideoMediaFrame ** media_frame, GstClockTime * timestamp,
435 GstClockTime * duration)
436 {
437 GstMFCaptureWinRTFrame *winrt_frame = nullptr;
438 IMediaFrameReference *frame_ref;
439 HRESULT hr;
440 ComPtr < IReference < TimeSpan >> winrt_timestamp;
441 TimeSpan winrt_duration;
442
443 *media_frame = nullptr;
444 *timestamp = GST_CLOCK_TIME_NONE;
445 *duration = GST_CLOCK_TIME_NONE;
446
447 g_mutex_lock (&self->lock);
448 if (self->got_error) {
449 g_mutex_unlock (&self->lock);
450 return GST_FLOW_ERROR;
451 }
452
453 if (self->flushing) {
454 g_mutex_unlock (&self->lock);
455 return GST_FLOW_FLUSHING;
456 }
457
458 while (!self->flushing && !self->got_error &&
459 gst_queue_array_is_empty (self->queue))
460 g_cond_wait (&self->cond, &self->lock);
461
462 if (self->got_error) {
463 g_mutex_unlock (&self->lock);
464 return GST_FLOW_ERROR;
465 }
466
467 if (self->flushing) {
468 g_mutex_unlock (&self->lock);
469 return GST_FLOW_FLUSHING;
470 }
471
472 winrt_frame =
473 (GstMFCaptureWinRTFrame *) gst_queue_array_pop_head_struct (self->queue);
474
475 frame_ref = winrt_frame->frame;
476 g_assert (frame_ref);
477
478 hr = frame_ref->get_VideoMediaFrame (media_frame);
479 if (!gst_mf_result (hr)) {
480 GST_WARNING_OBJECT (self, "Couldn't get IVideoMediaFrame");
481 *media_frame = nullptr;
482 goto done;
483 }
484
485 hr = frame_ref->get_Duration (&winrt_duration);
486 if (gst_mf_result (hr))
487 *duration = winrt_duration.Duration * 100;
488
489 *timestamp = winrt_frame->clock_time;
490
491 done:
492 gst_mf_capture_winrt_frame_clear (winrt_frame);
493 g_mutex_unlock (&self->lock);
494
495 return GST_FLOW_OK;
496 }
497
498 static GstFlowReturn
gst_mf_capture_winrt_fill(GstMFSourceObject * object,GstBuffer * buffer)499 gst_mf_capture_winrt_fill (GstMFSourceObject * object, GstBuffer * buffer)
500 {
501 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
502 GstFlowReturn ret = GST_FLOW_OK;
503 HRESULT hr;
504 GstVideoFrame frame;
505 BYTE *data;
506 UINT32 size;
507 gint i, j;
508 ComPtr < IVideoMediaFrame > video_frame;
509 ComPtr < ISoftwareBitmap > bitmap;
510 ComPtr < IBitmapBuffer > bitmap_buffer;
511 ComPtr < IMemoryBuffer > mem_buf;
512 ComPtr < IMemoryBufferReference > mem_ref;
513 ComPtr < Windows::Foundation::IMemoryBufferByteAccess > byte_access;
514 INT32 plane_count;
515 BitmapPlaneDescription desc[GST_VIDEO_MAX_PLANES];
516 GstClockTime timestamp = GST_CLOCK_TIME_NONE;
517 GstClockTime duration = GST_CLOCK_TIME_NONE;
518
519 do {
520 ret = gst_mf_capture_winrt_get_video_media_frame (self,
521 video_frame.ReleaseAndGetAddressOf (), ×tamp, &duration);
522 } while (ret == GST_FLOW_OK && !video_frame);
523
524 if (ret != GST_FLOW_OK)
525 return ret;
526
527 hr = video_frame->get_SoftwareBitmap (&bitmap);
528 if (!gst_mf_result (hr)) {
529 GST_ERROR_OBJECT (self, "Couldn't get ISoftwareBitmap");
530 return GST_FLOW_ERROR;
531 }
532
533 hr = bitmap->LockBuffer (BitmapBufferAccessMode::BitmapBufferAccessMode_Read,
534 &bitmap_buffer);
535 if (!gst_mf_result (hr)) {
536 GST_ERROR_OBJECT (self, "Cannot lock ISoftwareBitmap");
537 return GST_FLOW_ERROR;
538 }
539
540 hr = bitmap_buffer->GetPlaneCount (&plane_count);
541 if (!gst_mf_result (hr)) {
542 GST_ERROR_OBJECT (self, "Cannot get plane count");
543 return GST_FLOW_ERROR;
544 }
545
546 if (plane_count > GST_VIDEO_MAX_PLANES) {
547 GST_ERROR_OBJECT (self, "Invalid plane count %d", plane_count);
548 return GST_FLOW_ERROR;
549 }
550
551 if (plane_count != GST_VIDEO_INFO_N_PLANES (&self->info)) {
552 GST_ERROR_OBJECT (self, "Ambiguous plane count %d", plane_count);
553 return GST_FLOW_ERROR;
554 }
555
556 for (i = 0; i < plane_count; i++) {
557 hr = bitmap_buffer->GetPlaneDescription (i, &desc[i]);
558 if (!gst_mf_result (hr)) {
559 GST_ERROR_OBJECT (self, "Cannot get description for plane %d", i);
560 return GST_FLOW_ERROR;
561 }
562 }
563
564 hr = bitmap_buffer.As (&mem_buf);
565 if (!gst_mf_result (hr)) {
566 GST_ERROR_OBJECT (self, "Cannot get IMemoryBuffer");
567 return GST_FLOW_ERROR;
568 }
569
570 hr = mem_buf->CreateReference (&mem_ref);
571 if (!gst_mf_result (hr)) {
572 GST_ERROR_OBJECT (self, "Cannot get IMemoryBufferReference");
573 return GST_FLOW_ERROR;
574 }
575
576 hr = mem_ref.As (&byte_access);
577 if (!gst_mf_result (hr)) {
578 GST_ERROR_OBJECT (self, "Cannot get IMemoryBufferByteAccess");
579 return GST_FLOW_ERROR;
580 }
581
582 hr = byte_access->GetBuffer (&data, &size);
583 if (!gst_mf_result (hr)) {
584 GST_ERROR_OBJECT (self, "Cannot get raw buffer data");
585 return GST_FLOW_ERROR;
586 }
587
588 if (size < GST_VIDEO_INFO_SIZE (&self->info)) {
589 GST_ERROR_OBJECT (self, "Too small buffer size %d", size);
590 return GST_FLOW_ERROR;
591 }
592
593 if (!gst_video_frame_map (&frame, &self->info, buffer, GST_MAP_WRITE)) {
594 GST_ERROR_OBJECT (self, "Failed to map buffer");
595 return GST_FLOW_ERROR;
596 }
597
598 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&self->info); i++) {
599 guint8 *src, *dst;
600 gint src_stride, dst_stride;
601 gint width;
602
603 src = data + desc[i].StartIndex;
604 dst = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, i);
605
606 src_stride = desc[i].Stride;
607 dst_stride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, i);
608 width = GST_VIDEO_INFO_COMP_WIDTH (&self->info, i)
609 * GST_VIDEO_INFO_COMP_PSTRIDE (&self->info, i);
610
611 for (j = 0; j < GST_VIDEO_INFO_COMP_HEIGHT (&self->info, i); j++) {
612 memcpy (dst, src, width);
613 src += src_stride;
614 dst += dst_stride;
615 }
616 }
617
618 gst_video_frame_unmap (&frame);
619
620 GST_BUFFER_PTS (buffer) = timestamp;
621 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
622 GST_BUFFER_DURATION (buffer) = duration;
623
624 return ret;
625 }
626
627 static gboolean
gst_mf_capture_winrt_unlock(GstMFSourceObject * object)628 gst_mf_capture_winrt_unlock (GstMFSourceObject * object)
629 {
630 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
631
632 g_mutex_lock (&self->lock);
633 if (self->flushing) {
634 g_mutex_unlock (&self->lock);
635 return TRUE;
636 }
637
638 self->flushing = TRUE;
639 g_cond_broadcast (&self->cond);
640 g_mutex_unlock (&self->lock);
641
642 return TRUE;
643 }
644
645 static gboolean
gst_mf_capture_winrt_unlock_stop(GstMFSourceObject * object)646 gst_mf_capture_winrt_unlock_stop (GstMFSourceObject * object)
647 {
648 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
649
650 g_mutex_lock (&self->lock);
651 if (!self->flushing) {
652 g_mutex_unlock (&self->lock);
653 return TRUE;
654 }
655
656 self->flushing = FALSE;
657 g_cond_broadcast (&self->cond);
658 g_mutex_unlock (&self->lock);
659
660 return TRUE;
661 }
662
663 static GstCaps *
gst_mf_capture_winrt_get_caps(GstMFSourceObject * object)664 gst_mf_capture_winrt_get_caps (GstMFSourceObject * object)
665 {
666 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
667
668 if (self->supported_caps)
669 return gst_caps_ref (self->supported_caps);
670
671 return NULL;
672 }
673
674 /* *INDENT-OFF* */
675 static gboolean
gst_mf_capture_winrt_set_caps(GstMFSourceObject * object,GstCaps * caps)676 gst_mf_capture_winrt_set_caps (GstMFSourceObject * object, GstCaps * caps)
677 {
678 GstMFCaptureWinRT *self = GST_MF_CAPTURE_WINRT (object);
679 std::vector<GstWinRTMediaDescription> desc_list;
680 HRESULT hr;
681 GstCaps *target_caps = NULL;
682
683 hr = self->capture->GetAvailableDescriptions(desc_list);
684 if (!gst_mf_result (hr) || desc_list.empty()) {
685 GST_ERROR_OBJECT (self, "No available media description");
686 return FALSE;
687 }
688
689 for (const auto& iter: desc_list) {
690 if (gst_caps_can_intersect (iter.caps_, caps)) {
691 target_caps = gst_caps_ref (iter.caps_);
692 self->capture->SetMediaDescription(iter);
693 break;
694 }
695 }
696
697 if (!target_caps) {
698 GST_ERROR_OBJECT (self,
699 "Could not determine target media type with given caps %"
700 GST_PTR_FORMAT, caps);
701
702 return FALSE;
703 }
704
705 gst_video_info_from_caps (&self->info, target_caps);
706 gst_caps_unref (target_caps);
707
708 return TRUE;
709 }
710 /* *INDENT-ON* */
711
712 static void
gst_mf_capture_winrt_frame_clear(GstMFCaptureWinRTFrame * winrt_frame)713 gst_mf_capture_winrt_frame_clear (GstMFCaptureWinRTFrame * winrt_frame)
714 {
715 if (!winrt_frame)
716 return;
717
718 if (winrt_frame->frame)
719 winrt_frame->frame->Release ();
720
721 winrt_frame->frame = nullptr;
722 winrt_frame->clock_time = GST_CLOCK_TIME_NONE;
723 }
724
725 GstMFSourceObject *
gst_mf_capture_winrt_new(GstMFSourceType type,gint device_index,const gchar * device_name,const gchar * device_path,gpointer dispatcher)726 gst_mf_capture_winrt_new (GstMFSourceType type, gint device_index,
727 const gchar * device_name, const gchar * device_path, gpointer dispatcher)
728 {
729 GstMFSourceObject *self;
730 ComPtr < ICoreDispatcher > core_dispatcher;
731 /* Multiple COM init is allowed */
732 RoInitializeWrapper init_wrapper (RO_INIT_MULTITHREADED);
733
734 /* TODO: Add audio capture support */
735 g_return_val_if_fail (type == GST_MF_SOURCE_TYPE_VIDEO, NULL);
736
737 /* If application didn't pass ICoreDispatcher object,
738 * try to get dispatcher object for the current thread */
739 if (!dispatcher) {
740 HRESULT hr;
741
742 hr = FindCoreDispatcherForCurrentThread (&core_dispatcher);
743 if (gst_mf_result (hr)) {
744 GST_DEBUG ("UI dispatcher is available");
745 dispatcher = core_dispatcher.Get ();
746 } else {
747 GST_DEBUG ("UI dispatcher is unavailable");
748 }
749 } else {
750 GST_DEBUG ("Use user passed UI dispatcher");
751 }
752
753 self = (GstMFSourceObject *) g_object_new (GST_TYPE_MF_CAPTURE_WINRT,
754 "source-type", type, "device-index", device_index, "device-name",
755 device_name, "device-path", device_path, "dispatcher", dispatcher, NULL);
756
757 /* Reset explicitly to ensure that it happens before
758 * RoInitializeWrapper dtor is called */
759 core_dispatcher.Reset ();
760
761 if (!self->opened) {
762 GST_WARNING_OBJECT (self, "Couldn't open device");
763 gst_object_unref (self);
764 return NULL;
765 }
766
767 gst_object_ref_sink (self);
768
769 return self;
770 }
771