Merge "bridge_softmix.c: Change remove_destination_streams() return meaning."
[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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #include "asterisk/module.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/bridge.h"
43 #include "asterisk/bridge_technology.h"
44 #include "asterisk/frame.h"
45 #include "asterisk/rtp_engine.h"
46
47 /*! \brief Internal structure which contains bridged RTP channel hook data */
48 struct native_rtp_framehook_data {
49         /*! \brief Framehook used to intercept certain control frames */
50         int id;
51         /*! \brief Set when this framehook has been detached */
52         unsigned int detached;
53 };
54
55 struct rtp_glue_stream {
56         /*! \brief RTP instance */
57         struct ast_rtp_instance *instance;
58         /*! \brief glue result */
59         enum ast_rtp_glue_result result;
60 };
61
62 struct rtp_glue_data {
63         /*!
64          * \brief glue callbacks
65          *
66          * \note The glue data is considered valid if cb is not NULL.
67          */
68         struct ast_rtp_glue *cb;
69         struct rtp_glue_stream audio;
70         struct rtp_glue_stream video;
71         /*! Combined glue result of both bridge channels. */
72         enum ast_rtp_glue_result result;
73 };
74
75 /*! \brief Internal structure which contains instance information about bridged RTP channels */
76 struct native_rtp_bridge_channel_data {
77         /*! \brief Channel's hook data */
78         struct native_rtp_framehook_data *hook_data;
79         /*!
80          * \brief Glue callbacks to bring remote channel streams back to Asterisk.
81          * \note NULL if channel streams are local.
82          */
83         struct ast_rtp_glue *remote_cb;
84         /*! \brief Channel's cached RTP glue information */
85         struct rtp_glue_data glue;
86 };
87
88 static void rtp_glue_data_init(struct rtp_glue_data *glue)
89 {
90         glue->cb = NULL;
91         glue->audio.instance = NULL;
92         glue->audio.result = AST_RTP_GLUE_RESULT_FORBID;
93         glue->video.instance = NULL;
94         glue->video.result = AST_RTP_GLUE_RESULT_FORBID;
95         glue->result = AST_RTP_GLUE_RESULT_FORBID;
96 }
97
98 static void rtp_glue_data_destroy(struct rtp_glue_data *glue)
99 {
100         if (!glue) {
101                 return;
102         }
103         ao2_cleanup(glue->audio.instance);
104         ao2_cleanup(glue->video.instance);
105 }
106
107 static void rtp_glue_data_reset(struct rtp_glue_data *glue)
108 {
109         rtp_glue_data_destroy(glue);
110         rtp_glue_data_init(glue);
111 }
112
113 static void native_rtp_bridge_channel_data_free(struct native_rtp_bridge_channel_data *data)
114 {
115         ast_debug(2, "Destroying channel tech_pvt data %p\n", data);
116
117         /*
118          * hook_data will probably already have been unreferenced by the framehook detach
119          * and the pointer set to null.
120          */
121         ao2_cleanup(data->hook_data);
122
123         rtp_glue_data_reset(&data->glue);
124         ast_free(data);
125 }
126
127 static struct native_rtp_bridge_channel_data *native_rtp_bridge_channel_data_alloc(void)
128 {
129         struct native_rtp_bridge_channel_data *data;
130
131         data = ast_calloc(1, sizeof(*data));
132         if (data) {
133                 rtp_glue_data_init(&data->glue);
134         }
135         return data;
136 }
137
138 /*!
139  * \internal
140  * \brief Helper function which gets all RTP information (glue and instances) relating to the given channels
141  *
142  * \retval 0 on success.
143  * \retval -1 on error.
144  */
145 static int rtp_glue_data_get(struct ast_channel *c0, struct rtp_glue_data *glue0,
146         struct ast_channel *c1, struct rtp_glue_data *glue1)
147 {
148         struct ast_rtp_glue *cb0;
149         struct ast_rtp_glue *cb1;
150         enum ast_rtp_glue_result combined_result;
151
152         cb0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type);
153         cb1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type);
154         if (!cb0 || !cb1) {
155                 /* One or both channels doesn't have any RTP glue registered. */
156                 return -1;
157         }
158
159         /* The glue callbacks bump the RTP instance refcounts for us. */
160
161         glue0->cb = cb0;
162         glue0->audio.result = cb0->get_rtp_info(c0, &glue0->audio.instance);
163         glue0->video.result = cb0->get_vrtp_info
164                 ? cb0->get_vrtp_info(c0, &glue0->video.instance) : AST_RTP_GLUE_RESULT_FORBID;
165
166         glue1->cb = cb1;
167         glue1->audio.result = cb1->get_rtp_info(c1, &glue1->audio.instance);
168         glue1->video.result = cb1->get_vrtp_info
169                 ? cb1->get_vrtp_info(c1, &glue1->video.instance) : AST_RTP_GLUE_RESULT_FORBID;
170
171         /*
172          * Now determine the combined glue result.
173          */
174
175         /* Apply any limitations on direct media bridging that may be present */
176         if (glue0->audio.result == glue1->audio.result && glue1->audio.result == AST_RTP_GLUE_RESULT_REMOTE) {
177                 if (glue0->cb->allow_rtp_remote && !glue0->cb->allow_rtp_remote(c0, glue1->audio.instance)) {
178                         /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
179                         glue0->audio.result = glue1->audio.result = AST_RTP_GLUE_RESULT_LOCAL;
180                 } else if (glue1->cb->allow_rtp_remote && !glue1->cb->allow_rtp_remote(c1, glue0->audio.instance)) {
181                         glue0->audio.result = glue1->audio.result = AST_RTP_GLUE_RESULT_LOCAL;
182                 }
183         }
184         if (glue0->video.result == glue1->video.result && glue1->video.result == AST_RTP_GLUE_RESULT_REMOTE) {
185                 if (glue0->cb->allow_vrtp_remote && !glue0->cb->allow_vrtp_remote(c0, glue1->video.instance)) {
186                         /* If the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
187                         glue0->video.result = glue1->video.result = AST_RTP_GLUE_RESULT_LOCAL;
188                 } else if (glue1->cb->allow_vrtp_remote && !glue1->cb->allow_vrtp_remote(c1, glue0->video.instance)) {
189                         glue0->video.result = glue1->video.result = AST_RTP_GLUE_RESULT_LOCAL;
190                 }
191         }
192
193         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
194         if (glue0->video.result != AST_RTP_GLUE_RESULT_FORBID
195                 && (glue0->audio.result != AST_RTP_GLUE_RESULT_REMOTE
196                         || glue0->video.result != AST_RTP_GLUE_RESULT_REMOTE)) {
197                 glue0->audio.result = AST_RTP_GLUE_RESULT_FORBID;
198         }
199         if (glue1->video.result != AST_RTP_GLUE_RESULT_FORBID
200                 && (glue1->audio.result != AST_RTP_GLUE_RESULT_REMOTE
201                         || glue1->video.result != AST_RTP_GLUE_RESULT_REMOTE)) {
202                 glue1->audio.result = AST_RTP_GLUE_RESULT_FORBID;
203         }
204
205         /* The order of preference is: forbid, local, and remote. */
206         if (glue0->audio.result == AST_RTP_GLUE_RESULT_FORBID
207                 || glue1->audio.result == AST_RTP_GLUE_RESULT_FORBID) {
208                 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
209                 combined_result = AST_RTP_GLUE_RESULT_FORBID;
210         } else if (glue0->audio.result == AST_RTP_GLUE_RESULT_LOCAL
211                 || glue1->audio.result == AST_RTP_GLUE_RESULT_LOCAL) {
212                 combined_result = AST_RTP_GLUE_RESULT_LOCAL;
213         } else {
214                 combined_result = AST_RTP_GLUE_RESULT_REMOTE;
215         }
216         glue0->result = combined_result;
217         glue1->result = combined_result;
218
219         return 0;
220 }
221
222 /*!
223  * \internal
224  * \brief Get the current RTP native bridge combined glue result.
225  * \since 15.0.0
226  *
227  * \param c0 First bridge channel
228  * \param c1 Second bridge channel
229  *
230  * \note Both channels must be locked when calling this function.
231  *
232  * \return Current combined glue result.
233  */
234 static enum ast_rtp_glue_result rtp_glue_get_current_combined_result(struct ast_channel *c0,
235         struct ast_channel *c1)
236 {
237         struct rtp_glue_data glue_a;
238         struct rtp_glue_data glue_b;
239         struct rtp_glue_data *glue0;
240         struct rtp_glue_data *glue1;
241         enum ast_rtp_glue_result combined_result;
242
243         rtp_glue_data_init(&glue_a);
244         glue0 = &glue_a;
245         rtp_glue_data_init(&glue_b);
246         glue1 = &glue_b;
247         if (rtp_glue_data_get(c0, glue0, c1, glue1)) {
248                 return AST_RTP_GLUE_RESULT_FORBID;
249         }
250
251         combined_result = glue0->result;
252         rtp_glue_data_destroy(glue0);
253         rtp_glue_data_destroy(glue1);
254         return combined_result;
255 }
256
257 /*!
258  * \internal
259  * \brief Start native RTP bridging of two channels
260  *
261  * \param bridge The bridge that had native RTP bridging happening on it
262  * \param target If remote RTP bridging, the channel that is unheld.
263  *
264  * \note Bridge must be locked when calling this function.
265  */
266 static void native_rtp_bridge_start(struct ast_bridge *bridge, struct ast_channel *target)
267 {
268         struct ast_bridge_channel *bc0 = AST_LIST_FIRST(&bridge->channels);
269         struct ast_bridge_channel *bc1 = AST_LIST_LAST(&bridge->channels);
270         struct native_rtp_bridge_channel_data *data0;
271         struct native_rtp_bridge_channel_data *data1;
272         struct rtp_glue_data *glue0;
273         struct rtp_glue_data *glue1;
274         struct ast_format_cap *cap0;
275         struct ast_format_cap *cap1;
276         enum ast_rtp_glue_result native_type;
277
278         if (bc0 == bc1) {
279                 return;
280         }
281         data0 = bc0->tech_pvt;
282         data1 = bc1->tech_pvt;
283         if (!data0 || !data1) {
284                 /* Not all channels are joined with the bridge tech yet */
285                 return;
286         }
287         glue0 = &data0->glue;
288         glue1 = &data1->glue;
289
290         ast_channel_lock_both(bc0->chan, bc1->chan);
291
292         if (!glue0->cb || !glue1->cb) {
293                 /*
294                  * Somebody doesn't have glue data so the bridge isn't running
295                  *
296                  * Actually neither side should have glue data.
297                  */
298                 ast_assert(!glue0->cb && !glue1->cb);
299
300                 if (rtp_glue_data_get(bc0->chan, glue0, bc1->chan, glue1)) {
301                         /*
302                          * This might happen if one of the channels got masqueraded
303                          * at a critical time.  It's a bit of a stretch even then
304                          * since the channel is in a bridge.
305                          */
306                         goto done;
307                 }
308         }
309
310         ast_debug(2, "Bridge '%s'.  Tech starting '%s' and '%s' with target '%s'\n",
311                 bridge->uniqueid, ast_channel_name(bc0->chan), ast_channel_name(bc1->chan),
312                 target ? ast_channel_name(target) : "none");
313
314         native_type = glue0->result;
315
316         switch (native_type) {
317         case AST_RTP_GLUE_RESULT_LOCAL:
318                 if (ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge) {
319                         ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge(glue0->audio.instance, glue1->audio.instance);
320                 }
321                 if (ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge) {
322                         ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge(glue1->audio.instance, glue0->audio.instance);
323                 }
324                 ast_rtp_instance_set_bridged(glue0->audio.instance, glue1->audio.instance);
325                 ast_rtp_instance_set_bridged(glue1->audio.instance, glue0->audio.instance);
326                 ast_verb(4, "Locally RTP bridged '%s' and '%s' in stack\n",
327                         ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
328                 break;
329         case AST_RTP_GLUE_RESULT_REMOTE:
330                 cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
331                 cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
332                 if (!cap0 || !cap1) {
333                         ao2_cleanup(cap0);
334                         ao2_cleanup(cap1);
335                         break;
336                 }
337
338                 if (glue0->cb->get_codec) {
339                         glue0->cb->get_codec(bc0->chan, cap0);
340                 }
341                 if (glue1->cb->get_codec) {
342                         glue1->cb->get_codec(bc1->chan, cap1);
343                 }
344
345                 /*
346                  * If we have a target, it's the channel that received the UNHOLD or
347                  * UPDATE_RTP_PEER frame and was told to resume
348                  */
349                 if (!target) {
350                         /* Send both channels to remote */
351                         data0->remote_cb = glue0->cb;
352                         data1->remote_cb = glue1->cb;
353                         glue0->cb->update_peer(bc0->chan, glue1->audio.instance, glue1->video.instance, NULL, cap1, 0);
354                         glue1->cb->update_peer(bc1->chan, glue0->audio.instance, glue0->video.instance, NULL, cap0, 0);
355                         ast_verb(4, "Remotely bridged '%s' and '%s' - media will flow directly between them\n",
356                                 ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
357                 } else {
358                         /*
359                          * If a target was provided, it is the recipient of an unhold or an update and needs to have
360                          * its media redirected to fit the current remote bridging needs. The other channel is either
361                          * already set up to handle the new media path or will have its own set of updates independent
362                          * of this pass.
363                          */
364                         ast_debug(2, "Bridge '%s'.  Sending '%s' back to remote\n",
365                                 bridge->uniqueid, ast_channel_name(target));
366                         if (bc0->chan == target) {
367                                 data0->remote_cb = glue0->cb;
368                                 glue0->cb->update_peer(bc0->chan, glue1->audio.instance, glue1->video.instance, NULL, cap1, 0);
369                         } else {
370                                 data1->remote_cb = glue1->cb;
371                                 glue1->cb->update_peer(bc1->chan, glue0->audio.instance, glue0->video.instance, NULL, cap0, 0);
372                         }
373                 }
374
375                 ao2_cleanup(cap0);
376                 ao2_cleanup(cap1);
377                 break;
378         case AST_RTP_GLUE_RESULT_FORBID:
379                 break;
380         }
381
382         if (native_type != AST_RTP_GLUE_RESULT_REMOTE) {
383                 /* Bring any remaining channels back to us. */
384                 if (data0->remote_cb) {
385                         ast_debug(2, "Bridge '%s'.  Bringing back '%s' to us\n",
386                                 bridge->uniqueid, ast_channel_name(bc0->chan));
387                         data0->remote_cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
388                         data0->remote_cb = NULL;
389                 }
390                 if (data1->remote_cb) {
391                         ast_debug(2, "Bridge '%s'.  Bringing back '%s' to us\n",
392                                 bridge->uniqueid, ast_channel_name(bc1->chan));
393                         data1->remote_cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
394                         data1->remote_cb = NULL;
395                 }
396         }
397
398 done:
399         ast_channel_unlock(bc0->chan);
400         ast_channel_unlock(bc1->chan);
401 }
402
403 /*!
404  * \internal
405  * \brief Stop native RTP bridging of two channels
406  *
407  * \param bridge The bridge that had native RTP bridging happening on it
408  * \param target If remote RTP bridging, the channel that is held.
409  *
410  * \note The first channel to leave the bridge triggers the cleanup for both channels
411  */
412 static void native_rtp_bridge_stop(struct ast_bridge *bridge, struct ast_channel *target)
413 {
414         struct ast_bridge_channel *bc0 = AST_LIST_FIRST(&bridge->channels);
415         struct ast_bridge_channel *bc1 = AST_LIST_LAST(&bridge->channels);
416         struct native_rtp_bridge_channel_data *data0;
417         struct native_rtp_bridge_channel_data *data1;
418         struct rtp_glue_data *glue0;
419         struct rtp_glue_data *glue1;
420
421         if (bc0 == bc1) {
422                 return;
423         }
424         data0 = bc0->tech_pvt;
425         data1 = bc1->tech_pvt;
426         if (!data0 || !data1) {
427                 /* Not all channels are joined with the bridge tech */
428                 return;
429         }
430         glue0 = &data0->glue;
431         glue1 = &data1->glue;
432
433         ast_debug(2, "Bridge '%s'.  Tech stopping '%s' and '%s' with target '%s'\n",
434                 bridge->uniqueid, ast_channel_name(bc0->chan), ast_channel_name(bc1->chan),
435                 target ? ast_channel_name(target) : "none");
436
437         if (!glue0->cb || !glue1->cb) {
438                 /*
439                  * Somebody doesn't have glue data so the bridge isn't running
440                  *
441                  * Actually neither side should have glue data.
442                  */
443                 ast_assert(!glue0->cb && !glue1->cb);
444                 /* At most one channel can be left at the remote endpoint here. */
445                 ast_assert(!data0->remote_cb || !data1->remote_cb);
446
447                 /* Bring selected channel streams back to us */
448                 if (data0->remote_cb && (!target || target == bc0->chan)) {
449                         ast_channel_lock(bc0->chan);
450                         ast_debug(2, "Bridge '%s'.  Bringing back '%s' to us\n",
451                                 bridge->uniqueid, ast_channel_name(bc0->chan));
452                         data0->remote_cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
453                         data0->remote_cb = NULL;
454                         ast_channel_unlock(bc0->chan);
455                 }
456                 if (data1->remote_cb && (!target || target == bc1->chan)) {
457                         ast_channel_lock(bc1->chan);
458                         ast_debug(2, "Bridge '%s'.  Bringing back '%s' to us\n",
459                                 bridge->uniqueid, ast_channel_name(bc1->chan));
460                         data1->remote_cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
461                         data1->remote_cb = NULL;
462                         ast_channel_unlock(bc1->chan);
463                 }
464                 return;
465         }
466
467         ast_channel_lock_both(bc0->chan, bc1->chan);
468
469         switch (glue0->result) {
470         case AST_RTP_GLUE_RESULT_LOCAL:
471                 if (ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge) {
472                         ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge(glue0->audio.instance, NULL);
473                 }
474                 if (ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge) {
475                         ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge(glue1->audio.instance, NULL);
476                 }
477                 ast_rtp_instance_set_bridged(glue0->audio.instance, NULL);
478                 ast_rtp_instance_set_bridged(glue1->audio.instance, NULL);
479                 break;
480         case AST_RTP_GLUE_RESULT_REMOTE:
481                 if (target) {
482                         /*
483                          * If a target was provided, it is being put on hold and should expect to
484                          * receive media from Asterisk instead of what it was previously connected to.
485                          */
486                         ast_debug(2, "Bridge '%s'.  Bringing back '%s' to us\n",
487                                 bridge->uniqueid, ast_channel_name(target));
488                         if (bc0->chan == target) {
489                                 data0->remote_cb = NULL;
490                                 glue0->cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
491                         } else {
492                                 data1->remote_cb = NULL;
493                                 glue1->cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
494                         }
495                 } else {
496                         data0->remote_cb = NULL;
497                         data1->remote_cb = NULL;
498                         /*
499                          * XXX We don't want to bring back the channels if we are
500                          * switching to T.38.  We have received a reinvite on one channel
501                          * and we will be sending a reinvite on the other to start T.38.
502                          * If we bring the streams back now we confuse the chan_pjsip
503                          * channel driver processing the incoming T.38 reinvite with
504                          * reinvite glare.  I think this is really a bug in chan_pjsip
505                          * that this exception case is working around.
506                          */
507                         if (rtp_glue_get_current_combined_result(bc0->chan, bc1->chan)
508                                 != AST_RTP_GLUE_RESULT_FORBID) {
509                                 ast_debug(2, "Bridge '%s'.  Bringing back '%s' and '%s' to us\n",
510                                         bridge->uniqueid, ast_channel_name(bc0->chan),
511                                         ast_channel_name(bc1->chan));
512                                 glue0->cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
513                                 glue1->cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
514                         } else {
515                                 ast_debug(2, "Bridge '%s'.  Skip bringing back '%s' and '%s' to us\n",
516                                         bridge->uniqueid, ast_channel_name(bc0->chan),
517                                         ast_channel_name(bc1->chan));
518                         }
519                 }
520                 break;
521         case AST_RTP_GLUE_RESULT_FORBID:
522                 break;
523         }
524
525         rtp_glue_data_reset(glue0);
526         rtp_glue_data_reset(glue1);
527
528         ast_debug(2, "Discontinued RTP bridging of '%s' and '%s' - media will flow through Asterisk core\n",
529                 ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
530
531         ast_channel_unlock(bc0->chan);
532         ast_channel_unlock(bc1->chan);
533 }
534
535 /*!
536  * \internal
537  * \brief Frame hook that is called to intercept hold/unhold
538  */
539 static struct ast_frame *native_rtp_framehook(struct ast_channel *chan,
540         struct ast_frame *f, enum ast_framehook_event event, void *data)
541 {
542         struct ast_bridge *bridge;
543         struct native_rtp_framehook_data *native_data = data;
544
545         if (!f
546                 || f->frametype != AST_FRAME_CONTROL
547                 || event != AST_FRAMEHOOK_EVENT_WRITE) {
548                 return f;
549         }
550
551         bridge = ast_channel_get_bridge(chan);
552         if (bridge) {
553                 /* native_rtp_bridge_start/stop are not being called from bridging
554                    core so we need to lock the bridge prior to calling these functions
555                    Unfortunately that means unlocking the channel, but as it
556                    should not be modified this should be okay... hopefully...
557                    unless this channel is being moved around right now and is in
558                    the process of having this framehook removed (which is fine). To
559                    ensure we then don't stop or start when we shouldn't we consult
560                    the data provided. If this framehook has been detached then the
561                    detached variable will be set. This is safe to check as it is only
562                    manipulated with the bridge lock held. */
563                 ast_channel_unlock(chan);
564                 ast_bridge_lock(bridge);
565                 if (!native_data->detached) {
566                         switch (f->subclass.integer) {
567                         case AST_CONTROL_HOLD:
568                                 native_rtp_bridge_stop(bridge, chan);
569                                 break;
570                         case AST_CONTROL_UNHOLD:
571                         case AST_CONTROL_UPDATE_RTP_PEER:
572                                 native_rtp_bridge_start(bridge, chan);
573                                 break;
574                         default:
575                                 break;
576                         }
577                 }
578                 ast_bridge_unlock(bridge);
579                 ao2_ref(bridge, -1);
580                 ast_channel_lock(chan);
581         }
582
583         return f;
584 }
585
586 /*!
587  * \internal
588  * \brief Callback function which informs upstream if we are consuming a frame of a specific type
589  */
590 static int native_rtp_framehook_consume(void *data, enum ast_frame_type type)
591 {
592         return (type == AST_FRAME_CONTROL ? 1 : 0);
593 }
594
595 /*!
596  * \internal
597  * \brief Internal helper function which checks whether a channel is compatible with our native bridging
598  */
599 static int native_rtp_bridge_capable(struct ast_channel *chan)
600 {
601         return !ast_channel_has_hook_requiring_audio(chan)
602                         && ast_channel_state(chan) == AST_STATE_UP;
603 }
604
605 /*!
606  * \internal
607  * \brief Internal helper function which checks whether both channels are compatible with our native bridging
608  */
609 static int native_rtp_bridge_compatible_check(struct ast_bridge *bridge, struct ast_bridge_channel *bc0, struct ast_bridge_channel *bc1)
610 {
611         enum ast_rtp_glue_result native_type;
612         int read_ptime0;
613         int read_ptime1;
614         int write_ptime0;
615         int write_ptime1;
616         struct rtp_glue_data glue_a;
617         struct rtp_glue_data glue_b;
618         RAII_VAR(struct ast_format_cap *, cap0, NULL, ao2_cleanup);
619         RAII_VAR(struct ast_format_cap *, cap1, NULL, ao2_cleanup);
620         RAII_VAR(struct rtp_glue_data *, glue0, NULL, rtp_glue_data_destroy);
621         RAII_VAR(struct rtp_glue_data *, glue1, NULL, rtp_glue_data_destroy);
622
623         ast_debug(1, "Bridge '%s'.  Checking compatability for channels '%s' and '%s'\n",
624                 bridge->uniqueid, ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
625
626         if (!native_rtp_bridge_capable(bc0->chan)) {
627                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
628                         bridge->uniqueid, ast_channel_name(bc0->chan));
629                 return 0;
630         }
631
632         if (!native_rtp_bridge_capable(bc1->chan)) {
633                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
634                         bridge->uniqueid, ast_channel_name(bc1->chan));
635                 return 0;
636         }
637
638         rtp_glue_data_init(&glue_a);
639         glue0 = &glue_a;
640         rtp_glue_data_init(&glue_b);
641         glue1 = &glue_b;
642         if (rtp_glue_data_get(bc0->chan, glue0, bc1->chan, glue1)) {
643                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as could not get details\n",
644                         bridge->uniqueid);
645                 return 0;
646         }
647         native_type = glue0->result;
648
649         if (native_type == AST_RTP_GLUE_RESULT_FORBID) {
650                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as it was forbidden while getting details\n",
651                         bridge->uniqueid);
652                 return 0;
653         }
654
655         if (ao2_container_count(bc0->features->dtmf_hooks)
656                 && ast_rtp_instance_dtmf_mode_get(glue0->audio.instance)) {
657                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
658                         bridge->uniqueid, ast_channel_name(bc0->chan));
659                 return 0;
660         }
661
662         if (ao2_container_count(bc1->features->dtmf_hooks)
663                 && ast_rtp_instance_dtmf_mode_get(glue1->audio.instance)) {
664                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
665                         bridge->uniqueid, ast_channel_name(bc1->chan));
666                 return 0;
667         }
668
669         if (native_type == AST_RTP_GLUE_RESULT_LOCAL
670                 && (ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge
671                         != ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge
672                         || (ast_rtp_instance_get_engine(glue0->audio.instance)->dtmf_compatible
673                                 && !ast_rtp_instance_get_engine(glue0->audio.instance)->dtmf_compatible(bc0->chan,
674                                         glue0->audio.instance, bc1->chan, glue1->audio.instance)))) {
675                 ast_debug(1, "Bridge '%s' can not use local native RTP bridge as local bridge or DTMF is not compatible\n",
676                         bridge->uniqueid);
677                 return 0;
678         }
679
680         cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
681         cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
682         if (!cap0 || !cap1) {
683                 return 0;
684         }
685
686         /* Make sure that codecs match */
687         if (glue0->cb->get_codec) {
688                 glue0->cb->get_codec(bc0->chan, cap0);
689         }
690         if (glue1->cb->get_codec) {
691                 glue1->cb->get_codec(bc1->chan, cap1);
692         }
693         if (ast_format_cap_count(cap0) != 0
694                 && ast_format_cap_count(cap1) != 0
695                 && !ast_format_cap_iscompatible(cap0, cap1)) {
696                 struct ast_str *codec_buf0 = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
697                 struct ast_str *codec_buf1 = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
698
699                 ast_debug(1, "Bridge '%s': Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
700                         bridge->uniqueid,
701                         ast_format_cap_get_names(cap0, &codec_buf0),
702                         ast_format_cap_get_names(cap1, &codec_buf1));
703                 return 0;
704         }
705
706         read_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawreadformat(bc0->chan));
707         read_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawreadformat(bc1->chan));
708         write_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawwriteformat(bc0->chan));
709         write_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawwriteformat(bc1->chan));
710
711         if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
712                 ast_debug(1, "Bridge '%s': Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
713                         bridge->uniqueid,
714                         read_ptime0, write_ptime1, read_ptime1, write_ptime0);
715                 return 0;
716         }
717
718         return 1;
719 }
720
721 /*!
722  * \internal
723  * \brief Called by the bridge core "compatible' callback
724  */
725 static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
726 {
727         struct ast_bridge_channel *bc0;
728         struct ast_bridge_channel *bc1;
729         int is_compatible;
730
731         /* We require two channels before even considering native bridging */
732         if (bridge->num_channels != 2) {
733                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as two channels are required\n",
734                         bridge->uniqueid);
735                 return 0;
736         }
737
738         bc0 = AST_LIST_FIRST(&bridge->channels);
739         bc1 = AST_LIST_LAST(&bridge->channels);
740
741         ast_channel_lock_both(bc0->chan, bc1->chan);
742         is_compatible = native_rtp_bridge_compatible_check(bridge, bc0, bc1);
743         ast_channel_unlock(bc0->chan);
744         ast_channel_unlock(bc1->chan);
745
746         return is_compatible;
747 }
748
749 /*!
750  * \internal
751  * \brief Helper function which adds frame hook to bridge channel
752  */
753 static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
754 {
755         struct native_rtp_bridge_channel_data *data = bridge_channel->tech_pvt;
756         static struct ast_framehook_interface hook = {
757                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
758                 .event_cb = native_rtp_framehook,
759                 .destroy_cb = __ao2_cleanup,
760                 .consume_cb = native_rtp_framehook_consume,
761                 .disable_inheritance = 1,
762         };
763
764         ast_assert(data->hook_data == NULL);
765         data->hook_data = ao2_alloc_options(sizeof(*data->hook_data), NULL,
766                 AO2_ALLOC_OPT_LOCK_NOLOCK);
767         if (!data->hook_data) {
768                 return -1;
769         }
770
771         ast_debug(2, "Bridge '%s'.  Attaching hook data %p to '%s'\n",
772                 bridge_channel->bridge->uniqueid, data, ast_channel_name(bridge_channel->chan));
773
774         ast_channel_lock(bridge_channel->chan);
775         /* We're giving 1 ref to the framehook and keeping the one from the alloc for ourselves */
776         hook.data = ao2_bump(data->hook_data);
777         data->hook_data->id = ast_framehook_attach(bridge_channel->chan, &hook);
778         ast_channel_unlock(bridge_channel->chan);
779         if (data->hook_data->id < 0) {
780                 /*
781                  * We need to drop both the reference we hold in data,
782                  * and the one the framehook would hold.
783                  */
784                 ao2_ref(data->hook_data, -2);
785                 data->hook_data = NULL;
786
787                 return -1;
788         }
789
790         return 0;
791 }
792
793 /*!
794  * \internal
795  * \brief Helper function which removes frame hook from bridge channel
796  */
797 static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
798 {
799         struct native_rtp_bridge_channel_data *data = bridge_channel->tech_pvt;
800
801         if (!data || !data->hook_data) {
802                 return;
803         }
804
805         ast_debug(2, "Bridge '%s'.  Detaching hook data %p from '%s'\n",
806                 bridge_channel->bridge->uniqueid, data->hook_data, ast_channel_name(bridge_channel->chan));
807
808         ast_channel_lock(bridge_channel->chan);
809         ast_framehook_detach(bridge_channel->chan, data->hook_data->id);
810         data->hook_data->detached = 1;
811         ast_channel_unlock(bridge_channel->chan);
812         ao2_cleanup(data->hook_data);
813         data->hook_data = NULL;
814 }
815
816 /*!
817  * \internal
818  * \brief Called by the bridge core 'join' callback for each channel joining he bridge
819  */
820 static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
821 {
822         ast_debug(2, "Bridge '%s'.  Channel '%s' is joining bridge tech\n",
823                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
824
825         ast_assert(bridge_channel->tech_pvt == NULL);
826
827         if (bridge_channel->suspended) {
828                 /* The channel will rejoin when it is unsuspended */
829                 return 0;
830         }
831
832         bridge_channel->tech_pvt = native_rtp_bridge_channel_data_alloc();
833         if (!bridge_channel->tech_pvt) {
834                 return -1;
835         }
836
837         if (native_rtp_bridge_framehook_attach(bridge_channel)) {
838                 native_rtp_bridge_channel_data_free(bridge_channel->tech_pvt);
839                 bridge_channel->tech_pvt = NULL;
840                 return -1;
841         }
842
843         native_rtp_bridge_start(bridge, NULL);
844         return 0;
845 }
846
847 /*!
848  * \internal
849  * \brief Add the channel back into the bridge
850  */
851 static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
852 {
853         ast_debug(2, "Bridge '%s'.  Channel '%s' is unsuspended back to bridge tech\n",
854                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
855         native_rtp_bridge_join(bridge, bridge_channel);
856 }
857
858 /*!
859  * \internal
860  * \brief Leave the bridge
861  */
862 static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
863 {
864         ast_debug(2, "Bridge '%s'.  Channel '%s' is leaving bridge tech\n",
865                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
866
867         if (!bridge_channel->tech_pvt) {
868                 return;
869         }
870
871         native_rtp_bridge_framehook_detach(bridge_channel);
872         native_rtp_bridge_stop(bridge, NULL);
873
874         native_rtp_bridge_channel_data_free(bridge_channel->tech_pvt);
875         bridge_channel->tech_pvt = NULL;
876 }
877
878 /*!
879  * \internal
880  * \brief Suspend the channel from the bridge
881  */
882 static void native_rtp_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
883 {
884         ast_debug(2, "Bridge '%s'.  Channel '%s' is suspending from bridge tech\n",
885                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
886         native_rtp_bridge_leave(bridge, bridge_channel);
887 }
888
889 static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
890 {
891         const struct ast_control_t38_parameters *t38_parameters;
892         int defer = 0;
893
894         if (!ast_bridge_queue_everyone_else(bridge, bridge_channel, frame)) {
895                 /* This frame was successfully queued so no need to defer */
896                 return 0;
897         }
898
899         /* Depending on the frame defer it so when the next channel joins it receives it */
900         switch (frame->frametype) {
901         case AST_FRAME_CONTROL:
902                 switch (frame->subclass.integer) {
903                 case AST_CONTROL_T38_PARAMETERS:
904                         t38_parameters = frame->data.ptr;
905                         switch (t38_parameters->request_response) {
906                         case AST_T38_REQUEST_NEGOTIATE:
907                                 defer = -1;
908                                 break;
909                         default:
910                                 break;
911                         }
912                         break;
913                 default:
914                         break;
915                 }
916                 break;
917         default:
918                 break;
919         }
920
921         return defer;
922 }
923
924 static struct ast_bridge_technology native_rtp_bridge = {
925         .name = "native_rtp",
926         .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
927         .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
928         .join = native_rtp_bridge_join,
929         .unsuspend = native_rtp_bridge_unsuspend,
930         .leave = native_rtp_bridge_leave,
931         .suspend = native_rtp_bridge_suspend,
932         .write = native_rtp_bridge_write,
933         .compatible = native_rtp_bridge_compatible,
934 };
935
936 static int unload_module(void)
937 {
938         ast_bridge_technology_unregister(&native_rtp_bridge);
939         return 0;
940 }
941
942 static int load_module(void)
943 {
944         if (ast_bridge_technology_register(&native_rtp_bridge)) {
945                 unload_module();
946                 return AST_MODULE_LOAD_DECLINE;
947         }
948         return AST_MODULE_LOAD_SUCCESS;
949 }
950
951 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");