2 * Copyright (C) 2015-2017 YouView TV Ltd
3 * Author: Vincent Penquerch <vincent.penquerch@collabora.co.uk>
5 * gstipcpipelinecomm.c:
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
30 #include <gst/base/gstbytewriter.h>
31 #include <gst/gstprotection.h>
32 #include "gstipcpipelinecomm.h"
34 GST_DEBUG_CATEGORY_STATIC (gst_ipc_pipeline_comm_debug);
35 #define GST_CAT_DEFAULT gst_ipc_pipeline_comm_debug
37 #define DEFAULT_ACK_TIME (10 * G_TIME_SPAN_SECOND)
50 COMM_REQUEST_TYPE_BUFFER,
51 COMM_REQUEST_TYPE_EVENT,
52 COMM_REQUEST_TYPE_QUERY,
53 COMM_REQUEST_TYPE_STATE_CHANGE,
54 COMM_REQUEST_TYPE_MESSAGE,
68 static const gchar *comm_request_ret_get_name (CommRequestType type,
70 static guint32 comm_request_ret_get_failure_value (CommRequestType type);
73 comm_request_new (guint32 id, CommRequestType type, GstQuery * query)
77 req = g_malloc (sizeof (CommRequest));
79 g_cond_init (&req->cond);
81 req->comm_error = FALSE;
83 req->ret = comm_request_ret_get_failure_value (type);
90 comm_request_wait (GstIpcPipelineComm * comm, CommRequest * req,
93 guint32 ret = comm_request_ret_get_failure_value (req->type);
96 if (ack_type == ACK_TYPE_TIMED)
97 end_time = g_get_monotonic_time () + comm->ack_time;
99 end_time = G_MAXUINT64;
101 GST_TRACE_OBJECT (comm->element, "Waiting for ACK/NAK for request %u",
103 while (!req->replied) {
104 if (ack_type == ACK_TYPE_TIMED) {
105 if (!g_cond_wait_until (&req->cond, &comm->mutex, end_time))
108 g_cond_wait (&req->cond, &comm->mutex);
113 GST_TRACE_OBJECT (comm->element, "Got reply for request %u: %d (%s)",
114 req->id, ret, comm_request_ret_get_name (req->type, ret));
116 req->comm_error = TRUE;
117 GST_ERROR_OBJECT (comm->element, "Timeout waiting for reply for request %u",
125 comm_request_free (CommRequest * req)
127 g_cond_clear (&req->cond);
132 comm_request_ret_get_name (CommRequestType type, guint32 ret)
135 case COMM_REQUEST_TYPE_BUFFER:
136 return gst_flow_get_name (ret);
137 case COMM_REQUEST_TYPE_EVENT:
138 case COMM_REQUEST_TYPE_QUERY:
139 case COMM_REQUEST_TYPE_MESSAGE:
140 return ret ? "TRUE" : "FALSE";
141 case COMM_REQUEST_TYPE_STATE_CHANGE:
142 return gst_element_state_change_return_get_name (ret);
144 g_assert_not_reached ();
149 comm_request_ret_get_failure_value (CommRequestType type)
152 case COMM_REQUEST_TYPE_BUFFER:
153 return GST_FLOW_COMM_ERROR;
154 case COMM_REQUEST_TYPE_EVENT:
155 case COMM_REQUEST_TYPE_MESSAGE:
156 case COMM_REQUEST_TYPE_QUERY:
158 case COMM_REQUEST_TYPE_STATE_CHANGE:
159 return GST_STATE_CHANGE_FAILURE;
161 g_assert_not_reached ();
166 gst_ipc_pipeline_comm_data_type_get_name (GstIpcPipelineCommDataType type)
169 case GST_IPC_PIPELINE_COMM_DATA_TYPE_ACK:
171 case GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY_RESULT:
172 return "QUERY_RESULT";
173 case GST_IPC_PIPELINE_COMM_DATA_TYPE_BUFFER:
175 case GST_IPC_PIPELINE_COMM_DATA_TYPE_EVENT:
177 case GST_IPC_PIPELINE_COMM_DATA_TYPE_SINK_MESSAGE_EVENT:
178 return "SINK_MESSAGE_EVENT";
179 case GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY:
181 case GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_CHANGE:
182 return "STATE_CHANGE";
183 case GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_LOST:
185 case GST_IPC_PIPELINE_COMM_DATA_TYPE_MESSAGE:
187 case GST_IPC_PIPELINE_COMM_DATA_TYPE_GERROR_MESSAGE:
188 return "GERROR_MESSAGE";
195 gst_ipc_pipeline_comm_sync_fd (GstIpcPipelineComm * comm, guint32 id,
196 GstQuery * query, guint32 * ret, AckType ack_type, CommRequestType type)
200 GHashTable *waiting_ids;
202 if (ack_type == ACK_TYPE_NONE)
205 req = comm_request_new (id, type, query);
206 waiting_ids = g_hash_table_ref (comm->waiting_ids);
207 g_hash_table_insert (waiting_ids, GINT_TO_POINTER (id), req);
208 *ret = comm_request_wait (comm, req, ack_type);
209 comm_error = req->comm_error;
210 g_hash_table_remove (waiting_ids, GINT_TO_POINTER (id));
211 g_hash_table_unref (waiting_ids);
216 write_to_fd_raw (GstIpcPipelineComm * comm, const void *data, size_t size)
222 GST_TRACE_OBJECT (comm->element, "Writing %zu bytes to fdout", size);
225 write (comm->fdout, (const unsigned char *) data + offset, size);
227 if (errno == EAGAIN || errno == EINTR)
229 GST_ERROR_OBJECT (comm->element, "Failed to write to fd: %s",
243 write_byte_writer_to_fd (GstIpcPipelineComm * comm, GstByteWriter * bw)
249 size = gst_byte_writer_get_size (bw);
250 data = gst_byte_writer_reset_and_get_data (bw);
253 ret = write_to_fd_raw (comm, data, size);
259 gst_ipc_pipeline_comm_write_ack_to_fd (GstIpcPipelineComm * comm, guint32 id,
260 guint32 ret, CommRequestType type)
262 const unsigned char payload_type = GST_IPC_PIPELINE_COMM_DATA_TYPE_ACK;
266 g_mutex_lock (&comm->mutex);
268 GST_TRACE_OBJECT (comm->element, "Writing ACK for %u: %s (%d)", id,
269 comm_request_ret_get_name (type, ret), ret);
270 gst_byte_writer_init (&bw);
271 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
273 if (!gst_byte_writer_put_uint32_le (&bw, id))
276 if (!gst_byte_writer_put_uint32_le (&bw, size))
278 if (!gst_byte_writer_put_uint32_le (&bw, ret))
281 if (!write_byte_writer_to_fd (comm, &bw))
285 g_mutex_unlock (&comm->mutex);
286 gst_byte_writer_reset (&bw);
290 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
291 ("Failed to write to socket"));
296 gst_ipc_pipeline_comm_write_flow_ack_to_fd (GstIpcPipelineComm * comm,
297 guint32 id, GstFlowReturn ret)
299 gst_ipc_pipeline_comm_write_ack_to_fd (comm, id, (guint32) ret,
300 COMM_REQUEST_TYPE_BUFFER);
304 gst_ipc_pipeline_comm_write_boolean_ack_to_fd (GstIpcPipelineComm * comm,
305 guint32 id, gboolean ret)
307 gst_ipc_pipeline_comm_write_ack_to_fd (comm, id, (guint32) ret,
308 COMM_REQUEST_TYPE_EVENT);
312 gst_ipc_pipeline_comm_write_state_change_ack_to_fd (GstIpcPipelineComm * comm,
313 guint32 id, GstStateChangeReturn ret)
315 gst_ipc_pipeline_comm_write_ack_to_fd (comm, id, (guint32) ret,
316 COMM_REQUEST_TYPE_STATE_CHANGE);
320 gst_ipc_pipeline_comm_write_query_result_to_fd (GstIpcPipelineComm * comm,
321 guint32 id, gboolean result, GstQuery * query)
323 const unsigned char payload_type =
324 GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY_RESULT;
325 guint8 result8 = result;
330 const GstStructure *structure;
333 g_mutex_lock (&comm->mutex);
335 GST_TRACE_OBJECT (comm->element,
336 "Writing query result for %u: %d, %" GST_PTR_FORMAT, id, result, query);
337 gst_byte_writer_init (&bw);
338 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
340 if (!gst_byte_writer_put_uint32_le (&bw, id))
342 structure = gst_query_get_structure (query);
344 str = gst_structure_to_string (structure);
350 size = 1 + sizeof (guint32) + len + 1;
351 if (!gst_byte_writer_put_uint32_le (&bw, size))
353 if (!gst_byte_writer_put_uint8 (&bw, result8))
355 type = GST_QUERY_TYPE (query);
356 if (!gst_byte_writer_put_uint32_le (&bw, type))
359 if (!gst_byte_writer_put_data (&bw, (const guint8 *) str, len + 1))
362 if (!gst_byte_writer_put_uint8 (&bw, 0))
366 if (!write_byte_writer_to_fd (comm, &bw))
370 g_mutex_unlock (&comm->mutex);
371 gst_byte_writer_reset (&bw);
376 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
377 ("Failed to write to socket"));
382 gst_ipc_pipeline_comm_read_query_result (GstIpcPipelineComm * comm,
383 guint32 size, GstQuery ** query)
386 GstStructure *structure;
389 const guint8 *payload = NULL;
390 guint32 mapped_size = size;
392 /* this should not be called if we don't have enough yet */
394 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, FALSE);
395 g_return_val_if_fail (size >= 1 + sizeof (guint32), FALSE);
397 payload = gst_adapter_map (comm->adapter, mapped_size);
401 memcpy (&type, payload, sizeof (type));
402 payload += sizeof (type);
404 size -= 1 + sizeof (guint32);
408 if (payload[size - 1]) {
413 structure = gst_structure_from_string ((const char *) payload, &end);
422 *query = gst_query_new_custom (type, structure);
425 gst_adapter_unmap (comm->adapter);
426 gst_adapter_flush (comm->adapter, mapped_size);
442 GstIpcPipelineComm *comm;
446 } MetaListRepresentation;
449 build_meta (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
451 MetaListRepresentation *repr = user_data;
454 repr->info = g_realloc (repr->info, repr->n_meta * sizeof (MetaBuildInfo));
455 repr->info[repr->n_meta - 1].bytes =
458 /* 4 byte GstMetaFlags */
460 /* GstMetaInfo::api */
461 + 4 + strlen (g_type_name ((*meta)->info->api)) + 1
462 /* GstMetaInfo::size */
467 repr->info[repr->n_meta - 1].flags = (*meta)->flags;
468 repr->info[repr->n_meta - 1].api = (*meta)->info->api;
469 repr->info[repr->n_meta - 1].size = (*meta)->info->size;
470 repr->info[repr->n_meta - 1].str = NULL;
472 /* GstMeta is a base class, and actual useful classes are all different...
473 So we list a few of them we know we want and ignore the open ended rest */
474 if ((*meta)->info->api == GST_PROTECTION_META_API_TYPE) {
475 GstProtectionMeta *m = (GstProtectionMeta *) * meta;
476 repr->info[repr->n_meta - 1].str = gst_structure_to_string (m->info);
477 repr->info[repr->n_meta - 1].bytes +=
478 strlen (repr->info[repr->n_meta - 1].str) + 1;
479 GST_TRACE_OBJECT (repr->comm->element, "Found GstMeta type %s: %s",
480 g_type_name ((*meta)->info->api), repr->info[repr->n_meta - 1].str);
482 GST_WARNING_OBJECT (repr->comm->element, "Ignoring GstMeta type %s",
483 g_type_name ((*meta)->info->api));
485 repr->total_bytes += repr->info[repr->n_meta - 1].bytes;
497 } CommBufferMetadata;
500 gst_ipc_pipeline_comm_write_buffer_to_fd (GstIpcPipelineComm * comm,
503 const unsigned char payload_type = GST_IPC_PIPELINE_COMM_DATA_TYPE_BUFFER;
505 guint32 ret32 = GST_FLOW_OK;
507 CommBufferMetadata meta;
509 MetaListRepresentation repr = { comm, 0, 4, NULL }; /* starts a 4 for n_meta */
512 g_mutex_lock (&comm->mutex);
515 GST_TRACE_OBJECT (comm->element, "Writing buffer %u: %" GST_PTR_FORMAT,
516 comm->send_id, buffer);
518 gst_byte_writer_init (&bw);
520 meta.pts = GST_BUFFER_PTS (buffer);
521 meta.dts = GST_BUFFER_DTS (buffer);
522 meta.duration = GST_BUFFER_DURATION (buffer);
523 meta.offset = GST_BUFFER_OFFSET (buffer);
524 meta.offset_end = GST_BUFFER_OFFSET_END (buffer);
525 meta.flags = GST_BUFFER_FLAGS (buffer);
527 /* work out meta size */
528 gst_buffer_foreach_meta (buffer, build_meta, &repr);
530 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
532 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
535 gst_buffer_get_size (buffer) + sizeof (guint32) +
536 sizeof (CommBufferMetadata) + repr.total_bytes;
537 if (!gst_byte_writer_put_uint32_le (&bw, size))
539 if (!gst_byte_writer_put_data (&bw, (const guint8 *) &meta, sizeof (meta)))
541 size = gst_buffer_get_size (buffer);
542 if (!gst_byte_writer_put_uint32_le (&bw, size))
544 if (!write_byte_writer_to_fd (comm, &bw))
547 if (!gst_buffer_map (buffer, &map, GST_MAP_READ))
549 ret = write_to_fd_raw (comm, map.data, map.size);
550 gst_buffer_unmap (buffer, &map);
555 gst_byte_writer_init (&bw);
556 if (!gst_byte_writer_put_uint32_le (&bw, repr.n_meta))
558 for (n = 0; n < repr.n_meta; ++n) {
559 const MetaBuildInfo *info = repr.info + n;
563 if (!gst_byte_writer_put_uint32_le (&bw, info->bytes))
566 if (!gst_byte_writer_put_uint32_le (&bw, info->flags))
569 s = g_type_name (info->api);
570 len = strlen (s) + 1;
571 if (!gst_byte_writer_put_uint32_le (&bw, len))
573 if (!gst_byte_writer_put_data (&bw, (const guint8 *) s, len))
576 if (!gst_byte_writer_put_uint64_le (&bw, info->size))
580 len = s ? (strlen (s) + 1) : 0;
581 if (!gst_byte_writer_put_uint32_le (&bw, len))
584 if (!gst_byte_writer_put_data (&bw, (const guint8 *) s, len))
588 if (!write_byte_writer_to_fd (comm, &bw))
591 if (!gst_ipc_pipeline_comm_sync_fd (comm, comm->send_id, NULL, &ret32,
592 ACK_TYPE_BLOCKING, COMM_REQUEST_TYPE_BUFFER))
597 g_mutex_unlock (&comm->mutex);
598 gst_byte_writer_reset (&bw);
599 for (n = 0; n < repr.n_meta; ++n)
600 g_free (repr.info[n].str);
605 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
606 ("Failed to write to socket"));
607 ret = GST_FLOW_COMM_ERROR;
611 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
612 ("Failed to wait for reply on socket"));
613 ret = GST_FLOW_COMM_ERROR;
617 GST_ELEMENT_ERROR (comm->element, RESOURCE, READ, (NULL),
618 ("Failed to map buffer"));
619 ret = GST_FLOW_ERROR;
624 gst_ipc_pipeline_comm_read_buffer (GstIpcPipelineComm * comm, guint32 size)
627 CommBufferMetadata meta;
629 const guint8 *payload = NULL;
630 guint32 mapped_size, buffer_data_size;
632 /* this should not be called if we don't have enough yet */
633 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, NULL);
634 g_return_val_if_fail (size >= sizeof (CommBufferMetadata), NULL);
636 mapped_size = sizeof (CommBufferMetadata) + sizeof (buffer_data_size);
637 payload = gst_adapter_map (comm->adapter, mapped_size);
640 memcpy (&meta, payload, sizeof (CommBufferMetadata));
641 payload += sizeof (CommBufferMetadata);
642 memcpy (&buffer_data_size, payload, sizeof (buffer_data_size));
644 gst_adapter_unmap (comm->adapter);
645 gst_adapter_flush (comm->adapter, mapped_size);
647 if (buffer_data_size == 0) {
648 buffer = gst_buffer_new ();
650 buffer = gst_adapter_get_buffer (comm->adapter, buffer_data_size);
651 gst_adapter_flush (comm->adapter, buffer_data_size);
653 size -= buffer_data_size;
655 GST_BUFFER_PTS (buffer) = meta.pts;
656 GST_BUFFER_DTS (buffer) = meta.dts;
657 GST_BUFFER_DURATION (buffer) = meta.duration;
658 GST_BUFFER_OFFSET (buffer) = meta.offset;
659 GST_BUFFER_OFFSET_END (buffer) = meta.offset_end;
660 GST_BUFFER_FLAGS (buffer) = meta.flags;
662 /* If you don't call that, the GType isn't yet known at the
663 g_type_from_name below */
664 gst_protection_meta_get_info ();
667 payload = gst_adapter_map (comm->adapter, mapped_size);
669 gst_buffer_unref (buffer);
672 memcpy (&n_meta, payload, sizeof (n_meta));
673 payload += sizeof (n_meta);
675 for (n = 0; n < n_meta; ++n) {
676 guint32 flags, len, bytes;
680 GstStructure *structure = NULL;
682 memcpy (&bytes, payload, sizeof (bytes));
683 payload += sizeof (bytes);
685 #define READ_FIELD(f) do { \
686 memcpy (&f, payload, sizeof (f)); \
687 payload += sizeof(f); \
692 api = g_type_from_name ((const char *) payload);
693 payload = (const guint8 *) strchr ((const char *) payload, 0) + 1;
697 structure = gst_structure_new_from_string ((const char *) payload);
701 /* Seems we can add a meta from the api nor type ? */
702 if (api == GST_PROTECTION_META_API_TYPE) {
704 gst_buffer_add_meta (buffer, gst_protection_meta_get_info (), NULL);
705 ((GstProtectionMeta *) meta)->info = structure;
707 GST_WARNING_OBJECT (comm->element, "Unsupported meta: %s",
710 gst_structure_free (structure);
717 gst_adapter_unmap (comm->adapter);
718 gst_adapter_flush (comm->adapter, mapped_size);
724 gst_ipc_pipeline_comm_write_sink_message_event_to_fd (GstIpcPipelineComm * comm,
727 const unsigned char payload_type =
728 GST_IPC_PIPELINE_COMM_DATA_TYPE_SINK_MESSAGE_EVENT;
730 guint32 type, size, eseqnum, mseqnum, ret32 = TRUE, slen, structure_slen;
732 const GstStructure *structure;
733 GstMessage *message = NULL;
737 g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SINK_MESSAGE,
740 g_mutex_lock (&comm->mutex);
743 GST_TRACE_OBJECT (comm->element,
744 "Writing sink message event %u: %" GST_PTR_FORMAT, comm->send_id, event);
746 gst_byte_writer_init (&bw);
747 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
749 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
751 name = gst_structure_get_name (gst_event_get_structure (event));
752 slen = strlen (name) + 1;
753 gst_event_parse_sink_message (event, &message);
754 structure = gst_message_get_structure (message);
756 str = gst_structure_to_string (structure);
757 structure_slen = strlen (str);
762 size = sizeof (type) + sizeof (eseqnum) + sizeof (mseqnum) + sizeof (slen) +
763 strlen (name) + 1 + structure_slen + 1;
764 if (!gst_byte_writer_put_uint32_le (&bw, size))
767 type = GST_MESSAGE_TYPE (message);
768 if (!gst_byte_writer_put_uint32_le (&bw, type))
770 size -= sizeof (type);
772 eseqnum = GST_EVENT_SEQNUM (event);
773 if (!gst_byte_writer_put_uint32_le (&bw, eseqnum))
775 size -= sizeof (eseqnum);
777 mseqnum = GST_MESSAGE_SEQNUM (message);
778 if (!gst_byte_writer_put_uint32_le (&bw, mseqnum))
780 size -= sizeof (mseqnum);
782 if (!gst_byte_writer_put_uint32_le (&bw, slen))
784 size -= sizeof (slen);
786 if (!gst_byte_writer_put_data (&bw, (const guint8 *) name, slen))
791 if (!gst_byte_writer_put_data (&bw, (const guint8 *) str, size))
794 if (!gst_byte_writer_put_uint8 (&bw, 0))
798 if (!write_byte_writer_to_fd (comm, &bw))
801 if (!gst_ipc_pipeline_comm_sync_fd (comm, comm->send_id, NULL, &ret32,
802 GST_EVENT_IS_SERIALIZED (event) ? ACK_TYPE_BLOCKING : ACK_TYPE_TIMED,
803 COMM_REQUEST_TYPE_EVENT))
809 g_mutex_unlock (&comm->mutex);
810 gst_byte_writer_reset (&bw);
813 gst_message_unref (message);
817 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
818 ("Failed to write to socket"));
824 gst_ipc_pipeline_comm_read_sink_message_event (GstIpcPipelineComm * comm,
828 GstEvent *event = NULL;
830 GstStructure *structure;
831 guint32 type, eseqnum, mseqnum, slen;
833 guint32 mapped_size = size;
834 const guint8 *payload;
836 /* this should not be called if we don't have enough yet */
837 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, NULL);
838 g_return_val_if_fail (size >= sizeof (type) + sizeof (slen), NULL);
840 payload = gst_adapter_map (comm->adapter, mapped_size);
843 memcpy (&type, payload, sizeof (type));
844 payload += sizeof (type);
845 size -= sizeof (type);
849 memcpy (&eseqnum, payload, sizeof (eseqnum));
850 payload += sizeof (eseqnum);
851 size -= sizeof (eseqnum);
855 memcpy (&mseqnum, payload, sizeof (mseqnum));
856 payload += sizeof (mseqnum);
857 size -= sizeof (mseqnum);
861 memcpy (&slen, payload, sizeof (slen));
862 payload += sizeof (slen);
863 size -= sizeof (slen);
867 if (payload[slen - 1])
869 name = (const char *) payload;
873 if ((payload)[size - 1]) {
877 structure = gst_structure_from_string ((const char *) payload, &end);
883 gst_message_new_custom (type, GST_OBJECT (comm->element), structure);
884 gst_message_set_seqnum (message, mseqnum);
885 event = gst_event_new_sink_message (name, message);
886 gst_event_set_seqnum (event, eseqnum);
887 gst_message_unref (message);
890 gst_adapter_unmap (comm->adapter);
891 gst_adapter_flush (comm->adapter, mapped_size);
896 gst_ipc_pipeline_comm_write_event_to_fd (GstIpcPipelineComm * comm,
897 gboolean upstream, GstEvent * event)
899 const unsigned char payload_type = GST_IPC_PIPELINE_COMM_DATA_TYPE_EVENT;
901 guint32 type, size, ret32 = TRUE, seqnum, slen;
903 const GstStructure *structure;
906 /* we special case sink-message event as gst can't serialize/de-serialize it */
907 if (GST_EVENT_TYPE (event) == GST_EVENT_SINK_MESSAGE)
908 return gst_ipc_pipeline_comm_write_sink_message_event_to_fd (comm, event);
910 g_mutex_lock (&comm->mutex);
913 GST_TRACE_OBJECT (comm->element, "Writing event %u: %" GST_PTR_FORMAT,
914 comm->send_id, event);
916 gst_byte_writer_init (&bw);
917 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
919 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
921 structure = gst_event_get_structure (event);
924 if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
925 GstStructure *s = gst_structure_copy (structure);
926 gst_structure_remove_field (s, "stream");
927 str = gst_structure_to_string (s);
928 gst_structure_free (s);
930 str = gst_structure_to_string (structure);
938 size = sizeof (type) + sizeof (seqnum) + 1 + slen + 1;
939 if (!gst_byte_writer_put_uint32_le (&bw, size))
942 type = GST_EVENT_TYPE (event);
943 if (!gst_byte_writer_put_uint32_le (&bw, type))
946 seqnum = GST_EVENT_SEQNUM (event);
947 if (!gst_byte_writer_put_uint32_le (&bw, seqnum))
950 if (!gst_byte_writer_put_uint8 (&bw, upstream ? 1 : 0))
954 if (!gst_byte_writer_put_data (&bw, (const guint8 *) str, slen + 1))
957 if (!gst_byte_writer_put_uint8 (&bw, 0))
961 if (!write_byte_writer_to_fd (comm, &bw))
964 /* Upstream events get serialized, this is required to send seeks only
966 if (!gst_ipc_pipeline_comm_sync_fd (comm, comm->send_id, NULL, &ret32,
967 (GST_EVENT_IS_SERIALIZED (event) || GST_EVENT_IS_UPSTREAM (event)) ?
968 ACK_TYPE_BLOCKING : ACK_TYPE_NONE, COMM_REQUEST_TYPE_EVENT))
973 g_mutex_unlock (&comm->mutex);
975 gst_byte_writer_reset (&bw);
979 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
980 ("Failed to write to socket"));
986 gst_ipc_pipeline_comm_read_event (GstIpcPipelineComm * comm, guint32 size,
989 GstEvent *event = NULL;
991 GstStructure *structure;
992 guint32 type, seqnum;
993 guint32 mapped_size = size;
994 const guint8 *payload;
996 /* this should not be called if we don't have enough yet */
997 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, NULL);
998 g_return_val_if_fail (size >= sizeof (type), NULL);
1000 payload = gst_adapter_map (comm->adapter, mapped_size);
1004 memcpy (&type, payload, sizeof (type));
1005 payload += sizeof (type);
1006 size -= sizeof (type);
1010 memcpy (&seqnum, payload, sizeof (seqnum));
1011 payload += sizeof (seqnum);
1012 size -= sizeof (seqnum);
1016 *upstream = (*payload) ? TRUE : FALSE;
1022 if (payload[size - 1])
1025 structure = gst_structure_from_string ((const char *) payload, &end);
1030 event = gst_event_new_custom (type, structure);
1031 gst_event_set_seqnum (event, seqnum);
1034 gst_adapter_unmap (comm->adapter);
1035 gst_adapter_flush (comm->adapter, mapped_size);
1040 gst_ipc_pipeline_comm_write_query_to_fd (GstIpcPipelineComm * comm,
1041 gboolean upstream, GstQuery * query)
1043 const unsigned char payload_type = GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY;
1045 guint32 type, size, ret32 = TRUE, slen;
1047 const GstStructure *structure;
1050 g_mutex_lock (&comm->mutex);
1053 GST_TRACE_OBJECT (comm->element, "Writing query %u: %" GST_PTR_FORMAT,
1054 comm->send_id, query);
1056 gst_byte_writer_init (&bw);
1057 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
1059 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
1061 structure = gst_query_get_structure (query);
1063 str = gst_structure_to_string (structure);
1064 slen = strlen (str);
1069 size = sizeof (type) + 1 + slen + 1;
1070 if (!gst_byte_writer_put_uint32_le (&bw, size))
1073 type = GST_QUERY_TYPE (query);
1074 if (!gst_byte_writer_put_uint32_le (&bw, type))
1077 if (!gst_byte_writer_put_uint8 (&bw, upstream ? 1 : 0))
1081 if (!gst_byte_writer_put_data (&bw, (const guint8 *) str, slen + 1))
1084 if (!gst_byte_writer_put_uint8 (&bw, 0))
1088 if (!write_byte_writer_to_fd (comm, &bw))
1091 if (!gst_ipc_pipeline_comm_sync_fd (comm, comm->send_id, query, &ret32,
1092 GST_QUERY_IS_SERIALIZED (query) ? ACK_TYPE_BLOCKING : ACK_TYPE_TIMED,
1093 COMM_REQUEST_TYPE_QUERY))
1099 g_mutex_unlock (&comm->mutex);
1101 gst_byte_writer_reset (&bw);
1105 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
1106 ("Failed to write to socket"));
1112 gst_ipc_pipeline_comm_read_query (GstIpcPipelineComm * comm, guint32 size,
1113 gboolean * upstream)
1115 GstQuery *query = NULL;
1117 GstStructure *structure;
1119 guint32 mapped_size = size;
1120 const guint8 *payload;
1122 /* this should not be called if we don't have enough yet */
1123 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, NULL);
1124 g_return_val_if_fail (size >= sizeof (type), NULL);
1126 payload = gst_adapter_map (comm->adapter, mapped_size);
1130 memcpy (&type, payload, sizeof (type));
1131 payload += sizeof (type);
1132 size -= sizeof (type);
1136 *upstream = (*payload) ? TRUE : FALSE;
1142 if (payload[size - 1])
1145 structure = gst_structure_from_string ((const char *) payload, &end);
1150 query = gst_query_new_custom (type, structure);
1152 /* CAPS queries contain a filter field, of GstCaps type, which can be NULL.
1153 This does not play well with the serialization/deserialization system,
1154 which will give us a non-NULL GstCaps which has a value of NULL. This
1155 in turn wreaks havoc with any code that tests whether filter is NULL
1156 (which basically means, am I being given an optional GstCaps ?).
1157 So we look for non-NULL GstCaps which have NULL contents, and replace
1158 them with NULL instead. */
1159 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1161 gst_query_parse_caps (query, &filter);
1163 && !strcmp (gst_structure_get_name (gst_caps_get_structure (filter, 0)),
1165 gst_query_unref (query);
1166 query = gst_query_new_caps (NULL);
1171 gst_adapter_unmap (comm->adapter);
1172 gst_adapter_flush (comm->adapter, mapped_size);
1176 GstStateChangeReturn
1177 gst_ipc_pipeline_comm_write_state_change_to_fd (GstIpcPipelineComm * comm,
1178 GstStateChange transition)
1180 const unsigned char payload_type =
1181 GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_CHANGE;
1182 GstStateChangeReturn ret;
1183 guint32 size, ret32 = GST_STATE_CHANGE_SUCCESS;
1186 g_mutex_lock (&comm->mutex);
1189 GST_TRACE_OBJECT (comm->element, "Writing state change %u: %s -> %s",
1191 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1192 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1194 gst_byte_writer_init (&bw);
1195 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
1197 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
1199 size = sizeof (transition);
1200 if (!gst_byte_writer_put_uint32_le (&bw, size))
1202 if (!gst_byte_writer_put_uint32_le (&bw, transition))
1205 if (!write_byte_writer_to_fd (comm, &bw))
1208 if (!gst_ipc_pipeline_comm_sync_fd (comm, comm->send_id, NULL, &ret32,
1209 ACK_TYPE_TIMED, COMM_REQUEST_TYPE_STATE_CHANGE))
1214 g_mutex_unlock (&comm->mutex);
1215 gst_byte_writer_reset (&bw);
1219 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
1220 ("Failed to write to socket"));
1221 ret = GST_STATE_CHANGE_FAILURE;
1226 is_valid_state_change (GstStateChange transition)
1228 if (transition == GST_STATE_CHANGE_NULL_TO_READY)
1230 if (transition == GST_STATE_CHANGE_READY_TO_PAUSED)
1232 if (transition == GST_STATE_CHANGE_PAUSED_TO_PLAYING)
1234 if (transition == GST_STATE_CHANGE_PLAYING_TO_PAUSED)
1236 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1238 if (transition == GST_STATE_CHANGE_READY_TO_NULL)
1240 if (GST_STATE_TRANSITION_CURRENT (transition) ==
1241 GST_STATE_TRANSITION_NEXT (transition))
1247 gst_ipc_pipeline_comm_read_state_change (GstIpcPipelineComm * comm,
1248 guint32 size, guint32 * transition)
1250 guint32 mapped_size = size;
1251 const guint8 *payload;
1253 /* this should not be called if we don't have enough yet */
1254 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, FALSE);
1255 g_return_val_if_fail (size >= sizeof (*transition), FALSE);
1257 payload = gst_adapter_map (comm->adapter, size);
1260 memcpy (transition, payload, sizeof (*transition));
1261 gst_adapter_unmap (comm->adapter);
1262 gst_adapter_flush (comm->adapter, mapped_size);
1263 return is_valid_state_change (*transition);
1267 gst_ipc_pipeline_comm_write_state_lost_to_fd (GstIpcPipelineComm * comm)
1269 const unsigned char payload_type = GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_LOST;
1273 g_mutex_lock (&comm->mutex);
1276 GST_TRACE_OBJECT (comm->element, "Writing state-lost %u", comm->send_id);
1277 gst_byte_writer_init (&bw);
1278 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
1280 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
1283 if (!gst_byte_writer_put_uint32_le (&bw, size))
1286 if (!write_byte_writer_to_fd (comm, &bw))
1290 g_mutex_unlock (&comm->mutex);
1291 gst_byte_writer_reset (&bw);
1295 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
1296 ("Failed to write to socket"));
1301 gst_ipc_pipeline_comm_read_state_lost (GstIpcPipelineComm * comm, guint32 size)
1308 gst_ipc_pipeline_comm_write_gerror_message_to_fd (GstIpcPipelineComm * comm,
1309 GstMessage * message)
1311 const unsigned char payload_type =
1312 GST_IPC_PIPELINE_COMM_DATA_TYPE_GERROR_MESSAGE;
1314 guint32 code, size, ret32 = TRUE;
1317 char *extra_message;
1318 const char *domain_string;
1319 unsigned char msgtype;
1322 g_mutex_lock (&comm->mutex);
1325 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
1326 gst_message_parse_error (message, &error, &extra_message);
1328 } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) {
1329 gst_message_parse_warning (message, &error, &extra_message);
1332 gst_message_parse_info (message, &error, &extra_message);
1336 domain_string = g_quark_to_string (error->domain);
1337 GST_TRACE_OBJECT (comm->element,
1338 "Writing error %u: domain %s, code %u, message %s, extra message %s",
1339 comm->send_id, domain_string, error->code, error->message, extra_message);
1341 gst_byte_writer_init (&bw);
1342 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
1344 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
1347 size = sizeof (size);
1349 size += strlen (domain_string) + 1;
1350 size += sizeof (code);
1351 size += sizeof (size);
1352 size += error->message ? strlen (error->message) + 1 : 0;
1353 size += sizeof (size);
1354 size += extra_message ? strlen (extra_message) + 1 : 0;
1356 if (!gst_byte_writer_put_uint32_le (&bw, size))
1359 if (!gst_byte_writer_put_uint8 (&bw, msgtype))
1361 size = strlen (domain_string) + 1;
1362 if (!gst_byte_writer_put_uint32_le (&bw, size))
1364 if (!gst_byte_writer_put_data (&bw, (const guint8 *) domain_string, size))
1366 if (!gst_byte_writer_put_uint32_le (&bw, code))
1368 size = error->message ? strlen (error->message) + 1 : 0;
1369 if (!gst_byte_writer_put_uint32_le (&bw, size))
1371 if (error->message) {
1372 if (!gst_byte_writer_put_data (&bw, (const guint8 *) error->message, size))
1375 size = extra_message ? strlen (extra_message) + 1 : 0;
1376 if (!gst_byte_writer_put_uint32_le (&bw, size))
1378 if (extra_message) {
1379 if (!gst_byte_writer_put_data (&bw, (const guint8 *) extra_message, size))
1383 if (!write_byte_writer_to_fd (comm, &bw))
1386 if (!gst_ipc_pipeline_comm_sync_fd (comm, comm->send_id, NULL, &ret32,
1387 ACK_TYPE_NONE, COMM_REQUEST_TYPE_MESSAGE))
1393 g_mutex_unlock (&comm->mutex);
1395 g_error_free (error);
1396 g_free (extra_message);
1397 gst_byte_writer_reset (&bw);
1401 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
1402 ("Failed to write to socket"));
1408 gst_ipc_pipeline_comm_read_gerror_message (GstIpcPipelineComm * comm,
1411 GstMessage *message = NULL;
1414 const char *msg, *extra_message;
1416 unsigned char msgtype;
1417 guint32 mapped_size = size;
1418 const guint8 *payload;
1420 /* this should not be called if we don't have enough yet */
1421 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, NULL);
1422 g_return_val_if_fail (size >= sizeof (code) + sizeof (size) * 3 + 1 + 1,
1425 payload = gst_adapter_map (comm->adapter, mapped_size);
1428 msgtype = *payload++;
1429 memcpy (&size, payload, sizeof (size));
1430 payload += sizeof (size);
1431 if (payload[size - 1])
1433 domain = g_quark_from_string ((const char *) payload);
1436 memcpy (&code, payload, sizeof (code));
1437 payload += sizeof (code);
1439 memcpy (&size, payload, sizeof (size));
1440 payload += sizeof (size);
1442 if (payload[size - 1])
1444 msg = (const char *) payload;
1450 memcpy (&size, payload, sizeof (size));
1451 payload += sizeof (size);
1453 if (payload[size - 1])
1455 extra_message = (const char *) payload;
1457 extra_message = NULL;
1461 error = g_error_new (domain, code, "%s", msg);
1464 gst_message_new_error (GST_OBJECT (comm->element), error,
1466 else if (msgtype == 1)
1468 gst_message_new_warning (GST_OBJECT (comm->element), error,
1472 gst_message_new_info (GST_OBJECT (comm->element), error, extra_message);
1473 g_error_free (error);
1476 gst_adapter_unmap (comm->adapter);
1477 gst_adapter_flush (comm->adapter, mapped_size);
1483 gst_ipc_pipeline_comm_write_message_to_fd (GstIpcPipelineComm * comm,
1484 GstMessage * message)
1486 const unsigned char payload_type = GST_IPC_PIPELINE_COMM_DATA_TYPE_MESSAGE;
1488 guint32 type, size, ret32 = TRUE, slen;
1490 const GstStructure *structure;
1493 /* we special case error as gst can't serialize/de-serialize it */
1494 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR
1495 || GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING
1496 || GST_MESSAGE_TYPE (message) == GST_MESSAGE_INFO)
1497 return gst_ipc_pipeline_comm_write_gerror_message_to_fd (comm, message);
1499 g_mutex_lock (&comm->mutex);
1502 GST_TRACE_OBJECT (comm->element, "Writing message %u: %" GST_PTR_FORMAT,
1503 comm->send_id, message);
1505 gst_byte_writer_init (&bw);
1506 if (!gst_byte_writer_put_uint8 (&bw, payload_type))
1508 if (!gst_byte_writer_put_uint32_le (&bw, comm->send_id))
1510 structure = gst_message_get_structure (message);
1512 str = gst_structure_to_string (structure);
1513 slen = strlen (str);
1518 size = sizeof (type) + slen + 1;
1519 if (!gst_byte_writer_put_uint32_le (&bw, size))
1522 type = GST_MESSAGE_TYPE (message);
1523 if (!gst_byte_writer_put_uint32_le (&bw, type))
1525 size -= sizeof (type);
1527 if (!gst_byte_writer_put_data (&bw, (const guint8 *) str, size))
1530 if (!gst_byte_writer_put_uint8 (&bw, 0))
1534 if (!write_byte_writer_to_fd (comm, &bw))
1537 if (!gst_ipc_pipeline_comm_sync_fd (comm, comm->send_id, NULL, &ret32,
1538 ACK_TYPE_NONE, COMM_REQUEST_TYPE_MESSAGE))
1544 g_mutex_unlock (&comm->mutex);
1546 gst_byte_writer_reset (&bw);
1550 GST_ELEMENT_ERROR (comm->element, RESOURCE, WRITE, (NULL),
1551 ("Failed to write to socket"));
1557 gst_ipc_pipeline_comm_read_message (GstIpcPipelineComm * comm, guint32 size)
1559 GstMessage *message = NULL;
1561 GstStructure *structure;
1563 guint32 mapped_size = size;
1564 const guint8 *payload;
1566 /* this should not be called if we don't have enough yet */
1567 g_return_val_if_fail (gst_adapter_available (comm->adapter) >= size, NULL);
1568 g_return_val_if_fail (size >= sizeof (type), NULL);
1570 payload = gst_adapter_map (comm->adapter, mapped_size);
1573 memcpy (&type, payload, sizeof (type));
1574 payload += sizeof (type);
1575 size -= sizeof (type);
1579 if (payload[size - 1])
1582 structure = gst_structure_from_string ((const char *) payload, &end);
1588 gst_message_new_custom (type, GST_OBJECT (comm->element), structure);
1591 gst_adapter_unmap (comm->adapter);
1592 gst_adapter_flush (comm->adapter, mapped_size);
1598 gst_ipc_pipeline_comm_init (GstIpcPipelineComm * comm, GstElement * element)
1600 g_mutex_init (&comm->mutex);
1601 comm->element = element;
1602 comm->fdin = comm->fdout = -1;
1603 comm->ack_time = DEFAULT_ACK_TIME;
1605 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
1606 (GDestroyNotify) comm_request_free);
1607 comm->adapter = gst_adapter_new ();
1608 comm->poll = gst_poll_new (TRUE);
1609 gst_poll_fd_init (&comm->pollFDin);
1613 gst_ipc_pipeline_comm_clear (GstIpcPipelineComm * comm)
1615 g_hash_table_destroy (comm->waiting_ids);
1616 gst_object_unref (comm->adapter);
1617 gst_poll_free (comm->poll);
1618 g_mutex_clear (&comm->mutex);
1622 cancel_request (gpointer key, gpointer value, gpointer user_data,
1625 GstIpcPipelineComm *comm = (GstIpcPipelineComm *) user_data;
1626 guint32 id = GPOINTER_TO_INT (key);
1627 CommRequest *req = (CommRequest *) value;
1629 GST_TRACE_OBJECT (comm->element, "Cancelling request %u, type %d", id,
1632 req->replied = TRUE;
1633 g_cond_signal (&req->cond);
1637 cancel_request_error (gpointer key, gpointer value, gpointer user_data)
1639 CommRequest *req = (CommRequest *) value;
1640 GstFlowReturn fret = comm_request_ret_get_failure_value (req->type);
1642 cancel_request (key, value, user_data, fret);
1646 gst_ipc_pipeline_comm_cancel (GstIpcPipelineComm * comm, gboolean cleanup)
1648 g_mutex_lock (&comm->mutex);
1649 g_hash_table_foreach (comm->waiting_ids, cancel_request_error, comm);
1651 g_hash_table_unref (comm->waiting_ids);
1653 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
1654 (GDestroyNotify) comm_request_free);
1656 g_mutex_unlock (&comm->mutex);
1660 set_field (GQuark field_id, const GValue * value, gpointer user_data)
1662 GstStructure *structure = user_data;
1664 gst_structure_id_set_value (structure, field_id, value);
1670 gst_ipc_pipeline_comm_reply_request (GstIpcPipelineComm * comm, guint32 id,
1671 GstFlowReturn ret, GstQuery * query)
1675 req = g_hash_table_lookup (comm->waiting_ids, GINT_TO_POINTER (id));
1677 GST_WARNING_OBJECT (comm->element, "Got reply for unknown request %u", id);
1681 GST_TRACE_OBJECT (comm->element, "Got reply %d (%s) for request %u", ret,
1682 comm_request_ret_get_name (req->type, ret), req->id);
1683 req->replied = TRUE;
1687 /* We need to update the original query in place, as the caller
1688 will expect the object to be the same */
1689 GstStructure *structure = gst_query_writable_structure (req->query);
1690 gst_structure_remove_all_fields (structure);
1691 gst_structure_foreach (gst_query_get_structure (query), set_field,
1694 GST_WARNING_OBJECT (comm->element,
1695 "Got query reply, but no query was in the request");
1698 g_cond_signal (&req->cond);
1703 update_adapter (GstIpcPipelineComm * comm)
1705 GstMemory *mem = NULL;
1712 /* update pollFDin if necessary (fdin changed or we lost our parent).
1713 * we do not allow a parent-less element to communicate with its peer
1714 * in order to avoid race conditions where the slave tries to change
1715 * the state of its parent pipeline while it is not yet added in that
1717 if (comm->pollFDin.fd != comm->fdin || !GST_OBJECT_PARENT (comm->element)) {
1718 if (comm->pollFDin.fd != -1) {
1719 GST_DEBUG_OBJECT (comm->element, "Stop watching fd %d",
1721 gst_poll_remove_fd (comm->poll, &comm->pollFDin);
1722 gst_poll_fd_init (&comm->pollFDin);
1724 if (comm->fdin != -1 && GST_OBJECT_PARENT (comm->element)) {
1725 GST_DEBUG_OBJECT (comm->element, "Start watching fd %d", comm->fdin);
1726 comm->pollFDin.fd = comm->fdin;
1727 gst_poll_add_fd (comm->poll, &comm->pollFDin);
1728 gst_poll_fd_ctl_read (comm->poll, &comm->pollFDin, TRUE);
1732 /* wait for activity on fdin or a flush */
1733 if (gst_poll_wait (comm->poll, 100 * GST_MSECOND) < 0) {
1734 if (errno == EAGAIN)
1736 /* error out, unless interrupted or flushing */
1738 ret = (errno == EBUSY) ? 2 : 1;
1741 /* read from fdin if possible and push data to our adapter */
1742 if (comm->pollFDin.fd >= 0
1743 && gst_poll_fd_can_read (comm->poll, &comm->pollFDin)) {
1745 mem = gst_allocator_alloc (NULL, comm->read_chunk_size, NULL);
1747 gst_memory_map (mem, &map, GST_MAP_WRITE);
1748 sz = read (comm->pollFDin.fd, map.data, map.size);
1749 gst_memory_unmap (mem, &map);
1752 if (errno == EAGAIN)
1754 /* error out, unless interrupted */
1758 gst_memory_resize (mem, 0, sz);
1759 buf = gst_buffer_new ();
1760 gst_buffer_append_memory (buf, mem);
1762 GST_TRACE_OBJECT (comm->element, "Read %u bytes from fd", (unsigned) sz);
1763 gst_adapter_push (comm->adapter, buf);
1768 gst_memory_unref (mem);
1774 read_many (GstIpcPipelineComm * comm)
1776 gboolean ret = TRUE;
1778 const guint8 *payload;
1781 switch (comm->state) {
1782 case GST_IPC_PIPELINE_COMM_STATE_TYPE:
1785 guint32 mapped_size;
1787 available = gst_adapter_available (comm->adapter);
1788 mapped_size = 1 + sizeof (gint32) * 2;
1789 if (available < mapped_size)
1792 payload = gst_adapter_map (comm->adapter, mapped_size);
1794 g_mutex_lock (&comm->mutex);
1795 memcpy (&comm->id, payload, sizeof (guint32));
1796 memcpy (&comm->payload_length, payload + 4, sizeof (guint32));
1797 g_mutex_unlock (&comm->mutex);
1798 gst_adapter_unmap (comm->adapter);
1799 gst_adapter_flush (comm->adapter, mapped_size);
1800 GST_TRACE_OBJECT (comm->element, "Got id %u, type %d, payload %u",
1801 comm->id, type, comm->payload_length);
1803 case GST_IPC_PIPELINE_COMM_DATA_TYPE_ACK:
1804 case GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY_RESULT:
1805 case GST_IPC_PIPELINE_COMM_DATA_TYPE_BUFFER:
1806 case GST_IPC_PIPELINE_COMM_DATA_TYPE_EVENT:
1807 case GST_IPC_PIPELINE_COMM_DATA_TYPE_SINK_MESSAGE_EVENT:
1808 case GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY:
1809 case GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_CHANGE:
1810 case GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_LOST:
1811 case GST_IPC_PIPELINE_COMM_DATA_TYPE_MESSAGE:
1812 case GST_IPC_PIPELINE_COMM_DATA_TYPE_GERROR_MESSAGE:
1813 GST_TRACE_OBJECT (comm->element, "switching to state %s",
1814 gst_ipc_pipeline_comm_data_type_get_name (type));
1822 case GST_IPC_PIPELINE_COMM_DATA_TYPE_ACK:
1827 available = gst_adapter_available (comm->adapter);
1828 if (available < comm->payload_length)
1831 if (available < sizeof (guint32))
1834 rets = gst_adapter_map (comm->adapter, sizeof (guint32));
1835 memcpy (&ret32, rets, sizeof (ret32));
1836 gst_adapter_unmap (comm->adapter);
1837 gst_adapter_flush (comm->adapter, sizeof (guint32));
1838 GST_TRACE_OBJECT (comm->element, "Got ACK %s for id %u",
1839 gst_flow_get_name (ret32), comm->id);
1841 g_mutex_lock (&comm->mutex);
1842 gst_ipc_pipeline_comm_reply_request (comm, comm->id, ret32, NULL);
1843 g_mutex_unlock (&comm->mutex);
1845 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
1846 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
1849 case GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY_RESULT:
1851 GstQuery *query = NULL;
1854 available = gst_adapter_available (comm->adapter);
1855 if (available < comm->payload_length)
1859 gst_ipc_pipeline_comm_read_query_result (comm, comm->payload_length,
1862 GST_TRACE_OBJECT (comm->element,
1863 "deserialized query result %p: %d, %" GST_PTR_FORMAT, query, qret,
1866 g_mutex_lock (&comm->mutex);
1867 gst_ipc_pipeline_comm_reply_request (comm, comm->id, qret, query);
1868 g_mutex_unlock (&comm->mutex);
1870 gst_query_unref (query);
1872 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
1873 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
1876 case GST_IPC_PIPELINE_COMM_DATA_TYPE_BUFFER:
1880 available = gst_adapter_available (comm->adapter);
1881 if (available < comm->payload_length)
1884 buf = gst_ipc_pipeline_comm_read_buffer (comm, comm->payload_length);
1888 /* set caps and push */
1889 GST_TRACE_OBJECT (comm->element,
1890 "deserialized buffer %p, pushing, timestamp %" GST_TIME_FORMAT
1891 ", duration %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT
1892 ", offset_end %" G_GINT64_FORMAT ", size %" G_GSIZE_FORMAT
1893 ", flags 0x%x", buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1894 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
1895 GST_BUFFER_OFFSET_END (buf), gst_buffer_get_size (buf),
1896 GST_BUFFER_FLAGS (buf));
1898 gst_mini_object_set_qdata (GST_MINI_OBJECT (buf), QUARK_ID,
1899 GINT_TO_POINTER (comm->id), NULL);
1901 if (comm->on_buffer)
1902 (*comm->on_buffer) (comm->id, buf, comm->user_data);
1904 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
1905 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
1908 case GST_IPC_PIPELINE_COMM_DATA_TYPE_EVENT:
1913 available = gst_adapter_available (comm->adapter);
1914 if (available < comm->payload_length)
1917 event = gst_ipc_pipeline_comm_read_event (comm, comm->payload_length,
1922 GST_TRACE_OBJECT (comm->element, "deserialized event %p of type %s",
1923 event, gst_event_type_get_name (event->type));
1925 gst_mini_object_set_qdata (GST_MINI_OBJECT (event), QUARK_ID,
1926 GINT_TO_POINTER (comm->id), NULL);
1929 (*comm->on_event) (comm->id, event, upstream, comm->user_data);
1931 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
1932 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
1935 case GST_IPC_PIPELINE_COMM_DATA_TYPE_SINK_MESSAGE_EVENT:
1939 available = gst_adapter_available (comm->adapter);
1940 if (available < comm->payload_length)
1943 event = gst_ipc_pipeline_comm_read_sink_message_event (comm,
1944 comm->payload_length);
1948 GST_TRACE_OBJECT (comm->element, "deserialized sink message event %p",
1951 gst_mini_object_set_qdata (GST_MINI_OBJECT (event), QUARK_ID,
1952 GINT_TO_POINTER (comm->id), NULL);
1955 (*comm->on_event) (comm->id, event, FALSE, comm->user_data);
1957 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
1958 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
1961 case GST_IPC_PIPELINE_COMM_DATA_TYPE_QUERY:
1966 available = gst_adapter_available (comm->adapter);
1967 if (available < comm->payload_length)
1970 query = gst_ipc_pipeline_comm_read_query (comm, comm->payload_length,
1975 GST_TRACE_OBJECT (comm->element, "deserialized query %p of type %s",
1976 query, gst_query_type_get_name (query->type));
1978 gst_mini_object_set_qdata (GST_MINI_OBJECT (query), QUARK_ID,
1979 GINT_TO_POINTER (comm->id), NULL);
1982 (*comm->on_query) (comm->id, query, upstream, comm->user_data);
1984 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
1985 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
1988 case GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_CHANGE:
1992 available = gst_adapter_available (comm->adapter);
1993 if (available < comm->payload_length)
1996 if (!gst_ipc_pipeline_comm_read_state_change (comm,
1997 comm->payload_length, &transition))
1998 goto state_change_failed;
2000 GST_TRACE_OBJECT (comm->element,
2001 "deserialized state change request: %s -> %s",
2002 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT
2004 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT
2007 if (comm->on_state_change)
2008 (*comm->on_state_change) (comm->id, transition, comm->user_data);
2010 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
2011 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
2014 case GST_IPC_PIPELINE_COMM_DATA_TYPE_STATE_LOST:
2016 available = gst_adapter_available (comm->adapter);
2017 if (available < comm->payload_length)
2020 if (!gst_ipc_pipeline_comm_read_state_lost (comm, comm->payload_length))
2023 GST_TRACE_OBJECT (comm->element, "deserialized state-lost");
2025 if (comm->on_state_lost)
2026 (*comm->on_state_lost) (comm->user_data);
2028 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
2029 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
2032 case GST_IPC_PIPELINE_COMM_DATA_TYPE_MESSAGE:
2034 GstMessage *message;
2036 available = gst_adapter_available (comm->adapter);
2037 if (available < comm->payload_length)
2040 message = gst_ipc_pipeline_comm_read_message (comm,
2041 comm->payload_length);
2043 goto message_failed;
2045 GST_TRACE_OBJECT (comm->element, "deserialized message %p of type %s",
2046 message, gst_message_type_get_name (message->type));
2048 if (comm->on_message)
2049 (*comm->on_message) (comm->id, message, comm->user_data);
2051 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
2052 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
2055 case GST_IPC_PIPELINE_COMM_DATA_TYPE_GERROR_MESSAGE:
2057 GstMessage *message;
2059 available = gst_adapter_available (comm->adapter);
2060 if (available < comm->payload_length)
2063 message = gst_ipc_pipeline_comm_read_gerror_message (comm,
2064 comm->payload_length);
2066 goto message_failed;
2068 GST_TRACE_OBJECT (comm->element, "deserialized message %p of type %s",
2069 message, gst_message_type_get_name (message->type));
2071 if (comm->on_message)
2072 (*comm->on_message) (comm->id, message, comm->user_data);
2074 GST_TRACE_OBJECT (comm->element, "switching to state TYPE");
2075 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
2086 GST_ELEMENT_ERROR (comm->element, STREAM, DECODE, (NULL),
2087 ("Socket out of sync"));
2091 state_change_failed:
2093 GST_ELEMENT_ERROR (comm->element, STREAM, DECODE, (NULL),
2094 ("could not read state change from fd"));
2100 GST_ELEMENT_ERROR (comm->element, STREAM, DECODE, (NULL),
2101 ("could not read ack from fd"));
2107 GST_ELEMENT_ERROR (comm->element, STREAM, DECODE, (NULL),
2108 ("could not read buffer from fd"));
2114 GST_ELEMENT_ERROR (comm->element, STREAM, DECODE, (NULL),
2115 ("could not read event from fd"));
2121 GST_ELEMENT_ERROR (comm->element, STREAM, DECODE, (NULL),
2122 ("could not read message from fd"));
2128 GST_ELEMENT_ERROR (comm->element, STREAM, DECODE, (NULL),
2129 ("could not read query from fd"));
2136 reader_thread (gpointer data)
2138 GstIpcPipelineComm *comm = (GstIpcPipelineComm *) data;
2139 gboolean running = TRUE;
2143 ret = update_adapter (comm);
2146 GST_ELEMENT_ERROR (comm->element, RESOURCE, READ, (NULL),
2147 ("Failed to read from socket"));
2151 GST_INFO_OBJECT (comm->element, "We're stopping, all good");
2160 GST_INFO_OBJECT (comm->element, "Reader thread ending");
2165 gst_ipc_pipeline_comm_start_reader_thread (GstIpcPipelineComm * comm,
2166 void (*on_buffer) (guint32, GstBuffer *, gpointer),
2167 void (*on_event) (guint32, GstEvent *, gboolean, gpointer),
2168 void (*on_query) (guint32, GstQuery *, gboolean, gpointer),
2169 void (*on_state_change) (guint32, GstStateChange, gpointer),
2170 void (*on_state_lost) (gpointer),
2171 void (*on_message) (guint32, GstMessage *, gpointer), gpointer user_data)
2173 if (comm->reader_thread)
2176 comm->state = GST_IPC_PIPELINE_COMM_STATE_TYPE;
2177 comm->on_buffer = on_buffer;
2178 comm->on_event = on_event;
2179 comm->on_query = on_query;
2180 comm->on_state_change = on_state_change;
2181 comm->on_state_lost = on_state_lost;
2182 comm->on_message = on_message;
2183 comm->user_data = user_data;
2184 gst_poll_set_flushing (comm->poll, FALSE);
2185 comm->reader_thread =
2186 g_thread_new ("reader", (GThreadFunc) reader_thread, comm);
2191 gst_ipc_pipeline_comm_stop_reader_thread (GstIpcPipelineComm * comm)
2193 if (!comm->reader_thread)
2196 gst_poll_set_flushing (comm->poll, TRUE);
2197 g_thread_join (comm->reader_thread);
2198 comm->reader_thread = NULL;
2202 gst_value_serialize_event (const GValue * value)
2204 const GstStructure *structure;
2206 gchar *type, *ts, *seqnum, *rt_offset, *str, *str64, *s;
2207 GValue val = G_VALUE_INIT;
2209 ev = g_value_get_boxed (value);
2211 g_value_init (&val, gst_event_type_get_type ());
2212 g_value_set_enum (&val, ev->type);
2213 type = gst_value_serialize (&val);
2214 g_value_unset (&val);
2216 g_value_init (&val, G_TYPE_UINT64);
2217 g_value_set_uint64 (&val, ev->timestamp);
2218 ts = gst_value_serialize (&val);
2219 g_value_unset (&val);
2221 g_value_init (&val, G_TYPE_UINT);
2222 g_value_set_uint (&val, ev->seqnum);
2223 seqnum = gst_value_serialize (&val);
2224 g_value_unset (&val);
2226 g_value_init (&val, G_TYPE_INT64);
2227 g_value_set_int64 (&val, gst_event_get_running_time_offset (ev));
2228 rt_offset = gst_value_serialize (&val);
2229 g_value_unset (&val);
2231 structure = gst_event_get_structure (ev);
2232 str = gst_structure_to_string (structure);
2233 str64 = g_base64_encode ((guchar *) str, strlen (str) + 1);
2234 g_strdelimit (str64, "=", '_');
2237 s = g_strconcat (type, ":", ts, ":", seqnum, ":", rt_offset, ":", str64,
2250 gst_value_deserialize_event (GValue * dest, const gchar * s)
2252 GstEvent *ev = NULL;
2253 GValue val = G_VALUE_INIT;
2254 gboolean ret = FALSE;
2258 fields = g_strsplit (s, ":", -1);
2259 if (g_strv_length (fields) != 5)
2262 g_strdelimit (fields[4], "_", '=');
2263 g_base64_decode_inplace (fields[4], &len);
2265 g_value_init (&val, gst_event_type_get_type ());
2266 if (!gst_value_deserialize (&val, fields[0]))
2268 ev = gst_event_new_custom (g_value_get_enum (&val),
2269 gst_structure_new_from_string (fields[4]));
2271 g_value_unset (&val);
2272 g_value_init (&val, G_TYPE_UINT64);
2273 if (!gst_value_deserialize (&val, fields[1]))
2275 ev->timestamp = g_value_get_uint64 (&val);
2277 g_value_unset (&val);
2278 g_value_init (&val, G_TYPE_UINT);
2279 if (!gst_value_deserialize (&val, fields[2]))
2281 ev->seqnum = g_value_get_uint (&val);
2283 g_value_unset (&val);
2284 g_value_init (&val, G_TYPE_INT64);
2285 if (!gst_value_deserialize (&val, fields[3]))
2287 gst_event_set_running_time_offset (ev, g_value_get_int64 (&val));
2289 g_value_take_boxed (dest, ev);
2294 g_clear_pointer (&ev, gst_event_unref);
2295 g_value_unset (&val);
2298 g_strfreev (fields);
2302 #define REGISTER_SERIALIZATION_NO_COMPARE(_gtype, _type) \
2304 static GstValueTable gst_value = \
2306 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type }; \
2307 gst_value.type = _gtype; \
2308 gst_value_register (&gst_value); \
2312 gst_ipc_pipeline_comm_plugin_init (void)
2314 static volatile gsize once = 0;
2316 if (g_once_init_enter (&once)) {
2317 GST_DEBUG_CATEGORY_INIT (gst_ipc_pipeline_comm_debug, "ipcpipelinecomm", 0,
2318 "ipc pipeline comm");
2319 QUARK_ID = g_quark_from_static_string ("ipcpipeline-id");
2320 REGISTER_SERIALIZATION_NO_COMPARE (gst_event_get_type (), event);
2321 g_once_init_leave (&once, (gsize) 1);