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