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