Index: channels/iax2-parser.h
===================================================================
--- channels/iax2-parser.h	(revision 38019)
+++ channels/iax2-parser.h	(working copy)
@@ -108,6 +108,8 @@
 	int iseqno;
 	/* Non-zero if should be sent to transfer peer */
 	int transfer;
+	/* Non-zero if should be sent to media peer */
+	int media;
 	/* Non-zero if this is the final message */
 	int final;
 	/* Ingress or outgres */
Index: channels/chan_iax2.c
===================================================================
--- channels/chan_iax2.c	(revision 38019)
+++ channels/chan_iax2.c	(working copy)
@@ -401,13 +401,9 @@
 	TRANSFER_BEGIN,
 	TRANSFER_READY,
 	TRANSFER_RELEASED,
-	TRANSFER_PASSTHROUGH,
 	TRANSFER_MBEGIN,
 	TRANSFER_MREADY,
-	TRANSFER_MRELEASED,
-	TRANSFER_MPASSTHROUGH,
-	TRANSFER_MEDIA,
-	TRANSFER_MEDIAPASS
+	TRANSFER_MRELEASED
 };
 
 struct iax2_registry {
@@ -578,7 +574,14 @@
 	unsigned short transfercallno;
 	/*! Transfer decrypt AES-128 Key */
 	aes_encrypt_ctx tdcx;
+	/*! If transfer has been attempted */
+	int triedtransfer;
 
+	/*! Whether media is released */
+	int mediareleased;
+	/*! If media released, the peer to send media to */
+	struct sockaddr_in media;
+
 	/*! Status of knowledge of peer ADSI capability */
 	int peeradsicpe;
 
@@ -774,7 +777,7 @@
 static int iax2_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
 static int iax2_poke_peer(struct iax2_peer *peer, int heldcall);
 static int iax2_provision(struct sockaddr_in *end, int sockfd, char *dest, const char *template, int force);
-static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final);
+static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final, int media);
 static int iax2_sendhtml(struct ast_channel *c, int subclass, const char *data, int datalen);
 static int iax2_sendimage(struct ast_channel *c, struct ast_frame *img);
 static int iax2_sendtext(struct ast_channel *c, const char *text);
@@ -786,6 +789,7 @@
 static int send_command_immediate(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int);
 static int send_command_locked(unsigned short callno, char, int, unsigned int, const unsigned char *, int, int);
 static int send_command_transfer(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int);
+static int send_command_media(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int);
 static struct ast_channel *iax2_request(const char *type, int format, void *data, int *cause);
 static struct ast_frame *iax2_read(struct ast_channel *c);
 static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, int temponly);
@@ -1038,6 +1042,8 @@
 	tmp->peercallno = 0;
 	tmp->transfercallno = 0;
 	tmp->bridgecallno = 0;
+	tmp->mediareleased = 0;
+	tmp->triedtransfer = 0; 
 	tmp->pingid = -1;
 	tmp->lagid = -1;
 	tmp->autoid = -1;
@@ -1613,8 +1619,13 @@
 	/* Called with iaxsl held */
 	if (option_debug > 2 && iaxdebug)
 		ast_log(LOG_DEBUG, "Sending %d on %d/%d to %s:%d\n", f->ts, callno, iaxs[callno]->peercallno, ast_inet_ntoa(iabuf, sizeof(iabuf), iaxs[callno]->addr.sin_addr), ntohs(iaxs[callno]->addr.sin_port));
-	if (f->transfer) {
+	if (f->media) {
 		if (iaxdebug)
+			iax_showframe(f, NULL, 0, &iaxs[callno]->media, f->datalen - sizeof(struct ast_iax2_full_hdr));
+		res = sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[callno]->media,
+					sizeof(iaxs[callno]->media));
+	} else if (f->transfer) {
+		if (iaxdebug)
 			iax_showframe(f, NULL, 0, &iaxs[callno]->transfer, f->datalen - sizeof(struct ast_iax2_full_hdr));
 		res = sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[callno]->transfer,
 					sizeof(iaxs[callno]->transfer));
