Fix assumption in bridge_native_rtp.c regarding number of participants in a bridge.
[asterisk/asterisk.git] / bridges / bridge_native_rtp.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Native RTP bridging technology module
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup bridges
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/bridge.h"
45 #include "asterisk/bridge_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/rtp_engine.h"
48
49 /*! \brief Internal structure which contains information about bridged RTP channels */
50 struct native_rtp_bridge_data {
51         /*! \brief Framehook used to intercept certain control frames */
52         int id;
53 };
54
55 /*! \brief Internal helper function which gets all RTP information (glue and instances) relating to the given channels */
56 static enum ast_rtp_glue_result native_rtp_bridge_get(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_glue **glue0,
57         struct ast_rtp_glue **glue1, struct ast_rtp_instance **instance0, struct ast_rtp_instance **instance1,
58         struct ast_rtp_instance **vinstance0, struct ast_rtp_instance **vinstance1)
59 {
60         enum ast_rtp_glue_result audio_glue0_res;
61         enum ast_rtp_glue_result video_glue0_res;
62         enum ast_rtp_glue_result audio_glue1_res;
63         enum ast_rtp_glue_result video_glue1_res;
64
65         if (!(*glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) ||
66                 !(*glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
67                 return AST_RTP_GLUE_RESULT_FORBID;
68         }
69
70         audio_glue0_res = (*glue0)->get_rtp_info(c0, instance0);
71         video_glue0_res = (*glue0)->get_vrtp_info ? (*glue0)->get_vrtp_info(c0, vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
72
73         audio_glue1_res = (*glue1)->get_rtp_info(c1, instance1);
74         video_glue1_res = (*glue1)->get_vrtp_info ? (*glue1)->get_vrtp_info(c1, vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
75
76         /* Apply any limitations on direct media bridging that may be present */
77         if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
78                 if ((*glue0)->allow_rtp_remote && !((*glue0)->allow_rtp_remote(c0, *instance1))) {
79                         /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
80                         audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
81                 } else if ((*glue1)->allow_rtp_remote && !((*glue1)->allow_rtp_remote(c1, *instance0))) {
82                         audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
83                 }
84         }
85         if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
86                 if ((*glue0)->allow_vrtp_remote && !((*glue0)->allow_vrtp_remote(c0, *instance1))) {
87                         /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
88                         video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
89                 } else if ((*glue1)->allow_vrtp_remote && !((*glue1)->allow_vrtp_remote(c1, *instance0))) {
90                         video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
91                 }
92         }
93
94         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
95         if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID
96                 && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE
97                         || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
98                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
99         }
100         if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID
101                 && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE
102                         || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
103                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
104         }
105
106         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
107         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID
108                 || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
109                 return AST_RTP_GLUE_RESULT_FORBID;
110         }
111
112         return audio_glue0_res;
113 }
114
115 static int native_rtp_bridge_start(struct ast_bridge *bridge, struct ast_channel *target)
116 {
117         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
118         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
119         enum ast_rtp_glue_result native_type;
120         struct ast_rtp_glue *glue0, *glue1;
121         RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
122         RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
123         RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
124         RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
125         RAII_VAR(struct ast_rtp_instance *, tinstance0, NULL, ao2_cleanup);
126         RAII_VAR(struct ast_rtp_instance *, tinstance1, NULL, ao2_cleanup);
127         RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
128         RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
129
130         if (c0 == c1) {
131                 return 0;
132         }
133
134         native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
135
136         if (glue0->get_codec) {
137                 glue0->get_codec(c0->chan, cap0);
138         }
139         if (glue1->get_codec) {
140                 glue1->get_codec(c1->chan, cap1);
141         }
142
143         switch (native_type) {
144         case AST_RTP_GLUE_RESULT_LOCAL:
145                 if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
146                         ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, instance1);
147                 }
148                 if (ast_rtp_instance_get_engine(instance1)->local_bridge) {
149                         ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, instance0);
150                 }
151                 ast_rtp_instance_set_bridged(instance0, instance1);
152                 ast_rtp_instance_set_bridged(instance1, instance0);
153                 ast_debug(2, "Locally RTP bridged '%s' and '%s' in stack\n",
154                         ast_channel_name(c0->chan), ast_channel_name(c1->chan));
155                 break;
156
157         case AST_RTP_GLUE_RESULT_REMOTE:
158
159                 /* If we have a target, it's the channel that received the UNHOLD or UPDATE_RTP_PEER frame and was told to resume */
160                 if (!target) {
161                         glue0->update_peer(c0->chan, instance1, vinstance1, tinstance1, cap1, 0);
162                         glue1->update_peer(c1->chan, instance0, vinstance0, tinstance0, cap0, 0);
163                         ast_debug(2, "Remotely bridged '%s' and '%s' - media will flow directly between them\n",
164                                 ast_channel_name(c0->chan), ast_channel_name(c1->chan));
165                 } else {
166                         /*
167                          * If a target was provided, it is the recipient of an unhold or an update and needs to have
168                          * its media redirected to fit the current remote bridging needs. The other channel is either
169                          * already set up to handle the new media path or will have its own set of updates independent
170                          * of this pass.
171                          */
172                         if (c0->chan == target) {
173                                 glue0->update_peer(c0->chan, instance1, vinstance1, tinstance1, cap1, 0);
174                         } else {
175                                 glue1->update_peer(c1->chan, instance0, vinstance0, tinstance0, cap0, 0);
176                         }
177                 }
178                 break;
179         case AST_RTP_GLUE_RESULT_FORBID:
180                 break;
181         }
182
183         return 0;
184 }
185
186 /*!
187  * \internal
188  * \brief Given a bridge channel, get its RTP instance
189  *
190  * The returned ast_rtp_instance has its refcount bumped.
191  *
192  * \param bridge_channel Take a guess
193  * \retval NULL No RTP instance on this bridge channel
194  * \retval non-NULL The RTP instance on this bridge channel
195  */
196 static struct ast_rtp_instance *bridge_channel_get_rtp_instance(struct ast_bridge_channel *bridge_channel)
197 {
198         struct ast_rtp_glue *glue;
199         struct ast_rtp_instance *instance;
200
201         glue = ast_rtp_instance_get_glue(ast_channel_tech(bridge_channel->chan)->type);
202         if (!glue) {
203                 return NULL;
204         }
205
206         glue->get_rtp_info(bridge_channel->chan, &instance);
207         return instance;
208 }
209
210 /*!
211  * \internal
212  * \brief Determine which two channels are bridged together
213  *
214  * Because of the nature of swapping, when the time comes for a channel to
215  * leave a native RTP bridge, it may be that there are more than two channels
216  * in the list of bridge channels. Therefore, it is important to correctly
217  * determine which two channels were bridged together.
218  *
219  * \param bridge The involved bridge
220  * \param leaving The bridge channel that is leaving the native RTP bridge
221  * \param[out] c0 The first bridged channel
222  * \param[out] c1 The second bridged channel
223  */
224 static void find_bridged_channels(struct ast_bridge *bridge, struct ast_bridge_channel *leaving,
225                 struct ast_bridge_channel **c0, struct ast_bridge_channel **c1)
226 {
227         RAII_VAR(struct ast_rtp_instance *, leaving_instance, bridge_channel_get_rtp_instance(leaving), ao2_cleanup);
228         struct ast_bridge_channel *iter;
229
230         if (!leaving_instance) {
231                 return;
232         }
233
234         AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
235                 RAII_VAR(struct ast_rtp_instance *, instance, NULL, ao2_cleanup);
236
237                 if (iter == leaving) {
238                         continue;
239                 }
240
241                 instance = bridge_channel_get_rtp_instance(iter);
242                 if (!instance) {
243                         continue;
244                 }
245
246                 if (instance == ast_rtp_instance_get_bridged(leaving_instance)) {
247                         break;
248                 }
249         }
250         *c0 = leaving;
251         *c1 = iter;
252         return;
253 }
254
255 /*!
256  * \internal
257  * \brief Stop native RTP bridging of two channels
258  *
259  * \param bridge The bridge that had native RTP bridging happening on it
260  * \param target If remote RTP bridging, the channel that is placed on hold.
261  * \param leaving If this is called because a channel is leaving, this is the
262  * bridge channel that is leaving the bridge
263  */
264 static void native_rtp_bridge_stop(struct ast_bridge *bridge, struct ast_channel *target,
265                 struct ast_bridge_channel *leaving)
266 {
267         struct ast_bridge_channel *c0 = NULL;
268         struct ast_bridge_channel *c1 = NULL;
269         enum ast_rtp_glue_result native_type;
270         struct ast_rtp_glue *glue0, *glue1 = NULL;
271         RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
272         RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
273         RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
274         RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
275
276         if (bridge->num_channels == 2) {
277                 c0 = AST_LIST_FIRST(&bridge->channels);
278                 c1 = AST_LIST_LAST(&bridge->channels);
279         } else if (bridge->num_channels > 2) {
280                 /* When a channel leaves a native RTP bridge, it is possible for
281                  * more channels to exist in the bridge than when the RTP bridge
282                  * was started. Thus we need to determine which two channels were
283                  * bridged based on the leaving channel
284                  */
285                 find_bridged_channels(bridge, leaving, &c0, &c1);
286         } else {
287                 return;
288         }
289
290         if (!c0 || !c1) {
291                 return;
292         }
293
294         native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
295
296         switch (native_type) {
297         case AST_RTP_GLUE_RESULT_LOCAL:
298                 if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
299                         ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, NULL);
300                 }
301                 if (instance1 && ast_rtp_instance_get_engine(instance1)->local_bridge) {
302                         ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, NULL);
303                 }
304                 ast_rtp_instance_set_bridged(instance0, NULL);
305                 if (instance1) {
306                         ast_rtp_instance_set_bridged(instance1, NULL);
307                 }
308                 break;
309         case AST_RTP_GLUE_RESULT_REMOTE:
310                 if (!target) {
311                         glue0->update_peer(c0->chan, NULL, NULL, NULL, NULL, 0);
312                         if (glue1) {
313                                 glue1->update_peer(c1->chan, NULL, NULL, NULL, NULL, 0);
314                         }
315                 } else {
316                         /*
317                          * If a target was provided, it is being put on hold and should expect to
318                          * receive mediafrom sterisk instead of what it was previously connected to.
319                          */
320                         if (c0->chan == target) {
321                                 glue0->update_peer(c0->chan, NULL, NULL, NULL, NULL, 0);
322                         } else if (glue1) {
323                                 glue1->update_peer(c1->chan, NULL, NULL, NULL, NULL, 0);
324                         }
325                 }
326                 break;
327         case AST_RTP_GLUE_RESULT_FORBID:
328                 break;
329         }
330
331         ast_debug(2, "Discontinued RTP bridging of '%s' and '%s' - media will flow through Asterisk core\n",
332                 ast_channel_name(c0->chan), ast_channel_name(c1->chan));
333 }
334
335 /*! \brief Frame hook that is called to intercept hold/unhold */
336 static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
337 {
338         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
339
340         if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
341                 return f;
342         }
343
344         ast_channel_lock(chan);
345         bridge = ast_channel_get_bridge(chan);
346         ast_channel_unlock(chan);
347
348         if (bridge) {
349                 if (f->subclass.integer == AST_CONTROL_HOLD) {
350                         native_rtp_bridge_stop(bridge, chan, NULL);
351                 } else if ((f->subclass.integer == AST_CONTROL_UNHOLD) || (f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
352                         native_rtp_bridge_start(bridge, chan);
353                 }
354         }
355
356         return f;
357 }
358
359 /*! \brief Internal helper function which checks whether the channels are compatible with our native bridging */
360 static int native_rtp_bridge_capable(struct ast_channel *chan)
361 {
362         return !ast_channel_has_audio_frame_or_monitor(chan);
363 }
364
365 static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
366 {
367         struct ast_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
368         struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
369         enum ast_rtp_glue_result native_type;
370         struct ast_rtp_glue *glue0, *glue1;
371         RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
372         RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
373         RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
374         RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
375         RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
376         RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
377         int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
378
379         /* We require two channels before even considering native bridging */
380         if (bridge->num_channels != 2) {
381                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as two channels are required\n",
382                         bridge->uniqueid);
383                 return 0;
384         }
385
386         if (!native_rtp_bridge_capable(c0->chan)) {
387                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
388                         bridge->uniqueid, ast_channel_name(c0->chan));
389                 return 0;
390         }
391
392         if (!native_rtp_bridge_capable(c1->chan)) {
393                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
394                         bridge->uniqueid, ast_channel_name(c1->chan));
395                 return 0;
396         }
397
398         if ((native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1))
399                 == AST_RTP_GLUE_RESULT_FORBID) {
400                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as it was forbidden while getting details\n",
401                         bridge->uniqueid);
402                 return 0;
403         }
404
405         if (ao2_container_count(c0->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance0)) {
406                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
407                         bridge->uniqueid, ast_channel_name(c0->chan));
408                 return 0;
409         }
410
411         if (ao2_container_count(c1->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance1)) {
412                 ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
413                         bridge->uniqueid, ast_channel_name(c1->chan));
414                 return 0;
415         }
416
417         if ((native_type == AST_RTP_GLUE_RESULT_LOCAL) && ((ast_rtp_instance_get_engine(instance0)->local_bridge !=
418                 ast_rtp_instance_get_engine(instance1)->local_bridge) ||
419                 (ast_rtp_instance_get_engine(instance0)->dtmf_compatible &&
420                         !ast_rtp_instance_get_engine(instance0)->dtmf_compatible(c0->chan, instance0, c1->chan, instance1)))) {
421                 ast_debug(1, "Bridge '%s' can not use local native RTP bridge as local bridge or DTMF is not compatible\n",
422                         bridge->uniqueid);
423                 return 0;
424         }
425
426         /* Make sure that codecs match */
427         if (glue0->get_codec) {
428                 glue0->get_codec(c0->chan, cap0);
429         }
430         if (glue1->get_codec) {
431                 glue1->get_codec(c1->chan, cap1);
432         }
433         if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
434                 char tmp0[256] = { 0, }, tmp1[256] = { 0, };
435
436                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
437                         ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
438                         ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
439                 return 0;
440         }
441
442         read_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawreadformat(c0->chan))).cur_ms;
443         read_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawreadformat(c1->chan))).cur_ms;
444         write_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawwriteformat(c0->chan))).cur_ms;
445         write_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawwriteformat(c1->chan))).cur_ms;
446
447         if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
448                 ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
449                                 read_ptime0, write_ptime1, read_ptime1, write_ptime0);
450                 return 0;
451         }
452
453         return 1;
454 }
455
456 /*! \brief Helper function which adds frame hook to bridge channel */
457 static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
458 {
459         struct native_rtp_bridge_data *data = ao2_alloc(sizeof(*data), NULL);
460         static struct ast_framehook_interface hook = {
461                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
462                 .event_cb = native_rtp_framehook,
463         };
464
465         if (!data) {
466                 return -1;
467         }
468
469         ast_channel_lock(bridge_channel->chan);
470         data->id = ast_framehook_attach(bridge_channel->chan, &hook);
471         ast_channel_unlock(bridge_channel->chan);
472         if (data->id < 0) {
473                 ao2_cleanup(data);
474                 return -1;
475         }
476
477         bridge_channel->tech_pvt = data;
478
479         return 0;
480 }
481
482 /*! \brief Helper function which removes frame hook from bridge channel */
483 static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
484 {
485         RAII_VAR(struct native_rtp_bridge_data *, data, bridge_channel->tech_pvt, ao2_cleanup);
486
487         if (!data) {
488                 return;
489         }
490
491         ast_channel_lock(bridge_channel->chan);
492         ast_framehook_detach(bridge_channel->chan, data->id);
493         ast_channel_unlock(bridge_channel->chan);
494         bridge_channel->tech_pvt = NULL;
495 }
496
497 static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
498 {
499         native_rtp_bridge_framehook_detach(bridge_channel);
500         if (native_rtp_bridge_framehook_attach(bridge_channel)) {
501                 return -1;
502         }
503
504         return native_rtp_bridge_start(bridge, NULL);
505 }
506
507 static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
508 {
509         native_rtp_bridge_join(bridge, bridge_channel);
510 }
511
512 static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
513 {
514         native_rtp_bridge_framehook_detach(bridge_channel);
515
516         native_rtp_bridge_stop(bridge, NULL, bridge_channel);
517 }
518
519 static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
520 {
521         return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
522 }
523
524 static struct ast_bridge_technology native_rtp_bridge = {
525         .name = "native_rtp",
526         .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
527         .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
528         .join = native_rtp_bridge_join,
529         .unsuspend = native_rtp_bridge_unsuspend,
530         .leave = native_rtp_bridge_leave,
531         .suspend = native_rtp_bridge_leave,
532         .write = native_rtp_bridge_write,
533         .compatible = native_rtp_bridge_compatible,
534 };
535
536 static int unload_module(void)
537 {
538         ast_format_cap_destroy(native_rtp_bridge.format_capabilities);
539         return ast_bridge_technology_unregister(&native_rtp_bridge);
540 }
541
542 static int load_module(void)
543 {
544         if (!(native_rtp_bridge.format_capabilities = ast_format_cap_alloc(0))) {
545                 return AST_MODULE_LOAD_DECLINE;
546         }
547         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
548         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
549         ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
550
551         return ast_bridge_technology_register(&native_rtp_bridge);
552 }
553
554 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");