2 * Copyright (C) 2008 Jan Schmidt <jan.schmidt@sun.com>
4 * gstpluginloader.c: GstPluginLoader helper for loading plugin files
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include <gst/gst_private.h>
30 #include <sys/types.h>
36 #define fsync(fd) _commit(fd)
42 #include <gst/gstconfig.h>
44 #include <gst/gstpoll.h>
45 #include <gst/gstutils.h>
47 #include <gst/gstpluginloader.h>
48 #include <gst/gstregistrychunks.h>
49 #include <gst/gstregistrybinary.h>
51 /* IMPORTANT: Bump the version number if the plugin loader packet protocol
52 * changes. Changes in the binary registry format itself are handled by
53 * bumping the GST_MAGIC_BINARY_VERSION_STR
55 static const guint32 loader_protocol_version = 3;
57 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
59 static GstPluginLoader *plugin_loader_new (GstRegistry * registry);
60 static gboolean plugin_loader_free (GstPluginLoader * loader);
61 static gboolean plugin_loader_load (GstPluginLoader * loader,
62 const gchar * filename, off_t file_size, time_t file_mtime);
64 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
65 plugin_loader_new, plugin_loader_free, plugin_loader_load
68 typedef struct _PendingPluginEntry
76 struct _GstPluginLoader
78 GstRegistry *registry;
81 gboolean child_running;
87 gboolean got_plugin_details;
100 gboolean rx_got_sync;
102 /* Head and tail of the pending plugins list. List of
103 PendingPluginEntry structs */
104 GList *pending_plugins;
105 GList *pending_plugins_tail;
108 #define PACKET_EXIT 1
109 #define PACKET_LOAD_PLUGIN 2
110 #define PACKET_SYNC 3
111 #define PACKET_PLUGIN_DETAILS 4
112 #define PACKET_VERSION 5
114 #define BUF_INIT_SIZE 512
115 #define BUF_GROW_EXTRA 512
116 #define BUF_MAX_SIZE (32 * 1024 * 1024)
118 #define HEADER_SIZE 12
119 /* 4 magic hex bytes to mark each packet */
120 #define HEADER_MAGIC 0xbefec0ae
121 #define ALIGNMENT (sizeof (void *))
123 static gboolean gst_plugin_loader_spawn (GstPluginLoader * loader);
124 static void put_packet (GstPluginLoader * loader, guint type, guint32 tag,
125 const guint8 * payload, guint32 payload_len);
126 static gboolean exchange_packets (GstPluginLoader * l);
127 static gboolean plugin_loader_replay_pending (GstPluginLoader * l);
128 static gboolean plugin_loader_load_and_sync (GstPluginLoader * l,
129 PendingPluginEntry * entry);
130 static void plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
131 PendingPluginEntry * entry);
132 static void plugin_loader_cleanup_child (GstPluginLoader * loader);
133 static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
135 static GstPluginLoader *
136 plugin_loader_new (GstRegistry * registry)
138 GstPluginLoader *l = g_slice_new0 (GstPluginLoader);
141 l->registry = gst_object_ref (registry);
142 l->fdset = gst_poll_new (FALSE);
143 gst_poll_fd_init (&l->fd_w);
144 gst_poll_fd_init (&l->fd_r);
146 l->tx_buf_size = BUF_INIT_SIZE;
147 l->tx_buf = g_malloc (BUF_INIT_SIZE);
151 l->rx_buf_size = BUF_INIT_SIZE;
152 l->rx_buf = g_malloc (BUF_INIT_SIZE);
158 plugin_loader_free (GstPluginLoader * loader)
161 gboolean got_plugin_details;
163 fsync (loader->fd_w.fd);
165 if (loader->child_running) {
166 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
168 /* Swap packets with the child until it exits cleanly */
169 while (!loader->rx_done) {
170 if (exchange_packets (loader) || loader->rx_done)
173 if (!plugin_loader_replay_pending (loader))
175 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
178 plugin_loader_cleanup_child (loader);
180 close (loader->fd_w.fd);
181 close (loader->fd_r.fd);
184 gst_poll_free (loader->fdset);
186 g_free (loader->rx_buf);
187 g_free (loader->tx_buf);
189 if (loader->registry)
190 gst_object_unref (loader->registry);
192 got_plugin_details = loader->got_plugin_details;
194 /* Free any pending plugin entries */
195 cur = loader->pending_plugins;
197 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
198 g_free (entry->filename);
199 g_slice_free (PendingPluginEntry, entry);
201 cur = g_list_delete_link (cur, cur);
204 g_slice_free (GstPluginLoader, loader);
206 return got_plugin_details;
210 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
211 off_t file_size, time_t file_mtime)
214 PendingPluginEntry *entry;
216 if (!gst_plugin_loader_spawn (loader))
219 /* Send a packet to the child requesting that it load the given file */
220 GST_LOG_OBJECT (loader->registry,
221 "Sending file %s to child. tag %u", filename, loader->next_tag);
223 entry = g_slice_new (PendingPluginEntry);
224 entry->tag = loader->next_tag++;
225 entry->filename = g_strdup (filename);
226 entry->file_size = file_size;
227 entry->file_mtime = file_mtime;
228 loader->pending_plugins_tail =
229 g_list_append (loader->pending_plugins_tail, entry);
231 if (loader->pending_plugins == NULL)
232 loader->pending_plugins = loader->pending_plugins_tail;
234 loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
236 len = strlen (filename);
237 put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
238 (guint8 *) filename, len + 1);
240 if (!exchange_packets (loader)) {
241 if (!plugin_loader_replay_pending (loader))
249 plugin_loader_replay_pending (GstPluginLoader * l)
254 if (!gst_plugin_loader_spawn (l))
257 /* Load each plugin one by one synchronously until we find the
259 while ((cur = l->pending_plugins)) {
260 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
262 if (!plugin_loader_load_and_sync (l, entry)) {
263 /* Create dummy plugin entry to block re-scanning this file */
264 GST_ERROR ("Plugin file %s failed to load. Blacklisting",
266 plugin_loader_create_blacklist_plugin (l, entry);
267 l->got_plugin_details = TRUE;
268 /* Now remove this crashy plugin from the head of the list */
269 l->pending_plugins = g_list_delete_link (cur, cur);
270 if (l->pending_plugins == NULL)
271 l->pending_plugins_tail = NULL;
272 if (!gst_plugin_loader_spawn (l))
278 /* We exited after finding the crashing one. If there's any more pending,
279 * dispatch them post-haste, but don't wait */
280 for (cur = l->pending_plugins; cur != NULL; cur = next) {
281 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
283 next = g_list_next (cur);
285 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
286 (guint8 *) entry->filename, strlen (entry->filename) + 1);
288 /* This might invalidate cur, which is why we grabbed 'next' above */
289 if (!exchange_packets (l))
297 plugin_loader_sync_with_child (GstPluginLoader * l)
299 put_packet (l, PACKET_SYNC, 0, NULL, 0);
301 l->rx_got_sync = FALSE;
302 while (!l->rx_got_sync) {
303 if (!exchange_packets (l))
310 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
314 GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
317 len = strlen (entry->filename);
318 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
319 (guint8 *) entry->filename, len + 1);
321 return plugin_loader_sync_with_child (l);
325 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
326 PendingPluginEntry * entry)
328 GstPlugin *plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
330 plugin->filename = g_strdup (entry->filename);
331 plugin->file_mtime = entry->file_mtime;
332 plugin->file_size = entry->file_size;
333 plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
335 plugin->basename = g_path_get_basename (plugin->filename);
336 plugin->desc.name = g_intern_string (plugin->basename);
337 plugin->desc.description = g_strdup_printf ("Plugin for blacklisted file");
338 plugin->desc.version = g_intern_string ("0.0.0");
339 plugin->desc.license = g_intern_string ("BLACKLIST");
340 plugin->desc.source = plugin->desc.license;
341 plugin->desc.package = plugin->desc.license;
342 plugin->desc.origin = plugin->desc.license;
344 GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
345 gst_registry_add_plugin (l->registry, plugin);
349 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
351 char *argv[] = { location, (char *) "-l", NULL };
353 GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s", location);
354 if (!g_spawn_async_with_pipes (NULL, argv, NULL,
355 G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
356 NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
360 gst_poll_add_fd (loader->fdset, &loader->fd_w);
361 gst_poll_add_fd (loader->fdset, &loader->fd_r);
363 gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
365 loader->tx_buf_write = loader->tx_buf_read = 0;
367 put_packet (loader, PACKET_VERSION, 0, NULL, 0);
368 if (!plugin_loader_sync_with_child (loader))
371 loader->child_running = TRUE;
377 gst_plugin_loader_spawn (GstPluginLoader * loader)
381 gboolean res = FALSE;
383 if (loader->child_running)
386 /* Find the gst-plugin-scanner: first try the env-var if it is set,
387 * otherwise use the installed version */
388 env = g_getenv ("GST_PLUGIN_SCANNER");
390 if (env != NULL && *env != '\0') {
391 GST_LOG ("Trying GST_PLUGIN_SCANNER env var: %s", env);
392 helper_bin = g_strdup (env);
393 res = gst_plugin_loader_try_helper (loader, helper_bin);
398 GST_LOG ("Trying installed plugin scanner");
399 helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
400 res = gst_plugin_loader_try_helper (loader, helper_bin);
404 GST_INFO ("No gst-plugin-scanner available, or not working");
408 return loader->child_running;
412 plugin_loader_cleanup_child (GstPluginLoader * l)
414 if (!l->child_running || l->is_child)
417 gst_poll_remove_fd (l->fdset, &l->fd_w);
418 gst_poll_remove_fd (l->fdset, &l->fd_r);
424 GST_LOG ("waiting for child process to exit");
425 waitpid (l->child_pid, NULL, 0);
427 g_warning ("FIXME: Implement child process shutdown for Win32");
429 g_spawn_close_pid (l->child_pid);
431 l->child_running = FALSE;
435 _gst_plugin_loader_client_run (void)
439 l = plugin_loader_new (NULL);
443 /* On entry, the inward pipe is STDIN, and outward is STDOUT.
444 * Dup those somewhere better so that plugins printing things
445 * won't interfere with anything */
450 dup_fd = dup (0); /* STDIN */
452 GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
458 dup_fd = dup (1); /* STDOUT */
460 GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
466 /* Dup stderr down to stdout so things that plugins print are visible,
467 * but don't care if it fails */
471 /* FIXME: Use DuplicateHandle and friends on win32 */
472 l->fd_w.fd = 1; /* STDOUT */
473 l->fd_r.fd = 0; /* STDIN */
476 gst_poll_add_fd (l->fdset, &l->fd_w);
477 gst_poll_add_fd (l->fdset, &l->fd_r);
478 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE);
482 GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
484 /* Loop, listening for incoming packets on the fd and writing responses */
485 while (!l->rx_done && exchange_packets (l));
487 plugin_loader_free (l);
493 put_packet (GstPluginLoader * l, guint type, guint32 tag,
494 const guint8 * payload, guint32 payload_len)
497 guint len = payload_len + HEADER_SIZE;
499 if (l->tx_buf_write + len >= l->tx_buf_size) {
500 GST_LOG ("Expanding tx buf from %d to %d for packet of size %d",
501 l->tx_buf_size, l->tx_buf_write + len + BUF_GROW_EXTRA, len);
502 l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
503 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
506 out = l->tx_buf + l->tx_buf_write;
508 /* one byte packet type */
510 /* 3 byte packet tag number */
511 GST_WRITE_UINT24_BE (out + 1, tag);
512 /* 4 bytes packet length */
513 GST_WRITE_UINT32_BE (out + 4, payload_len);
515 memcpy (out + HEADER_SIZE, payload, payload_len);
516 /* Write magic into the header */
517 GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
519 l->tx_buf_write += len;
520 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
524 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
530 /* Might need to align the chunk */
531 if (chunk->align && ((*pos) % ALIGNMENT) != 0)
532 padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
534 len = padsize + chunk->size;
536 if (G_UNLIKELY (l->tx_buf_write + len >= l->tx_buf_size)) {
537 guint new_size = MAX (l->tx_buf_write + len,
538 l->tx_buf_size + l->tx_buf_size / 4) + BUF_GROW_EXTRA;
539 GST_LOG ("Expanding tx buf from %d to %d for chunk of size %d",
540 l->tx_buf_size, new_size, chunk->size);
541 l->tx_buf_size = new_size;
542 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
545 out = l->tx_buf + l->tx_buf_write;
546 /* Clear the padding */
548 memset (out, 0, padsize);
549 memcpy (out + padsize, chunk->data, chunk->size);
551 l->tx_buf_write += len;
554 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
558 write_one (GstPluginLoader * l)
561 guint32 to_write, magic;
564 if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
567 out = l->tx_buf + l->tx_buf_read;
569 magic = GST_READ_UINT32_BE (out + 8);
570 if (magic != HEADER_MAGIC) {
571 GST_ERROR ("Packet magic number is missing. Memory corruption detected");
572 goto fail_and_cleanup;
575 to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE;
576 /* Check that the magic is intact, and the size is sensible */
577 if (to_write > l->tx_buf_size) {
578 GST_ERROR ("Indicated packet size is too large. Corruption detected");
579 goto fail_and_cleanup;
582 l->tx_buf_read += to_write;
584 GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
587 res = write (l->fd_w.fd, out, to_write);
588 if (G_UNLIKELY (res < 0)) {
589 if (errno == EAGAIN || errno == EINTR)
591 /* Failed to write -> child died */
592 goto fail_and_cleanup;
596 } while (to_write > 0);
598 if (l->tx_buf_read == l->tx_buf_write) {
599 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
600 l->tx_buf_read = l->tx_buf_write = 0;
606 plugin_loader_cleanup_child (l);
611 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
613 GstPlugin *newplugin;
614 GList *chunks = NULL;
616 GST_DEBUG ("Plugin scanner loading file %s. tag %u", filename, tag);
618 #if 0 /* Test code - crash based on filename */
619 if (strstr (filename, "coreelements") == NULL) {
620 g_printerr ("Crashing on file %s\n", filename);
621 g_printerr ("%d", *(gint *) (NULL));
625 newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
630 /* Now serialise the plugin details and send */
631 if (!_priv_gst_registry_chunks_save_plugin (&chunks,
632 gst_registry_get_default (), newplugin))
635 /* Store where the header is, write an empty one, then write
636 * all the payload chunks, then fix up the header size */
637 hdr_pos = l->tx_buf_write;
638 offset = HEADER_SIZE;
639 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
643 for (walk = chunks; walk; walk = g_list_next (walk)) {
644 GstRegistryChunk *cur = walk->data;
645 put_chunk (l, cur, &offset);
647 _priv_gst_registry_chunk_free (cur);
650 g_list_free (chunks);
652 /* Store the size of the written payload */
653 GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
655 #if 0 /* Test code - corrupt the tx buffer based on filename */
656 if (strstr (filename, "sink") != NULL) {
658 g_printerr ("Corrupting tx buf on file %s\n", filename);
659 fd = open ("/dev/urandom", O_RDONLY);
660 res = read (fd, l->tx_buf, l->tx_buf_size);
665 gst_object_unref (newplugin);
667 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
672 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
675 for (walk = chunks; walk; walk = g_list_next (walk)) {
676 GstRegistryChunk *cur = walk->data;
678 _priv_gst_registry_chunk_free (cur);
681 g_list_free (chunks);
688 check_protocol_version (GstPluginLoader * l, guint8 * payload,
692 guint8 *binary_reg_ver;
694 if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
697 got_version = GST_READ_UINT32_BE (payload);
698 GST_LOG ("Got VERSION %u from child. Ours is %u", got_version,
699 loader_protocol_version);
700 if (got_version != loader_protocol_version)
703 binary_reg_ver = payload + sizeof (guint32);
704 if (strcmp ((gchar *) binary_reg_ver, GST_MAGIC_BINARY_VERSION_STR)) {
705 GST_LOG ("Binary chunk format of child is different. Ours: %s, child %s\n",
706 GST_MAGIC_BINARY_VERSION_STR, binary_reg_ver);
714 handle_rx_packet (GstPluginLoader * l,
715 guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
721 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
723 /* Respond, then we keep looping until the parent closes the fd */
724 put_packet (l, PACKET_EXIT, 0, NULL, 0);
726 l->rx_done = TRUE; /* All done reading from child */
729 case PACKET_LOAD_PLUGIN:{
733 /* Payload is the filename to load */
734 res = do_plugin_load (l, (gchar *) payload, tag);
738 case PACKET_PLUGIN_DETAILS:{
739 gchar *tmp = (gchar *) payload;
740 PendingPluginEntry *entry = NULL;
743 GST_DEBUG_OBJECT (l->registry,
744 "Received plugin details from child w/ tag %u. %d bytes info",
747 /* Assume that tagged details come back in the order
748 * we requested, and delete anything before (but not
749 * including) this one */
750 cur = l->pending_plugins;
752 PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
761 cur = g_list_delete_link (cur, cur);
762 g_free (e->filename);
763 g_slice_free (PendingPluginEntry, e);
767 l->pending_plugins = cur;
769 l->pending_plugins_tail = NULL;
771 if (payload_len > 0) {
772 GstPlugin *newplugin = NULL;
773 if (!_priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
774 tmp + payload_len, &newplugin)) {
775 /* Got garbage from the child, so fail and trigger replay of plugins */
776 GST_ERROR_OBJECT (l->registry,
777 "Problems loading plugin details with tag %u from scanner", tag);
781 newplugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
782 GST_LOG_OBJECT (l->registry,
783 "marking plugin %p as registered as %s", newplugin,
784 newplugin->filename);
785 newplugin->registered = TRUE;
787 /* We got a set of plugin details - remember it for later */
788 l->got_plugin_details = TRUE;
789 } else if (entry != NULL) {
790 /* Create a blacklist entry for this file to prevent scanning every time */
791 plugin_loader_create_blacklist_plugin (l, entry);
792 l->got_plugin_details = TRUE;
796 g_free (entry->filename);
797 g_slice_free (PendingPluginEntry, entry);
800 /* Remove the plugin entry we just loaded */
801 cur = l->pending_plugins;
803 cur = g_list_delete_link (cur, cur);
804 l->pending_plugins = cur;
806 l->pending_plugins_tail = NULL;
812 /* Respond with our reply - also a sync */
813 put_packet (l, PACKET_SYNC, tag, NULL, 0);
814 GST_LOG ("Got SYNC in child - replying");
816 l->rx_got_sync = TRUE;
820 /* Respond with our reply - a version packet, with the version */
821 const gint version_len =
822 sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN;
823 guint8 version_info[sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN];
824 memset (version_info, 0, version_len);
825 GST_WRITE_UINT32_BE (version_info, loader_protocol_version);
826 memcpy (version_info + sizeof (guint32), GST_MAGIC_BINARY_VERSION_STR,
827 strlen (GST_MAGIC_BINARY_VERSION_STR));
828 put_packet (l, PACKET_VERSION, tag, version_info, version_len);
829 GST_LOG ("Got VERSION in child - replying %u", loader_protocol_version);
831 res = check_protocol_version (l, payload, payload_len);
835 return FALSE; /* Invalid packet -> something is wrong */
842 read_one (GstPluginLoader * l)
845 guint32 to_read, packet_len, tag;
849 to_read = HEADER_SIZE;
852 res = read (l->fd_r.fd, in, to_read);
853 if (G_UNLIKELY (res < 0)) {
854 if (errno == EAGAIN || errno == EINTR)
856 GST_LOG ("Failed reading packet header");
861 } while (to_read > 0);
863 magic = GST_READ_UINT32_BE (l->rx_buf + 8);
864 if (magic != HEADER_MAGIC) {
866 ("Invalid packet (bad magic number) received from plugin scanner subprocess");
870 packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
871 if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
873 ("Received excessively large packet for plugin scanner subprocess");
876 tag = GST_READ_UINT24_BE (l->rx_buf + 1);
878 if (packet_len > 0) {
879 if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
880 GST_LOG ("Expanding rx buf from %d to %d",
881 l->rx_buf_size, packet_len + HEADER_SIZE + BUF_GROW_EXTRA);
882 l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
883 l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
886 in = l->rx_buf + HEADER_SIZE;
887 to_read = packet_len;
889 res = read (l->fd_r.fd, in, to_read);
890 if (G_UNLIKELY (res < 0)) {
891 if (errno == EAGAIN || errno == EINTR)
893 GST_ERROR ("Packet payload read failed");
898 } while (to_read > 0);
900 GST_LOG ("No payload to read for 0 length packet type %d tag %u",
904 return handle_rx_packet (l, l->rx_buf[0], tag,
905 l->rx_buf + HEADER_SIZE, packet_len);
909 exchange_packets (GstPluginLoader * l)
913 /* Wait for activity on our FDs */
916 res = gst_poll_wait (l->fdset, GST_SECOND);
917 } while (res == -1 && (errno == EINTR || errno == EAGAIN));
922 GST_LOG ("Poll res = %d. %d bytes pending for write", res,
923 l->tx_buf_write - l->tx_buf_read);
926 if (gst_poll_fd_has_error (l->fdset, &l->fd_r) ||
927 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
928 GST_LOG ("read fd %d closed/errored", l->fd_r.fd);
929 goto fail_and_cleanup;
932 if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
934 goto fail_and_cleanup;
938 if (l->tx_buf_read < l->tx_buf_write) {
939 if (gst_poll_fd_has_error (l->fdset, &l->fd_w) ||
940 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
941 GST_ERROR ("write fd %d closed/errored", l->fd_w.fd);
942 goto fail_and_cleanup;
944 if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
946 goto fail_and_cleanup;
949 } while (l->tx_buf_read < l->tx_buf_write);
953 plugin_loader_cleanup_child (l);