@@ -1813,7 +1824,13 @@
 				if (f->retries >= max_retries) {
 					if (f->transfer) {
 						/* Transfer timeout */
-						send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
+						struct iax_ie_data ied;
+						memset(&ied, 0, sizeof(ied));
+						iax_ie_append_int(&ied, IAX_IE_TRANSFERID, iaxs[callno]->transferid);
+						if (iaxs[callno]->mediareleased)
+							send_command_media(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, ied.buf, ied.pos);
+						else
+							send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, ied.buf, ied.pos, -1);
 					} else if (f->final) {
 						if (f->final) 
 							iax2_destroy_nolock(callno);
@@ -2993,24 +3010,35 @@
 	struct iax_ie_data ied0;
 	struct iax_ie_data ied1;
 	unsigned int transferid = (unsigned int)ast_random();
+
 	memset(&ied0, 0, sizeof(ied0));
+	iaxs[callno0]->transferid = transferid;
 	iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr);
 	iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno);
 	iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, transferid);
 
 	memset(&ied1, 0, sizeof(ied1));
+	iaxs[callno1]->transferid = transferid;
 	iax_ie_append_addr(&ied1, IAX_IE_APPARENT_ADDR, &iaxs[callno0]->addr);
 	iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[callno0]->peercallno);
 	iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
 	
-	res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
+	if (iaxs[callno0]->mediareleased)
+		res = send_command_media(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos);
+	else
+		res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
 	if (res)
 		return -1;
-	res = send_command(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos, -1);
+	if (iaxs[callno1]->mediareleased)
+		res = send_command_media(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos);
+	else
+		res = send_command(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos, -1);
 	if (res)
 		return -1;
 	iaxs[callno0]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
 	iaxs[callno1]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
+	iaxs[callno0]->triedtransfer = 1;
+	iaxs[callno1]->triedtransfer = 1;
 	return 0;
 }
 
@@ -3036,7 +3064,6 @@
 	struct ast_channel *who, *other;
 	int to = -1;
 	int res = -1;
-	int transferstarted=0;
 	struct ast_frame *f;
 	unsigned short callno0 = PTR_TO_CALLNO(c0->tech_pvt);
 	unsigned short callno1 = PTR_TO_CALLNO(c1->tech_pvt);
@@ -3086,13 +3113,16 @@
 			unlock_both(callno0, callno1);
 			return AST_BRIDGE_FAILED_NOWARN;
 		}
-		/* check if transfered and if we really want native bridging */
-		if (!transferstarted && !ast_test_flag(iaxs[callno0], IAX_NOTRANSFER) && !ast_test_flag(iaxs[callno1], IAX_NOTRANSFER)) {
-			/* Try the transfer */
-			if (iax2_start_transfer(callno0, callno1, (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1)) ||
-							ast_test_flag(iaxs[callno0], IAX_TRANSFERMEDIA) | ast_test_flag(iaxs[callno1], IAX_TRANSFERMEDIA)))
-				ast_log(LOG_WARNING, "Unable to start the transfer\n");
-			transferstarted = 1;
+		/* check if if we really want native bridging */
+		if (!ast_test_flag(iaxs[callno0], IAX_NOTRANSFER) && !ast_test_flag(iaxs[callno1], IAX_NOTRANSFER)) {
+			if (!iaxs[callno0]->triedtransfer && !iaxs[callno1]->triedtransfer &&
+			    (iaxs[callno0]->transferring == TRANSFER_NONE) &&
+			    (iaxs[callno1]->transferring == TRANSFER_NONE)) {
+				/* Try the transfer */
+				if (iax2_start_transfer(callno0, callno1, (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1)) ||
+								ast_test_flag(iaxs[callno0], IAX_TRANSFERMEDIA) | ast_test_flag(iaxs[callno1], IAX_TRANSFERMEDIA)))
+					ast_log(LOG_WARNING, "Unable to start the transfer\n");
+			}
 		}
 		if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
 			/* Call has been transferred.  We're no longer involved */
@@ -3774,7 +3804,7 @@
 	return res;
 }
 
