bridge_native_rtp: Fix hold chain bugs caused by native RTP bridge framehook
[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/bridge.h"
45 #include "asterisk/bridge_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/rtp_engine.h"
48
49 /*! \brief Internal structure which contains information about bridged RTP channels */
50 struct native_rtp_bridge_data {
51         /*! \brief Framehook used to intercept certain control frames */
52         int id;
53 };
54
55 /*! \brief Internal helper function which gets all RTP information (glue and instances) relating to the given channels */
56 static enum ast_rtp_glue_result native_rtp_bridge_get(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_glue **glue0,
57         struct ast_rtp_glue **glue1, struct ast_rtp_instance **instance0, struct ast_rtp_instance **instance1,
58         struct ast_rtp_instance **vinstance0, struct ast_rtp_instance **vinstance1)
59 {
60         enum ast_rtp_glue_result audio_glue0_res;
61         enum ast_rtp_glue_result video_glue0_res;
62         enum ast_rtp_glue_result audio_glue1_res;
63         enum ast_rtp_glue_result video_glue1_res;
64
65         if (!(*glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) ||
66                 !(*glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
67                 return AST_RTP_GLUE_RESULT_FORBID;
68         }
69
70         audio_glue0_res = (*glue0)->get_rtp_info(c0, instance0);
71         video_glue0_res = (*glue0)->get_vrtp_info ? (*glue0)->get_vrtp_info(c0, vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
72
73         audio_glue1_res = (*glue1)->get_rtp_info(c1, instance1);
74         video_glue1_res = (*glue1)->get_vrtp_info ? (*glue1)->get_vrtp_info(c1, vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
75
76         /* Apply any limitations on direct media bridging that may be present */
77         if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
78                 if ((*glue0)->allow_rtp_remote && !((*glue0)->allow_rtp_remote(c0, *instance1))) {
79                         /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
80                         audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
81                 } else if ((*glue1)->allow_rtp_remote && !((*glue1)->allow_rtp_remote(c1, *instance0))) {
82                         audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
83                 }
84         }
85         if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
86                 if ((*glue0)->allow_vrtp_remote && !((*glue0)->allow_vrtp_remote(c0, *instance1))) {
87                         /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
88                         video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
89                 } else if ((*glue1)->allow_vrtp_remote && !((*glue1)->allow_vrtp_remote(c1, *instance0))) {
90                         video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
91                 }
92         }
93
94         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
95         if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID
96                 && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE
97                         || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
98                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
99         }
100         if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID
101                 && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE
102                         || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
103                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
104         }
105
106         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
107         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID
108                 || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
109                 return AST_RTP_GLUE_RESULT_FORBID;
110         }
111
112         return audio_glue0_res;
113 }
114
115 static int native_rtp_bridge_start(struct ast_bridge *bridge, struct ast_channel *target)
116 {
117         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
118         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
119         enum ast_rtp_glue_result native_type;
120         struct ast_rtp_glue *glue0, *glue1;
121         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL;
122         struct ast_rtp_instance *vinstance1 = NULL, *tinstance0 = NULL, *tinstance1 = NULL;
123         RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
124         RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
125
126         if (c0 == c1) {
127                 return 0;
128         }
129
130         native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
131
132         if (glue0->get_codec) {
133                 glue0->get_codec(c0->chan, cap0);
134         }
135         if (glue1->get_codec) {
136                 glue1->get_codec(c1->chan, cap1);
137         }
138
139         switch (native_type) {
140         case AST_RTP_GLUE_RESULT_LOCAL:
141                 if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
142                         ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, instance1);
143                 }
144                 if (ast_rtp_instance_get_engine(instance1)->local_bridge) {
145                         ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, instance0);
146                 }
147                 ast_rtp_instance_set_bridged(instance0, instance1);
148                 ast_rtp_instance_set_bridged(instance1, instance0);
149                 ast_debug(2, "Locally RTP bridged '%s' and '%s' in stack\n",
150                         ast_channel_name(c0->chan), ast_channel_name(c1->chan));
151                 break;
152
153         case AST_RTP_GLUE_RESULT_REMOTE:
154
155                 /* If we have a target, it's the channel that received the UNHOLD or UPDATE_RTP_PEER frame and was told to resume */
156                 if (!target) {
157                         glue0->update_peer(c0->chan, instance1, vinstance1, tinstance1, cap1, 0);
158                         glue1->update_peer(c1->chan, instance0, vinstance0, tinstance0, cap0, 0);
159                         ast_debug(2, "Remotely bridged '%s' and '%s' - media will flow directly between them\n",
160                                 ast_channel_name(c0->chan), ast_channel_name(c1->chan));
161                 } else {
162                         /*
163                          * If a target was provided, it is the recipient of an unhold or an update and needs to have
164                          * its media redirected to fit the current remote bridging needs. The other channel is either
165                          * already set up to handle the new media path or will have its own set of updates independent
166                          * of this pass.
167                          */
168                         if (c0->chan == target) {
169                                 glue0->update_peer(c0->chan, instance1, vinstance1, tinstance1, cap1, 0);
170                         } else {
171                                 glue1->update_peer(c1->chan, instance0, vinstance0, tinstance0, cap0, 0);
172                         }
173                 }
174                 break;
175         case AST_RTP_GLUE_RESULT_FORBID:
176                 break;
177         }
178
179         return 0;
180 }
181
182 static void native_rtp_bridge_stop(struct ast_bridge *bridge, struct ast_channel *target)
183 {
184         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
185         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
186         enum ast_rtp_glue_result native_type;
187         struct ast_rtp_glue *glue0, *glue1 = NULL;
188         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL, *vinstance1 = NULL;
189
190         if (c0 == c1) {
191                 return;
192         }
193
194         native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
195
196         switch (native_type) {
197         case AST_RTP_GLUE_RESULT_LOCAL:
198                 if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
199                         ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, NULL);
200                 }
201                 if (instance1 && ast_rtp_instance_get_engine(instance1)->local_bridge) {
202                         ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, NULL);
203                 }
204                 ast_rtp_instance_set_bridged(instance0, NULL);
205                 if (instance1) {
206                         ast_rtp_instance_set_bridged(instance1, NULL);
207                 }
208                 break;
209         case AST_RTP_GLUE_RESULT_REMOTE:
210                 if (!target) {
211                         glue0->update_peer(c0->chan, NULL, NULL, NULL, NULL, 0);
212                         if (glue1) {
213                                 glue1->update_peer(c1->chan, NULL, NULL, NULL, NULL, 0);
214                         }
215                 } else {
216                         /*
217                          * If a target was provided, it is being put on hold and should expect to
218                          * receive mediafrom sterisk instead of what it was previously connected to.
219                          */
220                         if (c0->chan == target) {
221                                 glue0->update_peer(c0->chan, NULL, NULL, NULL, NULL, 0);
222                         } else if (glue1) {
223                                 glue1->update_peer(c1->chan, NULL, NULL, NULL, NULL, 0);
224                         }
225                 }
226                 break;
227         case AST_RTP_GLUE_RESULT_FORBID:
228                 break;
229         }
230
231         ast_debug(2, "Discontinued RTP bridging of '%s' and '%s' - media will flow through Asterisk core\n",
232                 ast_channel_name(c0->chan), ast_channel_name(c1->chan));
233 }
234
235 /*! \brief Frame hook that is called to intercept hold/unhold */
236 static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
237 {
238         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
239
240         if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
241                 return f;
242         }
243
244         ast_channel_lock(chan);
245         bridge = ast_channel_get_bridge(chan);
246         ast_channel_unlock(chan);
247
248         if (bridge) {
249                 if (f->subclass.integer == AST_CONTROL_HOLD) {
250                         native_rtp_bridge_stop(bridge, chan);
251                 } else if ((f->subclass.integer == AST_CONTROL_UNHOLD) || (f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
252                         native_rtp_bridge_start(bridge, chan);
253                 }
254         }
255
256         return f;
257 }
258
259 /*! \brief Internal helper function which checks whether the channels are compatible with our native bridging */
260 static int native_rtp_bridge_capable(struct ast_channel *chan)
261 {
262         return !ast_channel_has_audio_frame_or_monitor(chan);
263 }
264
265 static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
266 {
267         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
268         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
269         enum ast_rtp_glue_result native_type;
270         struct ast_rtp_glue *glue0, *glue1;
271         RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
272         RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
273         RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
274         RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
275         RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
276         RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
277         int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
278
279         /* We require two channels before even considering native bridging */
280         if (bridge->num_channels != 2) {
281                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as two channels are required\n",
282                         bridge->uniqueid);
283                 return 0;
284         }
285
286         if (!native_rtp_bridge_capable(c0->chan)) {
287                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
288                         bridge->uniqueid, ast_channel_name(c0->chan));
289                 return 0;
290         }
291
292         if (!native_rtp_bridge_capable(c1->chan)) {
293                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
294                         bridge->uniqueid, ast_channel_name(c1->chan));
295                 return 0;
296         }
297
298         if ((native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1))
299                 == AST_RTP_GLUE_RESULT_FORBID) {
300                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as it was forbidden while getting details\n",
301                         bridge->uniqueid);
302                 return 0;
303         }
304
305         if (ao2_container_count(c0->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance0)) {
306                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
307                         bridge->uniqueid, ast_channel_name(c0->chan));
308                 return 0;
309         }
310
311         if (ao2_container_count(c1->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance1)) {
312                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
313                         bridge->uniqueid, ast_channel_name(c1->chan));
314                 return 0;
315         }
316
317         if ((native_type == AST_RTP_GLUE_RESULT_LOCAL) && ((ast_rtp_instance_get_engine(instance0)->local_bridge !=
318                 ast_rtp_instance_get_engine(instance1)->local_bridge) ||
319                 (ast_rtp_instance_get_engine(instance0)->dtmf_compatible &&
320                         !ast_rtp_instance_get_engine(instance0)->dtmf_compatible(c0->chan, instance0, c1->chan, instance1)))) {
321                 ast_debug(1, "Bridge '%s' can not use local native RTP bridge as local bridge or DTMF is not compatible\n",
322                         bridge->uniqueid);
323                 return 0;
324         }
325
326         /* Make sure that codecs match */
327         if (glue0->get_codec) {
328                 glue0->get_codec(c0->chan, cap0);
329         }
330         if (glue1->get_codec) {
331                 glue1->get_codec(c1->chan, cap1);
332         }
333         if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
334                 char tmp0[256] = { 0, }, tmp1[256] = { 0, };
335
336                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
337                         ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
338                         ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
339                 return 0;
340         }
341
342         read_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawreadformat(c0->chan))).cur_ms;
343         read_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawreadformat(c1->chan))).cur_ms;
344         write_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawwriteformat(c0->chan))).cur_ms;
345         write_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawwriteformat(c1->chan))).cur_ms;
346
347         if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
348                 ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
349                                 read_ptime0, write_ptime1, read_ptime1, write_ptime0);
350                 return 0;
351         }
352
353         return 1;
354 }
355
356 /*! \brief Helper function which adds frame hook to bridge channel */
357 static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
358 {
359         struct native_rtp_bridge_data *data = ao2_alloc(sizeof(*data), NULL);
360         static struct ast_framehook_interface hook = {
361                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
362                 .event_cb = native_rtp_framehook,
363         };
364
365         if (!data) {
366                 return -1;
367         }
368
369         ast_channel_lock(bridge_channel->chan);
370         data->id = ast_framehook_attach(bridge_channel->chan, &hook);
371         ast_channel_unlock(bridge_channel->chan);
372         if (data->id < 0) {
373                 ao2_cleanup(data);
374                 return -1;
375         }
376
377         bridge_channel->tech_pvt = data;
378
379         return 0;
380 }
381
382 /*! \brief Helper function which removes frame hook from bridge channel */
383 static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
384 {
385         RAII_VAR(struct native_rtp_bridge_data *, data, bridge_channel->tech_pvt, ao2_cleanup);
386
387         if (!data) {
388                 return;
389         }
390
391         ast_channel_lock(bridge_channel->chan);
392         ast_framehook_detach(bridge_channel->chan, data->id);
393         ast_channel_unlock(bridge_channel->chan);
394         bridge_channel->tech_pvt = NULL;
395 }
396
397 static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
398 {
399         native_rtp_bridge_framehook_detach(bridge_channel);
400         if (native_rtp_bridge_framehook_attach(bridge_channel)) {
401                 return -1;
402         }
403
404         return native_rtp_bridge_start(bridge, NULL);
405 }
406
407 static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
408 {
409         native_rtp_bridge_join(bridge, bridge_channel);
410 }
411
412 static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
413 {
414         native_rtp_bridge_framehook_detach(bridge_channel);
415
416         native_rtp_bridge_stop(bridge, NULL);
417 }
418
419 static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
420 {
421         return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
422 }
423
424 static struct ast_bridge_technology native_rtp_bridge = {
425         .name = "native_rtp",
426         .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
427         .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
428         .join = native_rtp_bridge_join,
429         .unsuspend = native_rtp_bridge_unsuspend,
430         .leave = native_rtp_bridge_leave,
431         .suspend = native_rtp_bridge_leave,
432         .write = native_rtp_bridge_write,
433         .compatible = native_rtp_bridge_compatible,
434 };
435
436 static int unload_module(void)
437 {
438         ast_format_cap_destroy(native_rtp_bridge.format_capabilities);
439         return ast_bridge_technology_unregister(&native_rtp_bridge);
440 }
441
442 static int load_module(void)
443 {
444         if (!(native_rtp_bridge.format_capabilities = ast_format_cap_alloc())) {
445                 return AST_MODULE_LOAD_DECLINE;
446         }
447         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
448         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
449         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
450
451         return ast_bridge_technology_register(&native_rtp_bridge);
452 }
453
454 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");