1 /* GStreamer
2  * Copyright (C) <2014> William Manley <will@williammanley.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION:gstnetcontrolmessagemeta
22  * @title: GstNetControlMessageMeta
23  * @short_description: Network Control Message Meta
24  *
25  * #GstNetControlMessageMeta can be used to store control messages (ancillary
26  * data) which was received with or is to be sent alongside the buffer data.
27  * When used with socket sinks and sources which understand this meta it allows
28  * sending and receiving ancillary data such as unix credentials (See
29  * #GUnixCredentialsMessage) and Unix file descriptions (See #GUnixFDMessage).
30  */
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <string.h>
36 
37 #include "gstnetcontrolmessagemeta.h"
38 
39 static gboolean
net_control_message_meta_init(GstMeta * meta,gpointer params,GstBuffer * buffer)40 net_control_message_meta_init (GstMeta * meta, gpointer params,
41     GstBuffer * buffer)
42 {
43   GstNetControlMessageMeta *nmeta = (GstNetControlMessageMeta *) meta;
44 
45   nmeta->message = NULL;
46 
47   return TRUE;
48 }
49 
50 static gboolean
net_control_message_meta_transform(GstBuffer * transbuf,GstMeta * meta,GstBuffer * buffer,GQuark type,gpointer data)51 net_control_message_meta_transform (GstBuffer * transbuf, GstMeta * meta,
52     GstBuffer * buffer, GQuark type, gpointer data)
53 {
54   GstNetControlMessageMeta *smeta, *dmeta;
55   smeta = (GstNetControlMessageMeta *) meta;
56 
57   /* we always copy no matter what transform */
58   dmeta = gst_buffer_add_net_control_message_meta (transbuf, smeta->message);
59   if (!dmeta)
60     return FALSE;
61 
62   return TRUE;
63 }
64 
65 static void
net_control_message_meta_free(GstMeta * meta,GstBuffer * buffer)66 net_control_message_meta_free (GstMeta * meta, GstBuffer * buffer)
67 {
68   GstNetControlMessageMeta *nmeta = (GstNetControlMessageMeta *) meta;
69 
70   if (nmeta->message)
71     g_object_unref (nmeta->message);
72   nmeta->message = NULL;
73 }
74 
75 GType
gst_net_control_message_meta_api_get_type(void)76 gst_net_control_message_meta_api_get_type (void)
77 {
78   static volatile GType type;
79   static const gchar *tags[] = { "origin", NULL };
80 
81   if (g_once_init_enter (&type)) {
82     GType _type =
83         gst_meta_api_type_register ("GstNetControlMessageMetaAPI", tags);
84     g_once_init_leave (&type, _type);
85   }
86   return type;
87 }
88 
89 const GstMetaInfo *
gst_net_control_message_meta_get_info(void)90 gst_net_control_message_meta_get_info (void)
91 {
92   static const GstMetaInfo *meta_info = NULL;
93 
94   if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
95     const GstMetaInfo *mi =
96         gst_meta_register (GST_NET_CONTROL_MESSAGE_META_API_TYPE,
97         "GstNetControlMessageMeta",
98         sizeof (GstNetControlMessageMeta),
99         net_control_message_meta_init,
100         net_control_message_meta_free,
101         net_control_message_meta_transform);
102     g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) mi);
103   }
104   return meta_info;
105 }
106 
107 /**
108  * gst_buffer_add_net_control_message_meta:
109  * @buffer: a #GstBuffer
110  * @message: a @GSocketControlMessage to attach to @buffer
111  *
112  * Attaches @message as metadata in a #GstNetControlMessageMeta to @buffer.
113  *
114  * Returns: (transfer none): a #GstNetControlMessageMeta connected to @buffer
115  */
116 GstNetControlMessageMeta *
gst_buffer_add_net_control_message_meta(GstBuffer * buffer,GSocketControlMessage * message)117 gst_buffer_add_net_control_message_meta (GstBuffer * buffer,
118     GSocketControlMessage * message)
119 {
120   GstNetControlMessageMeta *meta;
121 
122   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
123   g_return_val_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message), NULL);
124 
125   meta =
126       (GstNetControlMessageMeta *) gst_buffer_add_meta (buffer,
127       GST_NET_CONTROL_MESSAGE_META_INFO, NULL);
128 
129   meta->message = g_object_ref (message);
130 
131   return meta;
132 }
133