Create a netbuffer directly instead of using gst_pad_buffer_alloc
[sipe-libnice.git] / gst / test.c
blob8eca27ac5a9033d2d8a7fd354ee620af1afa95fe
1 /*
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
17 * License.
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.
24 * Contributors:
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.
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
41 #include <string.h>
43 #include <gst/gst.h>
45 #include <nice/nice.h>
47 #include "stun.h"
49 #include "gstnice.h"
51 static GMainLoop *loop = NULL;
53 /* XXX: code duplicated from agent/test-send.c */
54 static void
55 send_connectivity_check (
56 NiceAgent *agent,
57 NiceAddress *remote_addr)
59 NiceUDPSocket *sock;
60 NiceCandidate *local;
61 NiceCandidate *remote;
62 gchar *username;
65 GSList *candidates;
67 candidates = nice_agent_get_local_candidates (agent, 1, 1);
68 g_assert (candidates);
69 local = candidates->data;
70 g_assert (local->id == 1);
71 g_slist_free (candidates);
75 GSList *candidates;
77 candidates = nice_agent_get_remote_candidates (agent, 1, 1);
78 g_assert (candidates);
79 remote = candidates->data;
80 g_slist_free (candidates);
83 sock = &local->sock;
85 username = g_strconcat (local->username, remote->username, NULL);
88 StunMessage *msg;
89 gchar *packed;
90 guint len;
92 msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1);
93 msg->attributes[0] = stun_attribute_username_new (username);
94 len = stun_message_pack (msg, &packed);
95 nice_udp_fake_socket_push_recv (sock, remote_addr, len, packed);
96 g_free (packed);
97 stun_message_free (msg);
100 nice_agent_poll_read (agent, NULL, NULL, NULL);
103 StunMessage *msg;
104 NiceAddress addr = {0,};
105 gchar packed[1024];
106 gchar *dump;
107 guint len;
109 len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed);
110 g_assert (nice_address_equal (&addr, remote_addr));
111 msg = stun_message_unpack (len, packed);
112 dump = stun_message_dump (msg);
113 g_assert (0 == strcmp (dump,
114 "BINDING-RESPONSE 00000000:00000000:00000000:00000000\n"
115 " MAPPED-ADDRESS 192.168.0.2:2345\n"
116 " USERNAME \"S9PObXR5username\"\n"));
117 g_free (dump);
118 stun_message_free (msg);
122 StunMessage *msg;
123 NiceAddress addr = {0,};
124 gchar packed[1024];
125 gchar *dump;
126 guint len;
128 len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed);
129 g_assert (nice_address_equal (&addr, remote_addr));
130 msg = stun_message_unpack (len, packed);
131 dump = stun_message_dump (msg);
132 g_assert (0 == strcmp (dump,
133 "BINDING-REQUEST 588c3ac1:e62757ae:5851a519:4d480994\n"
134 " USERNAME \"usernameS9PObXR5\"\n"));
135 g_free (dump);
136 stun_message_free (msg);
139 g_free (username);
142 static gboolean
143 recv_cb (
144 GIOChannel *source,
145 GIOCondition condition,
146 gpointer data)
148 /* return value is whether to keep the source */
150 NiceAgent *agent = data;
151 NiceCandidate *candidate;
152 NiceUDPSocket *sock;
153 NiceAddress from;
154 guint len;
155 gchar buf[1024];
158 GSList *candidates;
160 candidates = nice_agent_get_local_candidates (agent, 1, 1);
161 g_assert (candidates);
162 candidate = candidates->data;
163 g_slist_free (candidates);
166 sock = &candidate->sock;
167 len = nice_udp_fake_socket_pop_send (
168 sock, &from, 1024, buf);
170 g_assert (len == 6);
171 g_assert (0 == strncmp (buf, "\x80hello", len));
173 g_main_loop_quit (loop);
174 return FALSE;
178 main (gint argc, gchar *argv[])
180 GstElement *src;
181 GstElement *sink;
182 GstElement *pipeline;
183 NiceAgent *agent;
184 NiceAddress addr = {0,};
185 NiceUDPSocketFactory factory;
187 gst_init (&argc, &argv);
189 nice_rng_set_new_func (nice_rng_glib_new_predictable);
191 // set up agent
193 nice_address_set_ipv4 (&addr, 0x7f000001);
194 nice_udp_fake_socket_factory_init (&factory);
195 agent = nice_agent_new (&factory);
196 nice_agent_add_local_address (agent, &addr);
197 nice_address_set_ipv4 (&addr, 0xc0a80002);
198 addr.port = 2345;
199 nice_agent_add_stream (agent, 1);
200 nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
201 &addr, "username", "password");
203 // send connectivity check so that sending works
205 send_connectivity_check (agent, &addr);
208 GIOChannel *io;
209 GSource *source;
210 NiceCandidate *candidate;
211 NiceUDPSocket *sock;
212 GSList *candidates;
214 candidates = nice_agent_get_local_candidates (agent, 1, 1);
215 g_assert (candidates);
216 candidate = candidates->data;
217 sock = &candidate->sock;
218 g_slist_free (candidates);
220 // send test packet
222 nice_udp_fake_socket_push_recv (sock, &addr, 6, "\x80hello");
224 // watch socket for reveived data
226 io = g_io_channel_unix_new (nice_udp_fake_socket_get_peer_fd (sock));
227 source = g_io_create_watch (io, G_IO_IN);
228 g_source_set_callback (source, (GSourceFunc) recv_cb,
229 agent, NULL);
230 g_source_attach (source, NULL);
233 // set up pipeline
235 src = g_object_new (GST_TYPE_NICE_SRC,
236 "agent", agent,
237 "stream", 1,
238 "component", 1,
239 NULL);
241 sink = g_object_new (GST_TYPE_NICE_SINK,
242 "agent", agent,
243 "stream", 1,
244 "component", 1,
245 NULL);
247 pipeline = gst_pipeline_new (NULL);
248 gst_bin_add (GST_BIN (pipeline), src);
249 gst_bin_add (GST_BIN (pipeline), sink);
250 g_assert (gst_element_link (src, sink));
251 gst_element_set_state (pipeline, GST_STATE_PLAYING);
253 // loop
255 loop = g_main_loop_new (NULL, FALSE);
256 g_main_loop_run (loop);
257 g_main_loop_unref (loop);
259 // clean up
261 gst_object_unref (pipeline);
262 g_object_unref (agent);
263 nice_udp_socket_factory_close (&factory);
264 gst_deinit ();
265 return 0;