-static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
+static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final, int media)
 {
 	/* Queue a packet for delivery on a given private structure.  Use "ts" for
 	   timestamp, or calculate if ts is 0.  Send immediately without retransmission
@@ -3843,6 +3873,7 @@
 	fr->ts = fts;
 	fr->callno = pvt->callno;
 	fr->transfer = transfer;
+	fr->media = media;
 	fr->final = final;
 	if (!sendmini) {
 		/* We need a full frame */
@@ -3855,12 +3886,12 @@
 		fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
 		fh->ts = htonl(fr->ts);
 		fh->oseqno = fr->oseqno;
-		if (transfer) {
+		if (transfer || media) {
 			fh->iseqno = 0;
 		} else
 			fh->iseqno = fr->iseqno;
 		/* Keep track of the last thing we've acknowledged */
-		if (!transfer)
+		if (!(transfer || media))
 			pvt->aseqno = fr->iseqno;
 		fh->type = fr->af.frametype & 0xFF;
 		if (fr->af.frametype == AST_FRAME_VIDEO)
@@ -3893,6 +3924,8 @@
 				if (iaxdebug) {
 					if (fr->transfer)
 						iax_showframe(fr, NULL, 2, &pvt->transfer, fr->datalen - sizeof(struct ast_iax2_full_hdr));
+					else if (fr->media)
+						iax_showframe(fr, NULL, 2, &pvt->media, fr->datalen - sizeof(struct ast_iax2_full_hdr));
 					else
 						iax_showframe(fr, NULL, 2, &pvt->addr, fr->datalen - sizeof(struct ast_iax2_full_hdr));
 				}
@@ -3920,6 +3953,8 @@
 			fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_video_hdr);
 			fr->data = vh;
 			fr->retries = -1;
+			if (pvt->mediareleased)
+				fr->media = 1;
 			res = send_packet(fr);			
 		} else {
 			/* Mini-frames have no sequence number */
@@ -3932,8 +3967,8 @@
 			fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
 			fr->data = mh;
 			fr->retries = -1;
-			if (pvt->transferring == TRANSFER_MEDIAPASS)
-				fr->transfer = 1;
+			if (pvt->mediareleased)
+				fr->media = 1;
 			if (ast_test_flag(pvt, IAX_ENCRYPTED)) {
 				if (ast_test_flag(pvt, IAX_KEYPOPULATED)) {
 					encrypt_frame(&pvt->ecx, (struct ast_iax2_full_hdr *)mh, pvt->semirand, &fr->datalen);
@@ -4499,7 +4534,7 @@
 				res = 0;
 			else
 			/* Simple, just queue for transmission */
-				res = iax2_send(iaxs[callno], f, 0, -1, 0, 0, 0);
+				res = iax2_send(iaxs[callno], f, 0, -1, 0, 0, 0, 0);
 		} else {
 			ast_log(LOG_DEBUG, "Write error: %s\n", strerror(errno));
 		}
@@ -4510,7 +4545,7 @@
 }
 
 static int __send_command(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno, 
-		int now, int transfer, int final)
+		int now, int transfer, int final, int media)
 {
 	struct ast_frame f;
 	f.frametype = type;
@@ -4521,12 +4556,12 @@
 	f.offset = 0;
 	f.src = (char *)__FUNCTION__;
 	f.data = (char *)data;
-	return iax2_send(i, &f, ts, seqno, now, transfer, final);
+	return iax2_send(i, &f, ts, seqno, now, transfer, final, media);
 }
 
 static int send_command(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno)
 {
-	return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 0);
+	return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 0, 0);
 }
 
 static int send_command_locked(unsigned short callno, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno)
@@ -4542,19 +4577,24 @@
 {
 	/* It is assumed that the callno has already been locked */
 	iax2_predestroy_nolock(i->callno);
-	return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 1);
+	return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 1, 0);
 }
 
 static int send_command_immediate(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno)
 {
-	return __send_command(i, type, command, ts, data, datalen, seqno, 1, 0, 0);
+	return __send_command(i, type, command, ts, data, datalen, seqno, 1, 0, 0, 0);
 }
 
 static int send_command_transfer(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen)
 {
-	return __send_command(i, type, command, ts, data, datalen, 0, 0, 1, 0);
+	return __send_command(i, type, command, ts, data, datalen, 0, 0, 1, 0, 0);
 }
 
