2 * This file is part of the Nice GLib ICE library.
4 * (C) 2006, 2007 Collabora Ltd.
5 * Contact: Dafydd Harries
6 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
25 * Dafydd Harries, Collabora Ltd.
27 * Alternatively, the contents of this file may be used under the terms of the
28 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
29 * case the provisions of LGPL are applicable instead of those above. If you
30 * wish to allow use of your version of this file only under the terms of the
31 * LGPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replace
33 * them with the notice and other provisions required by the LGPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the LGPL.
43 #include "gstnicesrc.h"
45 GST_DEBUG_CATEGORY_STATIC (nicesrc_debug
);
46 #define GST_CAT_DEFAULT nicesrc_debug
49 #define BUFFER_SIZE (65536)
63 gst_nice_src_unlock_stop (
67 gst_nice_src_set_property (
74 gst_nice_src_get_property (
82 gst_nice_src_dispose (GObject
*object
);
84 static GstStateChangeReturn
85 gst_nice_src_change_state (
87 GstStateChange transition
);
89 static const GstElementDetails gst_nice_src_details
=
93 "Interactive UDP connectivity establishment",
94 "Dafydd Harries <dafydd.harries@collabora.co.uk>");
96 static GstStaticPadTemplate gst_nice_src_src_template
=
97 GST_STATIC_PAD_TEMPLATE (
101 GST_STATIC_CAPS_ANY
);
103 GST_BOILERPLATE (GstNiceSrc
, gst_nice_src
, GstBaseSrc
, GST_TYPE_BASE_SRC
);
113 gst_nice_src_base_init (gpointer g_class
)
115 GstElementClass
*element_class
= GST_ELEMENT_CLASS (g_class
);
117 gst_element_class_add_pad_template (element_class
,
118 gst_static_pad_template_get (&gst_nice_src_src_template
));
119 gst_element_class_set_details (element_class
, &gst_nice_src_details
);
123 gst_nice_src_class_init (GstNiceSrcClass
*klass
)
125 GstBaseSrcClass
*gstbasesrc_class
;
126 GstElementClass
*gstelement_class
;
127 GObjectClass
*gobject_class
;
129 GST_DEBUG_CATEGORY_INIT (nicesrc_debug
, "nicesrc",
130 0, "libnice source");
132 gstbasesrc_class
= (GstBaseSrcClass
*) klass
;
133 gstbasesrc_class
->create
= GST_DEBUG_FUNCPTR (gst_nice_src_create
);
134 gstbasesrc_class
->unlock
= GST_DEBUG_FUNCPTR (gst_nice_src_unlock
);
135 gstbasesrc_class
->unlock_stop
= GST_DEBUG_FUNCPTR (gst_nice_src_unlock_stop
);
137 gobject_class
= (GObjectClass
*) klass
;
138 gobject_class
->set_property
= gst_nice_src_set_property
;
139 gobject_class
->get_property
= gst_nice_src_get_property
;
140 gobject_class
->dispose
= gst_nice_src_dispose
;
142 gstelement_class
= (GstElementClass
*) klass
;
143 gstelement_class
->change_state
= gst_nice_src_change_state
;
145 g_object_class_install_property (gobject_class
, PROP_AGENT
,
146 g_param_spec_object (
149 "The NiceAgent this source is bound to",
153 g_object_class_install_property (gobject_class
, PROP_STREAM
,
157 "The ID of the stream to read from",
163 g_object_class_install_property (gobject_class
, PROP_COMPONENT
,
167 "The ID of the component to read from",
175 gst_nice_src_init (GstNiceSrc
*src
, GstNiceSrcClass
*g_class
)
177 gst_base_src_set_live (GST_BASE_SRC (src
), TRUE
);
178 gst_base_src_set_format (GST_BASE_SRC (src
), GST_FORMAT_TIME
);
179 gst_base_src_set_do_timestamp (GST_BASE_SRC (src
), TRUE
);
182 src
->component_id
= 0;
183 src
->mainctx
= g_main_context_new ();
184 src
->mainloop
= g_main_loop_new (src
->mainctx
, FALSE
);
185 src
->unlocked
= FALSE
;
186 src
->idle_source
= NULL
;
187 src
->outbufs
= g_queue_new ();
191 gst_nice_src_read_callback (NiceAgent
*agent
,
198 GstBaseSrc
*basesrc
= GST_BASE_SRC (data
);
199 GstNiceSrc
*nicesrc
= GST_NICE_SRC (basesrc
);
200 GstBuffer
*buffer
= NULL
;
202 GST_LOG_OBJECT (agent
, "Got buffer, getting out of the main loop");
204 nicesrc
->flow_ret
= gst_pad_alloc_buffer (basesrc
->srcpad
, nicesrc
->offset
,
205 len
, GST_PAD_CAPS (basesrc
->srcpad
), &buffer
);
206 if (nicesrc
->flow_ret
== GST_FLOW_OK
) {
207 memcpy (buffer
->data
, buf
, len
);
209 g_queue_push_tail (nicesrc
->outbufs
, buffer
);
212 g_main_loop_quit (nicesrc
->mainloop
);
216 gst_nice_src_unlock_idler (gpointer data
)
218 GstNiceSrc
*nicesrc
= GST_NICE_SRC (data
);
220 GST_OBJECT_LOCK (nicesrc
);
221 if (nicesrc
->unlocked
)
222 g_main_loop_quit (nicesrc
->mainloop
);
224 if (nicesrc
->idle_source
) {
225 g_source_destroy (nicesrc
->idle_source
);
226 g_source_unref (nicesrc
->idle_source
);
227 nicesrc
->idle_source
= NULL
;
229 GST_OBJECT_UNLOCK (nicesrc
);
235 gst_nice_src_unlock (GstBaseSrc
*src
)
237 GstNiceSrc
*nicesrc
= GST_NICE_SRC (src
);
239 GST_OBJECT_LOCK (src
);
240 nicesrc
->unlocked
= TRUE
;
242 g_main_loop_quit (nicesrc
->mainloop
);
244 if (!nicesrc
->idle_source
) {
245 nicesrc
->idle_source
= g_idle_source_new ();
246 g_source_set_priority (nicesrc
->idle_source
, G_PRIORITY_HIGH
);
247 g_source_set_callback (nicesrc
->idle_source
, gst_nice_src_unlock_idler
, src
, NULL
);
248 g_source_attach (nicesrc
->idle_source
, g_main_loop_get_context (nicesrc
->mainloop
));
250 GST_OBJECT_UNLOCK (src
);
256 gst_nice_src_unlock_stop (GstBaseSrc
*src
)
258 GstNiceSrc
*nicesrc
= GST_NICE_SRC (src
);
260 GST_OBJECT_LOCK (src
);
261 nicesrc
->unlocked
= FALSE
;
262 if (nicesrc
->idle_source
) {
263 g_source_destroy (nicesrc
->idle_source
);
264 g_source_unref(nicesrc
->idle_source
);
266 nicesrc
->idle_source
= NULL
;
267 GST_OBJECT_UNLOCK (src
);
273 gst_nice_src_create (
279 GstNiceSrc
*nicesrc
= GST_NICE_SRC (basesrc
);
281 GST_LOG_OBJECT (nicesrc
, "create called");
283 nicesrc
->offset
= offset
;
285 GST_OBJECT_LOCK (basesrc
);
286 if (nicesrc
->unlocked
) {
287 GST_OBJECT_UNLOCK (basesrc
);
288 return GST_FLOW_WRONG_STATE
;
290 GST_OBJECT_UNLOCK (basesrc
);
292 if (g_queue_is_empty (nicesrc
->outbufs
))
293 g_main_loop_run (nicesrc
->mainloop
);
295 *buffer
= g_queue_pop_head (nicesrc
->outbufs
);
296 if (*buffer
!= NULL
) {
297 GST_LOG_OBJECT (nicesrc
, "Got buffer, pushing");
298 return nicesrc
->flow_ret
;
300 GST_LOG_OBJECT (nicesrc
, "Got interrupting, returning wrong-state");
301 return GST_FLOW_WRONG_STATE
;
307 gst_nice_src_dispose (GObject
*object
)
309 GstNiceSrc
*src
= GST_NICE_SRC (object
);
312 g_object_unref (src
->agent
);
316 g_main_loop_unref (src
->mainloop
);
317 src
->mainloop
= NULL
;
320 g_main_context_unref (src
->mainctx
);
324 g_queue_free (src
->outbufs
);
327 GST_CALL_PARENT (G_OBJECT_CLASS
, dispose
, (object
));
331 gst_nice_src_set_property (
337 GstNiceSrc
*src
= GST_NICE_SRC (object
);
343 GST_ERROR_OBJECT (object
,
344 "Changing the agent on a nice src not allowed");
346 src
->agent
= g_value_dup_object (value
);
350 src
->stream_id
= g_value_get_uint (value
);
354 src
->component_id
= g_value_get_uint (value
);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
364 gst_nice_src_get_property (
370 GstNiceSrc
*src
= GST_NICE_SRC (object
);
375 g_value_set_object (value
, src
->agent
);
379 g_value_set_uint (value
, src
->stream_id
);
383 g_value_set_uint (value
, src
->component_id
);
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
392 static GstStateChangeReturn
393 gst_nice_src_change_state (GstElement
* element
, GstStateChange transition
)
396 GstStateChangeReturn ret
;
398 src
= GST_NICE_SRC (element
);
400 switch (transition
) {
401 case GST_STATE_CHANGE_NULL_TO_READY
:
402 if (src
->agent
== NULL
|| src
->stream_id
== 0 || src
->component_id
== 0)
404 GST_ERROR_OBJECT (element
,
405 "Trying to start Nice source without an agent set");
406 return GST_STATE_CHANGE_FAILURE
;
410 nice_agent_attach_recv (src
->agent
, src
->stream_id
, src
->component_id
,
411 src
->mainctx
, gst_nice_src_read_callback
, (gpointer
) src
);
414 case GST_STATE_CHANGE_READY_TO_NULL
:
415 nice_agent_attach_recv (src
->agent
, src
->stream_id
, src
->component_id
,
416 src
->mainctx
, NULL
, NULL
);
422 ret
= GST_ELEMENT_CLASS (parent_class
)->change_state (element
, transition
);