Fix memory leaks in stasis_channels and bridge_native_rtp
[asterisk/asterisk.git] / bridges / bridge_native_rtp.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Native RTP bridging technology module
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup bridges
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/bridging.h"
45 #include "asterisk/bridging_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/rtp_engine.h"
48 #include "asterisk/audiohook.h"
49
50 /*! \brief Forward declarations for frame hook usage */
51 static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);
52 static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);
53
54 /*! \brief Internal structure which contains information about bridged RTP channels */
55 struct native_rtp_bridge_data {
56         /*! \brief Framehook used to intercept certain control frames */
57         int id;
58 };
59
60 /*! \brief Frame hook that is called to intercept hold/unhold */
61 static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
62 {
63         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
64
65         if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
66                 return f;
67         }
68
69         ast_channel_lock(chan);
70         bridge = ast_channel_get_bridge(chan);
71         ast_channel_unlock(chan);
72
73         /* It's safe for NULL to be passed to both of these, bridge_channel isn't used at all */
74         if (bridge) {
75                 if (f->subclass.integer == AST_CONTROL_HOLD) {
76                         native_rtp_bridge_leave(ast_channel_internal_bridge(chan), NULL);
77                 } else if ((f->subclass.integer == AST_CONTROL_UNHOLD) || (f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
78                         native_rtp_bridge_join(ast_channel_internal_bridge(chan), NULL);
79                 }
80         }
81
82         return f;
83 }
84
85 /*! \brief Internal helper function which checks whether the channels are compatible with our native bridging */
86 static int native_rtp_bridge_capable(struct ast_channel *chan)
87 {
88         if (ast_channel_monitor(chan) || (ast_channel_audiohooks(chan) &&
89                 !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))) ||
90                 !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan))) {
91                 return 0;
92         } else {
93                 return 1;
94         }
95 }
96
97 /*! \brief Internal helper function which gets all RTP information (glue and instances) relating to the given channels */
98 static enum ast_rtp_glue_result native_rtp_bridge_get(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_glue **glue0,
99         struct ast_rtp_glue **glue1, struct ast_rtp_instance **instance0, struct ast_rtp_instance **instance1,
100         struct ast_rtp_instance **vinstance0, struct ast_rtp_instance **vinstance1)
101 {
102         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
103         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
104
105         if (!(*glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) ||
106                 (c1 && !(*glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)))) {
107                 return AST_RTP_GLUE_RESULT_FORBID;
108         }
109
110         audio_glue0_res = (*glue0)->get_rtp_info(c0, instance0);
111         video_glue0_res = (*glue0)->get_vrtp_info ? (*glue0)->get_vrtp_info(c0, vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
112
113         if (c1) {
114                 audio_glue1_res = (*glue1)->get_rtp_info(c1, instance1);
115                 video_glue1_res = (*glue1)->get_vrtp_info ? (*glue1)->get_vrtp_info(c1, vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
116         }
117
118         /* Apply any limitations on direct media bridging that may be present */
119         if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
120                 if ((*glue0)->allow_rtp_remote && !((*glue0)->allow_rtp_remote(c0, *instance1))) {
121                         /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
122                         audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
123                 } else if ((*glue1)->allow_rtp_remote && !((*glue1)->allow_rtp_remote(c1, *instance0))) {
124                         audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
125                 }
126         }
127         if (c1 && video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
128                 if ((*glue0)->allow_vrtp_remote && !((*glue0)->allow_vrtp_remote(c0, *instance1))) {
129                         /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
130                         video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
131                 } else if ((*glue1)->allow_vrtp_remote && !((*glue1)->allow_vrtp_remote(c1, *instance0))) {
132                         video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
133                 }
134         }
135
136         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
137         if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
138                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
139         }
140         if (c1 && video_glue1_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
141                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
142         }
143
144         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
145         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || (c1 && audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID)) {
146                 return AST_RTP_GLUE_RESULT_FORBID;
147         }
148
149         return audio_glue0_res;
150 }
151
152 static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
153 {
154         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
155         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
156         enum ast_rtp_glue_result native_type;
157         struct ast_rtp_glue *glue0, *glue1;
158         RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
159         RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
160         RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
161         RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
162         RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
163         RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
164         int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
165
166         /* We require two channels before even considering native bridging */
167         if (bridge->num_channels != 2) {
168                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as two channels are required\n",
169                         bridge->uniqueid);
170                 return 0;
171         }
172
173         if (!native_rtp_bridge_capable(c0->chan)) {
174                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
175                         bridge->uniqueid, ast_channel_name(c0->chan));
176                 return 0;
177         }
178
179         if (!native_rtp_bridge_capable(c1->chan)) {
180                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
181                         bridge->uniqueid, ast_channel_name(c1->chan));
182                 return 0;
183         }
184
185         if ((native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1))
186                 == AST_RTP_GLUE_RESULT_FORBID) {
187                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as it was forbidden while getting details\n",
188                         bridge->uniqueid);
189                 return 0;
190         }
191
192         if (ao2_container_count(c0->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance0)) {
193                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
194                         bridge->uniqueid, ast_channel_name(c0->chan));
195                 return 0;
196         }
197
198         if (ao2_container_count(c1->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance1)) {
199                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
200                         bridge->uniqueid, ast_channel_name(c1->chan));
201                 return 0;
202         }
203
204         if ((native_type == AST_RTP_GLUE_RESULT_LOCAL) && ((ast_rtp_instance_get_engine(instance0)->local_bridge !=
205                 ast_rtp_instance_get_engine(instance1)->local_bridge) ||
206                 (ast_rtp_instance_get_engine(instance0)->dtmf_compatible &&
207                         !ast_rtp_instance_get_engine(instance0)->dtmf_compatible(c0->chan, instance0, c1->chan, instance1)))) {
208                 ast_debug(1, "Bridge '%s' can not use local native RTP bridge as local bridge or DTMF is not compatible\n",
209                         bridge->uniqueid);
210                 return 0;
211         }
212
213         /* Make sure that codecs match */
214         if (glue0->get_codec) {
215                 glue0->get_codec(c0->chan, cap0);
216         }
217         if (glue1->get_codec) {
218                 glue1->get_codec(c1->chan, cap1);
219         }
220         if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
221                 char tmp0[256] = { 0, }, tmp1[256] = { 0, };
222
223                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
224                         ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
225                         ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
226                 return 0;
227         }
228
229         read_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawreadformat(c0->chan))).cur_ms;
230         read_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawreadformat(c1->chan))).cur_ms;
231         write_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawwriteformat(c0->chan))).cur_ms;
232         write_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawwriteformat(c1->chan))).cur_ms;
233
234         if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
235                 ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
236                                 read_ptime0, write_ptime1, read_ptime1, write_ptime0);
237                 return 0;
238         }
239
240         return 1;
241 }
242
243 /*! \brief Helper function which adds frame hook to bridge channel */
244 static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
245 {
246         struct native_rtp_bridge_data *data = ao2_alloc(sizeof(*data), NULL);
247         static struct ast_framehook_interface hook = {
248                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
249                 .event_cb = native_rtp_framehook,
250         };
251
252         if (!data) {
253                 return -1;
254         }
255
256         ast_channel_lock(bridge_channel->chan);
257         data->id = ast_framehook_attach(bridge_channel->chan, &hook);
258         ast_channel_unlock(bridge_channel->chan);
259         if (!data->id < 0) {
260                 ao2_cleanup(data);
261                 return -1;
262         }
263
264 /*
265  * BUGBUG The RTP native bridge technology should use tech_pvt not bridge_pvt.
266  *
267  * This technology needs to be reworked to not change the
268  * tech_pvt of channels other than the one that is currently
269  * entering/leaving before it can actually use the correct
270  * pointer.
271  */
272         bridge_channel->bridge_pvt = data;
273
274         return 0;
275 }
276
277 /*! \brief Helper function which removes frame hook from bridge channel */
278 static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
279 {
280         RAII_VAR(struct native_rtp_bridge_data *, data, bridge_channel->bridge_pvt, ao2_cleanup);
281
282         if (!data) {
283                 return;
284         }
285
286         ast_channel_lock(bridge_channel->chan);
287         ast_framehook_detach(bridge_channel->chan, data->id);
288         ast_channel_unlock(bridge_channel->chan);
289         bridge_channel->bridge_pvt = NULL;
290 }
291
292 static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
293 {
294         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
295         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
296         enum ast_rtp_glue_result native_type;
297         struct ast_rtp_glue *glue0, *glue1;
298         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL;
299         struct ast_rtp_instance *vinstance1 = NULL, *tinstance0 = NULL, *tinstance1 = NULL;
300         RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
301         RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
302
303         native_rtp_bridge_framehook_detach(c0);
304         if (native_rtp_bridge_framehook_attach(c0)) {
305                 return -1;
306         }
307
308         native_rtp_bridge_framehook_detach(c1);
309         if (native_rtp_bridge_framehook_attach(c1)) {
310                 native_rtp_bridge_framehook_detach(c0);
311                 return -1;
312         }
313
314         native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
315
316         if (glue0->get_codec) {
317                 glue0->get_codec(c0->chan, cap0);
318         }
319         if (glue1->get_codec) {
320                 glue1->get_codec(c1->chan, cap1);
321         }
322
323         switch (native_type) {
324         case AST_RTP_GLUE_RESULT_LOCAL:
325                 if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
326                         ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, instance1);
327                 }
328                 if (ast_rtp_instance_get_engine(instance1)->local_bridge) {
329                         ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, instance0);
330                 }
331                 ast_rtp_instance_set_bridged(instance0, instance1);
332                 ast_rtp_instance_set_bridged(instance1, instance0);
333                 break;
334
335         case AST_RTP_GLUE_RESULT_REMOTE:
336                 glue0->update_peer(c0->chan, instance1, vinstance1, tinstance1, cap1, 0);
337                 glue1->update_peer(c1->chan, instance0, vinstance0, tinstance0, cap0, 0);
338                 break;
339         case AST_RTP_GLUE_RESULT_FORBID:
340                 break;
341         }
342
343         return 0;
344 }
345
346 static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
347 {
348         native_rtp_bridge_join(bridge, bridge_channel);
349 }
350
351 static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
352 {
353         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels) ? AST_LIST_FIRST(&bridge->channels) : bridge_channel;
354         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
355         enum ast_rtp_glue_result native_type;
356         struct ast_rtp_glue *glue0, *glue1 = NULL;
357         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL, *vinstance1 = NULL;
358
359         native_rtp_bridge_framehook_detach(c0);
360         if (c1) {
361                 native_rtp_bridge_framehook_detach(c1);
362         }
363
364         native_type = native_rtp_bridge_get(c0->chan, c1 ? c1->chan : NULL, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
365
366         switch (native_type) {
367         case AST_RTP_GLUE_RESULT_LOCAL:
368                 if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
369                         ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, NULL);
370                 }
371                 if (instance1 && ast_rtp_instance_get_engine(instance1)->local_bridge) {
372                         ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, NULL);
373                 }
374                 ast_rtp_instance_set_bridged(instance0, instance1);
375                 if (instance1) {
376                         ast_rtp_instance_set_bridged(instance1, instance0);
377                 }
378                 break;
379         case AST_RTP_GLUE_RESULT_REMOTE:
380                 glue0->update_peer(c0->chan, NULL, NULL, NULL, NULL, 0);
381                 if (glue1) {
382                         glue1->update_peer(c1->chan, NULL, NULL, NULL, NULL, 0);
383                 }
384                 break;
385         case AST_RTP_GLUE_RESULT_FORBID:
386                 break;
387         }
388 }
389
390 static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
391 {
392         struct ast_bridge_channel *other = ast_bridge_channel_peer(bridge_channel);
393
394         if (!other) {
395                 return -1;
396         }
397
398         /* The bridging core takes care of freeing the passed in frame. */
399         ast_bridge_channel_queue_frame(other, frame);
400
401         return 0;
402 }
403
404 static struct ast_bridge_technology native_rtp_bridge = {
405         .name = "native_rtp",
406         .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
407         .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
408         .join = native_rtp_bridge_join,
409         .unsuspend = native_rtp_bridge_unsuspend,
410         .leave = native_rtp_bridge_leave,
411         .suspend = native_rtp_bridge_leave,
412         .write = native_rtp_bridge_write,
413         .compatible = native_rtp_bridge_compatible,
414 };
415
416 static int unload_module(void)
417 {
418         ast_format_cap_destroy(native_rtp_bridge.format_capabilities);
419         return ast_bridge_technology_unregister(&native_rtp_bridge);
420 }
421
422 static int load_module(void)
423 {
424         if (!(native_rtp_bridge.format_capabilities = ast_format_cap_alloc())) {
425                 return AST_MODULE_LOAD_DECLINE;
426         }
427         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
428         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
429         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
430
431         return ast_bridge_technology_register(&native_rtp_bridge);
432 }
433
434 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");