b8f42dc8d9ad0122e8025d0539f017b40ab870ac
[platform/upstream/libnice.git] / agent / stream.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2006-2009 Collabora Ltd.
5  *  Contact: Youness Alaoui
6  * (C) 2006-2009 Nokia Corporation. All rights reserved.
7  *  Contact: Kai Vehmanen
8  *
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/
13  *
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.
18  *
19  * The Original Code is the Nice GLib ICE library.
20  *
21  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22  * Corporation. All Rights Reserved.
23  *
24  * Contributors:
25  *   Dafydd Harries, Collabora Ltd.
26  *   Youness Alaoui, Collabora Ltd.
27  *
28  * Alternatively, the contents of this file may be used under the terms of the
29  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30  * case the provisions of LGPL are applicable instead of those above. If you
31  * wish to allow use of your version of this file only under the terms of the
32  * LGPL and not to allow others to use your version of this file under the
33  * MPL, indicate your decision by deleting the provisions above and replace
34  * them with the notice and other provisions required by the LGPL. If you do
35  * not delete the provisions above, a recipient may use your version of this
36  * file under either the MPL or the LGPL.
37  */
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41
42 #include <string.h>
43
44 #include "stream.h"
45
46 /* Simple tracking for the number of alive streams. These must be accessed
47  * atomically. */
48 static volatile unsigned int n_streams_created = 0;
49 static volatile unsigned int n_streams_destroyed = 0;
50
51 G_DEFINE_TYPE (NiceStream, nice_stream, G_TYPE_OBJECT);
52
53 static void
54 nice_stream_finalize (GObject *obj);
55
56 /*
57  * @file stream.c
58  * @brief ICE stream functionality
59  */
60 NiceStream *
61 nice_stream_new (guint stream_id, guint n_components, NiceAgent *agent)
62 {
63   NiceStream *stream = NULL;
64   guint n;
65
66   stream = g_object_new (NICE_TYPE_STREAM, NULL);
67
68   stream->id = stream_id;
69
70   /* Create the components. */
71   for (n = 0; n < n_components; n++) {
72     NiceComponent *component = NULL;
73
74     component = nice_component_new (n + 1, agent, stream);
75     stream->components = g_slist_append (stream->components, component);
76   }
77
78   stream->n_components = n_components;
79
80   stream->peer_gathering_done = !agent->use_ice_trickle;
81
82   return stream;
83 }
84
85 void
86 nice_stream_close (NiceAgent *agent, NiceStream *stream)
87 {
88   GSList *i;
89
90   for (i = stream->components; i; i = i->next) {
91     NiceComponent *component = i->data;
92     nice_component_close (agent, component);
93   }
94 }
95
96 NiceComponent *
97 nice_stream_find_component_by_id (NiceStream *stream, guint id)
98 {
99   GSList *i;
100
101   for (i = stream->components; i; i = i->next) {
102     NiceComponent *component = i->data;
103     if (component && component->id == id)
104       return component;
105   }
106
107   return NULL;
108 }
109
110 /*
111  * Initialized the local crendentials for the stream.
112  */
113 void
114 nice_stream_initialize_credentials (NiceStream *stream, NiceRNG *rng)
115 {
116   /* note: generate ufrag/pwd for the stream (see ICE 15.4.
117    *       '"ice-ufrag" and "ice-pwd" Attributes', ID-19) */
118   nice_rng_generate_bytes_print (rng, NICE_STREAM_DEF_UFRAG - 1, stream->local_ufrag);
119   nice_rng_generate_bytes_print (rng, NICE_STREAM_DEF_PWD - 1, stream->local_password);
120 }
121
122 /*
123  * Resets the stream state to that of a ICE restarted
124  * session.
125  */
126 void
127 nice_stream_restart (NiceStream *stream, NiceAgent *agent)
128 {
129   GSList *i;
130
131   /* step: clean up all connectivity checks */
132   conn_check_prune_stream (agent, stream);
133
134   stream->initial_binding_request_received = FALSE;
135
136   nice_stream_initialize_credentials (stream, agent->rng);
137
138   for (i = stream->components; i; i = i->next) {
139     NiceComponent *component = i->data;
140
141     nice_component_restart (component);
142   }
143 }
144
145 static void
146 nice_stream_class_init (NiceStreamClass *klass)
147 {
148   GObjectClass *object_class = G_OBJECT_CLASS (klass);
149
150   object_class->finalize = nice_stream_finalize;
151 }
152
153 static void
154 nice_stream_init (NiceStream *stream)
155 {
156   g_atomic_int_inc (&n_streams_created);
157   nice_debug ("Created NiceStream (%u created, %u destroyed)",
158       n_streams_created, n_streams_destroyed);
159
160   stream->n_components = 0;
161   stream->initial_binding_request_received = FALSE;
162 }
163
164 /* Must be called with the agent lock released as it could dispose of
165  * NiceIOStreams. */
166 static void
167 nice_stream_finalize (GObject *obj)
168 {
169   NiceStream *stream;
170
171   stream = NICE_STREAM (obj);
172
173   g_free (stream->name);
174   g_slist_free_full (stream->components, (GDestroyNotify) g_object_unref);
175
176   g_atomic_int_inc (&n_streams_destroyed);
177   nice_debug ("Destroyed NiceStream (%u created, %u destroyed)",
178       n_streams_created, n_streams_destroyed);
179
180   G_OBJECT_CLASS (nice_stream_parent_class)->finalize (obj);
181 }