+static int send_command_media(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen)
+{
+	return __send_command(i, type, command, ts, data, datalen, 0, 0, 0, 0, 1);
+}
+
 static int apply_context(struct iax2_context *con, char *context)
 {
 	while(con) {
@@ -5310,6 +5350,8 @@
 	}
 	memcpy(&pvt->addr, &pvt->transfer, sizeof(pvt->addr));
 	memset(&pvt->transfer, 0, sizeof(pvt->transfer));
+	pvt->mediareleased = 0;
+	memset(&pvt->media, 0, sizeof(pvt->media));
 	/* Reset sequence numbers */
 	pvt->oseqno = 0;
 	pvt->rseqno = 0;
@@ -5679,7 +5721,7 @@
 		iax_ie_append_str(&ied, IAX_IE_USERNAME, name);
 		if (ast_test_flag(p, IAX_TEMPONLY))
 			destroy_peer(p);
-		return send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGAUTH, 0, ied.buf, ied.pos, -1);;
+		return send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGAUTH, 0, ied.buf, ied.pos, -1);
 	} 
 	ast_log(LOG_WARNING, "No such peer '%s'\n", name);
 	return 0;
@@ -6507,6 +6549,7 @@
 				((f.subclass != IAX_COMMAND_TXCNT) &&
 				(f.subclass != IAX_COMMAND_TXREADY) &&		/* for attended transfer */
 				(f.subclass != IAX_COMMAND_TXREL) &&		/* for attended transfer */
+				(f.subclass != IAX_COMMAND_TXMEDIA) &&		/* for attended transfer */
 				(f.subclass != IAX_COMMAND_UNQUELCH ) &&	/* for attended transfer */
 				(f.subclass != IAX_COMMAND_TXACC)) ||
 				(f.frametype != AST_FRAME_IAX))) {
@@ -6516,6 +6559,7 @@
 			  (f.subclass != IAX_COMMAND_TXCNT) &&
 			  (f.subclass != IAX_COMMAND_TXREADY) &&		/* for attended transfer */
 			  (f.subclass != IAX_COMMAND_TXREL) &&		/* for attended transfer */
+			  (f.subclass != IAX_COMMAND_TXMEDIA) &&		/* for attended transfer */
 			  (f.subclass != IAX_COMMAND_UNQUELCH ) &&	/* for attended transfer */
 			  (f.subclass != IAX_COMMAND_TXACC) &&
 			  (f.subclass != IAX_COMMAND_VNAK)) ||
@@ -6604,7 +6648,7 @@
 			} else
 				ast_log(LOG_DEBUG, "Received iseqno %d not within window %d->%d\n", fr->iseqno, iaxs[fr->callno]->rseqno, iaxs[fr->callno]->oseqno);
 		}
-		if (inaddrcmp(&sin, &iaxs[fr->callno]->addr) && 
+		if (inaddrcmp(&sin, &iaxs[fr->callno]->addr) && inaddrcmp(&sin, &iaxs[fr->callno]->media) && 
 			((f.frametype != AST_FRAME_IAX) || 
 			 ((f.subclass != IAX_COMMAND_TXACC) &&
 			  (f.subclass != IAX_COMMAND_TXCNT)))) {
@@ -6729,18 +6773,22 @@
 				}
 				break;
 			case IAX_COMMAND_TXACC:
-				if (iaxs[fr->callno]->transferring == TRANSFER_BEGIN) {
-					/* Ack the packet with the given timestamp */
+				if ((iaxs[fr->callno]->transferring == TRANSFER_BEGIN) &&
+				    (iaxs[fr->callno]->transferid == ies.transferid)) {
+					/* Cancel any outstanding txcnt's */
 					ast_mutex_lock(&iaxq.lock);
 					for (cur = iaxq.head; cur ; cur = cur->next) {
-						/* Cancel any outstanding txcnt's */
 						if ((fr->callno == cur->callno) && (cur->transfer))
 							cur->retries = -1;
 					}
 					ast_mutex_unlock(&iaxq.lock);
 					memset(&ied1, 0, sizeof(ied1));
+					iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, iaxs[fr->callno]->transferid);
 					iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->callno);
-					send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREADY, 0, ied1.buf, ied1.pos, -1);
+					if (iaxs[fr->callno]->mediareleased)
+						send_command_media(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREADY, 0, ied1.buf, ied1.pos);
+					else
+						send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREADY, 0, ied1.buf, ied1.pos, -1);
 					iaxs[fr->callno]->transferring = TRANSFER_READY;
 				}
 				break;
@@ -7370,20 +7418,28 @@
 				}
 				break;
 			case IAX_COMMAND_TXREJ:
