| 1 |
|
| 2 |
|
| 3 |
m> |
| 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 |
#ifdef HAVE_CONFIG_H |
| 30 |
#include "config.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "gstvautils.h" |
| 34 |
|
| 35 |
#ifdef G_OS_WIN32 |
| 36 |
#include "gstvadisplay_win32.h" |
| 37 |
#else |
| 38 |
#include "gstvadisplay_drm.h" |
| 39 |
#endif |
| 40 |
#include "gstvadisplay_wrapped.h" |
| 41 |
|
| 42 |
GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT); |
| 43 |
|
| 44 |
static void |
| 45 |
_init_context_debug (void) |
| 46 |
{ |
| 47 |
#ifndef GST_DISABLE_GST_DEBUG |
| 48 |
static gsize _init = 0; |
| 49 |
|
| 50 |
if (g_once_init_enter (&_init)) { |
| 51 |
GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT"); |
| 52 |
g_once_init_leave (&_init, 1); |
| 53 |
} |
| 54 |
#endif |
| 55 |
} |
| 56 |
|
| 57 |
static gboolean |
| 58 |
gst_va_display_found (GstElement * element, GstVaDisplay * display) |
| 59 |
{ |
| 60 |
if (display) { |
| 61 |
GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element, "already have a display (%p)", |
| 62 |
display); |
| 63 |
return TRUE; |
| 64 |
} |
| 65 |
|
| 66 |
return FALSE; |
| 67 |
} |
| 68 |
|
| 69 |
static gboolean |
| 70 |
pad_query (const GValue * item, GValue * value, gpointer user_data) |
| 71 |
{ |
| 72 |
GstPad *pad = g_value_get_object (item); |
| 73 |
GstQuery *query = user_data; |
| 74 |
gboolean res; |
| 75 |
|
| 76 |
res = gst_pad_peer_query (pad, query); |
| 77 |
|
| 78 |
if (res) { |
| 79 |
g_value_set_boolean (value, TRUE); |
| 80 |
return FALSE; |
| 81 |
} |
| 82 |
|
| 83 |
GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed"); |
| 84 |
return TRUE; |
| 85 |
} |
| 86 |
|
| 87 |
static gboolean |
| 88 |
_gst_va_run_query (GstElement * element, GstQuery * query, |
| 89 |
GstPadDirection direction) |
| 90 |
{ |
| 91 |
GstIterator *it; |
| 92 |
GstIteratorFoldFunction func = pad_query; |
| 93 |
GValue res = G_VALUE_INIT; |
| 94 |
|
| 95 |
g_value_init (&res, G_TYPE_BOOLEAN); |
| 96 |
g_value_set_boolean (&res, FALSE); |
| 97 |
|
| 98 |
if (direction == GST_PAD_SRC) |
| 99 |
it = gst_element_iterate_src_pads (element); |
| 100 |
else |
| 101 |
it = gst_element_iterate_sink_pads (element); |
| 102 |
|
| 103 |
while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC) |
| 104 |
gst_iterator_resync (it); |
| 105 |
|
| 106 |
gst_iterator_free (it); |
| 107 |
|
| 108 |
return g_value_get_boolean (&res); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
void |
| 121 |
gst_va_context_query (GstElement * element, const gchar * context_type) |
| 122 |
{ |
| 123 |
GstQuery *query; |
| 124 |
GstContext *ctxt = NULL; |
| 125 |
|
| 126 |
_init_context_debug (); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
query = gst_query_new_context (context_type); |
| 133 |
if (_gst_va_run_query (element, query, GST_PAD_SRC)) { |
| 134 |
gst_query_parse_context (query, &ctxt); |
| 135 |
GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, |
| 136 |
"found context (%p) in downstream query", ctxt); |
| 137 |
gst_element_set_context (element, ctxt); |
| 138 |
} else if (_gst_va_run_query (element, query, GST_PAD_SINK)) { |
| 139 |
gst_query_parse_context (query, &ctxt); |
| 140 |
GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, |
| 141 |
"found context (%p) in upstream query", ctxt); |
| 142 |
gst_element_set_context (element, ctxt); |
| 143 |
} else { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
GstMessage *msg; |
| 151 |
|
| 152 |
GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, |
| 153 |
"posting need context message"); |
| 154 |
msg = gst_message_new_need_context (GST_OBJECT_CAST (element), |
| 155 |
context_type); |
| 156 |
gst_element_post_message (element, msg); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
gst_query_unref (query); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
void |
| 178 |
gst_va_element_propagate_display_context (GstElement * element, |
| 179 |
GstVaDisplay * display) |
| 180 |
{ |
| 181 |
GstContext *ctxt; |
| 182 |
GstMessage *msg; |
| 183 |
|
| 184 |
_init_context_debug (); |
| 185 |
|
| 186 |
if (!display) { |
| 187 |
GST_ERROR_OBJECT (element, "Could not get VA display connection"); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
ctxt = gst_context_new (GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR, TRUE); |
| 195 |
gst_context_set_va_display (ctxt, display); |
| 196 |
|
| 197 |
GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, |
| 198 |
"post have context (%p) message with display (%p)", ctxt, display); |
| 199 |
msg = gst_message_new_have_context (GST_OBJECT_CAST (element), ctxt); |
| 200 |
gst_element_post_message (element, msg); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
gboolean |
| 216 |
gst_va_ensure_element_data (gpointer element, const gchar * render_device_path, |
| 217 |
GstVaDisplay ** display_ptr) |
| 218 |
{ |
| 219 |
GstVaDisplay *display; |
| 220 |
|
| 221 |
_init_context_debug (); |
| 222 |
|
| 223 |
g_return_val_if_fail (element, FALSE); |
| 224 |
g_return_val_if_fail (render_device_path, FALSE); |
| 225 |
g_return_val_if_fail (display_ptr, FALSE); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if (gst_va_display_found (element, g_atomic_pointer_get (display_ptr))) |
| 231 |
goto done; |
| 232 |
|
| 233 |
gst_va_context_query (element, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR); |
| 234 |
|
| 235 |
|
| 236 |
if (gst_va_display_found (element, g_atomic_pointer_get (display_ptr))) |
| 237 |
goto done; |
| 238 |
|
| 239 |
#ifdef G_OS_WIN32 |
| 240 |
display = gst_va_display_win32_new (render_device_path); |
| 241 |
#else |
| 242 |
|
| 243 |
display = gst_va_display_drm_new_from_path (render_device_path); |
| 244 |
#endif |
| 245 |
|
| 246 |
gst_object_replace ((GstObject **) display_ptr, (GstObject *) display); |
| 247 |
|
| 248 |
gst_va_element_propagate_display_context (element, display); |
| 249 |
|
| 250 |
gst_clear_object (&display); |
| 251 |
|
| 252 |
done: |
| 253 |
return g_atomic_pointer_get (display_ptr) != NULL; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
gboolean |
| 272 |
gst_va_handle_set_context (GstElement * element, GstContext * context, |
| 273 |
const gchar * render_device_path, GstVaDisplay ** display_ptr) |
| 274 |
{ |
| 275 |
GstVaDisplay *display_replacement = NULL; |
| 276 |
const gchar *context_type, *type_name; |
| 277 |
|
| 278 |
_init_context_debug (); |
| 279 |
|
| 280 |
g_return_val_if_fail (display_ptr, FALSE); |
| 281 |
|
| 282 |
if (!context) |
| 283 |
return FALSE; |
| 284 |
|
| 285 |
context_type = gst_context_get_context_type (context); |
| 286 |
|
| 287 |
if (g_strcmp0 (context_type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR) == 0) { |
| 288 |
type_name = G_OBJECT_TYPE_NAME (element); |
| 289 |
if (!gst_context_get_va_display (context, type_name, render_device_path, |
| 290 |
&display_replacement)) { |
| 291 |
GST_CAT_WARNING_OBJECT (GST_CAT_CONTEXT, element, |
| 292 |
"Failed to get display from context"); |
| 293 |
return FALSE; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if (display_replacement) { |
| 298 |
gst_object_replace ((GstObject **) display_ptr, |
| 299 |
(GstObject *) display_replacement); |
| 300 |
gst_object_unref (display_replacement); |
| 301 |
} |
| 302 |
|
| 303 |
return TRUE; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
gboolean |
| 320 |
gst_va_handle_context_query (GstElement * element, GstQuery * query, |
| 321 |
GstVaDisplay * display) |
| 322 |
{ |
| 323 |
const gchar *context_type; |
| 324 |
GstContext *ctxt, *old_ctxt; |
| 325 |
|
| 326 |
_init_context_debug (); |
| 327 |
|
| 328 |
g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE); |
| 329 |
g_return_val_if_fail (GST_IS_QUERY (query), FALSE); |
| 330 |
g_return_val_if_fail (!display || GST_IS_VA_DISPLAY (display), FALSE); |
| 331 |
|
| 332 |
GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element, |
| 333 |
"handle context query %" GST_PTR_FORMAT, query); |
| 334 |
gst_query_parse_context_type (query, &context_type); |
| 335 |
|
| 336 |
if (!display |
| 337 |
|| g_strcmp0 (context_type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR) != 0) |
| 338 |
return FALSE; |
| 339 |
|
| 340 |
gst_query_parse_context (query, &old_ctxt); |
| 341 |
|
| 342 |
if (old_ctxt) |
| 343 |
ctxt = gst_context_copy (old_ctxt); |
| 344 |
else |
| 345 |
ctxt = gst_context_new (GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR, TRUE); |
| 346 |
|
| 347 |
gst_context_set_va_display (ctxt, display); |
| 348 |
gst_query_set_context (query, ctxt); |
| 349 |
gst_context_unref (ctxt); |
| 350 |
GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element, |
| 351 |
"successfully %" GST_PTR_FORMAT " on %" GST_PTR_FORMAT, display, query); |
| 352 |
|
| 353 |
return TRUE; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
gboolean |
| 368 |
gst_context_get_va_display (GstContext * context, const gchar * type_name, |
| 369 |
const gchar * render_device_path, GstVaDisplay ** display_ptr) |
| 370 |
{ |
| 371 |
const GstStructure *s; |
| 372 |
GstVaDisplay *display = NULL; |
| 373 |
gpointer dpy; |
| 374 |
gboolean is_devnode; |
| 375 |
|
| 376 |
_init_context_debug (); |
| 377 |
|
| 378 |
g_return_val_if_fail (display_ptr, FALSE); |
| 379 |
g_return_val_if_fail (context, FALSE); |
| 380 |
|
| 381 |
is_devnode = (g_strstr_len (type_name, -1, "renderD") != NULL); |
| 382 |
|
| 383 |
s = gst_context_get_structure (context); |
| 384 |
if (gst_structure_get (s, "gst-display", GST_TYPE_OBJECT, &display, NULL)) { |
| 385 |
gboolean ret; |
| 386 |
gchar *device_path = NULL; |
| 387 |
#ifdef G_OS_WIN32 |
| 388 |
if (GST_IS_VA_DISPLAY_WIN32 (display)) { |
| 389 |
#else |
| 390 |
if (GST_IS_VA_DISPLAY_DRM (display)) { |
| 391 |
#endif |
| 392 |
g_object_get (display, "path", &device_path, NULL); |
| 393 |
ret = (g_strcmp0 (device_path, render_device_path) == 0); |
| 394 |
g_free (device_path); |
| 395 |
if (ret) |
| 396 |
goto accept; |
| 397 |
} else if (GST_IS_VA_DISPLAY (display) && !is_devnode) { |
| 398 |
goto accept; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
gst_clear_object (&display); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
if (!is_devnode |
| 408 |
&& gst_structure_get (s, "va-display", G_TYPE_POINTER, &dpy, NULL)) { |
| 409 |
if ((display = gst_va_display_wrapped_new (dpy))) |
| 410 |
goto accept; |
| 411 |
} |
| 412 |
|
| 413 |
GST_CAT_DEBUG (GST_CAT_CONTEXT, "No valid GstVaDisplay from context (%p)", |
| 414 |
context); |
| 415 |
return FALSE; |
| 416 |
|
| 417 |
accept: |
| 418 |
{ |
| 419 |
*display_ptr = display; |
| 420 |
|
| 421 |
GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVaDisplay (%p) from context (%p)", |
| 422 |
*display_ptr, context); |
| 423 |
return TRUE; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
void |
| 437 |
gst_context_set_va_display (GstContext * context, GstVaDisplay * display) |
| 438 |
{ |
| 439 |
GstStructure *s; |
| 440 |
|
| 441 |
_init_context_debug (); |
| 442 |
|
| 443 |
g_return_if_fail (context != NULL); |
| 444 |
|
| 445 |
s = gst_context_writable_structure (context); |
| 446 |
gst_structure_set (s, "gst-display", GST_TYPE_OBJECT, display, NULL); |
| 447 |
|
| 448 |
if (display) { |
| 449 |
GObjectClass *klass = G_OBJECT_GET_CLASS (display); |
| 450 |
gchar *vendor_desc = NULL; |
| 451 |
gchar *path = NULL; |
| 452 |
|
| 453 |
g_object_get (display, "description", &vendor_desc, NULL); |
| 454 |
if (g_object_class_find_property (klass, "path")) |
| 455 |
g_object_get (display, "path", &path, NULL); |
| 456 |
|
| 457 |
GST_CAT_LOG (GST_CAT_CONTEXT, |
| 458 |
"setting GstVaDisplay (%" GST_PTR_FORMAT ") on context (%" |
| 459 |
GST_PTR_FORMAT "), description: \"%s\", path: %s", display, context, |
| 460 |
GST_STR_NULL (vendor_desc), GST_STR_NULL (path)); |
| 461 |
|
| 462 |
if (vendor_desc) { |
| 463 |
gst_structure_set (s, "description", G_TYPE_STRING, vendor_desc, NULL); |
| 464 |
g_free (vendor_desc); |
| 465 |
} |
| 466 |
|
| 467 |
if (path) { |
| 468 |
gst_structure_set (s, "path", G_TYPE_STRING, path, NULL); |
| 469 |
g_free (path); |
| 470 |
} |
| 471 |
} |
| 472 |
} |