diff --git a/debian/patches/series b/debian/patches/series
index a93be5ded..e6d1ed6f2 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -92,3 +92,4 @@ sipwise/pv_headers-store-To-info-in-xavp_parsed_name.r-on-re.patch
## git checkout -- debian/
### git commit + git-review as usual
###
+sipwise/fix_incorrect_branch_id_via_branch_next.patch
diff --git a/debian/patches/sipwise/fix_incorrect_branch_id_via_branch_next.patch b/debian/patches/sipwise/fix_incorrect_branch_id_via_branch_next.patch
new file mode 100644
index 000000000..16a535902
--- /dev/null
+++ b/debian/patches/sipwise/fix_incorrect_branch_id_via_branch_next.patch
@@ -0,0 +1,81 @@
+--- a/src/modules/rtpengine/rtpengine.c
++++ b/src/modules/rtpengine/rtpengine.c
+@@ -2255,6 +2255,10 @@
+ ng_flags->via = -1;
+ else if (str_eq(&val, "next"))
+ ng_flags->via = -2;
++ else if (str_eq(&val, "auto-next") || str_eq(&val, "next-auto"))
++ ng_flags->via = -3;
++ else if (str_eq(&val, "auto-extra") || str_eq(&val, "extra-auto"))
++ ng_flags->via = -4;
+ else
+ goto error;
+ goto next;
+@@ -2350,6 +2354,8 @@
+ char md5[MD5_LEN];
+ char branch_buf[MAX_BRANCH_PARAM_LEN];
+ bencode_item_t *result;
++ tm_cell_t *t;
++ unsigned int branch_idx;
+
+ /*** get & init basic stuff needed ***/
+
+@@ -2440,11 +2446,21 @@
+ bencode_dictionary_add_str(ng_flags.dict, "call-id", &ng_flags.call_id);
+
+ if (ng_flags.via) {
+- ret = -1;
++ /* pre-process */
+ switch (ng_flags.via) {
+ case 3:
+ ng_flags.via = (msg->first_line.type == SIP_REPLY) ? 2 : 1;
+- /* fall thru */
++ break;
++ case -3:
++ ng_flags.via = (msg->first_line.type == SIP_REPLY) ? 1 : -2;
++ break;
++ case -4:
++ ng_flags.via = (msg->first_line.type == SIP_REPLY) ? 1 : -1;
++ break;
++ }
++
++ ret = -1;
++ switch (ng_flags.via) {
+ case 1:
+ case 2:
+ ret = get_via_branch(msg, ng_flags.via, &viabranch);
+@@ -2456,10 +2472,16 @@
+ case -2:
+ if (!char_msg_val(msg, md5))
+ break;
++ branch_idx = 0;
++ if (tmb.t_gett) {
++ t = tmb.t_gett();
++ if (t && t != T_UNDEFINED)
++ branch_idx = t->nr_of_outgoings;
++ }
+ msg->hash_index = hash(msg->callid->body, get_cseq(msg)->number);
+
+ viabranch.s = branch_buf;
+- if (branch_builder(msg->hash_index, 0, md5, 0, branch_buf, &viabranch.len))
++ if (branch_builder(msg->hash_index, 0, md5, branch_idx, branch_buf, &viabranch.len))
+ ret = 0;
+ break;
+ }
+--- a/src/modules/rtpengine/doc/rtpengine_admin.xml
++++ b/src/modules/rtpengine/doc/rtpengine_admin.xml
+@@ -2063,7 +2063,13 @@
+ auto
- use the first Via
header if this is
+ a request, or the second one if this is a reply;
+ extra
- don't take the value from a header, but instead use
+- the value of the extra_id_pv
variable.
++ the value of the extra_id_pv
variable;
++ next
- use the branch ID generated by &kamailio; for the
++ next outgoing branch;
++ auto-next
- use next
in requests and
++ 1
in replies;
++ auto-extra
- use extra
in requests and
++ 1
in replies.
+ This can be used to create one media session per branch
+ on the &rtp; proxy. When sending a subsequent delete
command to
+ the &rtp; proxy, you can then stop just the session for a specific branch when