-				iaxs[fr->callno]->transferring = 0;
-				if (option_verbose > 2) 
-					ast_verbose(VERBOSE_PREFIX_3 "Channel '%s' unable to transfer\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
-				memset(&iaxs[fr->callno]->transfer, 0, sizeof(iaxs[fr->callno]->transfer));
-				if (iaxs[fr->callno]->bridgecallno) {
-					if (iaxs[iaxs[fr->callno]->bridgecallno]->transferring) {
-						iaxs[iaxs[fr->callno]->bridgecallno]->transferring = 0;
-						send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
+				if ((iaxs[fr->callno]->transferring != TRANSFER_NONE) && 
+				    (iaxs[fr->callno]->transferid == ies.transferid)) {
+					iaxs[fr->callno]->transferring = TRANSFER_NONE;
+					if (option_verbose > 2) 
+						ast_verbose(VERBOSE_PREFIX_3 "Channel '%s' transfer rejected\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
+					memset(&iaxs[fr->callno]->transfer, 0, sizeof(iaxs[fr->callno]->transfer));
+					if (iaxs[fr->callno]->bridgecallno &&
+					    (iaxs[fr->callno]->transferid == iaxs[iaxs[fr->callno]->bridgecallno]->transferid)) {
+						iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_NONE;
+						memset(&ied0, 0, sizeof(ied0));
+						iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, iaxs[iaxs[fr->callno]->bridgecallno]->transferid);
+						if (iaxs[iaxs[fr->callno]->bridgecallno]->mediareleased)
+							send_command_media(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, ied0.buf, ied0.pos);
+						else
+							send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, ied0.buf, ied0.pos, -1);
 					}
 				}
 				break;
 			case IAX_COMMAND_TXREADY:
-				if ((iaxs[fr->callno]->transferring == TRANSFER_BEGIN) ||
-				    (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN)) {
+				if (((iaxs[fr->callno]->transferring == TRANSFER_BEGIN) ||
+				     (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN)) &&
+				    (iaxs[fr->callno]->transferid == ies.transferid)) {
 					if (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN)
 						iaxs[fr->callno]->transferring = TRANSFER_MREADY;
 					else
@@ -7394,20 +7450,31 @@
 						if ((iaxs[iaxs[fr->callno]->bridgecallno]->transferring == TRANSFER_READY) ||
 						    (iaxs[iaxs[fr->callno]->bridgecallno]->transferring == TRANSFER_MREADY)) {
 							/* They're both ready, now release them. */
-							if (iaxs[fr->callno]->transferring == TRANSFER_MREADY) {
+							/* If a peer is media released, we must also do a media release as there may be peers in between */
+							if (iaxs[fr->callno]->mediareleased || 
+							    iaxs[iaxs[fr->callno]->bridgecallno]->mediareleased ||
+							    (iaxs[fr->callno]->transferring == TRANSFER_MREADY)) {
 								if (option_verbose > 2) 
 									ast_verbose(VERBOSE_PREFIX_3 "Attempting media bridge of %s and %s\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>",
 										iaxs[iaxs[fr->callno]->bridgecallno]->owner ? iaxs[iaxs[fr->callno]->bridgecallno]->owner->name : "<Unknown>");
 
-								iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_MEDIA;
-								iaxs[fr->callno]->transferring = TRANSFER_MEDIA;
+								iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_MRELEASED;
+								iaxs[fr->callno]->transferring = TRANSFER_MRELEASED;
 
 								memset(&ied0, 0, sizeof(ied0));
 								memset(&ied1, 0, sizeof(ied1));
 								iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
+								iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, iaxs[iaxs[fr->callno]->bridgecallno]->transferid);
 								iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
-								send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied0.buf, ied0.pos, -1);
-								send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied1.buf, ied1.pos, -1);
+								iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, iaxs[fr->callno]->transferid);
+								if (iaxs[fr->callno]->mediareleased)
+									send_command_media(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied0.buf, ied0.pos);
+								else
+									send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied0.buf, ied0.pos, -1);
+								if (iaxs[iaxs[fr->callno]->bridgecallno]->mediareleased)
+									send_command_media(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied1.buf, ied1.pos);
+								else
+									send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied1.buf, ied1.pos, -1);
 							} else {
 								if (option_verbose > 2) 
 									ast_verbose(VERBOSE_PREFIX_3 "Releasing %s and %s\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>",
@@ -7425,9 +7492,17 @@
 								memset(&ied0, 0, sizeof(ied0));
 								memset(&ied1, 0, sizeof(ied1));
 								iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
+								iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, iaxs[iaxs[fr->callno]->bridgecallno]->transferid);
 								iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
-								send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied0.buf, ied0.pos, -1);
-								send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied1.buf, ied1.pos, -1);
+								iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, iaxs[fr->callno]->transferid);
+								if (iaxs[fr->callno]->mediareleased)
+									send_command_media(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied0.buf, ied0.pos);
+								else
+									send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied0.buf, ied0.pos, -1);
+								if (iaxs[iaxs[fr->callno]->bridgecallno]->mediareleased)
+									send_command_media(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied1.buf, ied1.pos);
+								else
+									send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied1.buf, ied1.pos, -1);
 							}
 
 						}
