summaryrefslogtreecommitdiff
path: root/sys/mediafoundation/gstmfdevice.c
blob: 71356bec66e29c28ea40ea5c1856c4c77822f600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* GStreamer
 * 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

#include "gstmfconfig.h"

#include "gstmfvideosrc.h"
#include "gstmfutils.h"
#if GST_MF_WINAPI_ONLY_APP
#include "gstmfcapturewinrt.h"
#else /* GST_MF_WINAPI_ONLY_APP */
#include "gstmfsourcereader.h"
#if GST_MF_HAVE_CAPTURE_ENGINE
#include "gstmfcaptureengine.h"
#endif /* GST_MF_HAVE_CAPTURE_ENGINE */
#endif /* GST_MF_WINAPI_ONLY_APP */

#include "gstmfdevice.h"

GST_DEBUG_CATEGORY_EXTERN (gst_mf_debug);
#define GST_CAT_DEFAULT gst_mf_debug

enum
{
  PROP_0,
  PROP_DEVICE_PATH,
};

struct _GstMFDevice
{
  GstDevice parent;

  gchar *device_path;
};

G_DEFINE_TYPE (GstMFDevice, gst_mf_device, GST_TYPE_DEVICE);

static void gst_mf_device_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_mf_device_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_mf_device_finalize (GObject * object);
static GstElement *gst_mf_device_create_element (GstDevice * device,
    const gchar * name);

static void
gst_mf_device_class_init (GstMFDeviceClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);

  dev_class->create_element = gst_mf_device_create_element;

  gobject_class->get_property = gst_mf_device_get_property;
  gobject_class->set_property = gst_mf_device_set_property;
  gobject_class->finalize = gst_mf_device_finalize;

  g_object_class_install_property (gobject_class, PROP_DEVICE_PATH,
      g_param_spec_string ("device-path", "Device Path",
          "The device path", NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}

static void
gst_mf_device_init (GstMFDevice * self)
{
}

static void
gst_mf_device_finalize (GObject * object)
{
  GstMFDevice *self = GST_MF_DEVICE (object);

  g_free (self->device_path);

  G_OBJECT_CLASS (gst_mf_device_parent_class)->finalize (object);
}

static GstElement *
gst_mf_device_create_element (GstDevice * device, const gchar * name)
{
  GstMFDevice *self = GST_MF_DEVICE (device);
  GstElement *elem;

  elem = gst_element_factory_make ("mfvideosrc", name);

  g_object_set (elem, "device-path", self->device_path, NULL);

  return elem;
}

static void
gst_mf_device_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstMFDevice *self = GST_MF_DEVICE (object);

  switch (prop_id) {
    case PROP_DEVICE_PATH:
      g_value_set_string (value, self->device_path);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_mf_device_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstMFDevice *self = GST_MF_DEVICE (object);

  switch (prop_id) {
    case PROP_DEVICE_PATH:
      self->device_path = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

struct _GstMFDeviceProvider
{
  GstDeviceProvider parent;
};

G_DEFINE_TYPE (GstMFDeviceProvider, gst_mf_device_provider,
    GST_TYPE_DEVICE_PROVIDER);

static GList *gst_mf_device_provider_probe (GstDeviceProvider * provider);

static void
gst_mf_device_provider_class_init (GstMFDeviceProviderClass * klass)
{
  GstDeviceProviderClass *provider_class = GST_DEVICE_PROVIDER_CLASS (klass);

  provider_class->probe = GST_DEBUG_FUNCPTR (gst_mf_device_provider_probe);

  gst_device_provider_class_set_static_metadata (provider_class,
      "Media Foundation Device Provider",
      "Source/Video", "List Media Foundation source devices",
      "Seungha Yang <seungha@centricular.com>");
}

static void
gst_mf_device_provider_init (GstMFDeviceProvider * provider)
{
}

static GList *
gst_mf_device_provider_probe (GstDeviceProvider * provider)
{
  GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider);
  GList *list = NULL;
  gint i;

  for (i = 0;; i++) {
    GstMFSourceObject *obj = NULL;
    GstDevice *device;
    GstStructure *props = NULL;
    GstCaps *caps = NULL;
    gchar *device_name = NULL;
    gchar *device_path = NULL;

#if GST_MF_WINAPI_ONLY_APP
    obj = gst_mf_capture_winrt_new (GST_MF_SOURCE_TYPE_VIDEO, i, NULL, NULL);
#else /* !GST_MF_WINAPI_ONLY_APP */
#if GST_MF_HAVE_CAPTURE_ENGINE
    if (!obj)
      obj = gst_mf_capture_engine_new (GST_MF_SOURCE_TYPE_VIDEO, i, NULL, NULL);
#endif /* GST_MF_HAVE_CAPTURE_ENGINE */
    if (!obj)
      obj = gst_mf_source_reader_new (GST_MF_SOURCE_TYPE_VIDEO, i, NULL, NULL);
#endif /* GST_MF_WINAPI_ONLY_APP  */

    if (!obj)
      break;

    caps = gst_mf_source_object_get_caps (obj);
    if (!caps) {
      GST_WARNING_OBJECT (self, "Empty caps for device index %d", i);
      goto next;
    }

    g_object_get (obj,
        "device-path", &device_path, "device-name", &device_name, NULL);

    if (!device_path) {
      GST_WARNING_OBJECT (self, "Device path is unavailable");
      goto next;
    }

    if (!device_name) {
      GST_WARNING_OBJECT (self, "Device name is unavailable");
      goto next;
    }

    props = gst_structure_new ("mf-proplist",
        "device.api", G_TYPE_STRING, "mediafoundation",
        "device.path", G_TYPE_STRING, device_path,
        "device.name", G_TYPE_STRING, device_name, NULL);

    device = g_object_new (GST_TYPE_MF_DEVICE, "device-path", device_path,
        "display-name", device_name, "caps", caps,
        "device-class", "Source/Video", "properties", props, NULL);

    list = g_list_append (list, device);

  next:
    if (caps)
      gst_caps_unref (caps);
    if (props)
      gst_structure_free (props);
    g_free (device_path);
    g_free (device_name);
    gst_object_unref (obj);
  }

  return list;
}