res_pjsip_session: Ensure reused streams have correct bundle group
[asterisk/asterisk.git] / res / res_pjsip_session.c
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Mark Michelson <mmichelson@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 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <support_level>core</support_level>
23  ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28 #include <pjsip_ua.h>
29 #include <pjlib.h>
30 #include <pjmedia.h>
31
32 #include "asterisk/res_pjsip.h"
33 #include "asterisk/res_pjsip_session.h"
34 #include "asterisk/res_pjsip_session_caps.h"
35 #include "asterisk/callerid.h"
36 #include "asterisk/datastore.h"
37 #include "asterisk/module.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/res_pjsip.h"
40 #include "asterisk/astobj2.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/uuid.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/taskprocessor.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/sdp_srtp.h"
47 #include "asterisk/dsp.h"
48 #include "asterisk/acl.h"
49 #include "asterisk/features_config.h"
50 #include "asterisk/pickup.h"
51 #include "asterisk/test.h"
52 #include "asterisk/stream.h"
53 #include "asterisk/vector.h"
54
55 #define SDP_HANDLER_BUCKETS 11
56
57 #define MOD_DATA_ON_RESPONSE "on_response"
58 #define MOD_DATA_NAT_HOOK "nat_hook"
59
60 /* Most common case is one audio and one video stream */
61 #define DEFAULT_NUM_SESSION_MEDIA 2
62
63 /* Some forward declarations */
64 static void handle_session_begin(struct ast_sip_session *session);
65 static void handle_session_end(struct ast_sip_session *session);
66 static void handle_session_destroy(struct ast_sip_session *session);
67 static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata);
68 static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,
69                 enum ast_sip_session_response_priority response_priority);
70 static int handle_incoming(struct ast_sip_session *session, pjsip_rx_data *rdata,
71                 enum ast_sip_session_response_priority response_priority);
72 static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata);
73 static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata);
74 static int sip_session_refresh(struct ast_sip_session *session,
75                 ast_sip_session_request_creation_cb on_request_creation,
76                 ast_sip_session_sdp_creation_cb on_sdp_creation,
77                 ast_sip_session_response_cb on_response,
78                 enum ast_sip_session_refresh_method method, int generate_new_sdp,
79                 struct ast_sip_session_media_state *media_state,
80                 int queued);
81
82 /*! \brief NAT hook for modifying outgoing messages with SDP */
83 static struct ast_sip_nat_hook *nat_hook;
84
85 /*!
86  * \brief Registered SDP stream handlers
87  *
88  * This container is keyed on stream types. Each
89  * object in the container is a linked list of
90  * handlers for the stream type.
91  */
92 static struct ao2_container *sdp_handlers;
93
94 /*!
95  * These are the objects in the sdp_handlers container
96  */
97 struct sdp_handler_list {
98         /* The list of handlers to visit */
99         AST_LIST_HEAD_NOLOCK(, ast_sip_session_sdp_handler) list;
100         /* The handlers in this list handle streams of this type */
101         char stream_type[1];
102 };
103
104 static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv, struct ast_sip_session *session, const pjmedia_sdp_session *offer);
105
106 static int sdp_handler_list_hash(const void *obj, int flags)
107 {
108         const struct sdp_handler_list *handler_list = obj;
109         const char *stream_type = flags & OBJ_KEY ? obj : handler_list->stream_type;
110
111         return ast_str_hash(stream_type);
112 }
113
114 const char *ast_sip_session_get_name(const struct ast_sip_session *session)
115 {
116         if (!session) {
117                 return "(null session)";
118         }
119         if (session->channel) {
120                 return ast_channel_name(session->channel);
121         } else if (session->endpoint) {
122                 return ast_sorcery_object_get_id(session->endpoint);
123         } else {
124                 return "unknown";
125         }
126 }
127
128 static int sdp_handler_list_cmp(void *obj, void *arg, int flags)
129 {
130         struct sdp_handler_list *handler_list1 = obj;
131         struct sdp_handler_list *handler_list2 = arg;
132         const char *stream_type2 = flags & OBJ_KEY ? arg : handler_list2->stream_type;
133
134         return strcmp(handler_list1->stream_type, stream_type2) ? 0 : CMP_MATCH | CMP_STOP;
135 }
136
137 int ast_sip_session_register_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
138 {
139         RAII_VAR(struct sdp_handler_list *, handler_list,
140                         ao2_find(sdp_handlers, stream_type, OBJ_KEY), ao2_cleanup);
141         SCOPED_AO2LOCK(lock, sdp_handlers);
142
143         if (handler_list) {
144                 struct ast_sip_session_sdp_handler *iter;
145                 /* Check if this handler is already registered for this stream type */
146                 AST_LIST_TRAVERSE(&handler_list->list, iter, next) {
147                         if (!strcmp(iter->id, handler->id)) {
148                                 ast_log(LOG_WARNING, "Handler '%s' already registered for stream type '%s'.\n", handler->id, stream_type);
149                                 return -1;
150                         }
151                 }
152                 AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
153                 ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
154
155                 return 0;
156         }
157
158         /* No stream of this type has been registered yet, so we need to create a new list */
159         handler_list = ao2_alloc(sizeof(*handler_list) + strlen(stream_type), NULL);
160         if (!handler_list) {
161                 return -1;
162         }
163         /* Safe use of strcpy */
164         strcpy(handler_list->stream_type, stream_type);
165         AST_LIST_HEAD_INIT_NOLOCK(&handler_list->list);
166         AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
167         if (!ao2_link(sdp_handlers, handler_list)) {
168                 return -1;
169         }
170         ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
171
172         return 0;
173 }
174
175 static int remove_handler(void *obj, void *arg, void *data, int flags)
176 {
177         struct sdp_handler_list *handler_list = obj;
178         struct ast_sip_session_sdp_handler *handler = data;
179         struct ast_sip_session_sdp_handler *iter;
180         const char *stream_type = arg;
181
182         AST_LIST_TRAVERSE_SAFE_BEGIN(&handler_list->list, iter, next) {
183                 if (!strcmp(iter->id, handler->id)) {
184                         AST_LIST_REMOVE_CURRENT(next);
185                         ast_debug(1, "Unregistered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
186                 }
187         }
188         AST_LIST_TRAVERSE_SAFE_END;
189
190         if (AST_LIST_EMPTY(&handler_list->list)) {
191                 ast_debug(3, "No more handlers exist for stream type '%s'\n", stream_type);
192                 return CMP_MATCH;
193         } else {
194                 return CMP_STOP;
195         }
196 }
197
198 void ast_sip_session_unregister_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
199 {
200         ao2_callback_data(sdp_handlers, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, remove_handler, (void *)stream_type, handler);
201 }
202
203 static int media_stats_local_ssrc_cmp(
204                 const struct ast_rtp_instance_stats *vec_elem, const struct ast_rtp_instance_stats *srch)
205 {
206         if (vec_elem->local_ssrc == srch->local_ssrc) {
207                 return 1;
208         }
209
210         return 0;
211 }
212
213 static struct ast_sip_session_media_state *internal_sip_session_media_state_alloc(
214         size_t sessions, size_t read_callbacks)
215 {
216         struct ast_sip_session_media_state *media_state;
217
218         media_state = ast_calloc(1, sizeof(*media_state));
219         if (!media_state) {
220                 return NULL;
221         }
222
223         if (AST_VECTOR_INIT(&media_state->sessions, sessions) < 0) {
224                 ast_free(media_state);
225                 return NULL;
226         }
227
228         if (AST_VECTOR_INIT(&media_state->read_callbacks, read_callbacks) < 0) {
229                 AST_VECTOR_FREE(&media_state->sessions);
230                 ast_free(media_state);
231                 return NULL;
232         }
233
234         return media_state;
235 }
236
237 struct ast_sip_session_media_state *ast_sip_session_media_state_alloc(void)
238 {
239         return internal_sip_session_media_state_alloc(
240                 DEFAULT_NUM_SESSION_MEDIA, DEFAULT_NUM_SESSION_MEDIA);
241 }
242
243 void ast_sip_session_media_stats_save(struct ast_sip_session *sip_session, struct ast_sip_session_media_state *media_state)
244 {
245         int i;
246         int ret;
247
248         if (!media_state || !sip_session) {
249                 return;
250         }
251
252         for (i = 0; i < AST_VECTOR_SIZE(&media_state->sessions); i++) {
253                 struct ast_rtp_instance_stats *stats_tmp = NULL;
254                 struct ast_sip_session_media *media = AST_VECTOR_GET(&media_state->sessions, i);
255                 if (!media || !media->rtp) {
256                         continue;
257                 }
258
259                 stats_tmp = ast_calloc(1, sizeof(struct ast_rtp_instance_stats));
260                 if (!stats_tmp) {
261                         return;
262                 }
263
264                 ret = ast_rtp_instance_get_stats(media->rtp, stats_tmp, AST_RTP_INSTANCE_STAT_ALL);
265                 if (ret) {
266                         ast_free(stats_tmp);
267                         continue;
268                 }
269
270                 /* remove all the duplicated stats if exist */
271                 AST_VECTOR_REMOVE_CMP_UNORDERED(&sip_session->media_stats, stats_tmp, media_stats_local_ssrc_cmp, ast_free);
272
273                 AST_VECTOR_APPEND(&sip_session->media_stats, stats_tmp);
274         }
275 }
276
277 void ast_sip_session_media_state_reset(struct ast_sip_session_media_state *media_state)
278 {
279         int index;
280
281         if (!media_state) {
282                 return;
283         }
284
285         AST_VECTOR_RESET(&media_state->sessions, ao2_cleanup);
286         AST_VECTOR_RESET(&media_state->read_callbacks, AST_VECTOR_ELEM_CLEANUP_NOOP);
287
288         for (index = 0; index < AST_MEDIA_TYPE_END; ++index) {
289                 media_state->default_session[index] = NULL;
290         }
291
292         ast_stream_topology_free(media_state->topology);
293         media_state->topology = NULL;
294 }
295
296 struct ast_sip_session_media_state *ast_sip_session_media_state_clone(const struct ast_sip_session_media_state *media_state)
297 {
298         struct ast_sip_session_media_state *cloned;
299         int index;
300
301         if (!media_state) {
302                 return NULL;
303         }
304
305         cloned = internal_sip_session_media_state_alloc(
306                 AST_VECTOR_SIZE(&media_state->sessions),
307                 AST_VECTOR_SIZE(&media_state->read_callbacks));
308         if (!cloned) {
309                 return NULL;
310         }
311
312         if (media_state->topology) {
313                 cloned->topology = ast_stream_topology_clone(media_state->topology);
314                 if (!cloned->topology) {
315                         ast_sip_session_media_state_free(cloned);
316                         return NULL;
317                 }
318         }
319
320         for (index = 0; index < AST_VECTOR_SIZE(&media_state->sessions); ++index) {
321                 struct ast_sip_session_media *session_media = AST_VECTOR_GET(&media_state->sessions, index);
322                 enum ast_media_type type = ast_stream_get_type(ast_stream_topology_get_stream(cloned->topology, index));
323
324                 ao2_bump(session_media);
325                 if (AST_VECTOR_REPLACE(&cloned->sessions, index, session_media)) {
326                         ao2_cleanup(session_media);
327                 }
328                 if (ast_stream_get_state(ast_stream_topology_get_stream(cloned->topology, index)) != AST_STREAM_STATE_REMOVED &&
329                         !cloned->default_session[type]) {
330                         cloned->default_session[type] = session_media;
331                 }
332         }
333
334         for (index = 0; index < AST_VECTOR_SIZE(&media_state->read_callbacks); ++index) {
335                 struct ast_sip_session_media_read_callback_state *read_callback = AST_VECTOR_GET_ADDR(&media_state->read_callbacks, index);
336
337                 AST_VECTOR_REPLACE(&cloned->read_callbacks, index, *read_callback);
338         }
339
340         return cloned;
341 }
342
343 void ast_sip_session_media_state_free(struct ast_sip_session_media_state *media_state)
344 {
345         if (!media_state) {
346                 return;
347         }
348
349         /* This will reset the internal state so we only have to free persistent things */
350         ast_sip_session_media_state_reset(media_state);
351
352         AST_VECTOR_FREE(&media_state->sessions);
353         AST_VECTOR_FREE(&media_state->read_callbacks);
354
355         ast_free(media_state);
356 }
357
358 int ast_sip_session_is_pending_stream_default(const struct ast_sip_session *session, const struct ast_stream *stream)
359 {
360         int index;
361
362         if (!session->pending_media_state->topology) {
363                 ast_log(LOG_WARNING, "Pending topology was NULL for channel '%s'\n",
364                         session->channel ? ast_channel_name(session->channel) : "unknown");
365                 return 0;
366         }
367
368         if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
369                 return 0;
370         }
371
372         for (index = 0; index < ast_stream_topology_get_count(session->pending_media_state->topology); ++index) {
373                 if (ast_stream_get_type(ast_stream_topology_get_stream(session->pending_media_state->topology, index)) !=
374                         ast_stream_get_type(stream)) {
375                         continue;
376                 }
377
378                 return ast_stream_topology_get_stream(session->pending_media_state->topology, index) == stream ? 1 : 0;
379         }
380
381         return 0;
382 }
383
384 int ast_sip_session_media_add_read_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
385         int fd, ast_sip_session_media_read_cb callback)
386 {
387         struct ast_sip_session_media_read_callback_state callback_state = {
388                 .fd = fd,
389                 .read_callback = callback,
390                 .session = session_media,
391         };
392
393         /* The contents of the vector are whole structs and not pointers */
394         return AST_VECTOR_APPEND(&session->pending_media_state->read_callbacks, callback_state);
395 }
396
397 int ast_sip_session_media_set_write_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
398         ast_sip_session_media_write_cb callback)
399 {
400         if (session_media->write_callback) {
401                 if (session_media->write_callback == callback) {
402                         return 0;
403                 }
404
405                 return -1;
406         }
407
408         session_media->write_callback = callback;
409
410         return 0;
411 }
412
413 struct ast_sip_session_media *ast_sip_session_media_get_transport(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
414 {
415         int index;
416
417         if (!session->endpoint->media.bundle || ast_strlen_zero(session_media->mid)) {
418                 return session_media;
419         }
420
421         for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
422                 struct ast_sip_session_media *bundle_group_session_media;
423
424                 bundle_group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
425
426                 /* The first session which is in the bundle group is considered the authoritative session for transport */
427                 if (bundle_group_session_media->bundle_group == session_media->bundle_group) {
428                         return bundle_group_session_media;
429                 }
430         }
431
432         return session_media;
433 }
434
435 /*!
436  * \brief Set an SDP stream handler for a corresponding session media.
437  *
438  * \note Always use this function to set the SDP handler for a session media.
439  *
440  * This function will properly free resources on the SDP handler currently being
441  * used by the session media, then set the session media to use the new SDP
442  * handler.
443  */
444 static void session_media_set_handler(struct ast_sip_session_media *session_media,
445                 struct ast_sip_session_sdp_handler *handler)
446 {
447         ast_assert(session_media->handler != handler);
448
449         if (session_media->handler) {
450                 session_media->handler->stream_destroy(session_media);
451         }
452         session_media->handler = handler;
453 }
454
455 static int stream_destroy(void *obj, void *arg, int flags)
456 {
457         struct sdp_handler_list *handler_list = obj;
458         struct ast_sip_session_media *session_media = arg;
459         struct ast_sip_session_sdp_handler *handler;
460
461         AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
462                 handler->stream_destroy(session_media);
463         }
464
465         return 0;
466 }
467
468 static void session_media_dtor(void *obj)
469 {
470         struct ast_sip_session_media *session_media = obj;
471
472         /* It is possible for multiple handlers to have allocated memory on the
473          * session media (usually through a stream changing types). Therefore, we
474          * traverse all the SDP handlers and let them all call stream_destroy on
475          * the session_media
476          */
477         ao2_callback(sdp_handlers, 0, stream_destroy, session_media);
478
479         if (session_media->srtp) {
480                 ast_sdp_srtp_destroy(session_media->srtp);
481         }
482
483         ast_free(session_media->mid);
484         ast_free(session_media->remote_mslabel);
485 }
486
487 struct ast_sip_session_media *ast_sip_session_media_state_add(struct ast_sip_session *session,
488         struct ast_sip_session_media_state *media_state, enum ast_media_type type, int position)
489 {
490         struct ast_sip_session_media *session_media = NULL;
491
492         /* It is possible for this media state to already contain a session for the stream. If this
493          * is the case we simply return it.
494          */
495         if (position < AST_VECTOR_SIZE(&media_state->sessions)) {
496                 return AST_VECTOR_GET(&media_state->sessions, position);
497         }
498
499         /* Determine if we can reuse the session media from the active media state if present */
500         if (position < AST_VECTOR_SIZE(&session->active_media_state->sessions)) {
501                 session_media = AST_VECTOR_GET(&session->active_media_state->sessions, position);
502                 /* A stream can never exist without an accompanying media session */
503                 if (session_media->type == type) {
504                         ao2_ref(session_media, +1);
505                         /*
506                          * If this session_media was previously removed, its bundle group was probably reset
507                          * to -1 so if bundling is enabled on the endpoint, we need to reset it to 0, set
508                          * the bundled flag and reset its mid.
509                          */
510                         if (session->endpoint->media.bundle && session_media->bundle_group == -1) {
511                                 session_media->bundled = session->endpoint->media.webrtc;
512                                 session_media->bundle_group = 0;
513                                 ast_free(session_media->mid);
514                                 if (ast_asprintf(&session_media->mid, "%s-%d", ast_codec_media_type2str(type), position) < 0) {
515                                         ao2_ref(session_media, -1);
516                                         return NULL;
517                                 }
518                         }
519                 } else {
520                         session_media = NULL;
521                 }
522         }
523
524         if (!session_media) {
525                 /* No existing media session we can use so create a new one */
526                 session_media = ao2_alloc_options(sizeof(*session_media), session_media_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
527                 if (!session_media) {
528                         return NULL;
529                 }
530
531                 session_media->encryption = session->endpoint->media.rtp.encryption;
532                 session_media->remote_ice = session->endpoint->media.rtp.ice_support;
533                 session_media->remote_rtcp_mux = session->endpoint->media.rtcp_mux;
534                 session_media->keepalive_sched_id = -1;
535                 session_media->timeout_sched_id = -1;
536                 session_media->type = type;
537                 session_media->stream_num = position;
538
539                 if (session->endpoint->media.bundle) {
540                         /* This is a new stream so create a new mid based on media type and position, which makes it unique.
541                          * If this is the result of an offer the mid will just end up getting replaced.
542                          */
543                         if (ast_asprintf(&session_media->mid, "%s-%d", ast_codec_media_type2str(type), position) < 0) {
544                                 ao2_ref(session_media, -1);
545                                 return NULL;
546                         }
547                         session_media->bundle_group = 0;
548
549                         /* Some WebRTC clients can't handle an offer to bundle media streams. Instead they expect them to
550                          * already be bundled. Every client handles this scenario though so if WebRTC is enabled just go
551                          * ahead and treat the streams as having already been bundled.
552                          */
553                         session_media->bundled = session->endpoint->media.webrtc;
554                 } else {
555                         session_media->bundle_group = -1;
556                 }
557         }
558
559         if (AST_VECTOR_REPLACE(&media_state->sessions, position, session_media)) {
560                 ao2_ref(session_media, -1);
561
562                 return NULL;
563         }
564
565         /* If this stream will be active in some way and it is the first of this type then consider this the default media session to match */
566         if (!media_state->default_session[type] && ast_stream_get_state(ast_stream_topology_get_stream(media_state->topology, position)) != AST_STREAM_STATE_REMOVED) {
567                 media_state->default_session[type] = session_media;
568         }
569
570         return session_media;
571 }
572
573 static int is_stream_limitation_reached(enum ast_media_type type, const struct ast_sip_endpoint *endpoint, int *type_streams)
574 {
575         switch (type) {
576         case AST_MEDIA_TYPE_AUDIO:
577                 return !(type_streams[type] < endpoint->media.max_audio_streams);
578         case AST_MEDIA_TYPE_VIDEO:
579                 return !(type_streams[type] < endpoint->media.max_video_streams);
580         case AST_MEDIA_TYPE_IMAGE:
581                 /* We don't have an option for image (T.38) streams so cap it to one. */
582                 return (type_streams[type] > 0);
583         case AST_MEDIA_TYPE_UNKNOWN:
584         case AST_MEDIA_TYPE_TEXT:
585         default:
586                 /* We don't want any unknown or "other" streams on our endpoint,
587                  * so always just say we've reached the limit
588                  */
589                 return 1;
590         }
591 }
592
593 static int get_mid_bundle_group(const pjmedia_sdp_session *sdp, const char *mid)
594 {
595         int bundle_group = 0;
596         int index;
597
598         for (index = 0; index < sdp->attr_count; ++index) {
599                 pjmedia_sdp_attr *attr = sdp->attr[index];
600                 char value[pj_strlen(&attr->value) + 1], *mids = value, *attr_mid;
601
602                 if (pj_strcmp2(&attr->name, "group") || pj_strncmp2(&attr->value, "BUNDLE", 6)) {
603                         continue;
604                 }
605
606                 ast_copy_pj_str(value, &attr->value, sizeof(value));
607
608                 /* Skip the BUNDLE at the front */
609                 mids += 7;
610
611                 while ((attr_mid = strsep(&mids, " "))) {
612                         if (!strcmp(attr_mid, mid)) {
613                                 /* The ordering of attributes determines our internal identification of the bundle group based on number,
614                                  * with -1 being not in a bundle group. Since this is only exposed internally for response purposes it's
615                                  * actually even fine if things move around.
616                                  */
617                                 return bundle_group;
618                         }
619                 }
620
621                 bundle_group++;
622         }
623
624         return -1;
625 }
626
627 static int set_mid_and_bundle_group(struct ast_sip_session *session,
628         struct ast_sip_session_media *session_media,
629         const pjmedia_sdp_session *sdp,
630         const struct pjmedia_sdp_media *stream)
631 {
632         pjmedia_sdp_attr *attr;
633
634         if (!session->endpoint->media.bundle) {
635                 return 0;
636         }
637
638         /* By default on an incoming negotiation we assume no mid and bundle group is present */
639         ast_free(session_media->mid);
640         session_media->mid = NULL;
641         session_media->bundle_group = -1;
642         session_media->bundled = 0;
643
644         /* Grab the media identifier for the stream */
645         attr = pjmedia_sdp_media_find_attr2(stream, "mid", NULL);
646         if (!attr) {
647                 return 0;
648         }
649
650         session_media->mid = ast_calloc(1, attr->value.slen + 1);
651         if (!session_media->mid) {
652                 return 0;
653         }
654         ast_copy_pj_str(session_media->mid, &attr->value, attr->value.slen + 1);
655
656         /* Determine what bundle group this is part of */
657         session_media->bundle_group = get_mid_bundle_group(sdp, session_media->mid);
658
659         /* If this is actually part of a bundle group then the other side requested or accepted the bundle request */
660         session_media->bundled = session_media->bundle_group != -1;
661
662         return 0;
663 }
664
665 static void set_remote_mslabel_and_stream_group(struct ast_sip_session *session,
666         struct ast_sip_session_media *session_media,
667         const pjmedia_sdp_session *sdp,
668         const struct pjmedia_sdp_media *stream,
669         struct ast_stream *asterisk_stream)
670 {
671         int index;
672
673         ast_free(session_media->remote_mslabel);
674         session_media->remote_mslabel = NULL;
675
676         for (index = 0; index < stream->attr_count; ++index) {
677                 pjmedia_sdp_attr *attr = stream->attr[index];
678                 char attr_value[pj_strlen(&attr->value) + 1];
679                 char *ssrc_attribute_name, *ssrc_attribute_value = NULL;
680                 char *msid, *tmp = attr_value;
681                 static const pj_str_t STR_msid = { "msid", 4 };
682                 static const pj_str_t STR_ssrc = { "ssrc", 4 };
683
684                 if (!pj_strcmp(&attr->name, &STR_msid)) {
685                         ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
686                         msid = strsep(&tmp, " ");
687                         session_media->remote_mslabel = ast_strdup(msid);
688                         break;
689                 } else if (!pj_strcmp(&attr->name, &STR_ssrc)) {
690                         ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
691
692                         if ((ssrc_attribute_name = strchr(attr_value, ' '))) {
693                                 /* This has an actual attribute */
694                                 *ssrc_attribute_name++ = '\0';
695                                 ssrc_attribute_value = strchr(ssrc_attribute_name, ':');
696                                 if (ssrc_attribute_value) {
697                                         /* Values are actually optional according to the spec */
698                                         *ssrc_attribute_value++ = '\0';
699                                 }
700
701                                 if (!strcasecmp(ssrc_attribute_name, "mslabel") && !ast_strlen_zero(ssrc_attribute_value)) {
702                                         session_media->remote_mslabel = ast_strdup(ssrc_attribute_value);
703                                         break;
704                                 }
705                         }
706                 }
707         }
708
709         if (ast_strlen_zero(session_media->remote_mslabel)) {
710                 return;
711         }
712
713         /* Iterate through the existing streams looking for a match and if so then group this with it */
714         for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
715                 struct ast_sip_session_media *group_session_media;
716
717                 group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
718
719                 if (ast_strlen_zero(group_session_media->remote_mslabel) ||
720                         strcmp(group_session_media->remote_mslabel, session_media->remote_mslabel)) {
721                         continue;
722                 }
723
724                 ast_stream_set_group(asterisk_stream, index);
725                 break;
726         }
727 }
728
729 static void remove_stream_from_bundle(struct ast_sip_session_media *session_media,
730         struct ast_stream *stream)
731 {
732         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
733         ast_free(session_media->mid);
734         session_media->mid = NULL;
735         session_media->bundle_group = -1;
736         session_media->bundled = 0;
737 }
738
739 static int handle_incoming_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
740 {
741         int i;
742         int handled = 0;
743         int type_streams[AST_MEDIA_TYPE_END] = {0};
744         SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
745
746         if (session->inv_session && session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
747                 ast_log(LOG_ERROR, "Failed to handle incoming SDP. Session has been already disconnected\n");
748                 SCOPE_EXIT_RTN_VALUE(-1, "Already disconnected\n");
749         }
750
751         /* It is possible for SDP deferral to have already created a pending topology */
752         if (!session->pending_media_state->topology) {
753                 session->pending_media_state->topology = ast_stream_topology_alloc();
754                 if (!session->pending_media_state->topology) {
755                         SCOPE_EXIT_RTN_VALUE(-1, "No topology\n");
756                 }
757         }
758
759         for (i = 0; i < sdp->media_count; ++i) {
760                 /* See if there are registered handlers for this media stream type */
761                 char media[20];
762                 struct ast_sip_session_sdp_handler *handler;
763                 RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
764                 struct ast_sip_session_media *session_media = NULL;
765                 int res;
766                 enum ast_media_type type;
767                 struct ast_stream *stream = NULL;
768                 pjmedia_sdp_media *remote_stream = sdp->media[i];
769
770                 /* We need a null-terminated version of the media string */
771                 ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
772                 type = ast_media_type_from_str(media);
773
774                 /* See if we have an already existing stream, which can occur from SDP deferral checking */
775                 if (i < ast_stream_topology_get_count(session->pending_media_state->topology)) {
776                         stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
777                 }
778                 if (!stream) {
779                         struct ast_stream *existing_stream = NULL;
780
781                         if (session->active_media_state->topology &&
782                                 (i < ast_stream_topology_get_count(session->active_media_state->topology))) {
783                                 existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, i);
784                         }
785
786                         stream = ast_stream_alloc(existing_stream ? ast_stream_get_name(existing_stream) : ast_codec_media_type2str(type), type);
787                         if (!stream) {
788                                 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create stream\n");
789                         }
790                         if (ast_stream_topology_set_stream(session->pending_media_state->topology, i, stream)) {
791                                 ast_stream_free(stream);
792                                 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't set stream\n");
793                         }
794                         if (existing_stream) {
795                                 const char *stream_label = ast_stream_get_metadata(existing_stream, "SDP:LABEL");
796
797                                 if (!ast_strlen_zero(stream_label)) {
798                                         ast_stream_set_metadata(stream, "SDP:LABEL", stream_label);
799                                 }
800                         }
801
802                         /* For backwards compatibility with the core the default audio stream is always sendrecv */
803                         if (!ast_sip_session_is_pending_stream_default(session, stream) || strcmp(media, "audio")) {
804                                 if (pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL)) {
805                                         /* Stream state reflects our state of a stream, so in the case of
806                                          * sendonly and recvonly we store the opposite since that is what ours
807                                          * is.
808                                          */
809                                         ast_stream_set_state(stream, AST_STREAM_STATE_RECVONLY);
810                                 } else if (pjmedia_sdp_media_find_attr2(remote_stream, "recvonly", NULL)) {
811                                         ast_stream_set_state(stream, AST_STREAM_STATE_SENDONLY);
812                                 } else if (pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
813                                         ast_stream_set_state(stream, AST_STREAM_STATE_INACTIVE);
814                                 } else {
815                                         ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
816                                 }
817                         } else {
818                                 ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
819                         }
820                 }
821
822                 session_media = ast_sip_session_media_state_add(session, session->pending_media_state, ast_media_type_from_str(media), i);
823                 if (!session_media) {
824                         SCOPE_EXIT_RTN_VALUE(-1, "Couldn't add session media\n");
825                 }
826
827                 /* If this stream is already declined mark it as such, or mark it as such if we've reached the limit */
828                 if (!remote_stream->desc.port || is_stream_limitation_reached(type, session->endpoint, type_streams)) {
829                         ast_debug(1, "Declining incoming SDP media stream '%s' at position '%d'\n",
830                                 ast_codec_media_type2str(type), i);
831                         remove_stream_from_bundle(session_media, stream);
832                         continue;
833                 }
834
835                 set_mid_and_bundle_group(session, session_media, sdp, remote_stream);
836                 set_remote_mslabel_and_stream_group(session, session_media, sdp, remote_stream, stream);
837
838                 if (session_media->handler) {
839                         handler = session_media->handler;
840                         ast_debug(1, "Negotiating incoming SDP media stream '%s' using %s SDP handler\n",
841                                 ast_codec_media_type2str(session_media->type),
842                                 session_media->handler->id);
843                         res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp, i, stream);
844                         if (res < 0) {
845                                 /* Catastrophic failure. Abort! */
846                                 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't negotiate incoming sdp stream\n");
847                         } else if (res == 0) {
848                                 ast_debug(1, "Declining incoming SDP media stream '%s' at position '%d'\n",
849                                         ast_codec_media_type2str(type), i);
850                                 remove_stream_from_bundle(session_media, stream);
851                                 continue;
852                         } else if (res > 0) {
853                                 ast_debug(1, "Media stream '%s' handled by %s\n",
854                                         ast_codec_media_type2str(session_media->type),
855                                         session_media->handler->id);
856                                 /* Handled by this handler. Move to the next stream */
857                                 handled = 1;
858                                 ++type_streams[type];
859                                 continue;
860                         }
861                 }
862
863                 handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
864                 if (!handler_list) {
865                         ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
866                         continue;
867                 }
868                 AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
869                         if (handler == session_media->handler) {
870                                 continue;
871                         }
872                         ast_debug(1, "Negotiating incoming SDP media stream '%s' using %s SDP handler\n",
873                                 ast_codec_media_type2str(session_media->type),
874                                 handler->id);
875                         res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp, i, stream);
876                         if (res < 0) {
877                                 /* Catastrophic failure. Abort! */
878                                 return -1;
879                         } else if (res == 0) {
880                                 ast_debug(1, "Declining incoming SDP media stream '%s' at position '%d'\n",
881                                         ast_codec_media_type2str(type), i);
882                                 remove_stream_from_bundle(session_media, stream);
883                                 continue;
884                         } else if (res > 0) {
885                                 ast_debug(1, "Media stream '%s' handled by %s\n",
886                                         ast_codec_media_type2str(session_media->type),
887                                         handler->id);
888                                 /* Handled by this handler. Move to the next stream */
889                                 session_media_set_handler(session_media, handler);
890                                 handled = 1;
891                                 ++type_streams[type];
892                                 break;
893                         }
894                 }
895         }
896         if (!handled) {
897                 SCOPE_EXIT_RTN_VALUE(-1, "Not handled\n");
898         }
899         SCOPE_EXIT_RTN_VALUE(0, "Handled.  Active: %s  Pending: %s\n",
900                 ast_str_tmp(256, ast_stream_topology_to_str(session->active_media_state->topology, &STR_TMP)),
901                 ast_str_tmp(256, ast_stream_topology_to_str(session->pending_media_state->topology, &STR_TMP))
902                 );
903 }
904
905 static int handle_negotiated_sdp_session_media(struct ast_sip_session_media *session_media,
906                 struct ast_sip_session *session, const pjmedia_sdp_session *local,
907                 const pjmedia_sdp_session *remote, int index, struct ast_stream *asterisk_stream)
908 {
909         /* See if there are registered handlers for this media stream type */
910         struct pjmedia_sdp_media *local_stream = local->media[index];
911         char media[20];
912         struct ast_sip_session_sdp_handler *handler;
913         RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
914         int res;
915         SCOPE_ENTER(1, "%s\n", session ? ast_sip_session_get_name(session) : "unknown");
916
917         /* We need a null-terminated version of the media string */
918         ast_copy_pj_str(media, &local->media[index]->desc.media, sizeof(media));
919
920         /* For backwards compatibility we only reflect the stream state correctly on
921          * the non-default streams and any non-audio streams. This is because the stream
922          * state of the default audio stream is also used for signaling that someone has
923          * placed us on hold. This situation is not handled currently and can result in
924          * the remote side being sorted of placed on hold too.
925          */
926         if (!ast_sip_session_is_pending_stream_default(session, asterisk_stream) || strcmp(media, "audio")) {
927                 /* Determine the state of the stream based on our local SDP */
928                 if (pjmedia_sdp_media_find_attr2(local_stream, "sendonly", NULL)) {
929                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDONLY);
930                 } else if (pjmedia_sdp_media_find_attr2(local_stream, "recvonly", NULL)) {
931                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_RECVONLY);
932                 } else if (pjmedia_sdp_media_find_attr2(local_stream, "inactive", NULL)) {
933                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_INACTIVE);
934                 } else {
935                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDRECV);
936                 }
937         } else {
938                 ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDRECV);
939         }
940
941         set_mid_and_bundle_group(session, session_media, remote, remote->media[index]);
942         set_remote_mslabel_and_stream_group(session, session_media, remote, remote->media[index], asterisk_stream);
943
944         handler = session_media->handler;
945         if (handler) {
946                 ast_debug(1, "Applying negotiated SDP media stream '%s' using %s SDP handler\n",
947                         ast_codec_media_type2str(session_media->type),
948                         handler->id);
949                 res = handler->apply_negotiated_sdp_stream(session, session_media, local, remote, index, asterisk_stream);
950                 if (res >= 0) {
951                         ast_debug(1, "Applied negotiated SDP media stream '%s' using %s SDP handler\n",
952                                 ast_codec_media_type2str(session_media->type),
953                                 handler->id);
954                         SCOPE_EXIT_RTN_VALUE(0,  "%s: Applied negotiated SDP media stream '%s' using %s SDP handler\n",
955                                 ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
956                                 handler->id);
957                 }
958                 SCOPE_EXIT_RTN_VALUE(-1,  "%s: Failed to apply negotiated SDP media stream '%s' using %s SDP handler\n",
959                         ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
960                         handler->id);
961         }
962
963         handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
964         if (!handler_list) {
965                 ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
966                 SCOPE_EXIT_RTN_VALUE(-1, "%s: No registered SDP handlers for media type '%s'\n",
967                         ast_sip_session_get_name(session), media);
968         }
969         AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
970                 if (handler == session_media->handler) {
971                         continue;
972                 }
973                 ast_debug(1, "Applying negotiated SDP media stream '%s' using %s SDP handler\n",
974                         ast_codec_media_type2str(session_media->type),
975                         handler->id);
976                 res = handler->apply_negotiated_sdp_stream(session, session_media, local, remote, index, asterisk_stream);
977                 if (res < 0) {
978                         /* Catastrophic failure. Abort! */
979                         SCOPE_EXIT_RTN_VALUE(-1, "%s: Handler '%s' returned %d\n",
980                                 ast_sip_session_get_name(session), handler->id, res);
981                 }
982                 if (res > 0) {
983                         ast_debug(1, "Applied negotiated SDP media stream '%s' using %s SDP handler\n",
984                                 ast_codec_media_type2str(session_media->type),
985                                 handler->id);
986                         /* Handled by this handler. Move to the next stream */
987                         session_media_set_handler(session_media, handler);
988                         SCOPE_EXIT_RTN_VALUE(0, "%s: Handler '%s' handled this sdp stream\n",
989                                 ast_sip_session_get_name(session), handler->id);
990                 }
991         }
992
993         res = 0;
994         if (session_media->handler && session_media->handler->stream_stop) {
995                 res = 1;
996                 ast_debug(1, "Stopping SDP media stream '%s' as it is not currently negotiated\n",
997                         ast_codec_media_type2str(session_media->type));
998                 session_media->handler->stream_stop(session_media);
999         }
1000
1001         SCOPE_EXIT_RTN_VALUE(0, "%s: Media type '%s' %s\n",
1002                 ast_sip_session_get_name(session), ast_codec_media_type2str(session_media->type),
1003                 res ? "not negotiated.  Stopped" : "handled");
1004 }
1005
1006 static int handle_negotiated_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *local, const pjmedia_sdp_session *remote)
1007 {
1008         int i;
1009         struct ast_stream_topology *topology;
1010         unsigned int changed = 0; /* 0 = unchanged, 1 = new source, 2 = new topology */
1011         SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
1012
1013         if (!session->pending_media_state->topology) {
1014                 if (session->active_media_state->topology) {
1015                         /*
1016                          * This happens when we have negotiated media after receiving a 183,
1017                          * and we're now receiving a 200 with a new SDP.  In this case, there
1018                          * is active_media_state, but the pending_media_state has been reset.
1019                          */
1020                         struct ast_sip_session_media_state *active_media_state_clone;
1021
1022                         active_media_state_clone =
1023                                 ast_sip_session_media_state_clone(session->active_media_state);
1024                         if (!active_media_state_clone) {
1025                                 ast_log(LOG_WARNING, "Unable to clone active media state for channel '%s'\n",
1026                                         session->channel ? ast_channel_name(session->channel) : "unknown");
1027                                 SCOPE_EXIT_RTN_VALUE(-1, "Unable to clone active media state\n");
1028                         }
1029
1030                         ast_sip_session_media_state_free(session->pending_media_state);
1031                         session->pending_media_state = active_media_state_clone;
1032                 } else {
1033                         ast_log(LOG_WARNING, "No pending or active media state for channel '%s'\n",
1034                                 session->channel ? ast_channel_name(session->channel) : "unknown");
1035                         SCOPE_EXIT_RTN_VALUE(-1, "No media state\n");
1036                 }
1037         }
1038
1039         /* If we're handling negotiated streams, then we should already have set
1040          * up session media instances (and Asterisk streams) that correspond to
1041          * the local SDP, and there should be the same number of session medias
1042          * and streams as there are local SDP streams
1043          */
1044         if (ast_stream_topology_get_count(session->pending_media_state->topology) != local->media_count
1045                 || AST_VECTOR_SIZE(&session->pending_media_state->sessions) != local->media_count) {
1046                 ast_log(LOG_WARNING, "Local SDP for channel '%s' contains %d media streams while we expected it to contain %u\n",
1047                         session->channel ? ast_channel_name(session->channel) : "unknown",
1048                         ast_stream_topology_get_count(session->pending_media_state->topology), local->media_count);
1049                 SCOPE_EXIT_RTN_VALUE(-1, "Media stream count mismatch\n");
1050         }
1051
1052         for (i = 0; i < local->media_count; ++i) {
1053                 struct ast_sip_session_media *session_media;
1054                 struct ast_stream *stream;
1055
1056                 if (!remote->media[i]) {
1057                         continue;
1058                 }
1059
1060                 session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, i);
1061                 stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
1062
1063                 /* Make sure that this stream is in the correct state. If we need to change
1064                  * the state to REMOVED, then our work here is done, so go ahead and move on
1065                  * to the next stream.
1066                  */
1067                 if (!remote->media[i]->desc.port) {
1068                         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
1069                         continue;
1070                 }
1071
1072                 /* If the stream state is REMOVED, nothing needs to be done, so move on to the
1073                  * next stream. This can occur if an internal thing has requested it to be
1074                  * removed, or if we remove it as a result of the stream limit being reached.
1075                  */
1076                 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
1077                         /*
1078                          * Defer removing the handler until we are ready to activate
1079                          * the new topology.  The channel's thread may still be using
1080                          * the stream and we could crash before we are ready.
1081                          */
1082                         continue;
1083                 }
1084
1085                 if (handle_negotiated_sdp_session_media(session_media, session, local, remote, i, stream)) {
1086                         SCOPE_EXIT_RTN_VALUE(-1, "Unable to handle negotiated session media\n");
1087                 }
1088
1089                 changed |= session_media->changed;
1090                 session_media->changed = 0;
1091         }
1092
1093         /* Apply the pending media state to the channel and make it active */
1094         ast_channel_lock(session->channel);
1095
1096         /* Now update the stream handler for any declined/removed streams */
1097         for (i = 0; i < local->media_count; ++i) {
1098                 struct ast_sip_session_media *session_media;
1099                 struct ast_stream *stream;
1100
1101                 if (!remote->media[i]) {
1102                         continue;
1103                 }
1104
1105                 session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, i);
1106                 stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
1107
1108                 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED
1109                         && session_media->handler) {
1110                         /*
1111                          * This stream is no longer being used and the channel's thread
1112                          * is held off because we have the channel lock so release any
1113                          * resources the handler may have on it.
1114                          */
1115                         session_media_set_handler(session_media, NULL);
1116                 }
1117         }
1118
1119         /* Update the topology on the channel to match the accepted one */
1120         topology = ast_stream_topology_clone(session->pending_media_state->topology);
1121         if (topology) {
1122                 ast_channel_set_stream_topology(session->channel, topology);
1123                 /* If this is a remotely done renegotiation that has changed the stream topology notify what is
1124                  * currently handling this channel. Note that fax uses its own process, so if we are transitioning
1125                  * between audio and fax or vice versa we don't notify.
1126                  */
1127                 if (pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE &&
1128                         session->active_media_state && session->active_media_state->topology &&
1129                         !ast_stream_topology_equal(session->active_media_state->topology, topology) &&
1130                         !session->active_media_state->default_session[AST_MEDIA_TYPE_IMAGE] &&
1131                         !session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE]) {
1132                         changed = 2;
1133                 }
1134         }
1135
1136         /* Remove all current file descriptors from the channel */
1137         for (i = 0; i < AST_VECTOR_SIZE(&session->active_media_state->read_callbacks); ++i) {
1138                 ast_channel_internal_fd_clear(session->channel, i + AST_EXTENDED_FDS);
1139         }
1140
1141         /* Add all the file descriptors from the pending media state */
1142         for (i = 0; i < AST_VECTOR_SIZE(&session->pending_media_state->read_callbacks); ++i) {
1143                 struct ast_sip_session_media_read_callback_state *callback_state;
1144
1145                 callback_state = AST_VECTOR_GET_ADDR(&session->pending_media_state->read_callbacks, i);
1146                 ast_channel_internal_fd_set(session->channel, i + AST_EXTENDED_FDS, callback_state->fd);
1147         }
1148
1149         /* Active and pending flip flop as needed */
1150         ast_sip_session_media_stats_save(session, session->active_media_state);
1151         SWAP(session->active_media_state, session->pending_media_state);
1152         ast_sip_session_media_state_reset(session->pending_media_state);
1153
1154         ast_channel_unlock(session->channel);
1155
1156         if (changed == 1) {
1157                 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED };
1158
1159                 ast_queue_frame(session->channel, &f);
1160         } else if (changed == 2) {
1161                 ast_channel_stream_topology_changed_externally(session->channel);
1162         } else {
1163                 ast_queue_frame(session->channel, &ast_null_frame);
1164         }
1165
1166         SCOPE_EXIT_RTN_VALUE(0);
1167 }
1168
1169 #define DATASTORE_BUCKETS 53
1170 #define MEDIA_BUCKETS 7
1171
1172 static void session_datastore_destroy(void *obj)
1173 {
1174         struct ast_datastore *datastore = obj;
1175
1176         /* Using the destroy function (if present) destroy the data */
1177         if (datastore->info->destroy != NULL && datastore->data != NULL) {
1178                 datastore->info->destroy(datastore->data);
1179                 datastore->data = NULL;
1180         }
1181
1182         ast_free((void *) datastore->uid);
1183         datastore->uid = NULL;
1184 }
1185
1186 struct ast_datastore *ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
1187 {
1188         RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
1189         char uuid_buf[AST_UUID_STR_LEN];
1190         const char *uid_ptr = uid;
1191
1192         if (!info) {
1193                 return NULL;
1194         }
1195
1196         datastore = ao2_alloc(sizeof(*datastore), session_datastore_destroy);
1197         if (!datastore) {
1198                 return NULL;
1199         }
1200
1201         datastore->info = info;
1202         if (ast_strlen_zero(uid)) {
1203                 /* They didn't provide an ID so we'll provide one ourself */
1204                 uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
1205         }
1206
1207         datastore->uid = ast_strdup(uid_ptr);
1208         if (!datastore->uid) {
1209                 return NULL;
1210         }
1211
1212         ao2_ref(datastore, +1);
1213         return datastore;
1214 }
1215
1216 int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore)
1217 {
1218         ast_assert(datastore != NULL);
1219         ast_assert(datastore->info != NULL);
1220         ast_assert(ast_strlen_zero(datastore->uid) == 0);
1221
1222         if (!ao2_link(session->datastores, datastore)) {
1223                 return -1;
1224         }
1225         return 0;
1226 }
1227
1228 struct ast_datastore *ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
1229 {
1230         return ao2_find(session->datastores, name, OBJ_KEY);
1231 }
1232
1233 void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
1234 {
1235         ao2_callback(session->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
1236 }
1237
1238 enum delayed_method {
1239         DELAYED_METHOD_INVITE,
1240         DELAYED_METHOD_UPDATE,
1241         DELAYED_METHOD_BYE,
1242 };
1243
1244 /*!
1245  * \internal
1246  * \brief Convert delayed method enum value to a string.
1247  * \since 13.3.0
1248  *
1249  * \param method Delayed method enum value to convert to a string.
1250  *
1251  * \return String value of delayed method.
1252  */
1253 static const char *delayed_method2str(enum delayed_method method)
1254 {
1255         const char *str = "<unknown>";
1256
1257         switch (method) {
1258         case DELAYED_METHOD_INVITE:
1259                 str = "INVITE";
1260                 break;
1261         case DELAYED_METHOD_UPDATE:
1262                 str = "UPDATE";
1263                 break;
1264         case DELAYED_METHOD_BYE:
1265                 str = "BYE";
1266                 break;
1267         }
1268
1269         return str;
1270 }
1271
1272 /*!
1273  * \brief Structure used for sending delayed requests
1274  *
1275  * Requests are typically delayed because the current transaction
1276  * state of an INVITE. Once the pending INVITE transaction terminates,
1277  * the delayed request will be sent
1278  */
1279 struct ast_sip_session_delayed_request {
1280         /*! Method of the request */
1281         enum delayed_method method;
1282         /*! Callback to call when the delayed request is created. */
1283         ast_sip_session_request_creation_cb on_request_creation;
1284         /*! Callback to call when the delayed request SDP is created */
1285         ast_sip_session_sdp_creation_cb on_sdp_creation;
1286         /*! Callback to call when the delayed request receives a response */
1287         ast_sip_session_response_cb on_response;
1288         /*! Whether to generate new SDP */
1289         int generate_new_sdp;
1290         /*! Requested media state for the SDP */
1291         struct ast_sip_session_media_state *media_state;
1292         AST_LIST_ENTRY(ast_sip_session_delayed_request) next;
1293 };
1294
1295 static struct ast_sip_session_delayed_request *delayed_request_alloc(
1296         enum delayed_method method,
1297         ast_sip_session_request_creation_cb on_request_creation,
1298         ast_sip_session_sdp_creation_cb on_sdp_creation,
1299         ast_sip_session_response_cb on_response,
1300         int generate_new_sdp,
1301         struct ast_sip_session_media_state *media_state)
1302 {
1303         struct ast_sip_session_delayed_request *delay = ast_calloc(1, sizeof(*delay));
1304
1305         if (!delay) {
1306                 return NULL;
1307         }
1308         delay->method = method;
1309         delay->on_request_creation = on_request_creation;
1310         delay->on_sdp_creation = on_sdp_creation;
1311         delay->on_response = on_response;
1312         delay->generate_new_sdp = generate_new_sdp;
1313         delay->media_state = media_state;
1314         return delay;
1315 }
1316
1317 static void delayed_request_free(struct ast_sip_session_delayed_request *delay)
1318 {
1319         ast_sip_session_media_state_free(delay->media_state);
1320         ast_free(delay);
1321 }
1322
1323 /*!
1324  * \internal
1325  * \brief Send a delayed request
1326  *
1327  * \retval -1 failure
1328  * \retval 0 success
1329  * \retval 1 refresh request not sent as no change would occur
1330  */
1331 static int send_delayed_request(struct ast_sip_session *session, struct ast_sip_session_delayed_request *delay)
1332 {
1333         int res;
1334
1335         ast_debug(3, "Endpoint '%s(%s)' sending delayed %s request.\n",
1336                 ast_sorcery_object_get_id(session->endpoint),
1337                 session->channel ? ast_channel_name(session->channel) : "",
1338                 delayed_method2str(delay->method));
1339
1340         switch (delay->method) {
1341         case DELAYED_METHOD_INVITE:
1342                 res = sip_session_refresh(session, delay->on_request_creation,
1343                         delay->on_sdp_creation, delay->on_response,
1344                         AST_SIP_SESSION_REFRESH_METHOD_INVITE, delay->generate_new_sdp, delay->media_state, 1);
1345                 /* Ownership of media state transitions to ast_sip_session_refresh */
1346                 delay->media_state = NULL;
1347                 return res;
1348         case DELAYED_METHOD_UPDATE:
1349                 res = sip_session_refresh(session, delay->on_request_creation,
1350                         delay->on_sdp_creation, delay->on_response,
1351                         AST_SIP_SESSION_REFRESH_METHOD_UPDATE, delay->generate_new_sdp, delay->media_state, 1);
1352                 /* Ownership of media state transitions to ast_sip_session_refresh */
1353                 delay->media_state = NULL;
1354                 return res;
1355         case DELAYED_METHOD_BYE:
1356                 ast_sip_session_terminate(session, 0);
1357                 return 0;
1358         }
1359         ast_log(LOG_WARNING, "Don't know how to send delayed %s(%d) request.\n",
1360                 delayed_method2str(delay->method), delay->method);
1361         return -1;
1362 }
1363
1364 /*!
1365  * \internal
1366  * \brief The current INVITE transaction is in the PROCEEDING state.
1367  * \since 13.3.0
1368  *
1369  * \param vsession Session object.
1370  *
1371  * \retval 0 on success.
1372  * \retval -1 on error.
1373  */
1374 static int invite_proceeding(void *vsession)
1375 {
1376         struct ast_sip_session *session = vsession;
1377         struct ast_sip_session_delayed_request *delay;
1378         int found = 0;
1379         int res = 0;
1380
1381         AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
1382                 switch (delay->method) {
1383                 case DELAYED_METHOD_INVITE:
1384                         break;
1385                 case DELAYED_METHOD_UPDATE:
1386                         AST_LIST_REMOVE_CURRENT(next);
1387                         res = send_delayed_request(session, delay);
1388                         delayed_request_free(delay);
1389                         if (!res) {
1390                                 found = 1;
1391                         }
1392                         break;
1393                 case DELAYED_METHOD_BYE:
1394                         /* A BYE is pending so don't bother anymore. */
1395                         found = 1;
1396                         break;
1397                 }
1398                 if (found) {
1399                         break;
1400                 }
1401         }
1402         AST_LIST_TRAVERSE_SAFE_END;
1403
1404         ao2_ref(session, -1);
1405         return res;
1406 }
1407
1408 /*!
1409  * \internal
1410  * \brief The current INVITE transaction is in the TERMINATED state.
1411  * \since 13.3.0
1412  *
1413  * \param vsession Session object.
1414  *
1415  * \retval 0 on success.
1416  * \retval -1 on error.
1417  */
1418 static int invite_terminated(void *vsession)
1419 {
1420         struct ast_sip_session *session = vsession;
1421         struct ast_sip_session_delayed_request *delay;
1422         int found = 0;
1423         int res = 0;
1424         int timer_running;
1425
1426         /* re-INVITE collision timer running? */
1427         timer_running = pj_timer_entry_running(&session->rescheduled_reinvite);
1428
1429         AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
1430                 switch (delay->method) {
1431                 case DELAYED_METHOD_INVITE:
1432                         if (!timer_running) {
1433                                 found = 1;
1434                         }
1435                         break;
1436                 case DELAYED_METHOD_UPDATE:
1437                 case DELAYED_METHOD_BYE:
1438                         found = 1;
1439                         break;
1440                 }
1441                 if (found) {
1442                         AST_LIST_REMOVE_CURRENT(next);
1443                         res = send_delayed_request(session, delay);
1444                         delayed_request_free(delay);
1445                         if (!res) {
1446                                 break;
1447                         }
1448                 }
1449         }
1450         AST_LIST_TRAVERSE_SAFE_END;
1451
1452         ao2_ref(session, -1);
1453         return res;
1454 }
1455
1456 /*!
1457  * \internal
1458  * \brief INVITE collision timeout.
1459  * \since 13.3.0
1460  *
1461  * \param vsession Session object.
1462  *
1463  * \retval 0 on success.
1464  * \retval -1 on error.
1465  */
1466 static int invite_collision_timeout(void *vsession)
1467 {
1468         struct ast_sip_session *session = vsession;
1469         int res;
1470
1471         if (session->inv_session->invite_tsx) {
1472                 /*
1473                  * INVITE transaction still active.  Let it send
1474                  * the collision re-INVITE when it terminates.
1475                  */
1476                 ao2_ref(session, -1);
1477                 res = 0;
1478         } else {
1479                 res = invite_terminated(session);
1480         }
1481
1482         return res;
1483 }
1484
1485 /*!
1486  * \internal
1487  * \brief The current UPDATE transaction is in the COMPLETED state.
1488  * \since 13.3.0
1489  *
1490  * \param vsession Session object.
1491  *
1492  * \retval 0 on success.
1493  * \retval -1 on error.
1494  */
1495 static int update_completed(void *vsession)
1496 {
1497         struct ast_sip_session *session = vsession;
1498         int res;
1499
1500         if (session->inv_session->invite_tsx) {
1501                 res = invite_proceeding(session);
1502         } else {
1503                 res = invite_terminated(session);
1504         }
1505
1506         return res;
1507 }
1508
1509 static void check_delayed_requests(struct ast_sip_session *session,
1510         int (*cb)(void *vsession))
1511 {
1512         ao2_ref(session, +1);
1513         if (ast_sip_push_task(session->serializer, cb, session)) {
1514                 ao2_ref(session, -1);
1515         }
1516 }
1517
1518 static int delay_request(struct ast_sip_session *session,
1519         ast_sip_session_request_creation_cb on_request,
1520         ast_sip_session_sdp_creation_cb on_sdp_creation,
1521         ast_sip_session_response_cb on_response,
1522         int generate_new_sdp,
1523         enum delayed_method method,
1524         struct ast_sip_session_media_state *media_state)
1525 {
1526         struct ast_sip_session_delayed_request *delay = delayed_request_alloc(method,
1527                         on_request, on_sdp_creation, on_response, generate_new_sdp, media_state);
1528
1529         if (!delay) {
1530                 ast_sip_session_media_state_free(media_state);
1531                 return -1;
1532         }
1533
1534         if (method == DELAYED_METHOD_BYE) {
1535                 /* Send BYE as early as possible */
1536                 AST_LIST_INSERT_HEAD(&session->delayed_requests, delay, next);
1537         } else {
1538                 AST_LIST_INSERT_TAIL(&session->delayed_requests, delay, next);
1539         }
1540         return 0;
1541 }
1542
1543 static pjmedia_sdp_session *generate_session_refresh_sdp(struct ast_sip_session *session)
1544 {
1545         pjsip_inv_session *inv_session = session->inv_session;
1546         const pjmedia_sdp_session *previous_sdp = NULL;
1547         SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
1548
1549         if (inv_session->neg) {
1550                 if (pjmedia_sdp_neg_was_answer_remote(inv_session->neg)) {
1551                         pjmedia_sdp_neg_get_active_remote(inv_session->neg, &previous_sdp);
1552                 } else {
1553                         pjmedia_sdp_neg_get_active_local(inv_session->neg, &previous_sdp);
1554                 }
1555         }
1556         SCOPE_EXIT_RTN_VALUE(create_local_sdp(inv_session, session, previous_sdp));
1557 }
1558
1559 static void set_from_header(struct ast_sip_session *session)
1560 {
1561         struct ast_party_id effective_id;
1562         struct ast_party_id connected_id;
1563         pj_pool_t *dlg_pool;
1564         pjsip_fromto_hdr *dlg_info;
1565         pjsip_contact_hdr *dlg_contact;
1566         pjsip_name_addr *dlg_info_name_addr;
1567         pjsip_sip_uri *dlg_info_uri;
1568         pjsip_sip_uri *dlg_contact_uri;
1569         int restricted;
1570         const char *pjsip_from_domain;
1571
1572         if (!session->channel || session->saved_from_hdr) {
1573                 return;
1574         }
1575
1576         /* We need to save off connected_id for RPID/PAI generation */
1577         ast_party_id_init(&connected_id);
1578         ast_channel_lock(session->channel);
1579         effective_id = ast_channel_connected_effective_id(session->channel);
1580         ast_party_id_copy(&connected_id, &effective_id);
1581         ast_channel_unlock(session->channel);
1582
1583         restricted =
1584                 ((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED);
1585
1586         /* Now set up dlg->local.info so pjsip can correctly generate From */
1587
1588         dlg_pool = session->inv_session->dlg->pool;
1589         dlg_info = session->inv_session->dlg->local.info;
1590         dlg_contact = session->inv_session->dlg->local.contact;
1591         dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
1592         dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
1593         dlg_contact_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(dlg_contact->uri);
1594
1595         if (session->endpoint->id.trust_outbound || !restricted) {
1596                 ast_sip_modify_id_header(dlg_pool, dlg_info, &connected_id);
1597                 if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
1598                         pj_strdup2(dlg_pool, &dlg_contact_uri->user, S_COR(connected_id.number.valid, connected_id.number.str, ""));
1599                 }
1600         }
1601
1602         ast_party_id_free(&connected_id);
1603
1604         if (!ast_strlen_zero(session->endpoint->fromuser)) {
1605                 dlg_info_name_addr->display.ptr = NULL;
1606                 dlg_info_name_addr->display.slen = 0;
1607                 pj_strdup2(dlg_pool, &dlg_info_uri->user, session->endpoint->fromuser);
1608         }
1609
1610         if (!ast_strlen_zero(session->endpoint->fromdomain)) {
1611                 pj_strdup2(dlg_pool, &dlg_info_uri->host, session->endpoint->fromdomain);
1612         }
1613
1614         /*
1615          * Channel variable for compatibility with chan_sip SIPFROMDOMAIN
1616          */
1617         ast_channel_lock(session->channel);
1618         pjsip_from_domain = pbx_builtin_getvar_helper(session->channel, "SIPFROMDOMAIN");
1619         if (!ast_strlen_zero(pjsip_from_domain)) {
1620                 ast_debug(3, "From header domain reset by channel variable SIPFROMDOMAIN (%s)\n", pjsip_from_domain);
1621                 pj_strdup2(dlg_pool, &dlg_info_uri->host, pjsip_from_domain);
1622         }
1623         ast_channel_unlock(session->channel);
1624
1625         /* We need to save off the non-anonymized From for RPID/PAI generation (for domain) */
1626         session->saved_from_hdr = pjsip_hdr_clone(dlg_pool, dlg_info);
1627         ast_sip_add_usereqphone(session->endpoint, dlg_pool, session->saved_from_hdr->uri);
1628
1629         /* In chan_sip, fromuser and fromdomain trump restricted so we only
1630          * anonymize if they're not set.
1631          */
1632         if (restricted) {
1633                 /* fromuser doesn't provide a display name so we always set it */
1634                 pj_strdup2(dlg_pool, &dlg_info_name_addr->display, "Anonymous");
1635
1636                 if (ast_strlen_zero(session->endpoint->fromuser)) {
1637                         pj_strdup2(dlg_pool, &dlg_info_uri->user, "anonymous");
1638                 }
1639
1640                 if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
1641                         pj_strdup2(dlg_pool, &dlg_contact_uri->user, "anonymous");
1642                 }
1643
1644                 if (ast_strlen_zero(session->endpoint->fromdomain)) {
1645                         pj_strdup2(dlg_pool, &dlg_info_uri->host, "anonymous.invalid");
1646                 }
1647         } else {
1648                 ast_sip_add_usereqphone(session->endpoint, dlg_pool, dlg_info->uri);
1649     }
1650 }
1651
1652 static int sip_session_refresh(struct ast_sip_session *session,
1653                 ast_sip_session_request_creation_cb on_request_creation,
1654                 ast_sip_session_sdp_creation_cb on_sdp_creation,
1655                 ast_sip_session_response_cb on_response,
1656                 enum ast_sip_session_refresh_method method, int generate_new_sdp,
1657                 struct ast_sip_session_media_state *media_state,
1658                 int queued)
1659 {
1660         pjsip_inv_session *inv_session = session->inv_session;
1661         pjmedia_sdp_session *new_sdp = NULL;
1662         pjsip_tx_data *tdata;
1663
1664         if (media_state && (!media_state->topology || !generate_new_sdp)) {
1665                 ast_sip_session_media_state_free(media_state);
1666                 return -1;
1667         }
1668
1669         if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1670                 /* Don't try to do anything with a hung-up call */
1671                 ast_debug(3, "Not sending reinvite to %s because of disconnected state...\n",
1672                                 ast_sorcery_object_get_id(session->endpoint));
1673                 ast_sip_session_media_state_free(media_state);
1674                 return 0;
1675         }
1676
1677         /* If the dialog has not yet been established we have to defer until it has */
1678         if (inv_session->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED) {
1679                 ast_debug(3, "Delay sending request to %s because dialog has not been established...\n",
1680                         ast_sorcery_object_get_id(session->endpoint));
1681                 return delay_request(session, on_request_creation, on_sdp_creation, on_response,
1682                         generate_new_sdp,
1683                         method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
1684                                 ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE,
1685                         media_state);
1686         }
1687
1688         if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
1689                 if (inv_session->invite_tsx) {
1690                         /* We can't send a reinvite yet, so delay it */
1691                         ast_debug(3, "Delay sending reinvite to %s because of outstanding transaction...\n",
1692                                         ast_sorcery_object_get_id(session->endpoint));
1693                         return delay_request(session, on_request_creation, on_sdp_creation,
1694                                 on_response, generate_new_sdp, DELAYED_METHOD_INVITE, media_state);
1695                 } else if (inv_session->state != PJSIP_INV_STATE_CONFIRMED) {
1696                         /* Initial INVITE transaction failed to progress us to a confirmed state
1697                          * which means re-invites are not possible
1698                          */
1699                         ast_debug(3, "Not sending reinvite to %s because not in confirmed state...\n",
1700                                         ast_sorcery_object_get_id(session->endpoint));
1701                         ast_sip_session_media_state_free(media_state);
1702                         return 0;
1703                 }
1704         }
1705
1706         if (generate_new_sdp) {
1707                 /* SDP can only be generated if current negotiation has already completed */
1708                 if (inv_session->neg
1709                         && pjmedia_sdp_neg_get_state(inv_session->neg)
1710                                 != PJMEDIA_SDP_NEG_STATE_DONE) {
1711                         ast_debug(3, "Delay session refresh with new SDP to %s because SDP negotiation is not yet done...\n",
1712                                 ast_sorcery_object_get_id(session->endpoint));
1713                         return delay_request(session, on_request_creation, on_sdp_creation,
1714                                 on_response, generate_new_sdp,
1715                                 method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
1716                                         ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE, media_state);
1717                 }
1718
1719                 /* If an explicitly requested media state has been provided use it instead of any pending one */
1720                 if (media_state) {
1721                         int index;
1722                         int type_streams[AST_MEDIA_TYPE_END] = {0};
1723                         struct ast_stream *stream;
1724
1725                         /* Media state conveys a desired media state, so if there are outstanding
1726                          * delayed requests we need to ensure we go into the queue and not jump
1727                          * ahead. If we sent this media state now then updates could go out of
1728                          * order.
1729                          */
1730                         if (!queued && !AST_LIST_EMPTY(&session->delayed_requests)) {
1731                                 ast_debug(3, "Delay sending reinvite to %s because of outstanding requests...\n",
1732                                         ast_sorcery_object_get_id(session->endpoint));
1733                                 return delay_request(session, on_request_creation, on_sdp_creation,
1734                                         on_response, generate_new_sdp,
1735                                         method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
1736                                                 ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE, media_state);
1737                         }
1738
1739                         /* Prune the media state so the number of streams fit within the configured limits - we do it here
1740                          * so that the index of the resulting streams in the SDP match. If we simply left the streams out
1741                          * of the SDP when producing it we'd be in trouble. We also enforce formats here for media types that
1742                          * are configurable on the endpoint.
1743                          */
1744                         for (index = 0; index < ast_stream_topology_get_count(media_state->topology); ++index) {
1745                                 struct ast_stream *existing_stream = NULL;
1746
1747                                 stream = ast_stream_topology_get_stream(media_state->topology, index);
1748
1749                                 if (session->active_media_state->topology &&
1750                                         index < ast_stream_topology_get_count(session->active_media_state->topology)) {
1751                                         existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, index);
1752                                 }
1753
1754                                 if (is_stream_limitation_reached(ast_stream_get_type(stream), session->endpoint, type_streams)) {
1755                                         if (index < AST_VECTOR_SIZE(&media_state->sessions)) {
1756                                                 struct ast_sip_session_media *session_media = AST_VECTOR_GET(&media_state->sessions, index);
1757
1758                                                 ao2_cleanup(session_media);
1759                                                 AST_VECTOR_REMOVE(&media_state->sessions, index, 1);
1760                                         }
1761
1762                                         ast_stream_topology_del_stream(media_state->topology, index);
1763
1764                                         /* A stream has potentially moved into our spot so we need to jump back so we process it */
1765                                         index -= 1;
1766                                         continue;
1767                                 }
1768
1769                                 /* No need to do anything with stream if it's media state is removed */
1770                                 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
1771                                         /* If there is no existing stream we can just not have this stream in the topology at all. */
1772                                         if (!existing_stream) {
1773                                                 ast_stream_topology_del_stream(media_state->topology, index);
1774                                                 index -= 1;
1775                                         }
1776                                         continue;
1777                                 }
1778
1779                                 /* Enforce the configured allowed codecs on audio and video streams */
1780                                 if ((ast_stream_get_type(stream) == AST_MEDIA_TYPE_AUDIO || ast_stream_get_type(stream) == AST_MEDIA_TYPE_VIDEO) &&
1781                                         !ast_stream_get_metadata(stream, "pjsip_session_refresh")) {
1782                                         struct ast_format_cap *joint_cap;
1783
1784                                         joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1785                                         if (!joint_cap) {
1786                                                 ast_sip_session_media_state_free(media_state);
1787                                                 return 0;
1788                                         }
1789                                         ast_format_cap_get_compatible(ast_stream_get_formats(stream), session->endpoint->media.codecs, joint_cap);
1790                                         if (!ast_format_cap_count(joint_cap)) {
1791                                                 ao2_ref(joint_cap, -1);
1792
1793                                                 if (!existing_stream) {
1794                                                         /* If there is no existing stream we can just not have this stream in the topology
1795                                                          * at all.
1796                                                          */
1797                                                         ast_stream_topology_del_stream(media_state->topology, index);
1798                                                         index -= 1;
1799                                                         continue;
1800                                                 } else if (ast_stream_get_state(stream) != ast_stream_get_state(existing_stream) ||
1801                                                                 strcmp(ast_stream_get_name(stream), ast_stream_get_name(existing_stream))) {
1802                                                         /* If the underlying stream is a different type or different name then we have to
1803                                                          * mark it as removed, as it is replacing an existing stream. We do this so order
1804                                                          * is preserved.
1805                                                          */
1806                                                         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
1807                                                         continue;
1808                                                 } else {
1809                                                         /* However if the stream is otherwise remaining the same we can keep the formats
1810                                                          * that exist on it already which allows media to continue to flow. We don't modify
1811                                                          * the format capabilities but do need to cast it so that ao2_bump can raise the
1812                                                          * reference count.
1813                                                          */
1814                                                         joint_cap = ao2_bump((struct ast_format_cap *)ast_stream_get_formats(existing_stream));
1815                                                 }
1816                                         }
1817                                         ast_stream_set_formats(stream, joint_cap);
1818                                         ao2_cleanup(joint_cap);
1819                                 }
1820
1821                                 ++type_streams[ast_stream_get_type(stream)];
1822                         }
1823
1824                         if (session->active_media_state->topology) {
1825                                 /* SDP is a fun thing. Take for example the fact that streams are never removed. They just become
1826                                  * declined. To better handle this in the case where something requests a topology change for fewer
1827                                  * streams than are currently present we fill in the topology to match the current number of streams
1828                                  * that are active.
1829                                  */
1830                                 for (index = ast_stream_topology_get_count(media_state->topology);
1831                                         index < ast_stream_topology_get_count(session->active_media_state->topology); ++index) {
1832                                         struct ast_stream *cloned;
1833
1834                                         stream = ast_stream_topology_get_stream(session->active_media_state->topology, index);
1835                                         ast_assert(stream != NULL);
1836
1837                                         cloned = ast_stream_clone(stream, NULL);
1838                                         if (!cloned) {
1839                                                 ast_sip_session_media_state_free(media_state);
1840                                                 return -1;
1841                                         }
1842
1843                                         ast_stream_set_state(cloned, AST_STREAM_STATE_REMOVED);
1844                                         if (ast_stream_topology_append_stream(media_state->topology, cloned) < 0) {
1845                                                 ast_stream_free(cloned);
1846                                                 ast_sip_session_media_state_free(media_state);
1847                                                 return -1;
1848                                         }
1849                                 }
1850
1851                                 /* If the resulting media state matches the existing active state don't bother doing a session refresh */
1852                                 if (ast_stream_topology_equal(session->active_media_state->topology, media_state->topology)) {
1853                                         ast_sip_session_media_state_free(media_state);
1854                                         /* For external consumers we return 0 to say success, but internally for
1855                                          * send_delayed_request we return a separate value to indicate that this
1856                                          * session refresh would be redundant so we didn't send it
1857                                          */
1858                                         return queued ? 1 : 0;
1859                                 }
1860                         }
1861
1862                         ast_sip_session_media_state_free(session->pending_media_state);
1863                         session->pending_media_state = media_state;
1864                 }
1865
1866                 new_sdp = generate_session_refresh_sdp(session);
1867                 if (!new_sdp) {
1868                         ast_log(LOG_ERROR, "Failed to generate session refresh SDP. Not sending session refresh\n");
1869                         ast_sip_session_media_state_reset(session->pending_media_state);
1870                         return -1;
1871                 }
1872                 if (on_sdp_creation) {
1873                         if (on_sdp_creation(session, new_sdp)) {
1874                                 ast_sip_session_media_state_reset(session->pending_media_state);
1875                                 return -1;
1876                         }
1877                 }
1878         }
1879
1880         if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
1881                 if (pjsip_inv_reinvite(inv_session, NULL, new_sdp, &tdata)) {
1882                         ast_log(LOG_WARNING, "Failed to create reinvite properly.\n");
1883                         if (generate_new_sdp) {
1884                                 ast_sip_session_media_state_reset(session->pending_media_state);
1885                         }
1886                         return -1;
1887                 }
1888         } else if (pjsip_inv_update(inv_session, NULL, new_sdp, &tdata)) {
1889                 ast_log(LOG_WARNING, "Failed to create UPDATE properly.\n");
1890                 if (generate_new_sdp) {
1891                         ast_sip_session_media_state_reset(session->pending_media_state);
1892                 }
1893                 return -1;
1894         }
1895         if (on_request_creation) {
1896                 if (on_request_creation(session, tdata)) {
1897                         if (generate_new_sdp) {
1898                                 ast_sip_session_media_state_reset(session->pending_media_state);
1899                         }
1900                         return -1;
1901                 }
1902         }
1903         ast_debug(3, "Sending session refresh SDP via %s to %s\n",
1904                 method == AST_SIP_SESSION_REFRESH_METHOD_INVITE ? "re-INVITE" : "UPDATE",
1905                 ast_sorcery_object_get_id(session->endpoint));
1906         ast_sip_session_send_request_with_cb(session, tdata, on_response);
1907         return 0;
1908 }
1909
1910 int ast_sip_session_refresh(struct ast_sip_session *session,
1911                 ast_sip_session_request_creation_cb on_request_creation,
1912                 ast_sip_session_sdp_creation_cb on_sdp_creation,
1913                 ast_sip_session_response_cb on_response,
1914                 enum ast_sip_session_refresh_method method, int generate_new_sdp,
1915                 struct ast_sip_session_media_state *media_state)
1916 {
1917         return sip_session_refresh(session, on_request_creation, on_sdp_creation,
1918                 on_response, method, generate_new_sdp, media_state, 0);
1919 }
1920
1921 int ast_sip_session_regenerate_answer(struct ast_sip_session *session,
1922                 ast_sip_session_sdp_creation_cb on_sdp_creation)
1923 {
1924         pjsip_inv_session *inv_session = session->inv_session;
1925         pjmedia_sdp_session *new_answer = NULL;
1926         const pjmedia_sdp_session *previous_offer = NULL;
1927         SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
1928
1929         /* The SDP answer can only be regenerated if it is still pending to be sent */
1930         if (!inv_session->neg || (pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER &&
1931                 pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)) {
1932                 ast_log(LOG_WARNING, "Requested to regenerate local SDP answer for channel '%s' but negotiation in state '%s'\n",
1933                         ast_channel_name(session->channel), pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv_session->neg)));
1934                 SCOPE_EXIT_RTN_VALUE(-1, "Bad negotiation state\n");
1935         }
1936
1937         pjmedia_sdp_neg_get_neg_remote(inv_session->neg, &previous_offer);
1938         if (pjmedia_sdp_neg_get_state(inv_session->neg) == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) {
1939                 /* Transition the SDP negotiator back to when it received the remote offer */
1940                 pjmedia_sdp_neg_negotiate(inv_session->pool, inv_session->neg, 0);
1941                 pjmedia_sdp_neg_set_remote_offer(inv_session->pool, inv_session->neg, previous_offer);
1942         }
1943
1944         new_answer = create_local_sdp(inv_session, session, previous_offer);
1945         if (!new_answer) {
1946                 ast_log(LOG_WARNING, "Could not create a new local SDP answer for channel '%s'\n",
1947                         ast_channel_name(session->channel));
1948                 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create new SDP\n");
1949         }
1950
1951         if (on_sdp_creation) {
1952                 if (on_sdp_creation(session, new_answer)) {
1953                         SCOPE_EXIT_RTN_VALUE(-1, "Callback failed\n");
1954                 }
1955         }
1956
1957         pjsip_inv_set_sdp_answer(inv_session, new_answer);
1958
1959         SCOPE_EXIT_RTN_VALUE(0);
1960 }
1961
1962 void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
1963 {
1964         handle_outgoing_response(session, tdata);
1965         pjsip_inv_send_msg(session->inv_session, tdata);
1966         return;
1967 }
1968
1969 static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata);
1970 static pj_bool_t session_on_rx_response(pjsip_rx_data *rdata);
1971 static void session_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e);
1972
1973 static pjsip_module session_module = {
1974         .name = {"Session Module", 14},
1975         .priority = PJSIP_MOD_PRIORITY_APPLICATION,
1976         .on_rx_request = session_on_rx_request,
1977         .on_rx_response = session_on_rx_response,
1978         .on_tsx_state = session_on_tsx_state,
1979 };
1980
1981 /*! \brief Determine whether the SDP provided requires deferral of negotiating or not
1982  *
1983  * \retval 1 re-invite should be deferred and resumed later
1984  * \retval 0 re-invite should not be deferred
1985  */
1986 static int sdp_requires_deferral(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
1987 {
1988         int i;
1989
1990         if (!session->pending_media_state->topology) {
1991                 session->pending_media_state->topology = ast_stream_topology_alloc();
1992                 if (!session->pending_media_state->topology) {
1993                         return -1;
1994                 }
1995         }
1996
1997         for (i = 0; i < sdp->media_count; ++i) {
1998                 /* See if there are registered handlers for this media stream type */
1999                 char media[20];
2000                 struct ast_sip_session_sdp_handler *handler;
2001                 RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
2002                 struct ast_stream *existing_stream = NULL;
2003                 struct ast_stream *stream;
2004                 enum ast_media_type type;
2005                 struct ast_sip_session_media *session_media = NULL;
2006                 enum ast_sip_session_sdp_stream_defer res;
2007                 pjmedia_sdp_media *remote_stream = sdp->media[i];
2008
2009                 /* We need a null-terminated version of the media string */
2010                 ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
2011
2012                 if (session->active_media_state->topology &&
2013                         (i < ast_stream_topology_get_count(session->active_media_state->topology))) {
2014                         existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, i);
2015                 }
2016
2017                 type = ast_media_type_from_str(media);
2018                 stream = ast_stream_alloc(existing_stream ? ast_stream_get_name(existing_stream) : ast_codec_media_type2str(type), type);
2019                 if (!stream) {
2020                         return -1;
2021                 }
2022
2023                 /* As this is only called on an incoming SDP offer before processing it is not possible
2024                  * for streams and their media sessions to exist.
2025                  */
2026                 if (ast_stream_topology_set_stream(session->pending_media_state->topology, i, stream)) {
2027                         ast_stream_free(stream);
2028                         return -1;
2029                 }
2030
2031                 if (existing_stream) {
2032                         const char *stream_label = ast_stream_get_metadata(existing_stream, "SDP:LABEL");
2033
2034                         if (!ast_strlen_zero(stream_label)) {
2035                                 ast_stream_set_metadata(stream, "SDP:LABEL", stream_label);
2036                         }
2037                 }
2038
2039                 session_media = ast_sip_session_media_state_add(session, session->pending_media_state, ast_media_type_from_str(media), i);
2040                 if (!session_media) {
2041                         return -1;
2042                 }
2043
2044                 /* For backwards compatibility with the core the default audio stream is always sendrecv */
2045                 if (!ast_sip_session_is_pending_stream_default(session, stream) || strcmp(media, "audio")) {
2046                         if (pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL)) {
2047                                 /* Stream state reflects our state of a stream, so in the case of
2048                                  * sendonly and recvonly we store the opposite since that is what ours
2049                                  * is.
2050                                  */
2051                                 ast_stream_set_state(stream, AST_STREAM_STATE_RECVONLY);
2052                         } else if (pjmedia_sdp_media_find_attr2(remote_stream, "recvonly", NULL)) {
2053                                 ast_stream_set_state(stream, AST_STREAM_STATE_SENDONLY);
2054                         } else if (pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
2055                                 ast_stream_set_state(stream, AST_STREAM_STATE_INACTIVE);
2056                         } else {
2057                                 ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
2058                         }
2059                 } else {
2060                         ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
2061                 }
2062
2063                 if (session_media->handler) {
2064                         handler = session_media->handler;
2065                         if (handler->defer_incoming_sdp_stream) {
2066                                 res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
2067                                         sdp->media[i]);
2068                                 switch (res) {
2069                                 case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
2070                                         break;
2071                                 case AST_SIP_SESSION_SDP_DEFER_ERROR:
2072                                         return 0;
2073                                 case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
2074                                         break;
2075                                 case AST_SIP_SESSION_SDP_DEFER_NEEDED:
2076                                         return 1;
2077                                 }
2078                         }
2079                         /* Handled by this handler. Move to the next stream */
2080                         continue;
2081                 }
2082
2083                 handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
2084                 if (!handler_list) {
2085                         ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
2086                         continue;
2087                 }
2088                 AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
2089                         if (handler == session_media->handler) {
2090                                 continue;
2091                         }
2092                         if (!handler->defer_incoming_sdp_stream) {
2093                                 continue;
2094                         }
2095                         res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
2096                                 sdp->media[i]);
2097                         switch (res) {
2098                         case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
2099                                 continue;
2100                         case AST_SIP_SESSION_SDP_DEFER_ERROR:
2101                                 session_media_set_handler(session_media, handler);
2102                                 return 0;
2103                         case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
2104                                 /* Handled by this handler. */
2105                                 session_media_set_handler(session_media, handler);
2106                                 break;
2107                         case AST_SIP_SESSION_SDP_DEFER_NEEDED:
2108                                 /* Handled by this handler. */
2109                                 session_media_set_handler(session_media, handler);
2110                                 return 1;
2111                         }
2112                         /* Move to the next stream */
2113                         break;
2114                 }
2115         }
2116         return 0;
2117 }
2118
2119 static pj_bool_t session_reinvite_on_rx_request(pjsip_rx_data *rdata)
2120 {
2121         pjsip_dialog *dlg;
2122         RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2123         pjsip_rdata_sdp_info *sdp_info;
2124         int deferred;
2125
2126         if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD ||
2127                 !(dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag, PJ_FALSE)) ||
2128                 !(session = ast_sip_dialog_get_session(dlg)) ||
2129                 !session->channel) {
2130                 return PJ_FALSE;
2131         }
2132
2133         if (session->deferred_reinvite) {
2134                 pj_str_t key, deferred_key;
2135                 pjsip_tx_data *tdata;
2136
2137                 /* We use memory from the new request on purpose so the deferred reinvite pool does not grow uncontrollably */
2138                 pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, &rdata->msg_info.cseq->method, rdata);
2139                 pjsip_tsx_create_key(rdata->tp_info.pool, &deferred_key, PJSIP_ROLE_UAS, &session->deferred_reinvite->msg_info.cseq->method,
2140                         session->deferred_reinvite);
2141
2142                 /* If this is a retransmission ignore it */
2143                 if (!pj_strcmp(&key, &deferred_key)) {
2144                         return PJ_TRUE;
2145                 }
2146
2147                 /* Otherwise this is a new re-invite, so reject it */
2148                 if (pjsip_dlg_create_response(dlg, rdata, 491, NULL, &tdata) == PJ_SUCCESS) {
2149                         if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
2150                                 pjsip_tx_data_dec_ref(tdata);
2151                         }
2152                 }
2153
2154                 return PJ_TRUE;
2155         }
2156
2157         if (!(sdp_info = pjsip_rdata_get_sdp_info(rdata)) ||
2158                 (sdp_info->sdp_err != PJ_SUCCESS)) {
2159                 return PJ_FALSE;
2160         }
2161
2162         if (!sdp_info->sdp) {
2163                 const pjmedia_sdp_session *local;
2164                 int i;
2165
2166                 ast_queue_unhold(session->channel);
2167
2168                 pjmedia_sdp_neg_get_active_local(session->inv_session->neg, &local);
2169                 if (!local) {
2170                         return PJ_FALSE;
2171                 }
2172
2173                 /*
2174                  * Some devices indicate hold with deferred SDP reinvites (i.e. no SDP in the reinvite).
2175                  * When hold is initially indicated, we
2176                  * - Receive an INVITE with no SDP
2177                  * - Send a 200 OK with SDP, indicating sendrecv in the media streams
2178                  * - Receive an ACK with SDP, indicating sendonly in the media streams
2179                  *
2180                  * At this point, the pjmedia negotiator saves the state of the media direction so that
2181                  * if we are to send any offers, we'll offer recvonly in the media streams. This is
2182                  * problematic if the device is attempting to unhold, though. If the device unholds
2183                  * by sending a reinvite with no SDP, then we will respond with a 200 OK with recvonly.
2184                  * According to RFC 3264, if an offerer offers recvonly, then the answerer MUST respond
2185                  * with sendonly or inactive. The result of this is that the stream is not off hold.
2186                  *
2187                  * Therefore, in this case, when we receive a reinvite while the stream is on hold, we
2188                  * need to be sure to offer sendrecv. This way, the answerer can respond with sendrecv
2189                  * in order to get the stream off hold. If this is actually a different purpose reinvite
2190                  * (like a session timer refresh), then the answerer can respond to our sendrecv with
2191                  * sendonly, keeping the stream on hold.
2192                  */
2193                 for (i = 0; i < local->media_count; ++i) {
2194                         pjmedia_sdp_media *m = local->media[i];
2195                         pjmedia_sdp_attr *recvonly;
2196                         pjmedia_sdp_attr *inactive;
2197
2198                         recvonly = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "recvonly", NULL);
2199                         inactive = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "inactive", NULL);
2200                         if (recvonly || inactive) {
2201                                 pjmedia_sdp_attr *to_remove = recvonly ?: inactive;
2202                                 pjmedia_sdp_attr *sendrecv;
2203
2204                                 pjmedia_sdp_attr_remove(&m->attr_count, m->attr, to_remove);
2205
2206                                 sendrecv = pjmedia_sdp_attr_create(session->inv_session->pool, "sendrecv", NULL);
2207                                 pjmedia_sdp_media_add_attr(m, sendrecv);
2208                         }
2209                 }
2210
2211                 return PJ_FALSE;
2212         }
2213
2214         deferred = sdp_requires_deferral(session, sdp_info->sdp);
2215         if (deferred == -1) {
2216                 ast_sip_session_media_state_reset(session->pending_media_state);
2217                 return PJ_FALSE;
2218         } else if (!deferred) {
2219                 return PJ_FALSE;
2220         }
2221
2222         pjsip_rx_data_clone(rdata, 0, &session->deferred_reinvite);
2223
2224         return PJ_TRUE;
2225 }
2226
2227 void ast_sip_session_resume_reinvite(struct ast_sip_session *session)
2228 {
2229         if (!session->deferred_reinvite) {
2230                 return;
2231         }
2232
2233         if (session->channel) {
2234                 pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(),
2235                         session->deferred_reinvite, NULL, NULL);
2236         }
2237         pjsip_rx_data_free_cloned(session->deferred_reinvite);
2238         session->deferred_reinvite = NULL;
2239 }
2240
2241 static pjsip_module session_reinvite_module = {
2242         .name = { "Session Re-Invite Module", 24 },
2243         .priority = PJSIP_MOD_PRIORITY_UA_PROXY_LAYER - 1,
2244         .on_rx_request = session_reinvite_on_rx_request,
2245 };
2246
2247
2248 void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
2249                 ast_sip_session_response_cb on_response)
2250 {
2251         pjsip_inv_session *inv_session = session->inv_session;
2252
2253         /* For every request except BYE we disallow sending of the message when
2254          * the session has been disconnected. A BYE request is special though
2255          * because it can be sent again after the session is disconnected except
2256          * with credentials.
2257          */
2258         if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED &&
2259                 tdata->msg->line.req.method.id != PJSIP_BYE_METHOD) {
2260                 return;
2261         }
2262
2263         ast_sip_mod_data_set(tdata->pool, tdata->mod_data, session_module.id,
2264                              MOD_DATA_ON_RESPONSE, on_response);
2265
2266         handle_outgoing_request(session, tdata);
2267         pjsip_inv_send_msg(session->inv_session, tdata);
2268
2269         return;
2270 }
2271
2272 void ast_sip_session_send_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
2273 {
2274         ast_sip_session_send_request_with_cb(session, tdata, NULL);
2275 }
2276
2277 int ast_sip_session_create_invite(struct ast_sip_session *session, pjsip_tx_data **tdata)
2278 {
2279         pjmedia_sdp_session *offer;
2280         SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
2281
2282         if (!(offer = create_local_sdp(session->inv_session, session, NULL))) {
2283                 pjsip_inv_terminate(session->inv_session, 500, PJ_FALSE);
2284                 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create offer\n");
2285         }
2286
2287         pjsip_inv_set_local_sdp(session->inv_session, offer);
2288         pjmedia_sdp_neg_set_prefer_remote_codec_order(session->inv_session->neg, PJ_FALSE);
2289 #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS
2290         if (!session->endpoint->preferred_codec_only) {
2291                 pjmedia_sdp_neg_set_answer_multiple_codecs(session->inv_session->neg, PJ_TRUE);
2292         }
2293 #endif
2294
2295         /*
2296          * We MUST call set_from_header() before pjsip_inv_invite.  If we don't, the
2297          * From in the initial INVITE will be wrong but the rest of the messages will be OK.
2298          */
2299         set_from_header(session);
2300
2301         if (pjsip_inv_invite(session->inv_session, tdata) != PJ_SUCCESS) {
2302                 SCOPE_EXIT_RTN_VALUE(-1, "pjsip_inv_invite failed\n");
2303         }
2304
2305         SCOPE_EXIT_RTN_VALUE(0);
2306 }
2307
2308 static int datastore_hash(const void *obj, int flags)
2309 {
2310         const struct ast_datastore *datastore = obj;
2311         const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
2312
2313         ast_assert(uid != NULL);
2314
2315         return ast_str_hash(uid);
2316 }
2317
2318 static int datastore_cmp(void *obj, void *arg, int flags)
2319 {
2320         const struct ast_datastore *datastore1 = obj;
2321         const struct ast_datastore *datastore2 = arg;
2322         const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
2323
2324         ast_assert(datastore1->uid != NULL);
2325         ast_assert(uid2 != NULL);
2326
2327         return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
2328 }
2329
2330 static void session_destructor(void *obj)
2331 {
2332         struct ast_sip_session *session = obj;
2333         struct ast_sip_session_delayed_request *delay;
2334
2335         /* We dup the endpoint ID in case the endpoint gets freed out from under us */
2336         const char *endpoint_name = session->endpoint ?
2337                 ast_strdupa(ast_sorcery_object_get_id(session->endpoint)) : "<none>";
2338
2339         ast_debug(3, "Destroying SIP session with endpoint %s\n", endpoint_name);
2340
2341         ast_test_suite_event_notify("SESSION_DESTROYING",
2342                 "Endpoint: %s\r\n"
2343                 "AOR: %s\r\n"
2344                 "Contact: %s"
2345                 , endpoint_name
2346                 , session->aor ? ast_sorcery_object_get_id(session->aor) : "<none>"
2347                 , session->contact ? ast_sorcery_object_get_id(session->contact) : "<none>"
2348                 );
2349
2350         /* fire session destroy handler */
2351         handle_session_destroy(session);
2352
2353         /* remove all registered supplements */
2354         ast_sip_session_remove_supplements(session);
2355         AST_LIST_HEAD_DESTROY(&session->supplements);
2356
2357         /* remove all saved media stats */
2358         AST_VECTOR_RESET(&session->media_stats, ast_free);
2359         AST_VECTOR_FREE(&session->media_stats);
2360
2361         ast_taskprocessor_unreference(session->serializer);
2362         ao2_cleanup(session->datastores);
2363         ast_sip_session_media_state_free(session->active_media_state);
2364         ast_sip_session_media_state_free(session->pending_media_state);
2365
2366         while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
2367                 delayed_request_free(delay);
2368         }
2369         ast_party_id_free(&session->id);
2370         ao2_cleanup(session->endpoint);
2371         ao2_cleanup(session->aor);
2372         ao2_cleanup(session->contact);
2373         ao2_cleanup(session->direct_media_cap);
2374
2375         ast_dsp_free(session->dsp);
2376
2377         if (session->inv_session) {
2378                 pjsip_dlg_dec_session(session->inv_session->dlg, &session_module);
2379         }
2380
2381         ast_test_suite_event_notify("SESSION_DESTROYED", "Endpoint: %s", endpoint_name);
2382 }
2383
2384 /*! \brief Destructor for SIP channel */
2385 static void sip_channel_destroy(void *obj)
2386 {
2387         struct ast_sip_channel_pvt *channel = obj;
2388
2389         ao2_cleanup(channel->pvt);
2390         ao2_cleanup(channel->session);
2391 }
2392
2393 struct ast_sip_channel_pvt *ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_session *session)
2394 {
2395         struct ast_sip_channel_pvt *channel = ao2_alloc(sizeof(*channel), sip_channel_destroy);
2396
2397         if (!channel) {
2398                 return NULL;
2399         }
2400
2401         ao2_ref(pvt, +1);
2402         channel->pvt = pvt;
2403         ao2_ref(session, +1);
2404         channel->session = session;
2405
2406         return channel;
2407 }
2408
2409 struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
2410         struct ast_sip_contact *contact, pjsip_inv_session *inv_session, pjsip_rx_data *rdata)
2411 {
2412         RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2413         struct ast_sip_session *ret_session;
2414         int dsp_features = 0;
2415
2416         session = ao2_alloc(sizeof(*session), session_destructor);
2417         if (!session) {
2418                 return NULL;
2419         }
2420
2421         AST_LIST_HEAD_INIT(&session->supplements);
2422         AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
2423         ast_party_id_init(&session->id);
2424
2425         session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2426         if (!session->direct_media_cap) {
2427                 return NULL;
2428         }
2429         session->datastores = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
2430                 DATASTORE_BUCKETS, datastore_hash, NULL, datastore_cmp);
2431         if (!session->datastores) {
2432                 return NULL;
2433         }
2434         session->active_media_state = ast_sip_session_media_state_alloc();
2435         if (!session->active_media_state) {
2436                 return NULL;
2437         }
2438         session->pending_media_state = ast_sip_session_media_state_alloc();
2439         if (!session->pending_media_state) {
2440                 return NULL;
2441         }
2442         if (AST_VECTOR_INIT(&session->media_stats, 1) < 0) {
2443                 return NULL;
2444         }
2445
2446         if (endpoint->dtmf == AST_SIP_DTMF_INBAND || endpoint->dtmf == AST_SIP_DTMF_AUTO) {
2447                 dsp_features |= DSP_FEATURE_DIGIT_DETECT;
2448         }
2449         if (endpoint->faxdetect) {
2450                 dsp_features |= DSP_FEATURE_FAX_DETECT;
2451         }
2452         if (dsp_features) {
2453                 session->dsp = ast_dsp_new();
2454                 if (!session->dsp) {
2455                         return NULL;
2456                 }
2457
2458                 ast_dsp_set_features(session->dsp, dsp_features);
2459         }
2460
2461         session->endpoint = ao2_bump(endpoint);
2462
2463         if (rdata) {
2464                 /*
2465                  * We must continue using the serializer that the original
2466                  * INVITE came in on for the dialog.  There may be
2467                  * retransmissions already enqueued in the original
2468                  * serializer that can result in reentrancy and message
2469                  * sequencing problems.
2470                  */
2471                 session->serializer = ast_sip_get_distributor_serializer(rdata);
2472         } else {
2473                 char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
2474
2475                 /* Create name with seq number appended. */
2476                 ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outsess/%s",
2477                         ast_sorcery_object_get_id(endpoint));
2478
2479                 session->serializer = ast_sip_create_serializer(tps_name);
2480         }
2481         if (!session->serializer) {
2482                 return NULL;
2483         }
2484         ast_sip_dialog_set_serializer(inv_session->dlg, session->serializer);
2485         ast_sip_dialog_set_endpoint(inv_session->dlg, endpoint);
2486         pjsip_dlg_inc_session(inv_session->dlg, &session_module);
2487         inv_session->mod_data[session_module.id] = ao2_bump(session);
2488         session->contact = ao2_bump(contact);
2489         session->inv_session = inv_session;
2490
2491         session->dtmf = endpoint->dtmf;
2492         session->moh_passthrough = endpoint->moh_passthrough;
2493
2494         if (ast_sip_session_add_supplements(session)) {
2495                 /* Release the ref held by session->inv_session */
2496                 ao2_ref(session, -1);
2497                 return NULL;
2498         }
2499
2500         /* Fire seesion begin handlers */
2501         handle_session_begin(session);
2502
2503         /* Avoid unnecessary ref manipulation to return a session */
2504         ret_session = session;
2505         session = NULL;
2506         return ret_session;
2507 }
2508
2509 /*! \brief struct controlling the suspension of the session's serializer. */
2510 struct ast_sip_session_suspender {
2511         ast_cond_t cond_suspended;
2512         ast_cond_t cond_complete;
2513         int suspended;
2514         int complete;
2515 };
2516
2517 static void sip_session_suspender_dtor(void *vdoomed)
2518 {
2519         struct ast_sip_session_suspender *doomed = vdoomed;
2520
2521         ast_cond_destroy(&doomed->cond_suspended);
2522         ast_cond_destroy(&doomed->cond_complete);
2523 }
2524
2525 /*!
2526  * \internal
2527  * \brief Block the session serializer thread task.
2528  *
2529  * \param data Pushed serializer task data for suspension.
2530  *
2531  * \retval 0
2532  */
2533 static int sip_session_suspend_task(void *data)
2534 {
2535         struct ast_sip_session_suspender *suspender = data;
2536
2537         ao2_lock(suspender);
2538
2539         /* Signal that the serializer task is now suspended. */
2540         suspender->suspended = 1;
2541         ast_cond_signal(&suspender->cond_suspended);
2542
2543         /* Wait for the serializer suspension to be completed. */
2544         while (!suspender->complete) {
2545                 ast_cond_wait(&suspender->cond_complete, ao2_object_get_lockaddr(suspender));
2546         }
2547
2548         ao2_unlock(suspender);
2549         ao2_ref(suspender, -1);
2550
2551         return 0;
2552 }
2553
2554 void ast_sip_session_suspend(struct ast_sip_session *session)
2555 {
2556         struct ast_sip_session_suspender *suspender;
2557         int res;
2558
2559         ast_assert(session->suspended == NULL);
2560
2561         if (ast_taskprocessor_is_task(session->serializer)) {
2562                 /* I am the session's serializer thread so I cannot suspend. */
2563                 return;
2564         }
2565
2566         if (ast_taskprocessor_is_suspended(session->serializer)) {
2567                 /* The serializer already suspended. */
2568                 return;
2569         }
2570
2571         suspender = ao2_alloc(sizeof(*suspender), sip_session_suspender_dtor);
2572         if (!suspender) {
2573                 /* We will just have to hope that the system does not deadlock */
2574                 return;
2575         }
2576         ast_cond_init(&suspender->cond_suspended, NULL);
2577         ast_cond_init(&suspender->cond_complete, NULL);
2578
2579         ao2_ref(suspender, +1);
2580         res = ast_sip_push_task(session->serializer, sip_session_suspend_task, suspender);
2581         if (res) {
2582                 /* We will just have to hope that the system does not deadlock */
2583                 ao2_ref(suspender, -2);
2584                 return;
2585         }
2586
2587         session->suspended = suspender;
2588
2589         /* Wait for the serializer to get suspended. */
2590         ao2_lock(suspender);
2591         while (!suspender->suspended) {
2592                 ast_cond_wait(&suspender->cond_suspended, ao2_object_get_lockaddr(suspender));
2593         }
2594         ao2_unlock(suspender);
2595
2596         ast_taskprocessor_suspend(session->serializer);
2597 }
2598
2599 void ast_sip_session_unsuspend(struct ast_sip_session *session)
2600 {
2601         struct ast_sip_session_suspender *suspender = session->suspended;
2602
2603         if (!suspender) {
2604                 /* Nothing to do */
2605                 return;
2606         }
2607         session->suspended = NULL;
2608
2609         /* Signal that the serializer task suspension is now complete. */
2610         ao2_lock(suspender);
2611         suspender->complete = 1;
2612         ast_cond_signal(&suspender->cond_complete);
2613         ao2_unlock(suspender);
2614
2615         ao2_ref(suspender, -1);
2616
2617         ast_taskprocessor_unsuspend(session->serializer);
2618 }
2619
2620 /*!
2621  * \internal
2622  * \brief Handle initial INVITE challenge response message.
2623  * \since 13.5.0
2624  *
2625  * \param rdata PJSIP receive response message data.
2626  *
2627  * \retval PJ_FALSE Did not handle message.
2628  * \retval PJ_TRUE Handled message.
2629  */
2630 static pj_bool_t outbound_invite_auth(pjsip_rx_data *rdata)
2631 {
2632         pjsip_transaction *tsx;
2633         pjsip_dialog *dlg;
2634         pjsip_inv_session *inv;
2635         pjsip_tx_data *tdata;
2636         struct ast_sip_session *session;
2637
2638         if (rdata->msg_info.msg->line.status.code != 401
2639                 && rdata->msg_info.msg->line.status.code != 407) {
2640                 /* Doesn't pertain to us. Move on */
2641                 return PJ_FALSE;
2642         }
2643
2644         tsx = pjsip_rdata_get_tsx(rdata);
2645         dlg = pjsip_rdata_get_dlg(rdata);
2646         if (!dlg || !tsx) {
2647                 return PJ_FALSE;
2648         }
2649
2650         if (tsx->method.id != PJSIP_INVITE_METHOD) {
2651                 /* Not an INVITE that needs authentication */
2652                 return PJ_FALSE;
2653         }
2654
2655         inv = pjsip_dlg_get_inv_session(dlg);
2656         if (PJSIP_INV_STATE_CONFIRMED <= inv->state) {
2657                 /*
2658                  * We cannot handle reINVITE authentication at this
2659                  * time because the reINVITE transaction is still in
2660                  * progress.
2661                  */
2662                 ast_debug(1, "A reINVITE is being challenged.\n");
2663                 return PJ_FALSE;
2664         }
2665         ast_debug(1, "Initial INVITE is being challenged.\n");
2666
2667         session = inv->mod_data[session_module.id];
2668
2669         if (ast_sip_create_request_with_auth(&session->endpoint->outbound_auths, rdata,
2670                 tsx->last_tx, &tdata)) {
2671                 return PJ_FALSE;
2672         }
2673
2674         /*
2675          * Restart the outgoing initial INVITE transaction to deal
2676          * with authentication.
2677          */
2678         pjsip_inv_uac_restart(inv, PJ_FALSE);
2679
2680         ast_sip_session_send_request(session, tdata);
2681         return PJ_TRUE;
2682 }
2683
2684 static pjsip_module outbound_invite_auth_module = {
2685         .name = {"Outbound INVITE Auth", 20},
2686         .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
2687         .on_rx_response = outbound_invite_auth,
2688 };
2689
2690 /*!
2691  * \internal
2692  * \brief Setup outbound initial INVITE authentication.
2693  * \since 13.5.0
2694  *
2695  * \param dlg PJSIP dialog to attach outbound authentication.
2696  *
2697  * \retval 0 on success.
2698  * \retval -1 on error.
2699  */
2700 static int setup_outbound_invite_auth(pjsip_dialog *dlg)
2701 {
2702         pj_status_t status;
2703
2704         ++dlg->sess_count;
2705         status = pjsip_dlg_add_usage(dlg, &outbound_invite_auth_module, NULL);
2706         --dlg->sess_count;
2707
2708         return status != PJ_SUCCESS ? -1 : 0;
2709 }
2710
2711 struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint,
2712         struct ast_sip_contact *contact, const char *location, const char *request_user,
2713         struct ast_stream_topology *req_topology)
2714 {
2715         const char *uri = NULL;
2716         RAII_VAR(struct ast_sip_aor *, found_aor, NULL, ao2_cleanup);
2717         RAII_VAR(struct ast_sip_contact *, found_contact, NULL, ao2_cleanup);
2718         pjsip_timer_setting timer;
2719         pjsip_dialog *dlg;
2720         struct pjsip_inv_session *inv_session;
2721         RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2722         struct ast_sip_session *ret_session;
2723         SCOPE_ENTER(1, "%s %s Topology: %s\n", ast_sorcery_object_get_id(endpoint), request_user,
2724                 ast_str_tmp(256, ast_stream_topology_to_str(req_topology, &STR_TMP)));
2725
2726         /* If no location has been provided use the AOR list from the endpoint itself */
2727         if (location || !contact) {
2728                 location = S_OR(location, endpoint->aors);
2729
2730                 ast_sip_location_retrieve_contact_and_aor_from_list_filtered(location, AST_SIP_CONTACT_FILTER_REACHABLE,
2731                         &found_aor, &found_contact);
2732                 if (!found_contact || ast_strlen_zero(found_contact->uri)) {
2733                         uri = location;
2734                 } else {
2735                         uri = found_contact->uri;
2736                 }
2737         } else {
2738                 uri = contact->uri;
2739         }
2740
2741         /* If we still have no URI to dial fail to create the session */
2742         if (ast_strlen_zero(uri)) {
2743                 ast_log(LOG_ERROR, "Endpoint '%s': No URI available.  Is endpoint registered?\n",
2744                         ast_sorcery_object_get_id(endpoint));
2745                 SCOPE_EXIT_RTN_VALUE(NULL, "No URI\n");
2746         }
2747
2748         if (!(dlg = ast_sip_create_dialog_uac(endpoint, uri, request_user))) {
2749                 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create dialog\n");
2750         }
2751
2752         if (setup_outbound_invite_auth(dlg)) {
2753                 pjsip_dlg_terminate(dlg);
2754                 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't setup auth\n");
2755         }
2756
2757         if (pjsip_inv_create_uac(dlg, NULL, endpoint->extensions.flags, &inv_session) != PJ_SUCCESS) {
2758                 pjsip_dlg_terminate(dlg);
2759                 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create uac\n");
2760         }
2761 #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE)
2762         inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
2763 #endif
2764
2765         pjsip_timer_setting_default(&timer);
2766         timer.min_se = endpoint->extensions.timer.min_se;
2767         timer.sess_expires = endpoint->extensions.timer.sess_expires;
2768         pjsip_timer_init_session(inv_session, &timer);
2769
2770         session = ast_sip_session_alloc(endpoint, found_contact ? found_contact : contact,
2771                 inv_session, NULL);
2772         if (!session) {
2773                 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2774                 return NULL;
2775         }
2776         session->aor = ao2_bump(found_aor);
2777         session->call_direction = AST_SIP_SESSION_OUTGOING_CALL;
2778
2779         ast_party_id_copy(&session->id, &endpoint->id.self);
2780
2781         if (ast_stream_topology_get_count(req_topology) > 0) {
2782                 /* get joint caps between req_topology and endpoint topology */
2783                 int i;
2784
2785                 for (i = 0; i < ast_stream_topology_get_count(req_topology); ++i) {
2786                         struct ast_stream *req_stream;
2787                         struct ast_stream *clone_stream;
2788
2789                         req_stream = ast_stream_topology_get_stream(req_topology, i);
2790
2791                         if (ast_stream_get_state(req_stream) == AST_STREAM_STATE_REMOVED) {
2792                                 continue;
2793                         }
2794
2795                         clone_stream = ast_sip_session_create_joint_call_stream(session, req_stream);
2796                         if (!clone_stream || ast_stream_get_format_count(clone_stream) == 0) {
2797                                 ast_stream_free(clone_stream);
2798                                 continue;
2799                         }
2800
2801                         if (!session->pending_media_state->topology) {
2802                                 session->pending_media_state->topology = ast_stream_topology_alloc();
2803                                 if (!session->pending_media_state->topology) {
2804                                         pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2805                                         ao2_ref(session, -1);
2806                                         SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create topology\n");
2807                                 }
2808                         }
2809
2810                         if (ast_stream_topology_append_stream(session->pending_media_state->topology, clone_stream) < 0) {
2811                                 ast_stream_free(clone_stream);
2812                                 continue;
2813                         }
2814                 }
2815         }
2816
2817         if (!session->pending_media_state->topology) {
2818                 /* Use the configured topology on the endpoint as the pending one */
2819                 session->pending_media_state->topology = ast_stream_topology_clone(endpoint->media.topology);
2820                 if (!session->pending_media_state->topology) {
2821                         pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2822                         ao2_ref(session, -1);
2823                         SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't clone topology\n");
2824                 }
2825         }
2826
2827         if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
2828                 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2829                 /* Since we are not notifying ourselves that the INVITE session is being terminated
2830                  * we need to manually drop its reference to session
2831                  */
2832                 ao2_ref(session, -1);
2833                 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't add usage\n");
2834         }
2835
2836         /* Avoid unnecessary ref manipulation to return a session */
2837         ret_session = session;
2838         session = NULL;
2839         SCOPE_EXIT_RTN_VALUE(ret_session);
2840 }
2841
2842 static int session_end(void *vsession);
2843 static int session_end_completion(void *vsession);
2844
2845 void ast_sip_session_terminate(struct ast_sip_session *session, int response)
2846 {
2847         pj_status_t status;
2848         pjsip_tx_data *packet = NULL;
2849         SCOPE_ENTER(1, "%s Response %d\n", ast_sip_session_get_name(session), response);
2850
2851         if (session->defer_terminate) {
2852                 session->terminate_while_deferred = 1;
2853                 SCOPE_EXIT_RTN("Deferred\n");
2854         }
2855
2856         if (!response) {
2857                 response = 603;
2858         }
2859
2860         /* The media sessions need to exist for the lifetime of the underlying channel
2861          * to ensure that anything (such as bridge_native_rtp) has access to them as
2862          * appropriate. Since ast_sip_session_terminate is called by chan_pjsip and other
2863          * places when the session is to be terminated we terminate any existing
2864          * media sessions here.
2865          */
2866         ast_sip_session_media_stats_save(session, session->active_media_state);
2867         SWAP(session->active_media_state, session->pending_media_state);
2868         ast_sip_session_media_state_reset(session->pending_media_state);
2869
2870         switch (session->inv_session->state) {
2871         case PJSIP_INV_STATE_NULL:
2872                 if (!session->inv_session->invite_tsx) {
2873                         /*
2874                          * Normally, it's pjproject's transaction cleanup that ultimately causes the
2875                          * final session reference to be released but if both STATE and invite_tsx are NULL,
2876                          * we never created a transaction in the first place.  In this case, we need to
2877                          * do the cleanup ourselves.
2878                          */
2879                         /* Transfer the inv_session session reference to the session_end_task */
2880                         session->inv_session->mod_data[session_module.id] = NULL;
2881                         pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
2882                         session_end(session);
2883                         /*
2884                          * session_end_completion will cleanup the final session reference unless
2885                          * ast_sip_session_terminate's caller is holding one.
2886                          */
2887                         session_end_completion(session);
2888                 } else {
2889                         pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
2890                 }
2891                 break;
2892         case PJSIP_INV_STATE_CONFIRMED:
2893                 if (session->inv_session->invite_tsx) {
2894                         ast_debug(3, "Delay sending BYE to %s because of outstanding transaction...\n",
2895                                         ast_sorcery_object_get_id(session->endpoint));
2896                         /* If this is delayed the only thing that will happen is a BYE request so we don't
2897                          * actually need to store the response code for when it happens.
2898                          */
2899                         delay_request(session, NULL, NULL, NULL, 0, DELAYED_METHOD_BYE, NULL);
2900                         break;
2901                 }
2902                 /* Fall through */
2903         default:
2904                 status = pjsip_inv_end_session(session->inv_session, response, NULL, &packet);
2905                 if (status == PJ_SUCCESS && packet) {
2906                         struct ast_sip_session_delayed_request *delay;
2907
2908                         /* Flush any delayed requests so they cannot overlap this transaction. */
2909                         while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
2910                                 delayed_request_free(delay);
2911                         }
2912
2913                         if (packet->msg->type == PJSIP_RESPONSE_MSG) {
2914                                 ast_sip_session_send_response(session, packet);
2915                         } else {
2916                                 ast_sip_session_send_request(session, packet);
2917                         }
2918                 }
2919                 break;
2920         }
2921         SCOPE_EXIT_RTN();
2922 }
2923
2924 static int session_termination_task(void *data)
2925 {
2926         struct ast_sip_session *session = data;
2927
2928         if (session->defer_terminate) {
2929                 session->defer_terminate = 0;
2930                 if (session->inv_session) {
2931                         ast_sip_session_terminate(session, 0);
2932                 }
2933         }
2934
2935         ao2_ref(session, -1);
2936         return 0;
2937 }
2938
2939 static void session_termination_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
2940 {
2941         struct ast_sip_session *session = entry->user_data;
2942
2943         if (ast_sip_push_task(session->serializer, session_termination_task, session)) {
2944                 ao2_cleanup(session);
2945         }
2946 }
2947
2948 int ast_sip_session_defer_termination(struct ast_sip_session *session)
2949 {
2950         pj_time_val delay = { .sec = 60, };
2951         int res;
2952
2953         /* The session should not have an active deferred termination request. */
2954         ast_assert(!session->defer_terminate);
2955
2956         session->defer_terminate = 1;
2957
2958         session->defer_end = 1;
2959         session->ended_while_deferred = 0;
2960
2961         ao2_ref(session, +1);
2962         pj_timer_entry_init(&session->scheduled_termination, 0, session, session_termination_cb);
2963
2964         res = (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(),
2965                 &session->scheduled_termination, &delay) != PJ_SUCCESS) ? -1 : 0;
2966         if (res) {
2967                 session->defer_terminate = 0;
2968                 ao2_ref(session, -1);
2969         }
2970         return res;
2971 }
2972
2973 /*!
2974  * \internal
2975  * \brief Stop the defer termination timer if it is still running.
2976  * \since 13.5.0
2977  *
2978  * \param session Which session to stop the timer.
2979  *
2980  * \return Nothing
2981  */
2982 static void sip_session_defer_termination_stop_timer(struct ast_sip_session *session)
2983 {
2984         if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
2985                 &session->scheduled_termination, session->scheduled_termination.id)) {
2986                 ao2_ref(session, -1);
2987         }
2988 }
2989
2990 void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
2991 {
2992         if (!session->defer_terminate) {
2993                 /* Already canceled or timer fired. */
2994                 return;
2995         }
2996
2997         session->defer_terminate = 0;
2998
2999         if (session->terminate_while_deferred) {
3000                 /* Complete the termination started by the upper layer. */
3001                 ast_sip_session_terminate(session, 0);
3002         }
3003
3004         /* Stop the termination timer if it is still running. */
3005         sip_session_defer_termination_stop_timer(session);
3006 }
3007
3008 void ast_sip_session_end_if_deferred(struct ast_sip_session *session)
3009 {
3010         if (!session->defer_end) {
3011                 return;
3012         }
3013
3014         session->defer_end = 0;
3015
3016         if (session->ended_while_deferred) {
3017                 /* Complete the session end started by the remote hangup. */
3018                 ast_debug(3, "Ending session (%p) after being deferred\n", session);
3019                 session->ended_while_deferred = 0;
3020                 session_end(session);
3021         }
3022 }
3023
3024 struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg)
3025 {
3026         pjsip_inv_session *inv_session = pjsip_dlg_get_inv_session(dlg);
3027         struct ast_sip_session *session;
3028
3029         if (!inv_session ||
3030                 !(session = inv_session->mod_data[session_module.id])) {
3031                 return NULL;
3032         }
3033
3034         ao2_ref(session, +1);
3035
3036         return session;
3037 }
3038
3039 enum sip_get_destination_result {
3040         /*! The extension was successfully found */
3041         SIP_GET_DEST_EXTEN_FOUND,
3042         /*! The extension specified in the RURI was not found */
3043         SIP_GET_DEST_EXTEN_NOT_FOUND,
3044         /*! The extension specified in the RURI was a partial match */
3045         SIP_GET_DEST_EXTEN_PARTIAL,
3046         /*! The RURI is of an unsupported scheme */
3047         SIP_GET_DEST_UNSUPPORTED_URI,
3048 };
3049
3050 /*!
3051  * \brief Dete