@@ -7435,22 +7510,52 @@
 				}
 				break;
 			case IAX_COMMAND_TXREQ:
-				try_transfer(iaxs[fr->callno], &ies);
+				/* Try transfer only if none in progress, or use the transferid to resolve contention */
+				if ((iaxs[fr->callno]->transferring == TRANSFER_NONE) || 
+				    (iaxs[fr->callno]->transferid <= ies.transferid)) {
+					/* If there is a bridged channel and it is the same transfer, reject it */
+					if (iaxs[fr->callno]->bridgecallno &&
+					    (iaxs[fr->callno]->transferid == iaxs[iaxs[fr->callno]->bridgecallno]->transferid)) {
+						iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_NONE;
+						memset(&ied0, 0, sizeof(ied0));
+						iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, iaxs[iaxs[fr->callno]->bridgecallno]->transferid);
+						if (iaxs[iaxs[fr->callno]->bridgecallno]->mediareleased)
+							send_command_media(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, ied0.buf, ied0.pos);
+						else
+							send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, ied0.buf, ied0.pos, -1);
+						/* Start our transfer again later */
+						iaxs[fr->callno]->triedtransfer = 0;
+						iaxs[iaxs[fr->callno]->bridgecallno]->triedtransfer = 0;
+					}
+					try_transfer(iaxs[fr->callno], &ies);
+				}
 				break;
 			case IAX_COMMAND_TXCNT:
-				if (iaxs[fr->callno]->transferring)
-					send_command_transfer(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXACC, 0, NULL, 0);
+				if ((iaxs[fr->callno]->transferring == TRANSFER_BEGIN) &&
+				    (iaxs[fr->callno]->transferid == ies.transferid)) {
+					memcpy(&iaxs[fr->callno]->transfer, &sin, sizeof(iaxs[fr->callno]->transfer));
+					memset(&ied0, 0, sizeof(ied0));
+					iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, iaxs[fr->callno]->transferid);
+					send_command_transfer(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXACC, 0, ied0.buf, ied0.pos);
+				}
 				break;
 			case IAX_COMMAND_TXREL:
-				/* Send ack immediately, rather than waiting until we've changed addresses */
-				send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
-				complete_transfer(fr->callno, &ies);
-				stop_stuff(fr->callno);	/* for attended transfer to work with libiax */
+				if ((iaxs[fr->callno]->transferring == TRANSFER_READY) &&
+				    (iaxs[fr->callno]->transferid == ies.transferid)) {
+					/* Send ack immediately, rather than waiting until we've changed addresses */
+					send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
+					complete_transfer(fr->callno, &ies);
+					stop_stuff(fr->callno);	/* for attended transfer to work with libiax */
+				}
 				break;	
 			case IAX_COMMAND_TXMEDIA:
-				if (iaxs[fr->callno]->transferring == TRANSFER_READY) {
+				if ((iaxs[fr->callno]->transferring == TRANSFER_READY) &&
+				    (iaxs[fr->callno]->transferid == ies.transferid)) {
 					/* Start sending our media to the transfer address, but otherwise leave the call as-is */
-					iaxs[fr->callno]->transferring = TRANSFER_MEDIAPASS;
+					memcpy(&iaxs[fr->callno]->media, &iaxs[fr->callno]->transfer, sizeof(iaxs[fr->callno]->addr));
+					memset(&iaxs[fr->callno]->transfer, 0, sizeof(iaxs[fr->callno]->transfer));
+					iaxs[fr->callno]->transferring = TRANSFER_NONE;
+					iaxs[fr->callno]->mediareleased = 1;
 				}
 				break;	
 			case IAX_COMMAND_DPREP:
