2019-10-07 12:49:26 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
|
|
|
|
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
#include <gst/base/base.h>
|
2019-10-07 12:49:26 +00:00
|
|
|
#include <gst/video/video.h>
|
|
|
|
#include "gstmfsourcereader.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <wrl.h>
|
2020-07-21 06:48:08 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2021-07-03 13:56:48 +00:00
|
|
|
/* *INDENT-OFF* */
|
2019-10-07 12:49:26 +00:00
|
|
|
using namespace Microsoft::WRL;
|
2022-01-23 17:41:29 +00:00
|
|
|
/* *INDENT-ON* */
|
2020-05-25 11:59:50 +00:00
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
GST_DEBUG_CATEGORY_EXTERN (gst_mf_source_object_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_mf_source_object_debug
|
2020-05-25 11:59:50 +00:00
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
typedef struct _GstMFStreamMediaType
|
|
|
|
{
|
|
|
|
IMFMediaType *media_type;
|
|
|
|
|
|
|
|
/* the stream index of media type */
|
|
|
|
guint stream_index;
|
|
|
|
|
|
|
|
/* the media index in the stream index */
|
|
|
|
guint media_type_index;
|
|
|
|
|
|
|
|
GstCaps *caps;
|
|
|
|
} GstMFStreamMediaType;
|
|
|
|
|
2020-07-14 10:59:09 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
IMFActivate *handle;
|
|
|
|
guint index;
|
|
|
|
gchar *name;
|
|
|
|
gchar *path;
|
|
|
|
} GstMFDeviceActivate;
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
struct _GstMFSourceReader
|
|
|
|
{
|
|
|
|
GstMFSourceObject parent;
|
|
|
|
|
2020-07-14 10:59:09 +00:00
|
|
|
GThread *thread;
|
2019-10-07 12:49:26 +00:00
|
|
|
GMutex lock;
|
2020-07-14 10:59:09 +00:00
|
|
|
GCond cond;
|
|
|
|
GMainContext *context;
|
|
|
|
GMainLoop *loop;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
/* protected by lock */
|
2020-06-16 12:09:36 +00:00
|
|
|
GstQueueArray *queue;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2021-03-16 08:56:51 +00:00
|
|
|
IMFActivate *activate;
|
2019-10-07 12:49:26 +00:00
|
|
|
IMFMediaSource *source;
|
|
|
|
IMFSourceReader *reader;
|
|
|
|
|
|
|
|
GstCaps *supported_caps;
|
|
|
|
GList *media_types;
|
|
|
|
GstMFStreamMediaType *cur_type;
|
|
|
|
GstVideoInfo info;
|
|
|
|
|
2021-08-19 12:56:05 +00:00
|
|
|
gboolean top_down_image;
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
gboolean flushing;
|
|
|
|
};
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
typedef struct _GstMFSourceReaderSample
|
|
|
|
{
|
|
|
|
IMFSample *sample;
|
|
|
|
GstClockTime clock_time;
|
|
|
|
} GstMFSourceReaderSample;
|
|
|
|
|
2020-07-14 10:59:09 +00:00
|
|
|
static void gst_mf_source_reader_constructed (GObject * object);
|
2019-10-07 12:49:26 +00:00
|
|
|
static void gst_mf_source_reader_finalize (GObject * object);
|
|
|
|
|
|
|
|
static gboolean gst_mf_source_reader_start (GstMFSourceObject * object);
|
2021-07-03 13:56:48 +00:00
|
|
|
static gboolean gst_mf_source_reader_stop (GstMFSourceObject * object);
|
2019-10-07 12:49:26 +00:00
|
|
|
static GstFlowReturn gst_mf_source_reader_fill (GstMFSourceObject * object,
|
|
|
|
GstBuffer * buffer);
|
2020-06-15 15:12:03 +00:00
|
|
|
static GstFlowReturn gst_mf_source_reader_create (GstMFSourceObject * object,
|
|
|
|
GstBuffer ** buffer);
|
2019-10-07 12:49:26 +00:00
|
|
|
static gboolean gst_mf_source_reader_unlock (GstMFSourceObject * object);
|
|
|
|
static gboolean gst_mf_source_reader_unlock_stop (GstMFSourceObject * object);
|
2021-07-03 13:56:48 +00:00
|
|
|
static GstCaps *gst_mf_source_reader_get_caps (GstMFSourceObject * object);
|
2019-10-07 12:49:26 +00:00
|
|
|
static gboolean gst_mf_source_reader_set_caps (GstMFSourceObject * object,
|
|
|
|
GstCaps * caps);
|
2020-06-16 12:09:36 +00:00
|
|
|
static void
|
|
|
|
gst_mf_source_reader_sample_clear (GstMFSourceReaderSample * reader_sample);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-07-14 10:59:09 +00:00
|
|
|
static gboolean gst_mf_source_reader_open (GstMFSourceReader * object,
|
|
|
|
IMFActivate * activate);
|
|
|
|
static gboolean gst_mf_source_reader_close (GstMFSourceReader * object);
|
|
|
|
static gpointer gst_mf_source_reader_thread_func (GstMFSourceReader * self);
|
|
|
|
static gboolean gst_mf_source_enum_device_activate (GstMFSourceReader * self,
|
|
|
|
GstMFSourceType source_type, GList ** device_activates);
|
|
|
|
static void gst_mf_device_activate_free (GstMFDeviceActivate * activate);
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
#define gst_mf_source_reader_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstMFSourceReader, gst_mf_source_reader,
|
|
|
|
GST_TYPE_MF_SOURCE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mf_source_reader_class_init (GstMFSourceReaderClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstMFSourceObjectClass *source_class = GST_MF_SOURCE_OBJECT_CLASS (klass);
|
|
|
|
|
2020-07-14 10:59:09 +00:00
|
|
|
gobject_class->constructed = gst_mf_source_reader_constructed;
|
2019-10-07 12:49:26 +00:00
|
|
|
gobject_class->finalize = gst_mf_source_reader_finalize;
|
|
|
|
|
|
|
|
source_class->start = GST_DEBUG_FUNCPTR (gst_mf_source_reader_start);
|
|
|
|
source_class->stop = GST_DEBUG_FUNCPTR (gst_mf_source_reader_stop);
|
|
|
|
source_class->fill = GST_DEBUG_FUNCPTR (gst_mf_source_reader_fill);
|
2020-06-15 15:12:03 +00:00
|
|
|
source_class->create = GST_DEBUG_FUNCPTR (gst_mf_source_reader_create);
|
2019-10-07 12:49:26 +00:00
|
|
|
source_class->unlock = GST_DEBUG_FUNCPTR (gst_mf_source_reader_unlock);
|
|
|
|
source_class->unlock_stop =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_mf_source_reader_unlock_stop);
|
|
|
|
source_class->get_caps = GST_DEBUG_FUNCPTR (gst_mf_source_reader_get_caps);
|
|
|
|
source_class->set_caps = GST_DEBUG_FUNCPTR (gst_mf_source_reader_set_caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mf_source_reader_init (GstMFSourceReader * self)
|
|
|
|
{
|
2020-06-16 12:09:36 +00:00
|
|
|
self->queue =
|
|
|
|
gst_queue_array_new_for_struct (sizeof (GstMFSourceReaderSample), 2);
|
|
|
|
gst_queue_array_set_clear_func (self->queue,
|
|
|
|
(GDestroyNotify) gst_mf_source_reader_sample_clear);
|
2019-10-07 12:49:26 +00:00
|
|
|
g_mutex_init (&self->lock);
|
2020-07-14 10:59:09 +00:00
|
|
|
g_cond_init (&self->cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mf_source_reader_constructed (GObject * object)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
|
|
|
|
self->context = g_main_context_new ();
|
|
|
|
self->loop = g_main_loop_new (self->context, FALSE);
|
|
|
|
|
|
|
|
/* Create a new thread to ensure that COM thread can be MTA thread */
|
|
|
|
g_mutex_lock (&self->lock);
|
|
|
|
self->thread = g_thread_new ("GstMFSourceReader",
|
|
|
|
(GThreadFunc) gst_mf_source_reader_thread_func, self);
|
|
|
|
while (!g_main_loop_is_running (self->loop))
|
|
|
|
g_cond_wait (&self->cond, &self->lock);
|
|
|
|
g_mutex_unlock (&self->lock);
|
2022-11-28 17:10:42 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->constructed (object);
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mf_enum_media_type_from_source_reader (IMFSourceReader * source_reader,
|
|
|
|
GList ** media_types)
|
|
|
|
{
|
|
|
|
gint i, j;
|
|
|
|
HRESULT hr;
|
2022-01-23 17:41:29 +00:00
|
|
|
GList *list = nullptr;
|
2021-07-03 13:56:48 +00:00
|
|
|
std::vector < std::string > unhandled_caps;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2022-01-23 17:41:29 +00:00
|
|
|
g_return_val_if_fail (source_reader != nullptr, FALSE);
|
|
|
|
g_return_val_if_fail (media_types != nullptr, FALSE);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-10-06 20:05:25 +00:00
|
|
|
{
|
|
|
|
/* Retrive only the first video stream. non-first video stream might be
|
|
|
|
* photo stream which doesn't seem to be working propertly in this implementation.
|
|
|
|
*
|
|
|
|
* Note: Chromium seems to be using the first video stream
|
|
|
|
* https://github.com/chromium/chromium/blob/ccd149af47315e4c6f2fc45d55be1b271f39062c/media/capture/video/win/video_capture_device_factory_win.cc
|
|
|
|
*/
|
|
|
|
i = MF_SOURCE_READER_FIRST_VIDEO_STREAM;
|
2019-10-07 12:49:26 +00:00
|
|
|
for (j = 0;; j++) {
|
2021-07-03 13:56:48 +00:00
|
|
|
ComPtr < IMFMediaType > media_type;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
hr = source_reader->GetNativeMediaType (i, j, &media_type);
|
|
|
|
|
|
|
|
if (SUCCEEDED (hr)) {
|
|
|
|
GstMFStreamMediaType *mtype;
|
2022-01-23 17:41:29 +00:00
|
|
|
GstCaps *caps = nullptr;
|
2020-07-21 06:48:08 +00:00
|
|
|
GstStructure *s;
|
|
|
|
std::string name;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
caps = gst_mf_media_type_to_caps (media_type.Get ());
|
|
|
|
|
|
|
|
/* unknown format */
|
|
|
|
if (!caps)
|
|
|
|
continue;
|
|
|
|
|
2020-07-21 06:48:08 +00:00
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
name = gst_structure_get_name (s);
|
|
|
|
if (name != "video/x-raw" && name != "image/jpeg") {
|
|
|
|
auto it =
|
2021-07-03 13:56:48 +00:00
|
|
|
std::find (unhandled_caps.begin (), unhandled_caps.end (), name);
|
|
|
|
if (it == unhandled_caps.end ()) {
|
|
|
|
GST_FIXME ("Skip not supported format %s", name.c_str ());
|
|
|
|
unhandled_caps.push_back (name);
|
2020-07-21 06:48:08 +00:00
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
mtype = g_new0 (GstMFStreamMediaType, 1);
|
|
|
|
|
|
|
|
mtype->media_type = media_type.Detach ();
|
|
|
|
mtype->stream_index = i;
|
|
|
|
mtype->media_type_index = j;
|
|
|
|
mtype->caps = caps;
|
|
|
|
|
|
|
|
GST_DEBUG ("StreamIndex %d, MediaTypeIndex %d, %" GST_PTR_FORMAT,
|
|
|
|
i, j, caps);
|
|
|
|
|
|
|
|
list = g_list_prepend (list, mtype);
|
|
|
|
} else if (hr == MF_E_NO_MORE_TYPES) {
|
|
|
|
/* no more media type in this stream index, try next stream index */
|
|
|
|
break;
|
|
|
|
} else if (hr == MF_E_INVALIDSTREAMNUMBER) {
|
|
|
|
/* no more streams and media types */
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
/* undefined return */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2022-01-27 16:45:30 +00:00
|
|
|
if (!list)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
list = g_list_reverse (list);
|
|
|
|
*media_types = list;
|
|
|
|
|
2022-01-27 16:45:30 +00:00
|
|
|
return TRUE;
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mf_stream_media_type_free (GstMFStreamMediaType * media_type)
|
|
|
|
{
|
2022-01-23 17:41:29 +00:00
|
|
|
g_return_if_fail (media_type != nullptr);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
if (media_type->media_type)
|
|
|
|
media_type->media_type->Release ();
|
|
|
|
|
|
|
|
if (media_type->caps)
|
|
|
|
gst_caps_unref (media_type->caps);
|
|
|
|
|
|
|
|
g_free (media_type);
|
|
|
|
}
|
|
|
|
|
2020-07-21 07:52:33 +00:00
|
|
|
static gint
|
|
|
|
compare_caps_func (gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
GstMFStreamMediaType *m1, *m2;
|
|
|
|
|
|
|
|
m1 = (GstMFStreamMediaType *) a;
|
|
|
|
m2 = (GstMFStreamMediaType *) b;
|
|
|
|
|
|
|
|
return gst_mf_source_object_caps_compare (m1->caps, m2->caps);
|
|
|
|
}
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
static gboolean
|
2020-07-14 10:59:09 +00:00
|
|
|
gst_mf_source_reader_open (GstMFSourceReader * self, IMFActivate * activate)
|
2019-10-07 12:49:26 +00:00
|
|
|
{
|
|
|
|
GList *iter;
|
|
|
|
HRESULT hr;
|
2021-07-03 13:56:48 +00:00
|
|
|
ComPtr < IMFSourceReader > reader;
|
|
|
|
ComPtr < IMFMediaSource > source;
|
|
|
|
ComPtr < IMFAttributes > attr;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2021-07-03 15:36:27 +00:00
|
|
|
hr = activate->ActivateObject (IID_PPV_ARGS (&source));
|
2019-10-07 12:49:26 +00:00
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = MFCreateAttributes (&attr, 2);
|
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = attr->SetUINT32 (MF_READWRITE_DISABLE_CONVERTERS, TRUE);
|
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = MFCreateSourceReaderFromMediaSource (source.Get (),
|
|
|
|
attr.Get (), &reader);
|
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!gst_mf_enum_media_type_from_source_reader (reader.Get (),
|
|
|
|
&self->media_types)) {
|
|
|
|
GST_ERROR_OBJECT (self, "No available media types");
|
|
|
|
source->Shutdown ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:56:51 +00:00
|
|
|
self->activate = activate;
|
|
|
|
activate->AddRef ();
|
2019-10-07 12:49:26 +00:00
|
|
|
self->source = source.Detach ();
|
|
|
|
self->reader = reader.Detach ();
|
|
|
|
|
2020-07-21 07:52:33 +00:00
|
|
|
self->media_types = g_list_sort (self->media_types,
|
|
|
|
(GCompareFunc) compare_caps_func);
|
|
|
|
|
|
|
|
self->supported_caps = gst_caps_new_empty ();
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
for (iter = self->media_types; iter; iter = g_list_next (iter)) {
|
|
|
|
GstMFStreamMediaType *mtype = (GstMFStreamMediaType *) iter->data;
|
2020-07-21 07:52:33 +00:00
|
|
|
|
|
|
|
gst_caps_append (self->supported_caps, gst_caps_copy (mtype->caps));
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Available output caps %" GST_PTR_FORMAT,
|
|
|
|
self->supported_caps);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-05-14 11:47:06 +00:00
|
|
|
static gboolean
|
2020-07-14 10:59:09 +00:00
|
|
|
gst_mf_source_reader_close (GstMFSourceReader * self)
|
2019-10-07 12:49:26 +00:00
|
|
|
{
|
2020-05-14 11:47:06 +00:00
|
|
|
gst_clear_caps (&self->supported_caps);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2021-03-16 08:56:51 +00:00
|
|
|
if (self->activate) {
|
|
|
|
self->activate->ShutdownObject ();
|
|
|
|
self->activate->Release ();
|
2022-01-23 17:41:29 +00:00
|
|
|
self->activate = nullptr;
|
2021-03-16 08:56:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 11:47:06 +00:00
|
|
|
if (self->media_types) {
|
|
|
|
g_list_free_full (self->media_types,
|
|
|
|
(GDestroyNotify) gst_mf_stream_media_type_free);
|
2022-01-23 17:41:29 +00:00
|
|
|
self->media_types = nullptr;
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 11:47:06 +00:00
|
|
|
if (self->reader) {
|
|
|
|
self->reader->Release ();
|
2022-01-23 17:41:29 +00:00
|
|
|
self->reader = nullptr;
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 11:47:06 +00:00
|
|
|
if (self->source) {
|
|
|
|
self->source->Shutdown ();
|
|
|
|
self->source->Release ();
|
2022-01-23 17:41:29 +00:00
|
|
|
self->source = nullptr;
|
2020-05-14 11:47:06 +00:00
|
|
|
}
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-05-14 11:47:06 +00:00
|
|
|
return TRUE;
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mf_source_reader_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
|
2020-07-14 10:59:09 +00:00
|
|
|
g_main_loop_quit (self->loop);
|
|
|
|
g_thread_join (self->thread);
|
|
|
|
g_main_loop_unref (self->loop);
|
|
|
|
g_main_context_unref (self->context);
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
gst_queue_array_free (self->queue);
|
2020-07-14 10:59:09 +00:00
|
|
|
gst_clear_caps (&self->supported_caps);
|
2019-10-07 12:49:26 +00:00
|
|
|
g_mutex_clear (&self->lock);
|
2020-07-14 10:59:09 +00:00
|
|
|
g_cond_clear (&self->cond);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mf_source_reader_start (GstMFSourceObject * object)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
HRESULT hr;
|
|
|
|
GstMFStreamMediaType *type;
|
|
|
|
|
|
|
|
if (!self->cur_type) {
|
|
|
|
GST_ERROR_OBJECT (self, "MediaType wasn't specified");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = self->cur_type;
|
2021-08-19 12:56:05 +00:00
|
|
|
self->top_down_image = TRUE;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-06-15 15:12:03 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&self->info) != GST_VIDEO_FORMAT_ENCODED) {
|
2021-08-19 12:56:05 +00:00
|
|
|
UINT32 stride;
|
|
|
|
INT32 actual_stride = GST_VIDEO_INFO_PLANE_STRIDE (&self->info, 0);
|
|
|
|
|
|
|
|
/* This MF_MT_DEFAULT_STRIDE uses UINT32 type but actual value is
|
|
|
|
* INT32, which can be negative in case of RGB image, and negative means
|
|
|
|
* its stored as bottom-up manner */
|
|
|
|
hr = type->media_type->GetUINT32 (MF_MT_DEFAULT_STRIDE, &stride);
|
|
|
|
if (gst_mf_result (hr)) {
|
|
|
|
actual_stride = (INT32) stride;
|
|
|
|
if (actual_stride < 0) {
|
|
|
|
if (!GST_VIDEO_INFO_IS_RGB (&self->info)) {
|
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"Bottom-up image is allowed only for RGB format");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"Detected bottom-up image, stride %d", actual_stride);
|
|
|
|
|
|
|
|
self->top_down_image = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* If MF_MT_DEFAULT_STRIDE attribute is not specified, we can use our
|
|
|
|
* value */
|
|
|
|
type->media_type->SetUINT32 (MF_MT_DEFAULT_STRIDE,
|
|
|
|
(UINT32) actual_stride);
|
|
|
|
}
|
|
|
|
gst_mf_update_video_info_with_stride (&self->info,
|
|
|
|
std::abs (actual_stride));
|
2020-06-15 15:12:03 +00:00
|
|
|
}
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
hr = self->reader->SetStreamSelection (type->stream_index, TRUE);
|
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = self->reader->SetCurrentMediaType (type->stream_index,
|
2022-01-23 17:41:29 +00:00
|
|
|
nullptr, type->media_type);
|
2019-10-07 12:49:26 +00:00
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
static GstMFSourceReaderSample *
|
|
|
|
gst_mf_source_reader_sample_new (IMFSample * sample, GstClockTime timestamp)
|
|
|
|
{
|
|
|
|
GstMFSourceReaderSample *reader_sample = g_new0 (GstMFSourceReaderSample, 1);
|
|
|
|
|
|
|
|
reader_sample->sample = sample;
|
|
|
|
reader_sample->clock_time = timestamp;
|
|
|
|
|
|
|
|
return reader_sample;
|
|
|
|
}
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
static gboolean
|
|
|
|
gst_mf_source_reader_stop (GstMFSourceObject * object)
|
|
|
|
{
|
2020-05-14 11:47:06 +00:00
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
gst_queue_array_clear (self->queue);
|
2020-05-14 11:47:06 +00:00
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_mf_source_reader_read_sample (GstMFSourceReader * self)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
DWORD stream_flags = 0;
|
|
|
|
GstMFStreamMediaType *type = self->cur_type;
|
2020-06-16 12:09:36 +00:00
|
|
|
IMFSample *sample = nullptr;
|
|
|
|
GstMFSourceReaderSample reader_sample;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2022-01-23 17:41:29 +00:00
|
|
|
hr = self->reader->ReadSample (type->stream_index, 0, nullptr, &stream_flags,
|
|
|
|
nullptr, &sample);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
if (!gst_mf_result (hr)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to read sample");
|
2019-10-07 12:49:26 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2020-06-16 12:09:36 +00:00
|
|
|
}
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
if ((stream_flags & MF_SOURCE_READERF_ERROR) == MF_SOURCE_READERF_ERROR) {
|
|
|
|
GST_ERROR_OBJECT (self, "Error while reading sample, sample flags 0x%x",
|
|
|
|
stream_flags);
|
2019-10-07 12:49:26 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2020-06-16 12:09:36 +00:00
|
|
|
}
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
if (!sample) {
|
|
|
|
GST_WARNING_OBJECT (self, "Empty sample");
|
2019-10-07 12:49:26 +00:00
|
|
|
return GST_FLOW_OK;
|
2020-06-16 12:09:36 +00:00
|
|
|
}
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
reader_sample.sample = sample;
|
|
|
|
reader_sample.clock_time =
|
|
|
|
gst_mf_source_object_get_running_time (GST_MF_SOURCE_OBJECT (self));
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
gst_queue_array_push_tail_struct (self->queue, &reader_sample);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2020-06-15 15:12:03 +00:00
|
|
|
gst_mf_source_reader_get_media_buffer (GstMFSourceReader * self,
|
2020-06-16 12:09:36 +00:00
|
|
|
IMFMediaBuffer ** buffer, GstClockTime * timestamp, GstClockTime * duration)
|
2019-10-07 12:49:26 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2020-06-16 12:09:36 +00:00
|
|
|
IMFSample *sample = nullptr;
|
|
|
|
HRESULT hr;
|
|
|
|
DWORD count = 0;
|
|
|
|
LONGLONG mf_timestamp;
|
|
|
|
GstMFSourceReaderSample *reader_sample = nullptr;
|
|
|
|
|
|
|
|
*buffer = nullptr;
|
|
|
|
*timestamp = GST_CLOCK_TIME_NONE;
|
|
|
|
*duration = GST_CLOCK_TIME_NONE;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
while (gst_queue_array_is_empty (self->queue)) {
|
2019-10-07 12:49:26 +00:00
|
|
|
ret = gst_mf_source_reader_read_sample (self);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
g_mutex_lock (&self->lock);
|
|
|
|
if (self->flushing) {
|
|
|
|
g_mutex_unlock (&self->lock);
|
|
|
|
return GST_FLOW_FLUSHING;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&self->lock);
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
reader_sample =
|
|
|
|
(GstMFSourceReaderSample *) gst_queue_array_pop_head_struct (self->queue);
|
|
|
|
sample = reader_sample->sample;
|
|
|
|
g_assert (sample);
|
|
|
|
|
|
|
|
hr = sample->GetBufferCount (&count);
|
|
|
|
if (!gst_mf_result (hr) || count == 0) {
|
|
|
|
GST_WARNING_OBJECT (self, "Empty IMFSample, read again");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: read the first buffer and ignore the others for now */
|
|
|
|
hr = sample->GetBufferByIndex (0, buffer);
|
|
|
|
if (!gst_mf_result (hr)) {
|
|
|
|
GST_WARNING_OBJECT (self, "Couldn't get IMFMediaBuffer from sample");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = sample->GetSampleDuration (&mf_timestamp);
|
|
|
|
if (!gst_mf_result (hr)) {
|
|
|
|
GST_WARNING_OBJECT (self, "Couldn't get sample duration");
|
|
|
|
*duration = GST_CLOCK_TIME_NONE;
|
|
|
|
} else {
|
|
|
|
/* Media Foundation uses 100 nano seconds unit */
|
|
|
|
*duration = mf_timestamp * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
*timestamp = reader_sample->clock_time;
|
|
|
|
|
|
|
|
done:
|
|
|
|
gst_mf_source_reader_sample_clear (reader_sample);
|
2020-06-15 15:12:03 +00:00
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_mf_source_reader_fill (GstMFSourceObject * object, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2021-07-03 13:56:48 +00:00
|
|
|
ComPtr < IMFMediaBuffer > media_buffer;
|
2020-06-15 15:12:03 +00:00
|
|
|
GstVideoFrame frame;
|
|
|
|
BYTE *data;
|
|
|
|
gint i, j;
|
|
|
|
HRESULT hr;
|
2020-06-16 12:09:36 +00:00
|
|
|
GstClockTime timestamp = GST_CLOCK_TIME_NONE;
|
|
|
|
GstClockTime duration = GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = gst_mf_source_reader_get_media_buffer (self,
|
|
|
|
media_buffer.ReleaseAndGetAddressOf (), ×tamp, &duration);
|
|
|
|
} while (ret == GST_FLOW_OK && !media_buffer);
|
2020-06-15 15:12:03 +00:00
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2022-01-23 17:41:29 +00:00
|
|
|
hr = media_buffer->Lock (&data, nullptr, nullptr);
|
2019-10-07 12:49:26 +00:00
|
|
|
if (!gst_mf_result (hr)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to lock media buffer");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&frame, &self->info, buffer, GST_MAP_WRITE)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to map buffer");
|
|
|
|
media_buffer->Unlock ();
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-08-19 12:56:05 +00:00
|
|
|
if (!self->top_down_image) {
|
2019-10-07 12:49:26 +00:00
|
|
|
guint8 *src, *dst;
|
|
|
|
gint src_stride, dst_stride;
|
2021-08-19 12:56:05 +00:00
|
|
|
gint width, height;
|
|
|
|
|
|
|
|
/* must be single plane RGB */
|
|
|
|
width = GST_VIDEO_INFO_COMP_WIDTH (&self->info, 0)
|
|
|
|
* GST_VIDEO_INFO_COMP_PSTRIDE (&self->info, 0);
|
|
|
|
height = GST_VIDEO_INFO_HEIGHT (&self->info);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2021-08-19 12:56:05 +00:00
|
|
|
src_stride = GST_VIDEO_INFO_PLANE_STRIDE (&self->info, 0);
|
|
|
|
dst_stride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2021-08-19 12:56:05 +00:00
|
|
|
/* This is bottom up image, should copy lines in reverse order */
|
|
|
|
src = data + src_stride * (height - 1);
|
|
|
|
dst = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2021-08-19 12:56:05 +00:00
|
|
|
for (j = 0; j < height; j++) {
|
2019-10-07 12:49:26 +00:00
|
|
|
memcpy (dst, src, width);
|
2021-08-19 12:56:05 +00:00
|
|
|
src -= src_stride;
|
2019-10-07 12:49:26 +00:00
|
|
|
dst += dst_stride;
|
|
|
|
}
|
2021-08-19 12:56:05 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&self->info); i++) {
|
|
|
|
guint8 *src, *dst;
|
|
|
|
gint src_stride, dst_stride;
|
|
|
|
gint width;
|
|
|
|
|
|
|
|
src = data + GST_VIDEO_INFO_PLANE_OFFSET (&self->info, i);
|
|
|
|
dst = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, i);
|
|
|
|
|
|
|
|
src_stride = GST_VIDEO_INFO_PLANE_STRIDE (&self->info, i);
|
|
|
|
dst_stride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, i);
|
|
|
|
width = GST_VIDEO_INFO_COMP_WIDTH (&self->info, i)
|
|
|
|
* GST_VIDEO_INFO_COMP_PSTRIDE (&self->info, i);
|
|
|
|
|
|
|
|
for (j = 0; j < GST_VIDEO_INFO_COMP_HEIGHT (&self->info, i); j++) {
|
|
|
|
memcpy (dst, src, width);
|
|
|
|
src += src_stride;
|
|
|
|
dst += dst_stride;
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_frame_unmap (&frame);
|
|
|
|
media_buffer->Unlock ();
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
GST_BUFFER_PTS (buffer) = timestamp;
|
|
|
|
GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_DURATION (buffer) = duration;
|
|
|
|
|
2020-06-15 15:12:03 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_mf_source_reader_create (GstMFSourceObject * object, GstBuffer ** buffer)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2021-07-03 13:56:48 +00:00
|
|
|
ComPtr < IMFMediaBuffer > media_buffer;
|
2020-06-15 15:12:03 +00:00
|
|
|
HRESULT hr;
|
|
|
|
BYTE *data;
|
|
|
|
DWORD len = 0;
|
|
|
|
GstBuffer *buf;
|
|
|
|
GstMapInfo info;
|
2020-06-16 12:09:36 +00:00
|
|
|
GstClockTime timestamp = GST_CLOCK_TIME_NONE;
|
|
|
|
GstClockTime duration = GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = gst_mf_source_reader_get_media_buffer (self,
|
|
|
|
media_buffer.ReleaseAndGetAddressOf (), ×tamp, &duration);
|
|
|
|
} while (ret == GST_FLOW_OK && !media_buffer);
|
2020-06-15 15:12:03 +00:00
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
|
|
|
|
2022-01-23 17:41:29 +00:00
|
|
|
hr = media_buffer->Lock (&data, nullptr, &len);
|
2020-06-15 15:12:03 +00:00
|
|
|
if (!gst_mf_result (hr) || len == 0) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to lock media buffer");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = gst_buffer_new_and_alloc (len);
|
|
|
|
if (!buf) {
|
|
|
|
GST_ERROR_OBJECT (self, "Cannot allocate buffer");
|
|
|
|
media_buffer->Unlock ();
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_map (buf, &info, GST_MAP_WRITE);
|
|
|
|
memcpy (info.data, data, len);
|
|
|
|
gst_buffer_unmap (buf, &info);
|
|
|
|
|
|
|
|
media_buffer->Unlock ();
|
|
|
|
|
2020-10-07 08:37:25 +00:00
|
|
|
GST_BUFFER_PTS (buf) = timestamp;
|
2020-06-16 12:09:36 +00:00
|
|
|
/* Set DTS since this is compressed format */
|
2020-10-07 08:37:25 +00:00
|
|
|
GST_BUFFER_DTS (buf) = timestamp;
|
|
|
|
GST_BUFFER_DURATION (buf) = duration;
|
2020-06-16 12:09:36 +00:00
|
|
|
|
2020-06-15 15:12:03 +00:00
|
|
|
*buffer = buf;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mf_source_reader_unlock (GstMFSourceObject * object)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
|
|
|
|
g_mutex_lock (&self->lock);
|
|
|
|
self->flushing = TRUE;
|
|
|
|
g_mutex_unlock (&self->lock);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mf_source_reader_unlock_stop (GstMFSourceObject * object)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
|
|
|
|
g_mutex_lock (&self->lock);
|
|
|
|
self->flushing = FALSE;
|
|
|
|
g_mutex_unlock (&self->lock);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
gst_mf_source_reader_get_caps (GstMFSourceObject * object)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
|
|
|
|
if (self->supported_caps)
|
|
|
|
return gst_caps_ref (self->supported_caps);
|
|
|
|
|
2022-01-23 17:41:29 +00:00
|
|
|
return nullptr;
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mf_source_reader_set_caps (GstMFSourceObject * object, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstMFSourceReader *self = GST_MF_SOURCE_READER (object);
|
|
|
|
GList *iter;
|
2022-01-23 17:41:29 +00:00
|
|
|
GstMFStreamMediaType *best_type = nullptr;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
for (iter = self->media_types; iter; iter = g_list_next (iter)) {
|
|
|
|
GstMFStreamMediaType *minfo = (GstMFStreamMediaType *) iter->data;
|
2021-07-08 10:03:06 +00:00
|
|
|
if (gst_caps_can_intersect (minfo->caps, caps)) {
|
2019-10-07 12:49:26 +00:00
|
|
|
best_type = minfo;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!best_type) {
|
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"Could not determine target media type with given caps %"
|
|
|
|
GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->cur_type = best_type;
|
|
|
|
gst_video_info_from_caps (&self->info, best_type->caps);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-07-14 10:59:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_mf_source_reader_main_loop_running_cb (GstMFSourceReader * self)
|
|
|
|
{
|
|
|
|
GST_INFO_OBJECT (self, "Main loop running now");
|
|
|
|
|
|
|
|
g_mutex_lock (&self->lock);
|
|
|
|
g_cond_signal (&self->cond);
|
|
|
|
g_mutex_unlock (&self->lock);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gst_mf_source_reader_thread_func (GstMFSourceReader * self)
|
|
|
|
{
|
|
|
|
GstMFSourceObject *object = GST_MF_SOURCE_OBJECT (self);
|
|
|
|
GSource *source;
|
2022-01-23 17:41:29 +00:00
|
|
|
GList *activate_list = nullptr;
|
|
|
|
GstMFDeviceActivate *target = nullptr;
|
2020-07-14 10:59:09 +00:00
|
|
|
GList *iter;
|
|
|
|
|
2022-01-23 17:41:29 +00:00
|
|
|
CoInitializeEx (nullptr, COINIT_MULTITHREADED);
|
2020-07-14 10:59:09 +00:00
|
|
|
|
|
|
|
g_main_context_push_thread_default (self->context);
|
|
|
|
|
|
|
|
source = g_idle_source_new ();
|
|
|
|
g_source_set_callback (source,
|
2022-01-23 17:41:29 +00:00
|
|
|
(GSourceFunc) gst_mf_source_reader_main_loop_running_cb, self, nullptr);
|
2020-07-14 10:59:09 +00:00
|
|
|
g_source_attach (source, self->context);
|
|
|
|
g_source_unref (source);
|
|
|
|
|
|
|
|
if (!gst_mf_source_enum_device_activate (self,
|
|
|
|
object->source_type, &activate_list)) {
|
|
|
|
GST_WARNING_OBJECT (self, "No available video capture device");
|
|
|
|
goto run_loop;
|
|
|
|
}
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
for (iter = activate_list; iter; iter = g_list_next (iter)) {
|
|
|
|
GstMFDeviceActivate *activate = (GstMFDeviceActivate *) iter->data;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "device %d, name: \"%s\", path: \"%s\"",
|
|
|
|
activate->index, GST_STR_NULL (activate->name),
|
|
|
|
GST_STR_NULL (activate->path));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"Requested device index: %d, name: \"%s\", path \"%s\"",
|
|
|
|
object->device_index, GST_STR_NULL (object->device_name),
|
|
|
|
GST_STR_NULL (object->device_path));
|
|
|
|
|
|
|
|
for (iter = activate_list; iter; iter = g_list_next (iter)) {
|
|
|
|
GstMFDeviceActivate *activate = (GstMFDeviceActivate *) iter->data;
|
|
|
|
gboolean match;
|
|
|
|
|
|
|
|
if (object->device_path) {
|
|
|
|
match = g_ascii_strcasecmp (activate->path, object->device_path) == 0;
|
|
|
|
} else if (object->device_name) {
|
|
|
|
match = g_ascii_strcasecmp (activate->name, object->device_name) == 0;
|
|
|
|
} else if (object->device_index >= 0) {
|
|
|
|
match = activate->index == object->device_index;
|
|
|
|
} else {
|
|
|
|
/* pick the first entry */
|
|
|
|
match = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
target = activate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target) {
|
|
|
|
object->opened = gst_mf_source_reader_open (self, target->handle);
|
|
|
|
|
|
|
|
g_free (object->device_path);
|
|
|
|
object->device_path = g_strdup (target->path);
|
|
|
|
|
|
|
|
g_free (object->device_name);
|
|
|
|
object->device_name = g_strdup (target->name);
|
|
|
|
|
|
|
|
object->device_index = target->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activate_list)
|
|
|
|
g_list_free_full (activate_list,
|
|
|
|
(GDestroyNotify) gst_mf_device_activate_free);
|
|
|
|
|
|
|
|
run_loop:
|
|
|
|
GST_DEBUG_OBJECT (self, "Starting main loop");
|
|
|
|
g_main_loop_run (self->loop);
|
|
|
|
GST_DEBUG_OBJECT (self, "Stopped main loop");
|
|
|
|
|
|
|
|
gst_mf_source_reader_stop (object);
|
|
|
|
gst_mf_source_reader_close (self);
|
|
|
|
|
|
|
|
g_main_context_pop_thread_default (self->context);
|
|
|
|
|
|
|
|
CoUninitialize ();
|
|
|
|
|
2022-01-23 17:41:29 +00:00
|
|
|
return nullptr;
|
2020-07-14 10:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mf_source_enum_device_activate (GstMFSourceReader * self,
|
|
|
|
GstMFSourceType source_type, GList ** device_sources)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
2022-01-23 17:41:29 +00:00
|
|
|
GList *ret = nullptr;
|
2021-07-03 13:56:48 +00:00
|
|
|
ComPtr < IMFAttributes > attr;
|
2022-01-23 17:41:29 +00:00
|
|
|
IMFActivate **devices = nullptr;
|
2020-07-14 10:59:09 +00:00
|
|
|
UINT32 i, count = 0;
|
|
|
|
|
|
|
|
hr = MFCreateAttributes (&attr, 1);
|
|
|
|
if (!gst_mf_result (hr)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (source_type) {
|
|
|
|
case GST_MF_SOURCE_TYPE_VIDEO:
|
|
|
|
hr = attr->SetGUID (MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
|
|
|
|
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_ERROR_OBJECT (self, "Unknown source type %d", source_type);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = MFEnumDeviceSources (attr.Get (), &devices, &count);
|
|
|
|
if (!gst_mf_result (hr))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
GstMFDeviceActivate *entry;
|
|
|
|
LPWSTR name;
|
|
|
|
UINT32 name_len;
|
|
|
|
IMFActivate *activate = devices[i];
|
|
|
|
|
|
|
|
switch (source_type) {
|
|
|
|
case GST_MF_SOURCE_TYPE_VIDEO:
|
2021-07-03 13:56:48 +00:00
|
|
|
hr = activate->GetAllocatedString
|
|
|
|
(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &name,
|
|
|
|
&name_len);
|
2020-07-14 10:59:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = g_new0 (GstMFDeviceActivate, 1);
|
|
|
|
entry->index = i;
|
|
|
|
entry->handle = activate;
|
|
|
|
|
|
|
|
if (gst_mf_result (hr)) {
|
|
|
|
entry->path = g_utf16_to_utf8 ((const gunichar2 *) name,
|
2022-01-23 17:41:29 +00:00
|
|
|
-1, nullptr, nullptr, nullptr);
|
2020-07-14 10:59:09 +00:00
|
|
|
CoTaskMemFree (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = activate->GetAllocatedString (MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,
|
|
|
|
&name, &name_len);
|
|
|
|
if (gst_mf_result (hr)) {
|
|
|
|
entry->name = g_utf16_to_utf8 ((const gunichar2 *) name,
|
2022-01-23 17:41:29 +00:00
|
|
|
-1, nullptr, nullptr, nullptr);
|
2020-07-14 10:59:09 +00:00
|
|
|
CoTaskMemFree (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = g_list_prepend (ret, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
CoTaskMemFree (devices);
|
|
|
|
|
2022-01-27 16:45:30 +00:00
|
|
|
if (!ret)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*device_sources = g_list_reverse (ret);
|
2020-07-14 10:59:09 +00:00
|
|
|
|
2022-01-27 16:45:30 +00:00
|
|
|
return TRUE;
|
2020-07-14 10:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mf_device_activate_free (GstMFDeviceActivate * activate)
|
|
|
|
{
|
2022-01-23 17:41:29 +00:00
|
|
|
g_return_if_fail (activate != nullptr);
|
2020-07-14 10:59:09 +00:00
|
|
|
|
|
|
|
if (activate->handle)
|
|
|
|
activate->handle->Release ();
|
|
|
|
|
|
|
|
g_free (activate->name);
|
|
|
|
g_free (activate->path);
|
|
|
|
g_free (activate);
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:09:36 +00:00
|
|
|
static void
|
|
|
|
gst_mf_source_reader_sample_clear (GstMFSourceReaderSample * reader_sample)
|
|
|
|
{
|
|
|
|
if (!reader_sample)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (reader_sample->sample)
|
|
|
|
reader_sample->sample->Release ();
|
|
|
|
|
|
|
|
reader_sample->sample = nullptr;
|
|
|
|
reader_sample->clock_time = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
2019-10-07 12:49:26 +00:00
|
|
|
GstMFSourceObject *
|
|
|
|
gst_mf_source_reader_new (GstMFSourceType type, gint device_index,
|
|
|
|
const gchar * device_name, const gchar * device_path)
|
|
|
|
{
|
2020-05-14 11:47:06 +00:00
|
|
|
GstMFSourceObject *self;
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
/* TODO: add more type */
|
2022-01-23 17:41:29 +00:00
|
|
|
g_return_val_if_fail (type == GST_MF_SOURCE_TYPE_VIDEO, nullptr);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
2020-05-14 11:47:06 +00:00
|
|
|
self = (GstMFSourceObject *) g_object_new (GST_TYPE_MF_SOURCE_READER,
|
2020-05-25 12:18:16 +00:00
|
|
|
"source-type", type, "device-index", device_index, "device-name",
|
2022-01-23 17:41:29 +00:00
|
|
|
device_name, "device-path", device_path, nullptr);
|
2019-10-07 12:49:26 +00:00
|
|
|
|
|
|
|
gst_object_ref_sink (self);
|
|
|
|
|
2020-05-20 13:59:19 +00:00
|
|
|
if (!self->opened) {
|
2022-11-05 16:53:46 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Couldn't open device");
|
2020-05-14 11:47:06 +00:00
|
|
|
gst_object_unref (self);
|
2022-01-23 17:41:29 +00:00
|
|
|
return nullptr;
|
2019-10-07 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 11:47:06 +00:00
|
|
|
return self;
|
2021-07-03 13:56:48 +00:00
|
|
|
}
|