bridge_native_rtp: Keep rtp instance refs on bridge_channel
[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->audio.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->audio.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         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
543         struct native_rtp_framehook_data *native_data = data;
544
545         if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
546                 return f;
547         }
548
549         bridge = ast_channel_get_bridge(chan);
550         if (bridge) {
551                 /* native_rtp_bridge_start/stop are not being called from bridging
552                    core so we need to lock the bridge prior to calling these functions
553                    Unfortunately that means unlocking the channel, but as it
554                    should not be modified this should be okay... hopefully...
555                    unless this channel is being moved around right now and is in
556                    the process of having this framehook removed (which is fine). To
557                    ensure we then don't stop or start when we shouldn't we consult
558                    the data provided. If this framehook has been detached then the
559                    detached variable will be set. This is safe to check as it is only
560                    manipulated with the bridge lock held. */
561                 ast_channel_unlock(chan);
562                 ast_bridge_lock(bridge);
563                 if (!native_data->detached) {
564                         if (f->subclass.integer == AST_CONTROL_HOLD) {
565                                 native_rtp_bridge_stop(bridge, chan);
566                         } else if ((f->subclass.integer == AST_CONTROL_UNHOLD) ||
567                                 (f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
568                                 native_rtp_bridge_start(bridge, chan);
569                         }
570                 }
571                 ast_bridge_unlock(bridge);
572                 ast_channel_lock(chan);
573         }
574
575         return f;
576 }
577
578 /*!
579  * \internal
580  * \brief Callback function which informs upstream if we are consuming a frame of a specific type
581  */
582 static int native_rtp_framehook_consume(void *data, enum ast_frame_type type)
583 {
584         return (type == AST_FRAME_CONTROL ? 1 : 0);
585 }
586
587 /*!
588  * \internal
589  * \brief Internal helper function which checks whether a channel is compatible with our native bridging
590  */
591 static int native_rtp_bridge_capable(struct ast_channel *chan)
592 {
593         return !ast_channel_has_hook_requiring_audio(chan);
594 }
595
596 /*!
597  * \internal
598  * \brief Internal helper function which checks whether both channels are compatible with our native bridging
599  */
600 static int native_rtp_bridge_compatible_check(struct ast_bridge *bridge, struct ast_bridge_channel *bc0, struct ast_bridge_channel *bc1)
601 {
602         enum ast_rtp_glue_result native_type;
603         int read_ptime0;
604         int read_ptime1;
605         int write_ptime0;
606         int write_ptime1;
607         struct rtp_glue_data glue_a;
608         struct rtp_glue_data glue_b;
609         RAII_VAR(struct ast_format_cap *, cap0, NULL, ao2_cleanup);
610         RAII_VAR(struct ast_format_cap *, cap1, NULL, ao2_cleanup);
611         RAII_VAR(struct rtp_glue_data *, glue0, NULL, rtp_glue_data_destroy);
612         RAII_VAR(struct rtp_glue_data *, glue1, NULL, rtp_glue_data_destroy);
613
614         ast_debug(1, "Bridge '%s'.  Checking compatability for channels '%s' and '%s'\n",
615                 bridge->uniqueid, ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
616
617         if (!native_rtp_bridge_capable(bc0->chan)) {
618                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
619                         bridge->uniqueid, ast_channel_name(bc0->chan));
620                 return 0;
621         }
622
623         if (!native_rtp_bridge_capable(bc1->chan)) {
624                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
625                         bridge->uniqueid, ast_channel_name(bc1->chan));
626                 return 0;
627         }
628
629         rtp_glue_data_init(&glue_a);
630         glue0 = &glue_a;
631         rtp_glue_data_init(&glue_b);
632         glue1 = &glue_b;
633         if (rtp_glue_data_get(bc0->chan, glue0, bc1->chan, glue1)) {
634                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as could not get details\n",
635                         bridge->uniqueid);
636                 return 0;
637         }
638         native_type = glue0->result;
639
640         if (native_type == AST_RTP_GLUE_RESULT_FORBID) {
641                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as it was forbidden while getting details\n",
642                         bridge->uniqueid);
643                 return 0;
644         }
645
646         if (ao2_container_count(bc0->features->dtmf_hooks)
647                 && ast_rtp_instance_dtmf_mode_get(glue0->audio.instance)) {
648                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
649                         bridge->uniqueid, ast_channel_name(bc0->chan));
650                 return 0;
651         }
652
653         if (ao2_container_count(bc1->features->dtmf_hooks)
654                 && ast_rtp_instance_dtmf_mode_get(glue1->audio.instance)) {
655                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
656                         bridge->uniqueid, ast_channel_name(bc1->chan));
657                 return 0;
658         }
659
660         if (native_type == AST_RTP_GLUE_RESULT_LOCAL
661                 && (ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge
662                         != ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge
663                         || (ast_rtp_instance_get_engine(glue0->audio.instance)->dtmf_compatible
664                                 && !ast_rtp_instance_get_engine(glue0->audio.instance)->dtmf_compatible(bc0->chan,
665                                         glue0->audio.instance, bc1->chan, glue1->audio.instance)))) {
666                 ast_debug(1, "Bridge '%s' can not use local native RTP bridge as local bridge or DTMF is not compatible\n",
667                         bridge->uniqueid);
668                 return 0;
669         }
670
671         cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
672         cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
673         if (!cap0 || !cap1) {
674                 return 0;
675         }
676
677         /* Make sure that codecs match */
678         if (glue0->cb->get_codec) {
679                 glue0->cb->get_codec(bc0->chan, cap0);
680         }
681         if (glue1->cb->get_codec) {
682                 glue1->cb->get_codec(bc1->chan, cap1);
683         }
684         if (ast_format_cap_count(cap0) != 0
685                 && ast_format_cap_count(cap1) != 0
686                 && !ast_format_cap_iscompatible(cap0, cap1)) {
687                 struct ast_str *codec_buf0 = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
688                 struct ast_str *codec_buf1 = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
689
690                 ast_debug(1, "Bridge '%s': Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
691                         bridge->uniqueid,
692                         ast_format_cap_get_names(cap0, &codec_buf0),
693                         ast_format_cap_get_names(cap1, &codec_buf1));
694                 return 0;
695         }
696
697         read_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawreadformat(bc0->chan));
698         read_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawreadformat(bc1->chan));
699         write_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawwriteformat(bc0->chan));
700         write_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawwriteformat(bc1->chan));
701
702         if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
703                 ast_debug(1, "Bridge '%s': Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
704                         bridge->uniqueid,
705                         read_ptime0, write_ptime1, read_ptime1, write_ptime0);
706                 return 0;
707         }
708
709         return 1;
710 }
711
712 /*!
713  * \internal
714  * \brief Called by the bridge core "compatible' callback
715  */
716 static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
717 {
718         struct ast_bridge_channel *bc0;
719         struct ast_bridge_channel *bc1;
720         int is_compatible;
721
722         /* We require two channels before even considering native bridging */
723         if (bridge->num_channels != 2) {
724                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as two channels are required\n",
725                         bridge->uniqueid);
726                 return 0;
727         }
728
729         bc0 = AST_LIST_FIRST(&bridge->channels);
730         bc1 = AST_LIST_LAST(&bridge->channels);
731
732         ast_channel_lock_both(bc0->chan, bc1->chan);
733         is_compatible = native_rtp_bridge_compatible_check(bridge, bc0, bc1);
734         ast_channel_unlock(bc0->chan);
735         ast_channel_unlock(bc1->chan);
736
737         return is_compatible;
738 }
739
740 /*!
741  * \internal
742  * \brief Helper function which adds frame hook to bridge channel
743  */
744 static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
745 {
746         struct native_rtp_bridge_channel_data *data = bridge_channel->tech_pvt;
747         static struct ast_framehook_interface hook = {
748                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
749                 .event_cb = native_rtp_framehook,
750                 .destroy_cb = __ao2_cleanup,
751                 .consume_cb = native_rtp_framehook_consume,
752                 .disable_inheritance = 1,
753         };
754
755         ast_assert(data->hook_data == NULL);
756         data->hook_data = ao2_alloc_options(sizeof(*data->hook_data), NULL,
757                 AO2_ALLOC_OPT_LOCK_NOLOCK);
758         if (!data->hook_data) {
759                 return -1;
760         }
761
762         ast_debug(2, "Bridge '%s'.  Attaching hook data %p to '%s'\n",
763                 bridge_channel->bridge->uniqueid, data, ast_channel_name(bridge_channel->chan));
764
765         ast_channel_lock(bridge_channel->chan);
766         /* We're giving 1 ref to the framehook and keeping the one from the alloc for ourselves */
767         hook.data = ao2_bump(data->hook_data);
768         data->hook_data->id = ast_framehook_attach(bridge_channel->chan, &hook);
769         ast_channel_unlock(bridge_channel->chan);
770         if (data->hook_data->id < 0) {
771                 /*
772                  * We need to drop both the reference we hold in data,
773                  * and the one the framehook would hold.
774                  */
775                 ao2_ref(data->hook_data, -2);
776                 data->hook_data = NULL;
777
778                 return -1;
779         }
780
781         return 0;
782 }
783
784 /*!
785  * \internal
786  * \brief Helper function which removes frame hook from bridge channel
787  */
788 static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
789 {
790         struct native_rtp_bridge_channel_data *data = bridge_channel->tech_pvt;
791
792         if (!data || !data->hook_data) {
793                 return;
794         }
795
796         ast_debug(2, "Bridge '%s'.  Detaching hook data %p from '%s'\n",
797                 bridge_channel->bridge->uniqueid, data->hook_data, ast_channel_name(bridge_channel->chan));
798
799         ast_channel_lock(bridge_channel->chan);
800         ast_framehook_detach(bridge_channel->chan, data->hook_data->id);
801         data->hook_data->detached = 1;
802         ast_channel_unlock(bridge_channel->chan);
803         ao2_cleanup(data->hook_data);
804         data->hook_data = NULL;
805 }
806
807 /*!
808  * \internal
809  * \brief Called by the bridge core 'join' callback for each channel joining he bridge
810  */
811 static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
812 {
813         ast_debug(2, "Bridge '%s'.  Channel '%s' is joining bridge tech\n",
814                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
815
816         ast_assert(bridge_channel->tech_pvt == NULL);
817
818         if (bridge_channel->suspended) {
819                 /* The channel will rejoin when it is unsuspended */
820                 return 0;
821         }
822
823         bridge_channel->tech_pvt = native_rtp_bridge_channel_data_alloc();
824         if (!bridge_channel->tech_pvt) {
825                 return -1;
826         }
827
828         if (native_rtp_bridge_framehook_attach(bridge_channel)) {
829                 native_rtp_bridge_channel_data_free(bridge_channel->tech_pvt);
830                 bridge_channel->tech_pvt = NULL;
831                 return -1;
832         }
833
834         native_rtp_bridge_start(bridge, NULL);
835         return 0;
836 }
837
838 /*!
839  * \internal
840  * \brief Add the channel back into the bridge
841  */
842 static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
843 {
844         ast_debug(2, "Bridge '%s'.  Channel '%s' is unsuspended back to bridge tech\n",
845                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
846         native_rtp_bridge_join(bridge, bridge_channel);
847 }
848
849 /*!
850  * \internal
851  * \brief Leave the bridge
852  */
853 static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
854 {
855         ast_debug(2, "Bridge '%s'.  Channel '%s' is leaving bridge tech\n",
856                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
857
858         if (!bridge_channel->tech_pvt) {
859                 return;
860         }
861
862         native_rtp_bridge_framehook_detach(bridge_channel);
863         native_rtp_bridge_stop(bridge, NULL);
864
865         native_rtp_bridge_channel_data_free(bridge_channel->tech_pvt);
866         bridge_channel->tech_pvt = NULL;
867 }
868
869 /*!
870  * \internal
871  * \brief Suspend the channel from the bridge
872  */
873 static void native_rtp_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
874 {
875         ast_debug(2, "Bridge '%s'.  Channel '%s' is suspending from bridge tech\n",
876                 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
877         native_rtp_bridge_leave(bridge, bridge_channel);
878 }
879
880 static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
881 {
882         const struct ast_control_t38_parameters *t38_parameters;
883         int defer = 0;
884
885         if (!ast_bridge_queue_everyone_else(bridge, bridge_channel, frame)) {
886                 /* This frame was successfully queued so no need to defer */
887                 return 0;
888         }
889
890         /* Depending on the frame defer it so when the next channel joins it receives it */
891         switch (frame->frametype) {
892         case AST_FRAME_CONTROL:
893                 switch (frame->subclass.integer) {
894                 case AST_CONTROL_T38_PARAMETERS:
895                         t38_parameters = frame->data.ptr;
896                         switch (t38_parameters->request_response) {
897                         case AST_T38_REQUEST_NEGOTIATE:
898                                 defer = -1;
899                                 break;
900                         default:
901                                 break;
902                         }
903                         break;
904                 default:
905                         break;
906                 }
907                 break;
908         default:
909                 break;
910         }
911
912         return defer;
913 }
914
915 static struct ast_bridge_technology native_rtp_bridge = {
916         .name = "native_rtp",
917         .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
918         .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
919         .join = native_rtp_bridge_join,
920         .unsuspend = native_rtp_bridge_unsuspend,
921         .leave = native_rtp_bridge_leave,
922         .suspend = native_rtp_bridge_suspend,
923         .write = native_rtp_bridge_write,
924         .compatible = native_rtp_bridge_compatible,
925 };
926
927 static int unload_module(void)
928 {
929         ast_bridge_technology_unregister(&native_rtp_bridge);
930         return 0;
931 }
932
933 static int load_module(void)
934 {
935         if (ast_bridge_technology_register(&native_rtp_bridge)) {
936                 unload_module();
937                 return AST_MODULE_LOAD_DECLINE;
938         }
939         return AST_MODULE_LOAD_SUCCESS;
940 }
941
942 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");