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