| 1 |
|
| 2 |
/ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#define PW_ENABLE_DEPRECATED |
| 17 |
|
| 18 |
#include "gstpipewiresrc.h" |
| 19 |
#include "gstpipewireformat.h" |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <fcntl.h> |
| 24 |
#include <sys/socket.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#include <spa/param/video/format.h> |
| 28 |
#include <spa/pod/iter.h> |
| 29 |
#include <spa/pod/builder.h> |
| 30 |
#include <spa/utils/result.h> |
| 31 |
|
| 32 |
#include <gst/net/gstnetclientclock.h> |
| 33 |
#include <gst/allocators/gstfdmemory.h> |
| 34 |
#include <gst/allocators/gstdmabuf.h> |
| 35 |
#include <gst/video/video.h> |
| 36 |
|
| 37 |
#include "gstpipewireclock.h" |
| 38 |
|
| 39 |
static GQuark process_mem_data_quark; |
| 40 |
|
| 41 |
GST_DEBUG_CATEGORY_STATIC (pipewire_src_debug); |
| 42 |
#define GST_CAT_DEFAULT pipewire_src_debug |
| 43 |
|
| 44 |
#define DEFAULT_ALWAYS_COPY false |
| 45 |
#define DEFAULT_MIN_BUFFERS 1 |
| 46 |
#define DEFAULT_MAX_BUFFERS INT32_MAX |
| 47 |
#define DEFAULT_RESEND_LAST false |
| 48 |
#define DEFAULT_KEEPALIVE_TIME 0 |
| 49 |
#define DEFAULT_AUTOCONNECT true |
| 50 |
#define DEFAULT_USE_BUFFERPOOL USE_BUFFERPOOL_AUTO |
| 51 |
#define DEFAULT_ON_DISCONNECT GST_PIPEWIRE_SRC_ON_DISCONNECT_NONE |
| 52 |
#define DEFAULT_PROVIDE_CLOCK TRUE |
| 53 |
|
| 54 |
enum |
| 55 |
{ |
| 56 |
PROP_0, |
| 57 |
PROP_PATH, |
| 58 |
PROP_TARGET_OBJECT, |
| 59 |
PROP_CLIENT_NAME, |
| 60 |
PROP_CLIENT_PROPERTIES, |
| 61 |
PROP_STREAM_PROPERTIES, |
| 62 |
PROP_ALWAYS_COPY, |
| 63 |
PROP_MIN_BUFFERS, |
| 64 |
PROP_MAX_BUFFERS, |
| 65 |
PROP_FD, |
| 66 |
PROP_RESEND_LAST, |
| 67 |
PROP_KEEPALIVE_TIME, |
| 68 |
PROP_AUTOCONNECT, |
| 69 |
PROP_USE_BUFFERPOOL, |
| 70 |
PROP_ON_DISCONNECT, |
| 71 |
PROP_PROVIDE_CLOCK, |
| 72 |
}; |
| 73 |
|
| 74 |
GType |
| 75 |
gst_pipewire_src_on_disconnect_get_type (void) |
| 76 |
{ |
| 77 |
static gsize on_disconnect_type = 0; |
| 78 |
static const GEnumValue on_disconnect[] = { |
| 79 |
{GST_PIPEWIRE_SRC_ON_DISCONNECT_NONE, "GST_PIPEWIRE_SRC_ON_DISCONNECT_NONE", "none"}, |
| 80 |
{GST_PIPEWIRE_SRC_ON_DISCONNECT_EOS, "GST_PIPEWIRE_SRC_ON_DISCONNECT_EOS", "eos"}, |
| 81 |
{GST_PIPEWIRE_SRC_ON_DISCONNECT_ERROR, "GST_PIPEWIRE_SRC_ON_DISCONNECT_ERROR", "error"}, |
| 82 |
{0, NULL, NULL}, |
| 83 |
}; |
| 84 |
|
| 85 |
if (g_once_init_enter (&on_disconnect_type)) { |
| 86 |
GType tmp = |
| 87 |
g_enum_register_static ("GstPipeWireSrcOnDisconnect", on_disconnect); |
| 88 |
g_once_init_leave (&on_disconnect_type, tmp); |
| 89 |
} |
| 90 |
|
| 91 |
return (GType) on_disconnect_type; |
| 92 |
} |
| 93 |
|
| 94 |
static GstStaticPadTemplate gst_pipewire_src_template = |
| 95 |
GST_STATIC_PAD_TEMPLATE ("src", |
| 96 |
GST_PAD_SRC, |
| 97 |
GST_PAD_ALWAYS, |
| 98 |
GST_STATIC_CAPS_ANY |
| 99 |
); |
| 100 |
|
| 101 |
#define gst_pipewire_src_parent_class parent_class |
| 102 |
G_DEFINE_TYPE (GstPipeWireSrc, gst_pipewire_src, GST_TYPE_PUSH_SRC); |
| 103 |
|
| 104 |
static GstStateChangeReturn |
| 105 |
gst_pipewire_src_change_state (GstElement * element, GstStateChange transition); |
| 106 |
|
| 107 |
static gboolean gst_pipewire_src_send_event (GstElement * elem, GstEvent * event); |
| 108 |
|
| 109 |
static gboolean gst_pipewire_src_negotiate (GstBaseSrc * basesrc); |
| 110 |
|
| 111 |
static GstFlowReturn gst_pipewire_src_create (GstPushSrc * psrc, |
| 112 |
GstBuffer ** buffer); |
| 113 |
static gboolean gst_pipewire_src_unlock (GstBaseSrc * basesrc); |
| 114 |
static gboolean gst_pipewire_src_unlock_stop (GstBaseSrc * basesrc); |
| 115 |
static gboolean gst_pipewire_src_start (GstBaseSrc * basesrc); |
| 116 |
static gboolean gst_pipewire_src_stop (GstBaseSrc * basesrc); |
| 117 |
static gboolean gst_pipewire_src_event (GstBaseSrc * src, GstEvent * event); |
| 118 |
static gboolean gst_pipewire_src_query (GstBaseSrc * src, GstQuery * query); |
| 119 |
static void gst_pipewire_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer, |
| 120 |
GstClockTime * start, GstClockTime * end); |
| 121 |
|
| 122 |
static void |
| 123 |
gst_pipewire_src_set_property (GObject * object, guint prop_id, |
| 124 |
const GValue * value, GParamSpec * pspec) |
| 125 |
{ |
| 126 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (object); |
| 127 |
|
| 128 |
switch (prop_id) { |
| 129 |
case PROP_PATH: |
| 130 |
g_free (pwsrc->stream->path); |
| 131 |
pwsrc->stream->path = g_value_dup_string (value); |
| 132 |
break; |
| 133 |
|
| 134 |
case PROP_TARGET_OBJECT: |
| 135 |
g_free (pwsrc->stream->target_object); |
| 136 |
pwsrc->stream->target_object = g_value_dup_string (value); |
| 137 |
break; |
| 138 |
|
| 139 |
case PROP_CLIENT_NAME: |
| 140 |
g_free (pwsrc->stream->client_name); |
| 141 |
pwsrc->stream->client_name = g_value_dup_string (value); |
| 142 |
break; |
| 143 |
|
| 144 |
case PROP_CLIENT_PROPERTIES: |
| 145 |
if (pwsrc->stream->client_properties) |
| 146 |
gst_structure_free (pwsrc->stream->client_properties); |
| 147 |
pwsrc->stream->client_properties = |
| 148 |
gst_structure_copy (gst_value_get_structure (value)); |
| 149 |
break; |
| 150 |
|
| 151 |
case PROP_STREAM_PROPERTIES: |
| 152 |
if (pwsrc->stream->stream_properties) |
| 153 |
gst_structure_free (pwsrc->stream->stream_properties); |
| 154 |
pwsrc->stream->stream_properties = |
| 155 |
gst_structure_copy (gst_value_get_structure (value)); |
| 156 |
break; |
| 157 |
|
| 158 |
case PROP_ALWAYS_COPY: |
| 159 |
|
| 160 |
if (g_value_get_boolean (value)) |
| 161 |
pwsrc->use_bufferpool = USE_BUFFERPOOL_NO; |
| 162 |
else |
| 163 |
pwsrc->use_bufferpool = USE_BUFFERPOOL_YES; |
| 164 |
break; |
| 165 |
|
| 166 |
case PROP_MIN_BUFFERS: |
| 167 |
pwsrc->min_buffers = g_value_get_int (value); |
| 168 |
break; |
| 169 |
|
| 170 |
case PROP_MAX_BUFFERS: |
| 171 |
pwsrc->max_buffers = g_value_get_int (value); |
| 172 |
break; |
| 173 |
|
| 174 |
case PROP_FD: |
| 175 |
pwsrc->stream->fd = g_value_get_int (value); |
| 176 |
break; |
| 177 |
|
| 178 |
case PROP_RESEND_LAST: |
| 179 |
pwsrc->resend_last = g_value_get_boolean (value); |
| 180 |
break; |
| 181 |
|
| 182 |
case PROP_KEEPALIVE_TIME: |
| 183 |
pwsrc->keepalive_time = g_value_get_int (value); |
| 184 |
break; |
| 185 |
|
| 186 |
case PROP_AUTOCONNECT: |
| 187 |
pwsrc->autoconnect = g_value_get_boolean (value); |
| 188 |
break; |
| 189 |
|
| 190 |
case PROP_USE_BUFFERPOOL: |
| 191 |
if(g_value_get_boolean (value)) |
| 192 |
pwsrc->use_bufferpool = USE_BUFFERPOOL_YES; |
| 193 |
else |
| 194 |
pwsrc->use_bufferpool = USE_BUFFERPOOL_NO; |
| 195 |
break; |
| 196 |
|
| 197 |
case PROP_ON_DISCONNECT: |
| 198 |
pwsrc->on_disconnect = g_value_get_enum (value); |
| 199 |
break; |
| 200 |
|
| 201 |
case PROP_PROVIDE_CLOCK: |
| 202 |
gboolean provide = g_value_get_boolean (value); |
| 203 |
GST_OBJECT_LOCK (pwsrc); |
| 204 |
if (provide) |
| 205 |
GST_OBJECT_FLAG_SET (pwsrc, GST_ELEMENT_FLAG_PROVIDE_CLOCK); |
| 206 |
else |
| 207 |
GST_OBJECT_FLAG_UNSET (pwsrc, GST_ELEMENT_FLAG_PROVIDE_CLOCK); |
| 208 |
GST_OBJECT_UNLOCK (pwsrc); |
| 209 |
break; |
| 210 |
|
| 211 |
default: |
| 212 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
static void |
| 218 |
gst_pipewire_src_get_property (GObject * object, guint prop_id, |
| 219 |
GValue * value, GParamSpec * pspec) |
| 220 |
{ |
| 221 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (object); |
| 222 |
|
| 223 |
switch (prop_id) { |
| 224 |
case PROP_PATH: |
| 225 |
g_value_set_string (value, pwsrc->stream->path); |
| 226 |
break; |
| 227 |
|
| 228 |
case PROP_TARGET_OBJECT: |
| 229 |
g_value_set_string (value, pwsrc->stream->target_object); |
| 230 |
break; |
| 231 |
|
| 232 |
case PROP_CLIENT_NAME: |
| 233 |
g_value_set_string (value, pwsrc->stream->client_name); |
| 234 |
break; |
| 235 |
|
| 236 |
case PROP_CLIENT_PROPERTIES: |
| 237 |
gst_value_set_structure (value, pwsrc->stream->client_properties); |
| 238 |
break; |
| 239 |
|
| 240 |
case PROP_STREAM_PROPERTIES: |
| 241 |
gst_value_set_structure (value, pwsrc->stream->stream_properties); |
| 242 |
break; |
| 243 |
|
| 244 |
case PROP_ALWAYS_COPY: |
| 245 |
g_value_set_boolean (value, !pwsrc->use_bufferpool); |
| 246 |
break; |
| 247 |
|
| 248 |
case PROP_MIN_BUFFERS: |
| 249 |
g_value_set_int (value, pwsrc->min_buffers); |
| 250 |
break; |
| 251 |
|
| 252 |
case PROP_MAX_BUFFERS: |
| 253 |
g_value_set_int (value, pwsrc->max_buffers); |
| 254 |
break; |
| 255 |
|
| 256 |
case PROP_FD: |
| 257 |
g_value_set_int (value, pwsrc->stream->fd); |
| 258 |
break; |
| 259 |
|
| 260 |
case PROP_RESEND_LAST: |
| 261 |
g_value_set_boolean (value, pwsrc->resend_last); |
| 262 |
break; |
| 263 |
|
| 264 |
case PROP_KEEPALIVE_TIME: |
| 265 |
g_value_set_int (value, pwsrc->keepalive_time); |
| 266 |
break; |
| 267 |
|
| 268 |
case PROP_AUTOCONNECT: |
| 269 |
g_value_set_boolean (value, pwsrc->autoconnect); |
| 270 |
break; |
| 271 |
|
| 272 |
case PROP_USE_BUFFERPOOL: |
| 273 |
g_value_set_boolean (value, !!pwsrc->use_bufferpool); |
| 274 |
break; |
| 275 |
|
| 276 |
case PROP_ON_DISCONNECT: |
| 277 |
g_value_set_enum (value, pwsrc->on_disconnect); |
| 278 |
break; |
| 279 |
|
| 280 |
case PROP_PROVIDE_CLOCK: |
| 281 |
gboolean result; |
| 282 |
GST_OBJECT_LOCK (pwsrc); |
| 283 |
result = GST_OBJECT_FLAG_IS_SET (pwsrc, GST_ELEMENT_FLAG_PROVIDE_CLOCK); |
| 284 |
GST_OBJECT_UNLOCK (pwsrc); |
| 285 |
g_value_set_boolean (value, result); |
| 286 |
break; |
| 287 |
|
| 288 |
default: |
| 289 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
static GstClock * |
| 295 |
gst_pipewire_src_provide_clock (GstElement * elem) |
| 296 |
{ |
| 297 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (elem); |
| 298 |
GstClock *clock; |
| 299 |
|
| 300 |
GST_OBJECT_LOCK (pwsrc); |
| 301 |
if (!GST_OBJECT_FLAG_IS_SET (pwsrc, GST_ELEMENT_FLAG_PROVIDE_CLOCK)) |
| 302 |
goto clock_disabled; |
| 303 |
|
| 304 |
if (pwsrc->stream->clock && pwsrc->is_live) |
| 305 |
clock = GST_CLOCK_CAST (gst_object_ref (pwsrc->stream->clock)); |
| 306 |
else |
| 307 |
clock = NULL; |
| 308 |
GST_OBJECT_UNLOCK (pwsrc); |
| 309 |
|
| 310 |
return clock; |
| 311 |
|
| 312 |
|
| 313 |
clock_disabled: |
| 314 |
{ |
| 315 |
GST_DEBUG_OBJECT (pwsrc, "clock provide disabled"); |
| 316 |
GST_OBJECT_UNLOCK (pwsrc); |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static void |
| 322 |
gst_pipewire_src_finalize (GObject * object) |
| 323 |
{ |
| 324 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (object); |
| 325 |
|
| 326 |
gst_clear_object (&pwsrc->stream); |
| 327 |
|
| 328 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 329 |
} |
| 330 |
|
| 331 |
static void |
| 332 |
gst_pipewire_src_class_init (GstPipeWireSrcClass * klass) |
| 333 |
{ |
| 334 |
GObjectClass *gobject_class; |
| 335 |
GstElementClass *gstelement_class; |
| 336 |
GstBaseSrcClass *gstbasesrc_class; |
| 337 |
GstPushSrcClass *gstpushsrc_class; |
| 338 |
|
| 339 |
gobject_class = (GObjectClass *) klass; |
| 340 |
gstelement_class = (GstElementClass *) klass; |
| 341 |
gstbasesrc_class = (GstBaseSrcClass *) klass; |
| 342 |
gstpushsrc_class = (GstPushSrcClass *) klass; |
| 343 |
|
| 344 |
gobject_class->finalize = gst_pipewire_src_finalize; |
| 345 |
gobject_class->set_property = gst_pipewire_src_set_property; |
| 346 |
gobject_class->get_property = gst_pipewire_src_get_property; |
| 347 |
|
| 348 |
g_object_class_install_property (gobject_class, |
| 349 |
PROP_PATH, |
| 350 |
g_param_spec_string ("path", |
| 351 |
"Path", |
| 352 |
"The source path to connect to (NULL = default)", |
| 353 |
NULL, |
| 354 |
G_PARAM_READWRITE | |
| 355 |
G_PARAM_STATIC_STRINGS | |
| 356 |
G_PARAM_DEPRECATED)); |
| 357 |
|
| 358 |
g_object_class_install_property (gobject_class, |
| 359 |
PROP_TARGET_OBJECT, |
| 360 |
g_param_spec_string ("target-object", |
| 361 |
"Target object", |
| 362 |
"The source name/serial to connect to (NULL = default)", |
| 363 |
NULL, |
| 364 |
G_PARAM_READWRITE | |
| 365 |
G_PARAM_STATIC_STRINGS)); |
| 366 |
|
| 367 |
g_object_class_install_property (gobject_class, |
| 368 |
PROP_CLIENT_NAME, |
| 369 |
g_param_spec_string ("client-name", |
| 370 |
"Client Name", |
| 371 |
"The client name to use (NULL = default)", |
| 372 |
NULL, |
| 373 |
G_PARAM_READWRITE | |
| 374 |
G_PARAM_STATIC_STRINGS)); |
| 375 |
|
| 376 |
g_object_class_install_property (gobject_class, |
| 377 |
PROP_CLIENT_PROPERTIES, |
| 378 |
g_param_spec_boxed ("client-properties", |
| 379 |
"client properties", |
| 380 |
"list of PipeWire client properties", |
| 381 |
GST_TYPE_STRUCTURE, |
| 382 |
G_PARAM_READWRITE | |
| 383 |
G_PARAM_STATIC_STRINGS)); |
| 384 |
|
| 385 |
g_object_class_install_property (gobject_class, |
| 386 |
PROP_STREAM_PROPERTIES, |
| 387 |
g_param_spec_boxed ("stream-properties", |
| 388 |
"stream properties", |
| 389 |
"list of PipeWire stream properties", |
| 390 |
GST_TYPE_STRUCTURE, |
| 391 |
G_PARAM_READWRITE | |
| 392 |
G_PARAM_STATIC_STRINGS)); |
| 393 |
|
| 394 |
g_object_class_install_property (gobject_class, |
| 395 |
PROP_ALWAYS_COPY, |
| 396 |
g_param_spec_boolean ("always-copy", |
| 397 |
"Always copy", |
| 398 |
"Always copy the buffer and data", |
| 399 |
DEFAULT_ALWAYS_COPY, |
| 400 |
G_PARAM_READWRITE | |
| 401 |
G_PARAM_STATIC_STRINGS | |
| 402 |
G_PARAM_DEPRECATED)); |
| 403 |
|
| 404 |
g_object_class_install_property (gobject_class, |
| 405 |
PROP_MIN_BUFFERS, |
| 406 |
g_param_spec_int ("min-buffers", |
| 407 |
"Min Buffers", |
| 408 |
"Minimum number of buffers to negotiate with PipeWire", |
| 409 |
1, G_MAXINT, DEFAULT_MIN_BUFFERS, |
| 410 |
G_PARAM_READWRITE | |
| 411 |
G_PARAM_STATIC_STRINGS)); |
| 412 |
|
| 413 |
g_object_class_install_property (gobject_class, |
| 414 |
PROP_MAX_BUFFERS, |
| 415 |
g_param_spec_int ("max-buffers", |
| 416 |
"Max Buffers", |
| 417 |
"Maximum number of buffers to negotiate with PipeWire", |
| 418 |
1, G_MAXINT, DEFAULT_MAX_BUFFERS, |
| 419 |
G_PARAM_READWRITE | |
| 420 |
G_PARAM_STATIC_STRINGS)); |
| 421 |
|
| 422 |
g_object_class_install_property (gobject_class, |
| 423 |
PROP_FD, |
| 424 |
g_param_spec_int ("fd", |
| 425 |
"Fd", |
| 426 |
"The fd to connect with", |
| 427 |
-1, G_MAXINT, -1, |
| 428 |
G_PARAM_READWRITE | |
| 429 |
G_PARAM_STATIC_STRINGS)); |
| 430 |
|
| 431 |
g_object_class_install_property (gobject_class, |
| 432 |
PROP_RESEND_LAST, |
| 433 |
g_param_spec_boolean ("resend-last", |
| 434 |
"Resend last", |
| 435 |
"Resend last buffer on EOS", |
| 436 |
DEFAULT_RESEND_LAST, |
| 437 |
G_PARAM_READWRITE | |
| 438 |
G_PARAM_STATIC_STRINGS)); |
| 439 |
|
| 440 |
g_object_class_install_property (gobject_class, |
| 441 |
PROP_KEEPALIVE_TIME, |
| 442 |
g_param_spec_int ("keepalive-time", |
| 443 |
"Keepalive Time", |
| 444 |
"Periodically send last buffer (in milliseconds, 0 = disabled)", |
| 445 |
0, G_MAXINT, DEFAULT_KEEPALIVE_TIME, |
| 446 |
G_PARAM_READWRITE | |
| 447 |
G_PARAM_STATIC_STRINGS)); |
| 448 |
|
| 449 |
g_object_class_install_property (gobject_class, |
| 450 |
PROP_AUTOCONNECT, |
| 451 |
g_param_spec_boolean ("autoconnect", |
| 452 |
"Connect automatically", |
| 453 |
"Attempt to find a peer to connect to", |
| 454 |
DEFAULT_AUTOCONNECT, |
| 455 |
G_PARAM_READWRITE | |
| 456 |
G_PARAM_STATIC_STRINGS)); |
| 457 |
|
| 458 |
g_object_class_install_property (gobject_class, |
| 459 |
PROP_USE_BUFFERPOOL, |
| 460 |
g_param_spec_boolean ("use-bufferpool", |
| 461 |
"Use bufferpool", |
| 462 |
"Use bufferpool (default: true for video, false for audio)", |
| 463 |
DEFAULT_USE_BUFFERPOOL, |
| 464 |
G_PARAM_READWRITE | |
| 465 |
G_PARAM_STATIC_STRINGS)); |
| 466 |
|
| 467 |
g_object_class_install_property (gobject_class, |
| 468 |
PROP_ON_DISCONNECT, |
| 469 |
g_param_spec_enum ("on-disconnect", |
| 470 |
"On disconnect", |
| 471 |
"Action to take on disconnect", |
| 472 |
GST_TYPE_PIPEWIRE_SRC_ON_DISCONNECT, |
| 473 |
DEFAULT_ON_DISCONNECT, |
| 474 |
G_PARAM_READWRITE | |
| 475 |
G_PARAM_STATIC_STRINGS)); |
| 476 |
|
| 477 |
g_object_class_install_property (gobject_class, |
| 478 |
PROP_PROVIDE_CLOCK, |
| 479 |
g_param_spec_boolean ("provide-clock", |
| 480 |
"Provide Clock", |
| 481 |
"Provide a clock to be used as the global pipeline clock", |
| 482 |
DEFAULT_PROVIDE_CLOCK, |
| 483 |
G_PARAM_READWRITE | |
| 484 |
G_PARAM_STATIC_STRINGS)); |
| 485 |
|
| 486 |
gstelement_class->provide_clock = gst_pipewire_src_provide_clock; |
| 487 |
gstelement_class->change_state = gst_pipewire_src_change_state; |
| 488 |
gstelement_class->send_event = gst_pipewire_src_send_event; |
| 489 |
|
| 490 |
gst_element_class_set_static_metadata (gstelement_class, |
| 491 |
"PipeWire source", "Source/Audio/Video", |
| 492 |
"Uses PipeWire to create audio/video", "Wim Taymans <[email protected]>"); |
| 493 |
|
| 494 |
gst_element_class_add_pad_template (gstelement_class, |
| 495 |
gst_static_pad_template_get (&gst_pipewire_src_template)); |
| 496 |
|
| 497 |
gstbasesrc_class->negotiate = gst_pipewire_src_negotiate; |
| 498 |
gstbasesrc_class->unlock = gst_pipewire_src_unlock; |
| 499 |
gstbasesrc_class->unlock_stop = gst_pipewire_src_unlock_stop; |
| 500 |
gstbasesrc_class->start = gst_pipewire_src_start; |
| 501 |
gstbasesrc_class->stop = gst_pipewire_src_stop; |
| 502 |
gstbasesrc_class->event = gst_pipewire_src_event; |
| 503 |
gstbasesrc_class->query = gst_pipewire_src_query; |
| 504 |
gstbasesrc_class->get_times = gst_pipewire_src_get_times; |
| 505 |
gstpushsrc_class->create = gst_pipewire_src_create; |
| 506 |
|
| 507 |
GST_DEBUG_CATEGORY_INIT (pipewire_src_debug, "pipewiresrc", 0, |
| 508 |
"PipeWire Source"); |
| 509 |
|
| 510 |
process_mem_data_quark = g_quark_from_static_string ("GstPipeWireSrcProcessMemQuark"); |
| 511 |
} |
| 512 |
|
| 513 |
static void |
| 514 |
gst_pipewire_src_init (GstPipeWireSrc * src) |
| 515 |
{ |
| 516 |
|
| 517 |
gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME); |
| 518 |
|
| 519 |
|
| 520 |
gst_base_src_set_live (GST_BASE_SRC (src), TRUE); |
| 521 |
|
| 522 |
GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_PROVIDE_CLOCK); |
| 523 |
|
| 524 |
src->stream = gst_pipewire_stream_new (GST_ELEMENT (src)); |
| 525 |
|
| 526 |
src->use_bufferpool = DEFAULT_USE_BUFFERPOOL; |
| 527 |
src->min_buffers = DEFAULT_MIN_BUFFERS; |
| 528 |
src->max_buffers = DEFAULT_MAX_BUFFERS; |
| 529 |
src->resend_last = DEFAULT_RESEND_LAST; |
| 530 |
src->keepalive_time = DEFAULT_KEEPALIVE_TIME; |
| 531 |
src->autoconnect = DEFAULT_AUTOCONNECT; |
| 532 |
src->min_latency = 0; |
| 533 |
src->max_latency = GST_CLOCK_TIME_NONE; |
| 534 |
src->last_buffer_clock_time = GST_CLOCK_TIME_NONE; |
| 535 |
src->n_buffers = 0; |
| 536 |
src->flushing_on_remove_buffer = FALSE; |
| 537 |
src->on_disconnect = DEFAULT_ON_DISCONNECT; |
| 538 |
|
| 539 |
src->transform_value = UINT32_MAX; |
| 540 |
} |
| 541 |
|
| 542 |
static gboolean |
| 543 |
buffer_recycle (GstMiniObject *obj) |
| 544 |
{ |
| 545 |
GstPipeWirePoolData *data = gst_pipewire_pool_get_data (GST_BUFFER_CAST(obj)); |
| 546 |
GstPipeWireSrc *src = data->owner; |
| 547 |
int res; |
| 548 |
|
| 549 |
if (src->flushing_on_remove_buffer) { |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
GstBuffer *buffer = GST_BUFFER_CAST(obj); |
| 561 |
GST_DEBUG_OBJECT (src, |
| 562 |
"flush-start initiated, skipping buffer recycle %p", buffer); |
| 563 |
return TRUE; |
| 564 |
} |
| 565 |
|
| 566 |
GST_OBJECT_LOCK (data->pool); |
| 567 |
if (!obj->dispose) { |
| 568 |
GST_OBJECT_UNLOCK (data->pool); |
| 569 |
return TRUE; |
| 570 |
} |
| 571 |
|
| 572 |
GST_BUFFER_FLAGS (obj) = data->flags; |
| 573 |
|
| 574 |
pw_thread_loop_lock (src->stream->core->loop); |
| 575 |
if (!obj->dispose) { |
| 576 |
pw_thread_loop_unlock (src->stream->core->loop); |
| 577 |
GST_OBJECT_UNLOCK (data->pool); |
| 578 |
return TRUE; |
| 579 |
} |
| 580 |
|
| 581 |
gst_mini_object_ref (obj); |
| 582 |
|
| 583 |
data->queued = TRUE; |
| 584 |
|
| 585 |
if ((res = pw_stream_queue_buffer (src->stream->pwstream, data->b)) < 0) |
| 586 |
GST_WARNING_OBJECT (src, "can't queue recycled buffer %p, %s", obj, spa_strerror(res)); |
| 587 |
else |
| 588 |
GST_LOG_OBJECT (src, "recycle buffer %p", obj); |
| 589 |
|
| 590 |
pw_thread_loop_unlock (src->stream->core->loop); |
| 591 |
|
| 592 |
GST_OBJECT_UNLOCK (data->pool); |
| 593 |
|
| 594 |
return FALSE; |
| 595 |
} |
| 596 |
|
| 597 |
static void |
| 598 |
on_add_buffer (void *_data, struct pw_buffer *b) |
| 599 |
{ |
| 600 |
GstPipeWireSrc *pwsrc = _data; |
| 601 |
GstPipeWirePoolData *data; |
| 602 |
|
| 603 |
gst_pipewire_pool_wrap_buffer (pwsrc->stream->pool, b); |
| 604 |
data = b->user_data; |
| 605 |
GST_DEBUG_OBJECT (pwsrc, "add buffer %p", data->buf); |
| 606 |
data->owner = pwsrc; |
| 607 |
data->queued = TRUE; |
| 608 |
GST_MINI_OBJECT_CAST (data->buf)->dispose = buffer_recycle; |
| 609 |
|
| 610 |
pwsrc->n_buffers++; |
| 611 |
} |
| 612 |
|
| 613 |
static void |
| 614 |
on_remove_buffer (void *_data, struct pw_buffer *b) |
| 615 |
{ |
| 616 |
GstPipeWireSrc *pwsrc = _data; |
| 617 |
GstPipeWirePoolData *data = b->user_data; |
| 618 |
GstBuffer *buf = data->buf; |
| 619 |
gboolean flush_on_remove; |
| 620 |
int res; |
| 621 |
|
| 622 |
GST_DEBUG_OBJECT (pwsrc, "remove buffer %p, queued: %d", |
| 623 |
buf, data->queued); |
| 624 |
|
| 625 |
GST_MINI_OBJECT_CAST (buf)->dispose = NULL; |
| 626 |
|
| 627 |
flush_on_remove = |
| 628 |
pwsrc->on_disconnect == GST_PIPEWIRE_SRC_ON_DISCONNECT_ERROR || |
| 629 |
pwsrc->on_disconnect == GST_PIPEWIRE_SRC_ON_DISCONNECT_EOS; |
| 630 |
|
| 631 |
if (flush_on_remove && !pwsrc->flushing_on_remove_buffer) { |
| 632 |
pwsrc->flushing_on_remove_buffer = TRUE; |
| 633 |
|
| 634 |
GST_DEBUG_OBJECT (pwsrc, "flush-start on remove buffer"); |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
gst_pad_push_event (GST_BASE_SRC_PAD (pwsrc), |
| 646 |
gst_event_new_flush_start ()); |
| 647 |
} |
| 648 |
|
| 649 |
if (data->queued) { |
| 650 |
gst_buffer_unref (buf); |
| 651 |
} else { |
| 652 |
if ((res = pw_stream_queue_buffer (pwsrc->stream->pwstream, b)) < 0) |
| 653 |
GST_WARNING_OBJECT (pwsrc, "can't queue removed buffer %p, %s", |
| 654 |
buf, spa_strerror(res)); |
| 655 |
else |
| 656 |
GST_DEBUG_OBJECT (pwsrc, "queued buffer %p", buf); |
| 657 |
} |
| 658 |
|
| 659 |
pwsrc->n_buffers--; |
| 660 |
|
| 661 |
if (pwsrc->n_buffers == 0) { |
| 662 |
GST_DEBUG_OBJECT (pwsrc, "removed all buffers"); |
| 663 |
|
| 664 |
pwsrc->flushing_on_remove_buffer = FALSE; |
| 665 |
|
| 666 |
switch (pwsrc->on_disconnect) { |
| 667 |
case GST_PIPEWIRE_SRC_ON_DISCONNECT_ERROR: |
| 668 |
GST_DEBUG_OBJECT (pwsrc, "flush-stop on removing all buffers"); |
| 669 |
gst_pad_push_event (GST_BASE_SRC_PAD (pwsrc), |
| 670 |
gst_event_new_flush_stop (FALSE)); |
| 671 |
|
| 672 |
GST_ELEMENT_ERROR (pwsrc, RESOURCE, NOT_FOUND, |
| 673 |
("all buffers have been removed"), |
| 674 |
("PipeWire link to remote node was destroyed")); |
| 675 |
break; |
| 676 |
case GST_PIPEWIRE_SRC_ON_DISCONNECT_EOS: |
| 677 |
GST_DEBUG_OBJECT (pwsrc, "flush-stop on removing all buffers"); |
| 678 |
gst_pad_push_event (GST_BASE_SRC_PAD (pwsrc), |
| 679 |
gst_event_new_flush_stop (FALSE)); |
| 680 |
|
| 681 |
GST_DEBUG_OBJECT (pwsrc, "sending eos downstream"); |
| 682 |
gst_pad_push_event (GST_BASE_SRC_PAD (pwsrc), |
| 683 |
gst_event_new_eos()); |
| 684 |
break; |
| 685 |
case GST_PIPEWIRE_SRC_ON_DISCONNECT_NONE: |
| 686 |
GST_DEBUG_OBJECT (pwsrc, "stream closed or removed"); |
| 687 |
break; |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
static const char * const transform_map[] = { |
| 693 |
[SPA_META_TRANSFORMATION_None] = "rotate-0", |
| 694 |
[SPA_META_TRANSFORMATION_90] = "rotate-90", |
| 695 |
[SPA_META_TRANSFORMATION_180] = "rotate-180", |
| 696 |
[SPA_META_TRANSFORMATION_270] = "rotate-270", |
| 697 |
[SPA_META_TRANSFORMATION_Flipped] = "flip-rotate-0", |
| 698 |
[SPA_META_TRANSFORMATION_Flipped90] = "flip-rotate-270", |
| 699 |
[SPA_META_TRANSFORMATION_Flipped180] = "flip-rotate-180", |
| 700 |
[SPA_META_TRANSFORMATION_Flipped270] = "flip-rotate-90", |
| 701 |
}; |
| 702 |
|
| 703 |
static const char *spa_transform_value_to_gst_image_orientation(uint32_t transform_value) |
| 704 |
{ |
| 705 |
if (transform_value >= SPA_N_ELEMENTS(transform_map)) |
| 706 |
transform_value = SPA_META_TRANSFORMATION_None; |
| 707 |
|
| 708 |
return transform_map[transform_value]; |
| 709 |
} |
| 710 |
|
| 711 |
static GstBuffer *dequeue_buffer(GstPipeWireSrc *pwsrc) |
| 712 |
{ |
| 713 |
struct pw_buffer *b; |
| 714 |
GstBuffer *buf; |
| 715 |
GstPipeWirePoolData *data; |
| 716 |
struct spa_meta_header *h; |
| 717 |
struct spa_meta_region *crop; |
| 718 |
enum spa_meta_videotransform_value transform_value; |
| 719 |
struct spa_meta_cursor *cursor; |
| 720 |
struct pw_time time; |
| 721 |
guint i; |
| 722 |
|
| 723 |
b = pw_stream_dequeue_buffer (pwsrc->stream->pwstream); |
| 724 |
if (b == NULL) |
| 725 |
return NULL; |
| 726 |
|
| 727 |
data = b->user_data; |
| 728 |
|
| 729 |
if (!GST_IS_BUFFER (data->buf)) { |
| 730 |
GST_ERROR_OBJECT (pwsrc, "stream buffer %p is missing", data->buf); |
| 731 |
return NULL; |
| 732 |
} |
| 733 |
|
| 734 |
if (!data->queued) { |
| 735 |
GST_ERROR_OBJECT (pwsrc, "buffer %p was not recycled", data->buf); |
| 736 |
return NULL; |
| 737 |
} |
| 738 |
|
| 739 |
pw_stream_get_time_n(pwsrc->stream->pwstream, &time, sizeof(time)); |
| 740 |
|
| 741 |
if (pwsrc->delay != time.delay && time.rate.denom != 0) { |
| 742 |
pwsrc->min_latency = time.delay * GST_SECOND * time.rate.num / time.rate.denom; |
| 743 |
GST_LOG_OBJECT (pwsrc, "latency changed %"PRIi64" -> %"PRIi64" %"PRIu64, |
| 744 |
pwsrc->delay, time.delay, pwsrc->min_latency); |
| 745 |
pwsrc->delay = time.delay; |
| 746 |
gst_element_post_message (GST_ELEMENT_CAST (pwsrc), |
| 747 |
gst_message_new_latency (GST_OBJECT_CAST (pwsrc))); |
| 748 |
} |
| 749 |
|
| 750 |
GST_LOG_OBJECT (pwsrc, "got new buffer %p", data->buf); |
| 751 |
|
| 752 |
buf = gst_buffer_new (); |
| 753 |
|
| 754 |
data->queued = FALSE; |
| 755 |
GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE; |
| 756 |
GST_BUFFER_DTS (buf) = GST_CLOCK_TIME_NONE; |
| 757 |
|
| 758 |
h = data->header; |
| 759 |
if (h) { |
| 760 |
GST_LOG_OBJECT (pwsrc, "pts %" G_GUINT64_FORMAT ", dts_offset %" G_GUINT64_FORMAT, h->pts, h->dts_offset); |
| 761 |
|
| 762 |
if (GST_CLOCK_TIME_IS_VALID (h->pts)) { |
| 763 |
GST_BUFFER_PTS (buf) = h->pts; |
| 764 |
if (GST_BUFFER_PTS (buf) + h->dts_offset > 0) |
| 765 |
GST_BUFFER_DTS (buf) = GST_BUFFER_PTS (buf) + h->dts_offset; |
| 766 |
} |
| 767 |
GST_BUFFER_OFFSET (buf) = h->seq; |
| 768 |
} else { |
| 769 |
GST_BUFFER_PTS (buf) = b->time - pwsrc->delay; |
| 770 |
GST_BUFFER_DTS (buf) = b->time - pwsrc->delay; |
| 771 |
} |
| 772 |
|
| 773 |
if (pwsrc->media_type == SPA_MEDIA_TYPE_video) { |
| 774 |
if (pwsrc->video_info.fps_n) { |
| 775 |
GST_BUFFER_DURATION (buf) = gst_util_uint64_scale (GST_SECOND, |
| 776 |
pwsrc->video_info.fps_d, pwsrc->video_info.fps_n); |
| 777 |
} |
| 778 |
} else { |
| 779 |
GST_BUFFER_DURATION (buf) = gst_util_uint64_scale (GST_SECOND, |
| 780 |
time.size * time.rate.num, time.rate.denom); |
| 781 |
} |
| 782 |
|
| 783 |
crop = data->crop; |
| 784 |
if (crop) { |
| 785 |
GstVideoCropMeta *meta = gst_buffer_add_video_crop_meta(buf); |
| 786 |
if (meta) { |
| 787 |
meta->x = crop->region.position.x; |
| 788 |
meta->y = crop->region.position.y; |
| 789 |
meta->width = crop->region.size.width; |
| 790 |
meta->height = crop->region.size.height; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
transform_value = data->videotransform ? data->videotransform->transform : |
| 795 |
SPA_META_TRANSFORMATION_None; |
| 796 |
if (transform_value != pwsrc->transform_value) { |
| 797 |
GstEvent *tag_event; |
| 798 |
const char* tag_string; |
| 799 |
|
| 800 |
tag_string = spa_transform_value_to_gst_image_orientation(transform_value); |
| 801 |
|
| 802 |
GST_LOG_OBJECT (pwsrc, "got new videotransform: %u / %s", |
| 803 |
transform_value, tag_string); |
| 804 |
|
| 805 |
tag_event = gst_event_new_tag(gst_tag_list_new(GST_TAG_IMAGE_ORIENTATION, |
| 806 |
tag_string, NULL)); |
| 807 |
gst_pad_push_event (GST_BASE_SRC_PAD (pwsrc), tag_event); |
| 808 |
|
| 809 |
pwsrc->transform_value = transform_value; |
| 810 |
} |
| 811 |
|
| 812 |
cursor = data->cursor; |
| 813 |
if (cursor && cursor->id != 0) { |
| 814 |
|
| 815 |
|
| 816 |
gst_buffer_add_video_region_of_interest_meta (buf, "cursor", cursor->position.x, cursor->position.y, 0, 0); |
| 817 |
} |
| 818 |
|
| 819 |
if (pwsrc->is_rawvideo) { |
| 820 |
GstVideoInfo *info = &pwsrc->video_info; |
| 821 |
uint32_t n_datas = b->buffer->n_datas; |
| 822 |
uint32_t n_planes = GST_VIDEO_INFO_N_PLANES (info); |
| 823 |
gsize video_size = 0; |
| 824 |
|
| 825 |
GstVideoMeta *meta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE, |
| 826 |
GST_VIDEO_INFO_FORMAT (info), |
| 827 |
GST_VIDEO_INFO_WIDTH (info), |
| 828 |
GST_VIDEO_INFO_HEIGHT (info), |
| 829 |
GST_VIDEO_INFO_N_PLANES (info), |
| 830 |
info->offset, |
| 831 |
info->stride); |
| 832 |
|
| 833 |
for (i = 0; i < MIN (n_datas, n_planes); i++) { |
| 834 |
struct spa_data *d = &b->buffer->datas[i]; |
| 835 |
|
| 836 |
|
| 837 |
meta->offset[i] = video_size; |
| 838 |
meta->stride[i] = d->chunk->stride; |
| 839 |
|
| 840 |
video_size += d->chunk->size; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
if (b->buffer->n_datas != gst_buffer_n_memory(data->buf)) { |
| 845 |
GST_ERROR_OBJECT(pwsrc, "n_datas != n_memory, (%d != %d)", b->buffer->n_datas, gst_buffer_n_memory(data->buf)); |
| 846 |
} |
| 847 |
|
| 848 |
for (i = 0; i < b->buffer->n_datas; i++) { |
| 849 |
struct spa_data *d = &b->buffer->datas[i]; |
| 850 |
|
| 851 |
if (d->chunk->size == 0) { |
| 852 |
|
| 853 |
GST_DEBUG_OBJECT(pwsrc, "Chunk size is 0, skipping"); |
| 854 |
continue; |
| 855 |
} |
| 856 |
|
| 857 |
GstMemory *pmem = gst_buffer_peek_memory (data->buf, i); |
| 858 |
if (pmem) { |
| 859 |
GstMemory *mem; |
| 860 |
if (pwsrc->use_bufferpool != USE_BUFFERPOOL_NO) |
| 861 |
mem = gst_memory_share (pmem, d->chunk->offset, d->chunk->size); |
| 862 |
else |
| 863 |
mem = gst_memory_copy (pmem, d->chunk->offset, d->chunk->size); |
| 864 |
gst_buffer_insert_memory (buf, i, mem); |
| 865 |
} |
| 866 |
if (d->chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) { |
| 867 |
GST_DEBUG_OBJECT(pwsrc, "Buffer corrupted"); |
| 868 |
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_CORRUPTED); |
| 869 |
} |
| 870 |
} |
| 871 |
if (pwsrc->use_bufferpool != USE_BUFFERPOOL_NO) |
| 872 |
gst_buffer_add_parent_buffer_meta (buf, data->buf); |
| 873 |
gst_buffer_unref (data->buf); |
| 874 |
|
| 875 |
if (gst_buffer_get_size(buf) == 0) |
| 876 |
{ |
| 877 |
GST_ERROR_OBJECT(pwsrc, "Buffer is empty, dropping this"); |
| 878 |
gst_buffer_unref(buf); |
| 879 |
buf = NULL; |
| 880 |
} |
| 881 |
|
| 882 |
return buf; |
| 883 |
} |
| 884 |
|
| 885 |
static void |
| 886 |
on_process (void *_data) |
| 887 |
{ |
| 888 |
GstPipeWireSrc *pwsrc = _data; |
| 889 |
pw_thread_loop_signal (pwsrc->stream->core->loop, FALSE); |
| 890 |
} |
| 891 |
|
| 892 |
static void |
| 893 |
on_state_changed (void *data, |
| 894 |
enum pw_stream_state old, |
| 895 |
enum pw_stream_state state, const char *error) |
| 896 |
{ |
| 897 |
GstPipeWireSrc *pwsrc = data; |
| 898 |
GstState current_state = GST_ELEMENT_CAST (pwsrc)->current_state; |
| 899 |
|
| 900 |
GST_DEBUG_OBJECT (pwsrc, "got stream state %s", pw_stream_state_as_string (state)); |
| 901 |
|
| 902 |
switch (state) { |
| 903 |
case PW_STREAM_STATE_UNCONNECTED: |
| 904 |
case PW_STREAM_STATE_CONNECTING: |
| 905 |
break; |
| 906 |
case PW_STREAM_STATE_PAUSED: |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
if (current_state == GST_STATE_PLAYING && pwsrc->media_type == SPA_MEDIA_TYPE_audio) |
| 917 |
gst_element_post_message (GST_ELEMENT_CAST (pwsrc), |
| 918 |
gst_message_new_clock_lost (GST_OBJECT_CAST (pwsrc), |
| 919 |
GST_CLOCK_CAST (pwsrc->stream->clock))); |
| 920 |
break; |
| 921 |
case PW_STREAM_STATE_STREAMING: |
| 922 |
break; |
| 923 |
case PW_STREAM_STATE_ERROR: |
| 924 |
|
| 925 |
|
| 926 |
if (pw_stream_get_state (pwsrc->stream->pwstream, NULL) != PW_STREAM_STATE_ERROR) |
| 927 |
pw_stream_set_error (pwsrc->stream->pwstream, -EPIPE, "%s", error); |
| 928 |
else |
| 929 |
GST_ELEMENT_ERROR (pwsrc, RESOURCE, FAILED, |
| 930 |
("stream error: %s", error), (NULL)); |
| 931 |
break; |
| 932 |
} |
| 933 |
pw_thread_loop_signal (pwsrc->stream->core->loop, FALSE); |
| 934 |
} |
| 935 |
|
| 936 |
static void |
| 937 |
parse_stream_properties (GstPipeWireSrc *pwsrc, const struct pw_properties *props) |
| 938 |
{ |
| 939 |
const gchar *var; |
| 940 |
gboolean is_live; |
| 941 |
|
| 942 |
GST_OBJECT_LOCK (pwsrc); |
| 943 |
var = pw_properties_get (props, PW_KEY_STREAM_IS_LIVE); |
| 944 |
is_live = pwsrc->is_live = var ? pw_properties_parse_bool(var) : TRUE; |
| 945 |
GST_OBJECT_UNLOCK (pwsrc); |
| 946 |
|
| 947 |
GST_DEBUG_OBJECT (pwsrc, "live %d", is_live); |
| 948 |
|
| 949 |
gst_base_src_set_live (GST_BASE_SRC (pwsrc), is_live); |
| 950 |
} |
| 951 |
|
| 952 |
static gboolean |
| 953 |
gst_pipewire_src_stream_start (GstPipeWireSrc *pwsrc) |
| 954 |
{ |
| 955 |
const char *error = NULL; |
| 956 |
struct timespec abstime; |
| 957 |
|
| 958 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 959 |
GST_DEBUG_OBJECT (pwsrc, "doing stream start"); |
| 960 |
|
| 961 |
pw_thread_loop_get_time (pwsrc->stream->core->loop, &abstime, |
| 962 |
GST_PIPEWIRE_DEFAULT_TIMEOUT * SPA_NSEC_PER_SEC); |
| 963 |
|
| 964 |
while (TRUE) { |
| 965 |
enum pw_stream_state state = pw_stream_get_state (pwsrc->stream->pwstream, &error); |
| 966 |
|
| 967 |
GST_DEBUG_OBJECT (pwsrc, "waiting for STREAMING, now %s", pw_stream_state_as_string (state)); |
| 968 |
if (state == PW_STREAM_STATE_STREAMING) |
| 969 |
break; |
| 970 |
|
| 971 |
if (state == PW_STREAM_STATE_ERROR) |
| 972 |
goto start_error; |
| 973 |
|
| 974 |
if (pwsrc->flushing) { |
| 975 |
error = "flushing"; |
| 976 |
goto start_error; |
| 977 |
} |
| 978 |
|
| 979 |
if (pw_thread_loop_timed_wait_full (pwsrc->stream->core->loop, &abstime) < 0) { |
| 980 |
error = "timeout"; |
| 981 |
goto start_error; |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
parse_stream_properties (pwsrc, pw_stream_get_properties (pwsrc->stream->pwstream)); |
| 986 |
GST_DEBUG_OBJECT (pwsrc, "signal started"); |
| 987 |
pwsrc->started = TRUE; |
| 988 |
pw_thread_loop_signal (pwsrc->stream->core->loop, FALSE); |
| 989 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 990 |
|
| 991 |
return TRUE; |
| 992 |
|
| 993 |
start_error: |
| 994 |
{ |
| 995 |
GST_DEBUG_OBJECT (pwsrc, "error starting stream: %s", error); |
| 996 |
pwsrc->started = FALSE; |
| 997 |
pw_thread_loop_signal (pwsrc->stream->core->loop, FALSE); |
| 998 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 999 |
return FALSE; |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
static enum pw_stream_state |
| 1004 |
wait_started (GstPipeWireSrc *this) |
| 1005 |
{ |
| 1006 |
enum pw_stream_state state, prev_state = PW_STREAM_STATE_UNCONNECTED; |
| 1007 |
const char *error = NULL; |
| 1008 |
struct timespec abstime; |
| 1009 |
gboolean restart = FALSE; |
| 1010 |
|
| 1011 |
pw_thread_loop_lock (this->stream->core->loop); |
| 1012 |
|
| 1013 |
pw_thread_loop_get_time (this->stream->core->loop, &abstime, |
| 1014 |
GST_PIPEWIRE_DEFAULT_TIMEOUT * SPA_NSEC_PER_SEC); |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
if (this->started) { |
| 1019 |
GST_DEBUG_OBJECT (this, "restart in progress"); |
| 1020 |
restart = TRUE; |
| 1021 |
this->started = FALSE; |
| 1022 |
} |
| 1023 |
|
| 1024 |
while (TRUE) { |
| 1025 |
state = pw_stream_get_state (this->stream->pwstream, &error); |
| 1026 |
|
| 1027 |
GST_DEBUG_OBJECT (this, "waiting for started signal, state now %s", |
| 1028 |
pw_stream_state_as_string (state)); |
| 1029 |
|
| 1030 |
if (state == PW_STREAM_STATE_ERROR || |
| 1031 |
(state == PW_STREAM_STATE_UNCONNECTED && prev_state > PW_STREAM_STATE_UNCONNECTED && !restart) || |
| 1032 |
this->flushing) { |
| 1033 |
state = PW_STREAM_STATE_ERROR; |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (this->started) |
| 1038 |
break; |
| 1039 |
|
| 1040 |
if (this->autoconnect) { |
| 1041 |
if (pw_thread_loop_timed_wait_full (this->stream->core->loop, &abstime) < 0) { |
| 1042 |
state = PW_STREAM_STATE_ERROR; |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
} else { |
| 1046 |
pw_thread_loop_wait (this->stream->core->loop); |
| 1047 |
} |
| 1048 |
|
| 1049 |
if (restart) |
| 1050 |
restart = state != PW_STREAM_STATE_UNCONNECTED; |
| 1051 |
prev_state = state; |
| 1052 |
} |
| 1053 |
GST_DEBUG_OBJECT (this, "got started signal: %s", |
| 1054 |
pw_stream_state_as_string (state)); |
| 1055 |
pw_thread_loop_unlock (this->stream->core->loop); |
| 1056 |
|
| 1057 |
return state; |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
static enum pw_stream_state |
| 1062 |
wait_negotiated (GstPipeWireSrc *this) |
| 1063 |
{ |
| 1064 |
enum pw_stream_state state; |
| 1065 |
const char *error = NULL; |
| 1066 |
struct timespec abstime; |
| 1067 |
|
| 1068 |
pw_thread_loop_get_time (this->stream->core->loop, &abstime, |
| 1069 |
GST_PIPEWIRE_DEFAULT_TIMEOUT * SPA_NSEC_PER_SEC); |
| 1070 |
|
| 1071 |
while (TRUE) { |
| 1072 |
state = pw_stream_get_state (this->stream->pwstream, &error); |
| 1073 |
|
| 1074 |
GST_DEBUG_OBJECT (this, "waiting for NEGOTIATED, now %s", pw_stream_state_as_string (state)); |
| 1075 |
if (state == PW_STREAM_STATE_ERROR) |
| 1076 |
break; |
| 1077 |
if (this->flushing) { |
| 1078 |
state = PW_STREAM_STATE_ERROR; |
| 1079 |
break; |
| 1080 |
} |
| 1081 |
|
| 1082 |
if (this->negotiated) |
| 1083 |
break; |
| 1084 |
|
| 1085 |
if (this->autoconnect) { |
| 1086 |
if (pw_thread_loop_timed_wait_full (this->stream->core->loop, &abstime) < 0) { |
| 1087 |
state = PW_STREAM_STATE_ERROR; |
| 1088 |
break; |
| 1089 |
} |
| 1090 |
} else { |
| 1091 |
pw_thread_loop_wait (this->stream->core->loop); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
GST_DEBUG_OBJECT (this, state != PW_STREAM_STATE_ERROR ? "got negotiated signal" : "error during negotiation"); |
| 1095 |
return state; |
| 1096 |
} |
| 1097 |
|
| 1098 |
static gboolean |
| 1099 |
gst_pipewire_src_negotiate (GstBaseSrc * basesrc) |
| 1100 |
{ |
| 1101 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (basesrc); |
| 1102 |
g_autoptr (GstCaps) thiscaps = NULL; |
| 1103 |
g_autoptr (GstCaps) possible_caps = NULL; |
| 1104 |
g_autoptr (GstCaps) negotiated_caps = NULL; |
| 1105 |
g_autoptr (GstCaps) peercaps = NULL; |
| 1106 |
g_autoptr (GPtrArray) possible = NULL; |
| 1107 |
gboolean result = FALSE; |
| 1108 |
const char *error = NULL; |
| 1109 |
uint32_t target_id; |
| 1110 |
|
| 1111 |
|
| 1112 |
thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL); |
| 1113 |
GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps); |
| 1114 |
|
| 1115 |
if (thiscaps == NULL) |
| 1116 |
goto no_nego_needed; |
| 1117 |
|
| 1118 |
if (G_UNLIKELY (gst_caps_is_empty (thiscaps))) |
| 1119 |
goto no_caps; |
| 1120 |
|
| 1121 |
|
| 1122 |
peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps); |
| 1123 |
GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps); |
| 1124 |
if (peercaps) { |
| 1125 |
|
| 1126 |
possible_caps = g_steal_pointer (&peercaps); |
| 1127 |
} else { |
| 1128 |
|
| 1129 |
possible_caps = g_steal_pointer (&thiscaps); |
| 1130 |
} |
| 1131 |
|
| 1132 |
GST_DEBUG_OBJECT (basesrc, "have common caps: %" GST_PTR_FORMAT, possible_caps); |
| 1133 |
gst_caps_sanitize (&possible_caps); |
| 1134 |
|
| 1135 |
if (gst_caps_is_empty (possible_caps)) |
| 1136 |
goto no_common_caps; |
| 1137 |
|
| 1138 |
GST_DEBUG_OBJECT (basesrc, "have common caps (sanitized): %" GST_PTR_FORMAT, possible_caps); |
| 1139 |
|
| 1140 |
if (pw_stream_get_state(pwsrc->stream->pwstream, NULL) == PW_STREAM_STATE_STREAMING) { |
| 1141 |
g_autoptr (GstCaps) current_caps = NULL; |
| 1142 |
g_autoptr (GstCaps) preferred_new_caps = NULL; |
| 1143 |
|
| 1144 |
current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (pwsrc)); |
| 1145 |
preferred_new_caps = gst_caps_copy_nth (possible_caps, 0); |
| 1146 |
|
| 1147 |
if (current_caps && gst_caps_is_equal (current_caps, preferred_new_caps)) { |
| 1148 |
GST_DEBUG_OBJECT (pwsrc, |
| 1149 |
"Stream running and new caps equal current ones. " |
| 1150 |
"Skipping renegotiation."); |
| 1151 |
goto no_nego_needed; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
possible = gst_caps_to_format_all (possible_caps); |
| 1157 |
|
| 1158 |
|
| 1159 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1160 |
if (pw_stream_get_state(pwsrc->stream->pwstream, &error) != PW_STREAM_STATE_UNCONNECTED) { |
| 1161 |
GST_DEBUG_OBJECT (basesrc, "disconnect capture"); |
| 1162 |
pw_stream_disconnect (pwsrc->stream->pwstream); |
| 1163 |
while (TRUE) { |
| 1164 |
enum pw_stream_state state = pw_stream_get_state (pwsrc->stream->pwstream, &error); |
| 1165 |
|
| 1166 |
GST_DEBUG_OBJECT (basesrc, "waiting for UNCONNECTED, now %s", pw_stream_state_as_string (state)); |
| 1167 |
if (state == PW_STREAM_STATE_UNCONNECTED) |
| 1168 |
break; |
| 1169 |
|
| 1170 |
if (state == PW_STREAM_STATE_ERROR || pwsrc->flushing) |
| 1171 |
goto connect_error; |
| 1172 |
|
| 1173 |
pw_thread_loop_wait (pwsrc->stream->core->loop); |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
target_id = pwsrc->stream->path ? (uint32_t)atoi(pwsrc->stream->path) : PW_ID_ANY; |
| 1178 |
|
| 1179 |
if (pwsrc->stream->target_object) { |
| 1180 |
struct spa_dict_item items[2] = { |
| 1181 |
SPA_DICT_ITEM_INIT(PW_KEY_TARGET_OBJECT, pwsrc->stream->target_object), |
| 1182 |
|
| 1183 |
|
| 1184 |
SPA_DICT_ITEM_INIT(PW_KEY_NODE_TARGET, NULL), |
| 1185 |
}; |
| 1186 |
struct spa_dict dict = SPA_DICT_INIT_ARRAY(items); |
| 1187 |
uint64_t serial; |
| 1188 |
|
| 1189 |
|
| 1190 |
if (spa_atou64(pwsrc->stream->target_object, &serial, 0)) { |
| 1191 |
dict.n_items = 1; |
| 1192 |
} else { |
| 1193 |
target_id = PW_ID_ANY; |
| 1194 |
items[1].value = pwsrc->stream->target_object; |
| 1195 |
} |
| 1196 |
|
| 1197 |
pw_stream_update_properties (pwsrc->stream->pwstream, &dict); |
| 1198 |
} |
| 1199 |
|
| 1200 |
GST_DEBUG_OBJECT (basesrc, "connect capture with path %s, target-object %s", |
| 1201 |
pwsrc->stream->path, pwsrc->stream->target_object); |
| 1202 |
|
| 1203 |
gst_caps_replace (&pwsrc->possible_caps, possible_caps); |
| 1204 |
pwsrc->negotiated = FALSE; |
| 1205 |
|
| 1206 |
enum pw_stream_flags flags; |
| 1207 |
flags = PW_STREAM_FLAG_DONT_RECONNECT | |
| 1208 |
PW_STREAM_FLAG_ASYNC; |
| 1209 |
if (pwsrc->autoconnect) |
| 1210 |
flags |= PW_STREAM_FLAG_AUTOCONNECT; |
| 1211 |
pw_stream_connect (pwsrc->stream->pwstream, |
| 1212 |
PW_DIRECTION_INPUT, |
| 1213 |
target_id, |
| 1214 |
flags, |
| 1215 |
(const struct spa_pod **)possible->pdata, |
| 1216 |
possible->len); |
| 1217 |
|
| 1218 |
if (wait_negotiated(pwsrc) == PW_STREAM_STATE_ERROR) |
| 1219 |
goto connect_error; |
| 1220 |
|
| 1221 |
negotiated_caps = g_steal_pointer (&pwsrc->caps); |
| 1222 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1223 |
|
| 1224 |
if (negotiated_caps == NULL) |
| 1225 |
goto no_caps; |
| 1226 |
|
| 1227 |
gst_pipewire_clock_reset (GST_PIPEWIRE_CLOCK (pwsrc->stream->clock), 0); |
| 1228 |
|
| 1229 |
GST_INFO_OBJECT (pwsrc, "set format %" GST_PTR_FORMAT, negotiated_caps); |
| 1230 |
result = gst_base_src_set_caps (GST_BASE_SRC (pwsrc), negotiated_caps); |
| 1231 |
if (!result) |
| 1232 |
goto no_caps; |
| 1233 |
|
| 1234 |
result = gst_pipewire_src_stream_start (pwsrc); |
| 1235 |
|
| 1236 |
return result; |
| 1237 |
|
| 1238 |
no_nego_needed: |
| 1239 |
{ |
| 1240 |
GST_DEBUG_OBJECT (basesrc, "no negotiation needed"); |
| 1241 |
return TRUE; |
| 1242 |
} |
| 1243 |
no_caps: |
| 1244 |
{ |
| 1245 |
const gchar * error_string = "No supported formats found"; |
| 1246 |
|
| 1247 |
GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT, |
| 1248 |
("%s", error_string), |
| 1249 |
("This element did not produce valid caps")); |
| 1250 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1251 |
pw_stream_set_error (pwsrc->stream->pwstream, -EINVAL, "%s", error_string); |
| 1252 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1253 |
return FALSE; |
| 1254 |
} |
| 1255 |
no_common_caps: |
| 1256 |
{ |
| 1257 |
const gchar * error_string = "No supported formats found"; |
| 1258 |
|
| 1259 |
GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT, |
| 1260 |
("%s", error_string), |
| 1261 |
("This element does not have formats in common with the peer")); |
| 1262 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1263 |
pw_stream_set_error (pwsrc->stream->pwstream, -EPIPE, "%s", error_string); |
| 1264 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1265 |
return FALSE; |
| 1266 |
} |
| 1267 |
connect_error: |
| 1268 |
{ |
| 1269 |
g_clear_pointer (&pwsrc->caps, gst_caps_unref); |
| 1270 |
pwsrc->possible_caps = NULL; |
| 1271 |
GST_DEBUG_OBJECT (basesrc, "connect error"); |
| 1272 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1273 |
return FALSE; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
static void |
| 1278 |
handle_format_change (GstPipeWireSrc *pwsrc, |
| 1279 |
const struct spa_pod *param) |
| 1280 |
{ |
| 1281 |
GstStructure *structure; |
| 1282 |
g_autoptr (GstCaps) pw_peer_caps = NULL; |
| 1283 |
|
| 1284 |
g_clear_pointer (&pwsrc->caps, gst_caps_unref); |
| 1285 |
if (param == NULL) { |
| 1286 |
GST_DEBUG_OBJECT (pwsrc, "clear format"); |
| 1287 |
pwsrc->negotiated = FALSE; |
| 1288 |
pwsrc->media_type = SPA_MEDIA_TYPE_unknown; |
| 1289 |
return; |
| 1290 |
} |
| 1291 |
|
| 1292 |
pw_peer_caps = gst_caps_from_format (param); |
| 1293 |
|
| 1294 |
if (pw_peer_caps && pwsrc->possible_caps) { |
| 1295 |
GST_DEBUG_OBJECT (pwsrc, "peer caps %" GST_PTR_FORMAT, pw_peer_caps); |
| 1296 |
GST_DEBUG_OBJECT (pwsrc, "possible caps %" GST_PTR_FORMAT, pwsrc->possible_caps); |
| 1297 |
|
| 1298 |
pwsrc->caps = gst_caps_intersect_full (pw_peer_caps, |
| 1299 |
pwsrc->possible_caps, |
| 1300 |
GST_CAPS_INTERSECT_FIRST); |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
pwsrc->caps = gst_caps_fixate (pwsrc->caps); |
| 1315 |
gst_caps_maybe_fixate_dma_format (pwsrc->caps); |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (pwsrc->caps) { |
| 1319 |
g_return_if_fail (gst_caps_is_fixed (pwsrc->caps)); |
| 1320 |
|
| 1321 |
pwsrc->negotiated = TRUE; |
| 1322 |
|
| 1323 |
structure = gst_caps_get_structure (pwsrc->caps, 0); |
| 1324 |
if (g_str_has_prefix (gst_structure_get_name (structure), "video/") || |
| 1325 |
g_str_has_prefix (gst_structure_get_name (structure), "image/")) { |
| 1326 |
pwsrc->media_type = SPA_MEDIA_TYPE_video; |
| 1327 |
|
| 1328 |
#ifdef HAVE_GSTREAMER_DMA_DRM |
| 1329 |
if (gst_video_is_dma_drm_caps (pwsrc->caps)) { |
| 1330 |
if (!gst_video_info_dma_drm_from_caps (&pwsrc->drm_info, pwsrc->caps)) { |
| 1331 |
GST_WARNING_OBJECT (pwsrc, "Can't create drm video info from caps"); |
| 1332 |
pw_stream_set_error (pwsrc->stream->pwstream, -EINVAL, "internal error"); |
| 1333 |
return; |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (!gst_video_info_dma_drm_to_video_info (&pwsrc->drm_info, |
| 1337 |
&pwsrc->video_info)) { |
| 1338 |
GST_WARNING_OBJECT (pwsrc, "Can't create video info from drm video info"); |
| 1339 |
pw_stream_set_error (pwsrc->stream->pwstream, -EINVAL, "internal error"); |
| 1340 |
return; |
| 1341 |
} |
| 1342 |
pwsrc->is_rawvideo = TRUE; |
| 1343 |
} else { |
| 1344 |
gst_video_info_dma_drm_init (&pwsrc->drm_info); |
| 1345 |
#endif |
| 1346 |
gst_video_info_from_caps (&pwsrc->video_info, pwsrc->caps); |
| 1347 |
|
| 1348 |
if (GST_VIDEO_FORMAT_INFO_IS_VALID_RAW (pwsrc->video_info.finfo) |
| 1349 |
#ifdef HAVE_GSTREAMER_DMA_DRM |
| 1350 |
&& GST_VIDEO_FORMAT_INFO_FORMAT (pwsrc->video_info.finfo) != GST_VIDEO_FORMAT_DMA_DRM |
| 1351 |
#endif |
| 1352 |
) |
| 1353 |
pwsrc->is_rawvideo = TRUE; |
| 1354 |
else |
| 1355 |
pwsrc->is_rawvideo = FALSE; |
| 1356 |
|
| 1357 |
#ifdef HAVE_GSTREAMER_DMA_DRM |
| 1358 |
} |
| 1359 |
#endif |
| 1360 |
} else { |
| 1361 |
|
| 1362 |
|
| 1363 |
if (pwsrc->use_bufferpool != USE_BUFFERPOOL_YES) |
| 1364 |
pwsrc->use_bufferpool = USE_BUFFERPOOL_NO; |
| 1365 |
|
| 1366 |
pwsrc->media_type = SPA_MEDIA_TYPE_audio; |
| 1367 |
} |
| 1368 |
} else { |
| 1369 |
pwsrc->negotiated = FALSE; |
| 1370 |
pwsrc->media_type = SPA_MEDIA_TYPE_unknown; |
| 1371 |
pwsrc->is_rawvideo = FALSE; |
| 1372 |
} |
| 1373 |
|
| 1374 |
if (pwsrc->caps) { |
| 1375 |
const struct spa_pod *params[10]; |
| 1376 |
struct spa_pod_builder b = { NULL }; |
| 1377 |
uint8_t buffer[16384]; |
| 1378 |
uint32_t buffers = CLAMP (16, pwsrc->min_buffers, pwsrc->max_buffers); |
| 1379 |
int buffertypes, n_params = 0; |
| 1380 |
|
| 1381 |
buffertypes = (1<<SPA_DATA_DmaBuf); |
| 1382 |
if (!spa_pod_find_prop (param, NULL, SPA_FORMAT_VIDEO_modifier)) { |
| 1383 |
buffertypes |= ((1<<SPA_DATA_MemFd) | (1<<SPA_DATA_MemPtr)); |
| 1384 |
} |
| 1385 |
|
| 1386 |
GST_DEBUG_OBJECT (pwsrc, "we got format %" GST_PTR_FORMAT, pwsrc->caps); |
| 1387 |
|
| 1388 |
spa_pod_builder_init (&b, buffer, sizeof (buffer)); |
| 1389 |
params[n_params++] = spa_pod_builder_add_object (&b, |
| 1390 |
SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers, |
| 1391 |
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(buffers, |
| 1392 |
pwsrc->min_buffers, |
| 1393 |
pwsrc->max_buffers), |
| 1394 |
SPA_PARAM_BUFFERS_blocks, SPA_POD_CHOICE_RANGE_Int(0, 1, INT32_MAX), |
| 1395 |
SPA_PARAM_BUFFERS_size, SPA_POD_CHOICE_RANGE_Int(0, 1, INT32_MAX), |
| 1396 |
SPA_PARAM_BUFFERS_stride, SPA_POD_CHOICE_RANGE_Int(0, 0, INT32_MAX), |
| 1397 |
SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int(buffertypes)); |
| 1398 |
|
| 1399 |
params[n_params++] = spa_pod_builder_add_object (&b, |
| 1400 |
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, |
| 1401 |
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header), |
| 1402 |
SPA_PARAM_META_size, SPA_POD_Int(sizeof (struct spa_meta_header))); |
| 1403 |
params[n_params++] = spa_pod_builder_add_object (&b, |
| 1404 |
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, |
| 1405 |
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop), |
| 1406 |
SPA_PARAM_META_size, SPA_POD_Int(sizeof (struct spa_meta_region))); |
| 1407 |
params[n_params++] = spa_pod_builder_add_object (&b, |
| 1408 |
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, |
| 1409 |
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoTransform), |
| 1410 |
SPA_PARAM_META_size, SPA_POD_Int(sizeof (struct spa_meta_videotransform))); |
| 1411 |
#define CURSOR_META_SIZE(width, height) \ |
| 1412 |
(sizeof (struct spa_meta_cursor) + \ |
| 1413 |
sizeof (struct spa_meta_bitmap) + width * height * 4) |
| 1414 |
params[n_params++] = spa_pod_builder_add_object (&b, |
| 1415 |
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, |
| 1416 |
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Cursor), |
| 1417 |
SPA_PARAM_META_size, |
| 1418 |
SPA_POD_CHOICE_RANGE_Int (CURSOR_META_SIZE(384, 384), |
| 1419 |
sizeof (struct spa_meta_cursor), |
| 1420 |
CURSOR_META_SIZE(384, 384))); |
| 1421 |
|
| 1422 |
GST_DEBUG_OBJECT (pwsrc, "doing finish format"); |
| 1423 |
pw_stream_update_params (pwsrc->stream->pwstream, params, n_params); |
| 1424 |
} else { |
| 1425 |
GST_WARNING_OBJECT (pwsrc, "finish format with error"); |
| 1426 |
pw_stream_set_error (pwsrc->stream->pwstream, -EINVAL, "unhandled format"); |
| 1427 |
} |
| 1428 |
pw_thread_loop_signal (pwsrc->stream->core->loop, FALSE); |
| 1429 |
} |
| 1430 |
|
| 1431 |
static void |
| 1432 |
on_param_changed (void *data, uint32_t id, |
| 1433 |
const struct spa_pod *param) |
| 1434 |
{ |
| 1435 |
GstPipeWireSrc *pwsrc = data; |
| 1436 |
switch (id) { |
| 1437 |
case SPA_PARAM_Format: |
| 1438 |
handle_format_change(pwsrc, param); |
| 1439 |
break; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
static gboolean |
| 1444 |
gst_pipewire_src_unlock (GstBaseSrc * basesrc) |
| 1445 |
{ |
| 1446 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (basesrc); |
| 1447 |
|
| 1448 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1449 |
GST_DEBUG_OBJECT (pwsrc, "setting flushing"); |
| 1450 |
pwsrc->flushing = TRUE; |
| 1451 |
pw_thread_loop_signal (pwsrc->stream->core->loop, FALSE); |
| 1452 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1453 |
|
| 1454 |
return TRUE; |
| 1455 |
} |
| 1456 |
|
| 1457 |
static gboolean |
| 1458 |
gst_pipewire_src_unlock_stop (GstBaseSrc * basesrc) |
| 1459 |
{ |
| 1460 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (basesrc); |
| 1461 |
|
| 1462 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1463 |
GST_DEBUG_OBJECT (pwsrc, "unsetting flushing"); |
| 1464 |
pwsrc->flushing = FALSE; |
| 1465 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1466 |
|
| 1467 |
return TRUE; |
| 1468 |
} |
| 1469 |
|
| 1470 |
static gboolean |
| 1471 |
gst_pipewire_src_event (GstBaseSrc * src, GstEvent * event) |
| 1472 |
{ |
| 1473 |
gboolean res = FALSE; |
| 1474 |
|
| 1475 |
switch (GST_EVENT_TYPE (event)) { |
| 1476 |
case GST_EVENT_CUSTOM_UPSTREAM: |
| 1477 |
if (gst_video_event_is_force_key_unit (event)) { |
| 1478 |
GstClockTime running_time; |
| 1479 |
gboolean all_headers; |
| 1480 |
guint count; |
| 1481 |
|
| 1482 |
gst_video_event_parse_upstream_force_key_unit (event, |
| 1483 |
&running_time, &all_headers, &count); |
| 1484 |
|
| 1485 |
res = TRUE; |
| 1486 |
} else { |
| 1487 |
res = GST_BASE_SRC_CLASS (parent_class)->event (src, event); |
| 1488 |
} |
| 1489 |
break; |
| 1490 |
default: |
| 1491 |
res = GST_BASE_SRC_CLASS (parent_class)->event (src, event); |
| 1492 |
break; |
| 1493 |
} |
| 1494 |
return res; |
| 1495 |
} |
| 1496 |
|
| 1497 |
static gboolean |
| 1498 |
gst_pipewire_src_query (GstBaseSrc * src, GstQuery * query) |
| 1499 |
{ |
| 1500 |
gboolean res = FALSE; |
| 1501 |
GstPipeWireSrc *pwsrc; |
| 1502 |
|
| 1503 |
pwsrc = GST_PIPEWIRE_SRC (src); |
| 1504 |
|
| 1505 |
switch (GST_QUERY_TYPE (query)) { |
| 1506 |
case GST_QUERY_LATENCY: |
| 1507 |
GST_OBJECT_LOCK (pwsrc); |
| 1508 |
gst_query_set_latency (query, pwsrc->is_live, pwsrc->min_latency, pwsrc->max_latency); |
| 1509 |
GST_OBJECT_UNLOCK (pwsrc); |
| 1510 |
res = TRUE; |
| 1511 |
break; |
| 1512 |
default: |
| 1513 |
res = GST_BASE_SRC_CLASS (parent_class)->query (src, query); |
| 1514 |
break; |
| 1515 |
} |
| 1516 |
return res; |
| 1517 |
} |
| 1518 |
|
| 1519 |
static void |
| 1520 |
gst_pipewire_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer, |
| 1521 |
GstClockTime * start, GstClockTime * end) |
| 1522 |
{ |
| 1523 |
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (basesrc); |
| 1524 |
|
| 1525 |
|
| 1526 |
if (gst_base_src_is_live (basesrc)) { |
| 1527 |
GstClockTime timestamp = GST_BUFFER_PTS (buffer); |
| 1528 |
|
| 1529 |
if (GST_CLOCK_TIME_IS_VALID (timestamp)) { |
| 1530 |
|
| 1531 |
GstClockTime duration = GST_BUFFER_DURATION (buffer); |
| 1532 |
|
| 1533 |
if (GST_CLOCK_TIME_IS_VALID (duration)) { |
| 1534 |
*end = timestamp + duration; |
| 1535 |
} |
| 1536 |
*start = timestamp; |
| 1537 |
} |
| 1538 |
} else { |
| 1539 |
*start = GST_CLOCK_TIME_NONE; |
| 1540 |
*end = GST_CLOCK_TIME_NONE; |
| 1541 |
} |
| 1542 |
|
| 1543 |
GST_LOG_OBJECT (pwsrc, "start %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT |
| 1544 |
"), end %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT ")", |
| 1545 |
GST_TIME_ARGS (*start), *start, GST_TIME_ARGS (*end), *end); |
| 1546 |
} |
| 1547 |
|
| 1548 |
static GstFlowReturn |
| 1549 |
gst_pipewire_src_create (GstPushSrc * psrc, GstBuffer ** buffer) |
| 1550 |
{ |
| 1551 |
GstPipeWireSrc *pwsrc; |
| 1552 |
const char *error = NULL; |
| 1553 |
GstBuffer *buf; |
| 1554 |
gboolean update_time = FALSE, timeout = FALSE; |
| 1555 |
GstCaps *caps = NULL; |
| 1556 |
struct timespec abstime = { 0, }; |
| 1557 |
bool have_abstime = false; |
| 1558 |
|
| 1559 |
pwsrc = GST_PIPEWIRE_SRC (psrc); |
| 1560 |
|
| 1561 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1562 |
if (!pwsrc->negotiated) |
| 1563 |
goto not_negotiated; |
| 1564 |
|
| 1565 |
while (TRUE) { |
| 1566 |
enum pw_stream_state state; |
| 1567 |
|
| 1568 |
if (pwsrc->flushing) |
| 1569 |
goto streaming_stopped; |
| 1570 |
|
| 1571 |
if (pwsrc->stream == NULL) |
| 1572 |
goto streaming_error; |
| 1573 |
|
| 1574 |
state = pw_stream_get_state (pwsrc->stream->pwstream, &error); |
| 1575 |
if (state == PW_STREAM_STATE_ERROR) |
| 1576 |
goto streaming_error; |
| 1577 |
|
| 1578 |
if (state == PW_STREAM_STATE_UNCONNECTED) |
| 1579 |
goto streaming_stopped; |
| 1580 |
|
| 1581 |
if ((caps = pwsrc->caps) != NULL) { |
| 1582 |
pwsrc->caps = NULL; |
| 1583 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1584 |
|
| 1585 |
GST_DEBUG_OBJECT (pwsrc, "set format %" GST_PTR_FORMAT, caps); |
| 1586 |
gst_base_src_set_caps (GST_BASE_SRC (pwsrc), caps); |
| 1587 |
gst_caps_unref (caps); |
| 1588 |
|
| 1589 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1590 |
continue; |
| 1591 |
} |
| 1592 |
|
| 1593 |
if (pwsrc->eos) { |
| 1594 |
if (pwsrc->last_buffer == NULL) |
| 1595 |
goto streaming_eos; |
| 1596 |
buf = pwsrc->last_buffer; |
| 1597 |
pwsrc->last_buffer = NULL; |
| 1598 |
update_time = TRUE; |
| 1599 |
GST_LOG_OBJECT (pwsrc, "EOS, send last buffer"); |
| 1600 |
break; |
| 1601 |
} else if (timeout && pwsrc->last_buffer != NULL) { |
| 1602 |
buf = gst_buffer_copy (pwsrc->last_buffer); |
| 1603 |
update_time = TRUE; |
| 1604 |
GST_LOG_OBJECT (pwsrc, "timeout, send keepalive buffer"); |
| 1605 |
break; |
| 1606 |
} else { |
| 1607 |
buf = dequeue_buffer (pwsrc); |
| 1608 |
GST_LOG_OBJECT (pwsrc, "popped buffer %p", buf); |
| 1609 |
if (buf != NULL) { |
| 1610 |
if (pwsrc->resend_last || pwsrc->keepalive_time > 0) { |
| 1611 |
GstClock *clock; |
| 1612 |
GstBuffer *old; |
| 1613 |
|
| 1614 |
old = pwsrc->last_buffer; |
| 1615 |
pwsrc->last_buffer = gst_buffer_copy (buf); |
| 1616 |
gst_buffer_unref (old); |
| 1617 |
gst_buffer_add_parent_buffer_meta (pwsrc->last_buffer, buf); |
| 1618 |
|
| 1619 |
clock = gst_element_get_clock (GST_ELEMENT_CAST (pwsrc)); |
| 1620 |
if (clock != NULL) { |
| 1621 |
pwsrc->last_buffer_clock_time = gst_clock_get_time (clock); |
| 1622 |
gst_object_unref (clock); |
| 1623 |
} else { |
| 1624 |
pwsrc->last_buffer_clock_time = GST_CLOCK_TIME_NONE; |
| 1625 |
} |
| 1626 |
} |
| 1627 |
break; |
| 1628 |
} |
| 1629 |
} |
| 1630 |
timeout = FALSE; |
| 1631 |
if (pwsrc->keepalive_time > 0) { |
| 1632 |
if (!have_abstime) { |
| 1633 |
|
| 1634 |
|
| 1635 |
pw_thread_loop_get_time(pwsrc->stream->core->loop, &abstime, |
| 1636 |
pwsrc->keepalive_time * SPA_NSEC_PER_MSEC); |
| 1637 |
have_abstime = TRUE; |
| 1638 |
} |
| 1639 |
if (pw_thread_loop_timed_wait_full (pwsrc->stream->core->loop, &abstime) == -ETIMEDOUT) |
| 1640 |
timeout = TRUE; |
| 1641 |
} else { |
| 1642 |
pw_thread_loop_wait (pwsrc->stream->core->loop); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1646 |
|
| 1647 |
*buffer = buf; |
| 1648 |
|
| 1649 |
if (update_time) { |
| 1650 |
GstClock *clock; |
| 1651 |
GstClockTime current_clock_time; |
| 1652 |
|
| 1653 |
clock = gst_element_get_clock (GST_ELEMENT_CAST (pwsrc)); |
| 1654 |
if (clock != NULL) { |
| 1655 |
current_clock_time = gst_clock_get_time (clock); |
| 1656 |
gst_object_unref (clock); |
| 1657 |
} else { |
| 1658 |
current_clock_time = GST_CLOCK_TIME_NONE; |
| 1659 |
} |
| 1660 |
|
| 1661 |
if (GST_CLOCK_TIME_IS_VALID (current_clock_time) && |
| 1662 |
GST_CLOCK_TIME_IS_VALID (pwsrc->last_buffer_clock_time) && |
| 1663 |
GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (*buffer)) && |
| 1664 |
GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (*buffer))) { |
| 1665 |
GstClockTime diff; |
| 1666 |
|
| 1667 |
diff = current_clock_time - pwsrc->last_buffer_clock_time; |
| 1668 |
|
| 1669 |
GST_BUFFER_PTS (*buffer) += diff; |
| 1670 |
GST_BUFFER_DTS (*buffer) += diff; |
| 1671 |
} else { |
| 1672 |
GST_BUFFER_PTS (*buffer) = GST_BUFFER_DTS (*buffer) = current_clock_time; |
| 1673 |
} |
| 1674 |
|
| 1675 |
GST_LOG_OBJECT (pwsrc, "Sending keepalive buffer pts/dts: %" GST_TIME_FORMAT |
| 1676 |
" (%" G_GUINT64_FORMAT ")", GST_TIME_ARGS (current_clock_time), |
| 1677 |
current_clock_time); |
| 1678 |
} |
| 1679 |
|
| 1680 |
return GST_FLOW_OK; |
| 1681 |
|
| 1682 |
not_negotiated: |
| 1683 |
{ |
| 1684 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1685 |
return GST_FLOW_NOT_NEGOTIATED; |
| 1686 |
} |
| 1687 |
streaming_eos: |
| 1688 |
{ |
| 1689 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1690 |
return GST_FLOW_EOS; |
| 1691 |
} |
| 1692 |
streaming_error: |
| 1693 |
{ |
| 1694 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1695 |
return GST_FLOW_ERROR; |
| 1696 |
} |
| 1697 |
streaming_stopped: |
| 1698 |
{ |
| 1699 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1700 |
return GST_FLOW_FLUSHING; |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
static gboolean |
| 1705 |
gst_pipewire_src_start (GstBaseSrc * basesrc) |
| 1706 |
{ |
| 1707 |
return TRUE; |
| 1708 |
} |
| 1709 |
|
| 1710 |
static gboolean |
| 1711 |
gst_pipewire_src_stop (GstBaseSrc * basesrc) |
| 1712 |
{ |
| 1713 |
GstPipeWireSrc *pwsrc; |
| 1714 |
|
| 1715 |
pwsrc = GST_PIPEWIRE_SRC (basesrc); |
| 1716 |
|
| 1717 |
pw_thread_loop_lock (pwsrc->stream->core->loop); |
| 1718 |
pwsrc->eos = false; |
| 1719 |
gst_buffer_replace (&pwsrc->last_buffer, NULL); |
| 1720 |
gst_caps_replace(&pwsrc->caps, NULL); |
| 1721 |
gst_caps_replace(&pwsrc->possible_caps, NULL); |
| 1722 |
pwsrc->transform_value = UINT32_MAX; |
| 1723 |
pw_thread_loop_unlock (pwsrc->stream->core->loop); |
| 1724 |
|
| 1725 |
return TRUE; |
| 1726 |
} |
| 1727 |
|
| 1728 |
static const struct pw_stream_events stream_events = { |
| 1729 |
PW_VERSION_STREAM_EVENTS, |
| 1730 |
.state_changed = on_state_changed, |
| 1731 |
.param_changed = on_param_changed, |
| 1732 |
.add_buffer = on_add_buffer, |
| 1733 |
.remove_buffer = on_remove_buffer, |
| 1734 |
.process = on_process, |
| 1735 |
}; |
| 1736 |
|
| 1737 |
static gboolean |
| 1738 |
gst_pipewire_src_send_event (GstElement * elem, GstEvent * event) |
| 1739 |
{ |
| 1740 |
GstPipeWireSrc *this = GST_PIPEWIRE_SRC_CAST (elem); |
| 1741 |
gboolean ret; |
| 1742 |
|
| 1743 |
switch (GST_EVENT_TYPE (event)) { |
| 1744 |
case GST_EVENT_EOS: |
| 1745 |
GST_DEBUG_OBJECT (this, "got EOS"); |
| 1746 |
pw_thread_loop_lock (this->stream->core->loop); |
| 1747 |
this->eos = true; |
| 1748 |
pw_thread_loop_signal (this->stream->core->loop, FALSE); |
| 1749 |
pw_thread_loop_unlock (this->stream->core->loop); |
| 1750 |
ret = TRUE; |
| 1751 |
break; |
| 1752 |
default: |
| 1753 |
ret = GST_ELEMENT_CLASS (parent_class)->send_event (elem, event); |
| 1754 |
break; |
| 1755 |
} |
| 1756 |
return ret; |
| 1757 |
} |
| 1758 |
|
| 1759 |
static GstStateChangeReturn |
| 1760 |
gst_pipewire_src_change_state (GstElement * element, GstStateChange transition) |
| 1761 |
{ |
| 1762 |
GstStateChangeReturn ret; |
| 1763 |
GstPipeWireSrc *this = GST_PIPEWIRE_SRC_CAST (element); |
| 1764 |
|
| 1765 |
switch (transition) { |
| 1766 |
case GST_STATE_CHANGE_NULL_TO_READY: |
| 1767 |
if (!gst_pipewire_stream_open (this->stream, &stream_events)) |
| 1768 |
goto open_failed; |
| 1769 |
break; |
| 1770 |
case GST_STATE_CHANGE_READY_TO_PAUSED: |
| 1771 |
break; |
| 1772 |
case GST_STATE_CHANGE_PAUSED_TO_PLAYING: |
| 1773 |
|
| 1774 |
GST_DEBUG_OBJECT (this, "activating stream"); |
| 1775 |
|
| 1776 |
pw_thread_loop_lock (this->stream->core->loop); |
| 1777 |
pw_stream_set_active (this->stream->pwstream, true); |
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
if (wait_negotiated(this) == PW_STREAM_STATE_ERROR) |
| 1783 |
goto open_failed; |
| 1784 |
pw_thread_loop_unlock (this->stream->core->loop); |
| 1785 |
break; |
| 1786 |
case GST_STATE_CHANGE_PLAYING_TO_PAUSED: |
| 1787 |
|
| 1788 |
GST_DEBUG_OBJECT (this, "in-activating stream"); |
| 1789 |
pw_thread_loop_lock (this->stream->core->loop); |
| 1790 |
pw_stream_set_active (this->stream->pwstream, false); |
| 1791 |
pw_thread_loop_unlock (this->stream->core->loop); |
| 1792 |
break; |
| 1793 |
default: |
| 1794 |
break; |
| 1795 |
} |
| 1796 |
|
| 1797 |
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); |
| 1798 |
|
| 1799 |
switch (transition) { |
| 1800 |
case GST_STATE_CHANGE_READY_TO_PAUSED: |
| 1801 |
if (wait_started (this) == PW_STREAM_STATE_ERROR) |
| 1802 |
goto open_failed; |
| 1803 |
|
| 1804 |
pw_thread_loop_lock (this->stream->core->loop); |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
pw_stream_set_active (this->stream->pwstream, false); |
| 1812 |
pw_thread_loop_unlock (this->stream->core->loop); |
| 1813 |
|
| 1814 |
if (gst_base_src_is_live (GST_BASE_SRC (element))) |
| 1815 |
ret = GST_STATE_CHANGE_NO_PREROLL; |
| 1816 |
break; |
| 1817 |
case GST_STATE_CHANGE_PLAYING_TO_PAUSED: |
| 1818 |
break; |
| 1819 |
case GST_STATE_CHANGE_PAUSED_TO_READY: |
| 1820 |
pw_thread_loop_lock (this->stream->core->loop); |
| 1821 |
this->negotiated = FALSE; |
| 1822 |
pw_thread_loop_unlock (this->stream->core->loop); |
| 1823 |
break; |
| 1824 |
case GST_STATE_CHANGE_READY_TO_NULL: |
| 1825 |
gst_pipewire_stream_close (this->stream); |
| 1826 |
break; |
| 1827 |
default: |
| 1828 |
break; |
| 1829 |
} |
| 1830 |
return ret; |
| 1831 |
|
| 1832 |
|
| 1833 |
open_failed: |
| 1834 |
{ |
| 1835 |
return GST_STATE_CHANGE_FAILURE; |
| 1836 |
} |
| 1837 |
} |