diff --git a/app.c b/app.c
index 4c4cfab8bb9b4f970eb0cc6c480a1c9cce226f72..32a5f42ee13b14d50e393b84c47bdc2fe9fb5191 100755
--- a/app.c
+++ b/app.c
@@ -86,7 +86,7 @@ int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prom
 			return res;
 	}
 	rfmt = c->readformat;
-	res = ast_set_read_format(c, AST_FORMAT_SLINEAR);
+	res = ast_set_read_format(c, AST_FORMAT_SLINEAR, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
 		return -1;
@@ -135,7 +135,7 @@ int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prom
 			ast_frfree(f);
 		}
 	}
-	res = ast_set_read_format(c, rfmt);
+	res = ast_set_read_format(c, rfmt, 1);
 	if (res)
 		ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", c->name);
 	ast_dsp_free(sildet);
diff --git a/apps/app_agi.c b/apps/app_agi.c
index 529fbe2e1a0348244e67266508d17be6c31b98b3..e2cdd0ebc6e0ab5aba4d2c04c6918b06b86b71be 100755
--- a/apps/app_agi.c
+++ b/apps/app_agi.c
@@ -521,7 +521,7 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char
 
         if (silence > 0) {
         	rfmt = chan->readformat;
-                res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
+                res = ast_set_read_format(chan, AST_FORMAT_SLINEAR, 1);
                 if (res < 0) {
                 	ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
                         return -1;
@@ -633,7 +633,7 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char
 		fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
 
         if (silence > 0) {
-                res = ast_set_read_format(chan, rfmt);
+                res = ast_set_read_format(chan, rfmt, 1);
                 if (res)
                         ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
                 ast_dsp_free(sildet);
@@ -1484,13 +1484,13 @@ static int eagi_exec(struct ast_channel *chan, void *data)
 	int readformat;
 	int res;
 	readformat = chan->readformat;
-	if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
+	if (ast_set_read_format(chan, AST_FORMAT_SLINEAR, 1)) {
 		ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", chan->name);
 		return -1;
 	}
 	res = agi_exec_full(chan, data, 1, 0);
 	if (!res) {
-		if (ast_set_read_format(chan, readformat)) {
+		if (ast_set_read_format(chan, readformat, 1)) {
 			ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(readformat));
 		}
 	}
diff --git a/apps/app_disa.c b/apps/app_disa.c
index 26676d4bb5c6593fc8a81e8fe8e49b88cb467804..a518cb7e118a8dd7fc8582b854f2fba3582e3e1e 100755
--- a/apps/app_disa.c
+++ b/apps/app_disa.c
@@ -129,12 +129,12 @@ static int disa_exec(struct ast_channel *chan, void *data)
 	FILE *fp;
 	char *stringp=NULL;
 
-	if (ast_set_write_format(chan,AST_FORMAT_ULAW))
+	if (ast_set_write_format(chan,AST_FORMAT_ULAW, 1))
 	{
 		ast_log(LOG_WARNING, "Unable to set write format to Mu-law on %s\n",chan->name);
 		return -1;
 	}
-	if (ast_set_read_format(chan,AST_FORMAT_ULAW))
+	if (ast_set_read_format(chan,AST_FORMAT_ULAW, 1))
 	{
 		ast_log(LOG_WARNING, "Unable to set read format to Mu-law on %s\n",chan->name);
 		return -1;
diff --git a/apps/app_echo.c b/apps/app_echo.c
index 27ba0d86f5020b9ef06dc4b152752cd638c6c699..7de30008b646acf9be8a9765f20a3ecbf7decda8 100755
--- a/apps/app_echo.c
+++ b/apps/app_echo.c
@@ -45,13 +45,15 @@ static int echo_exec(struct ast_channel *chan, void *data)
 	struct localuser *u;
 	struct ast_frame *f;
 	LOCAL_USER_ADD(u);
-	ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
-	ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
+	ast_set_write_format(chan, ast_best_codec(chan->nativeformats), 1);
+	ast_set_read_format(chan, ast_best_codec(chan->nativeformats), 1);
 	/* Do our thing here */
 	while(ast_waitfor(chan, -1) > -1) {
 		f = ast_read(chan);
 		if (!f)
 			break;
+		f->delivery.tv_sec = 0;
+		f->delivery.tv_usec = 0;
 		if (f->frametype == AST_FRAME_VOICE) {
 			if (ast_write(chan, f)) 
 				break;
diff --git a/apps/app_festival.c b/apps/app_festival.c
index 337de43b619c5dab53e4a8c419f92d1df24cba0a..702fdd8e677797eb60dcfef81fa839db0bb0afd5 100755
--- a/apps/app_festival.c
+++ b/apps/app_festival.c
@@ -158,7 +158,7 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
 	ast_stopstream(chan);
 
 	owriteformat = chan->writeformat;
-	res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
+	res = ast_set_write_format(chan, AST_FORMAT_SLINEAR, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
 		return -1;
@@ -230,7 +230,7 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
 //	if (pid > -1)
 //		kill(pid, SIGKILL);
 	if (!res && owriteformat)
-		ast_set_write_format(chan, owriteformat);
+		ast_set_write_format(chan, owriteformat, 1);
 	return res;
 }
 
diff --git a/apps/app_intercom.c b/apps/app_intercom.c
index 925ea37c2530b9424c175645d21e6cfa62acc447..96219deec162be0b73ee1be01cb203e6ece1ce9b 100755
--- a/apps/app_intercom.c
+++ b/apps/app_intercom.c
@@ -138,7 +138,7 @@ static int intercom_exec(struct ast_channel *chan, void *data)
 	/* Remember original read format */
 	oreadformat = chan->readformat;
 	/* Set mode to signed linear */
-	res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
+	res = ast_set_read_format(chan, AST_FORMAT_SLINEAR, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set format to signed linear on channel %s\n", chan->name);
 		return -1;
@@ -170,7 +170,7 @@ static int intercom_exec(struct ast_channel *chan, void *data)
 	}
 	LOCAL_USER_REMOVE(u);
 	if (!res)
-		ast_set_read_format(chan, oreadformat);
+		ast_set_read_format(chan, oreadformat, 1);
 	return res;
 }
 
diff --git a/apps/app_meetme.c b/apps/app_meetme.c
index b7d488ad0bdc49cae4fc2593908a2f8de3fab1d1..ad58809b38b2a7414c65cea797848c6575c3f434 100755
--- a/apps/app_meetme.c
+++ b/apps/app_meetme.c
@@ -274,25 +274,25 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, int c
 
 	if (confflags & CONFFLAG_VIDEO) {	
 		/* Set it into linear mode (write) */
-		if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
+		if (ast_set_write_format(chan, AST_FORMAT_SLINEAR, 1) < 0) {
 			ast_log(LOG_WARNING, "Unable to set '%s' to write linear mode\n", chan->name);
 			goto outrun;
 		}
 
 		/* Set it into linear mode (read) */
-		if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
+		if (ast_set_read_format(chan, AST_FORMAT_SLINEAR, 1) < 0) {
 			ast_log(LOG_WARNING, "Unable to set '%s' to read linear mode\n", chan->name);
 			goto outrun;
 		}
 	} else {
 		/* Set it into U-law mode (write) */
-		if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
+		if (ast_set_write_format(chan, AST_FORMAT_ULAW, 1) < 0) {
 			ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
 			goto outrun;
 		}
 
 		/* Set it into U-law mode (read) */
-		if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
+		if (ast_set_read_format(chan, AST_FORMAT_ULAW, 1) < 0) {
 			ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
 			goto outrun;
 		}
diff --git a/apps/app_milliwatt.c b/apps/app_milliwatt.c
index 78f0604cec7f2681c1c349969c65358efe165d8d..03c2050a74d7c74296f8edd06825890b9a14b477 100755
--- a/apps/app_milliwatt.c
+++ b/apps/app_milliwatt.c
@@ -104,8 +104,8 @@ static int milliwatt_exec(struct ast_channel *chan, void *data)
 
 	struct localuser *u;
 	LOCAL_USER_ADD(u);
-	ast_set_write_format(chan, AST_FORMAT_ULAW);
-	ast_set_read_format(chan, AST_FORMAT_ULAW);
+	ast_set_write_format(chan, AST_FORMAT_ULAW, 1);
+	ast_set_read_format(chan, AST_FORMAT_ULAW, 1);
 	if (chan->_state != AST_STATE_UP)
 	{
 		ast_answer(chan);
diff --git a/apps/app_mp3.c b/apps/app_mp3.c
index d050d5896f4803b065cde5c511b551bc69fe1c67..7f7d318498c0464f5c17f048d9d74dfe746e5fc8 100755
--- a/apps/app_mp3.c
+++ b/apps/app_mp3.c
@@ -126,7 +126,7 @@ static int mp3_exec(struct ast_channel *chan, void *data)
 	ast_stopstream(chan);
 
 	owriteformat = chan->writeformat;
-	res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
+	res = ast_set_write_format(chan, AST_FORMAT_SLINEAR, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
 		return -1;
@@ -190,7 +190,7 @@ static int mp3_exec(struct ast_channel *chan, void *data)
 	if (pid > -1)
 		kill(pid, SIGKILL);
 	if (!res && owriteformat)
-		ast_set_write_format(chan, owriteformat);
+		ast_set_write_format(chan, owriteformat, 1);
 	return res;
 }
 
diff --git a/apps/app_qcall.c b/apps/app_qcall.c
index e876f0ae36ef42ac23e3d38bf6ea005c569f47d0..879b1c8adabb692a3d180c4d53fa7e3aa19b209d 100755
--- a/apps/app_qcall.c
+++ b/apps/app_qcall.c
@@ -219,8 +219,8 @@ time_t	t;
 	channel = ast_request(dialstr,AST_FORMAT_SLINEAR,tele);
 	if (channel)
 	   {
-		ast_set_read_format(channel,AST_FORMAT_SLINEAR);
-		ast_set_write_format(channel,AST_FORMAT_SLINEAR);
+		ast_set_read_format(channel,AST_FORMAT_SLINEAR, 1);
+		ast_set_write_format(channel,AST_FORMAT_SLINEAR, 1);
 #ifdef	OURCLID
 		if (channel->callerid)
 			free(channel->callerid);
diff --git a/apps/app_record.c b/apps/app_record.c
index 80dd1da8995efe75c988c590e6b2ca42f4d0e6d2..81a7704e8578c7fc20c060a76f24cbe9ebb850b2 100755
--- a/apps/app_record.c
+++ b/apps/app_record.c
@@ -141,7 +141,7 @@ static int record_exec(struct ast_channel *chan, void *data)
 
 		if (silence > 0) {
 	        	rfmt = chan->readformat;
-        		res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
+        		res = ast_set_read_format(chan, AST_FORMAT_SLINEAR, 1);
         		if (res < 0) {
                 		ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
         		        return -1;
@@ -223,7 +223,7 @@ static int record_exec(struct ast_channel *chan, void *data)
 
 	LOCAL_USER_REMOVE(u);
 	if (silence > 0) {
-	        res = ast_set_read_format(chan, rfmt);
+	        res = ast_set_read_format(chan, rfmt, 1);
         	if (res)
         	        ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
 		if (sildet)
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index fdf6208d92f6130b4f35e2127eb84879f181999c..5e562e3336b70d73dd8838b9b85cf06f23f999e5 100755
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -1028,7 +1028,7 @@ static int play_and_prepend(struct ast_channel *chan, char *playfile, char *reco
 	
 	if (maxsilence > 0) {
 		rfmt = chan->readformat;
-		res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
+		res = ast_set_read_format(chan, AST_FORMAT_SLINEAR, 1);
 		if (res < 0) {
 			ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
 			return -1;
@@ -1161,7 +1161,7 @@ static int play_and_prepend(struct ast_channel *chan, char *playfile, char *reco
 		}
 	}
 	if (rfmt) {
-		if (ast_set_read_format(chan, rfmt)) {
+		if (ast_set_read_format(chan, rfmt, 1)) {
 			ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
 		}
 	}
@@ -1239,7 +1239,7 @@ static int play_and_record(struct ast_channel *chan, char *playfile, char *recor
 	
 	if (maxsilence > 0) {
 		rfmt = chan->readformat;
-		res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
+		res = ast_set_read_format(chan, AST_FORMAT_SLINEAR, 1);
 		if (res < 0) {
 			ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
 			return -1;
@@ -1344,7 +1344,7 @@ static int play_and_record(struct ast_channel *chan, char *playfile, char *recor
 		ast_closestream(others[x]);
 	}
 	if (rfmt) {
-		if (ast_set_read_format(chan, rfmt)) {
+		if (ast_set_read_format(chan, rfmt, 1)) {
 			ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
 		}
 	}
diff --git a/apps/app_zapbarge.c b/apps/app_zapbarge.c
index 0b865df5bc4d98a1870244828c6e6b1dc55b0d86..1c03afc0b06fe1c4335aa190eb99e052cd8ed4ff 100755
--- a/apps/app_zapbarge.c
+++ b/apps/app_zapbarge.c
@@ -93,13 +93,13 @@ static int conf_run(struct ast_channel *chan, int confno, int confflags)
 	char *buf = __buf + AST_FRIENDLY_OFFSET;
 
 	/* Set it into U-law mode (write) */
-	if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
+	if (ast_set_write_format(chan, AST_FORMAT_ULAW, 1) < 0) {
 		ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
 		goto outrun;
 	}
 
 	/* Set it into U-law mode (read) */
-	if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
+	if (ast_set_read_format(chan, AST_FORMAT_ULAW, 1) < 0) {
 		ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
 		goto outrun;
 	}
diff --git a/apps/app_zapscan.c b/apps/app_zapscan.c
index b24fe5d57619429c720cec04a1efb9fb48c050c5..ef7ac4215062175c10f443346baadbd3fe90a2f8 100755
--- a/apps/app_zapscan.c
+++ b/apps/app_zapscan.c
@@ -93,13 +93,13 @@ static int conf_run(struct ast_channel *chan, int confno, int confflags)
         char *buf = __buf + AST_FRIENDLY_OFFSET;
 
         /* Set it into U-law mode (write) */
-        if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
+        if (ast_set_write_format(chan, AST_FORMAT_ULAW, 1) < 0) {
                 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
                 goto outrun;
         }
 
         /* Set it into U-law mode (read) */
-        if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
+        if (ast_set_read_format(chan, AST_FORMAT_ULAW, 1) < 0) {
                 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
                 goto outrun;
         }
diff --git a/channel.c b/channel.c
index f5b9fc3d1a794f1b7673dc9130063b86d8a75447..4c46e7017464ef539e1411693d0f376e07cc60f2 100755
--- a/channel.c
+++ b/channel.c
@@ -1499,12 +1499,13 @@ int ast_write(struct ast_channel *chan, struct ast_frame *fr)
 	return res;
 }
 
-int ast_set_write_format(struct ast_channel *chan, int fmts)
+int ast_set_write_format(struct ast_channel *chan, int fmts, int needlock)
 {
 	int fmt;
 	int native;
 	int res;
 	
+	ast_mutex_lock(&chan->lock);
 	native = chan->nativeformats;
 	fmt = fmts;
 	
@@ -1512,6 +1513,7 @@ int ast_set_write_format(struct ast_channel *chan, int fmts)
 	if (res < 0) {
 		ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
 			ast_getformatname(fmts), ast_getformatname(chan->nativeformats));
+		ast_mutex_unlock(&chan->lock);
 		return -1;
 	}
 	
@@ -1526,15 +1528,18 @@ int ast_set_write_format(struct ast_channel *chan, int fmts)
 	chan->pvt->writetrans = ast_translator_build_path(chan->pvt->rawwriteformat, chan->writeformat);
 	if (option_debug)
 		ast_log(LOG_DEBUG, "Set channel %s to write format %s\n", chan->name, ast_getformatname(chan->writeformat));
+	ast_mutex_unlock(&chan->lock);
 	return 0;
 }
 
-int ast_set_read_format(struct ast_channel *chan, int fmts)
+int ast_set_read_format(struct ast_channel *chan, int fmts, int needlock)
 {
 	int fmt;
 	int native;
 	int res;
 	
+	if (needlock)
+		ast_mutex_lock(&chan->lock);
 	native = chan->nativeformats;
 	fmt = fmts;
 	/* Find a translation path from the native read format to one of the user's read formats */
@@ -1542,6 +1547,7 @@ int ast_set_read_format(struct ast_channel *chan, int fmts)
 	if (res < 0) {
 		ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
 			ast_getformatname(chan->nativeformats), ast_getformatname(fmts));
+		ast_mutex_unlock(&chan->lock);
 		return -1;
 	}
 	
@@ -1557,6 +1563,7 @@ int ast_set_read_format(struct ast_channel *chan, int fmts)
 	if (option_debug)
 		ast_log(LOG_DEBUG, "Set channel %s to read format %s\n", 
 			chan->name, ast_getformatname(chan->readformat));
+	ast_mutex_unlock(&chan->lock);
 	return 0;
 }
 
@@ -1913,21 +1920,25 @@ int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *pe
 	int peerf;
 	int chanf;
 	int res;
+	ast_mutex_lock(&peer->lock);
 	peerf = peer->nativeformats;
+	ast_mutex_unlock(&peer->lock);
+	ast_mutex_lock(&chan->lock);
 	chanf = chan->nativeformats;
+	ast_mutex_unlock(&chan->lock);
 	res = ast_translator_best_choice(&peerf, &chanf);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, chan->nativeformats, peer->name, peer->nativeformats);
 		return -1;
 	}
 	/* Set read format on channel */
-	res = ast_set_read_format(chan, peerf);
+	res = ast_set_read_format(chan, peerf, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, chanf);
 		return -1;
 	}
 	/* Set write format on peer channel */
-	res = ast_set_write_format(peer, peerf);
+	res = ast_set_write_format(peer, peerf, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, peerf);
 		return -1;
@@ -1941,13 +1952,13 @@ int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *pe
 		return -1;
 	}
 	/* Set writeformat on channel */
-	res = ast_set_write_format(chan, chanf);
+	res = ast_set_write_format(chan, chanf, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, chanf);
 		return -1;
 	}
 	/* Set read format on peer channel */
-	res = ast_set_read_format(peer, chanf);
+	res = ast_set_read_format(peer, chanf, 1);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, peerf);
 		return -1;
@@ -2154,10 +2165,10 @@ int ast_do_masquerade(struct ast_channel *original, int needlock)
 	/* pvt switches.  pbx stays the same, as does next */
 	
 	/* Set the write format */
-	ast_set_write_format(original, wformat);
+	ast_set_write_format(original, wformat, 0);
 
 	/* Set the read format */
-	ast_set_read_format(original, rformat);
+	ast_set_read_format(original, rformat, 0);
 
 	ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
 
@@ -2453,7 +2464,7 @@ static void tonepair_release(struct ast_channel *chan, void *params)
 {
 	struct tonepair_state *ts = params;
 	if (chan) {
-		ast_set_write_format(chan, ts->origwfmt);
+		ast_set_write_format(chan, ts->origwfmt, 0);
 	}
 	free(ts);
 }
@@ -2467,7 +2478,7 @@ static void * tonepair_alloc(struct ast_channel *chan, void *params)
 		return NULL;
 	memset(ts, 0, sizeof(struct tonepair_state));
 	ts->origwfmt = chan->writeformat;
-	if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
+	if (ast_set_write_format(chan, AST_FORMAT_SLINEAR, 1)) {
 		ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
 		tonepair_release(NULL, ts);
 		ts = NULL;
diff --git a/channels/chan_agent.c b/channels/chan_agent.c
index e329f5fcbe2af590b5f8c972cdb2944c1f56b5aa..d47b0a6c45881bae8dbe8ede15672b9a0ef3067c 100755
--- a/channels/chan_agent.c
+++ b/channels/chan_agent.c
@@ -137,13 +137,13 @@ static struct agent_pvt {
 			/* Native formats changed, reset things */ \
 			ast->nativeformats = p->chan->nativeformats; \
 			ast_log(LOG_DEBUG, "Resetting read to %d and write to %d\n", ast->readformat, ast->writeformat);\
-			ast_set_read_format(ast, ast->readformat); \
-			ast_set_write_format(ast, ast->writeformat); \
+			ast_set_read_format(ast, ast->readformat, 0); \
+			ast_set_write_format(ast, ast->writeformat, 0); \
 		} \
 		if (p->chan->readformat != ast->pvt->rawreadformat)  \
-			ast_set_read_format(p->chan, ast->pvt->rawreadformat); \
+			ast_set_read_format(p->chan, ast->pvt->rawreadformat, 0); \
 		if (p->chan->writeformat != ast->pvt->rawwriteformat) \
-			ast_set_write_format(p->chan, ast->pvt->rawwriteformat); \
+			ast_set_write_format(p->chan, ast->pvt->rawwriteformat, 0); \
 	} \
 } while(0)
 
@@ -464,7 +464,7 @@ static int agent_call(struct ast_channel *ast, char *dest, int timeout)
 		ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
 	}
 	if (!res) {
-		res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
+		res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats), 0);
 		ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
 		if (res)
 			ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
@@ -474,7 +474,7 @@ static int agent_call(struct ast_channel *ast, char *dest, int timeout)
 	}
 
 	if (!res) {
-		ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
+		ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats), 0);
 		ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
 		if (res)
 			ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
@@ -1261,12 +1261,12 @@ static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
 						ast_mutex_lock(&agentlock);
 						ast_mutex_lock(&p->lock);
 						if (!res) {
-							res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
+							res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats), 0);
 							if (res)
 								ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
 						}
 						if (!res) {
-							ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
+							ast_set_write_format(chan, ast_best_codec(chan->nativeformats), 0);
 							if (res)
 								ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
 						}
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index c7d866a55edacc18faaa402a4d3a6cbc769f718e..daf52eaf6210f576fd56c3b698e827c51ca4915a 100755
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -4725,7 +4725,7 @@ retryowner:
 								orignative = iaxs[fr.callno]->owner->nativeformats;
 								iaxs[fr.callno]->owner->nativeformats = f.subclass;
 								if (iaxs[fr.callno]->owner->readformat)
-									ast_set_read_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->readformat);
+									ast_set_read_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->readformat, 0);
 								iaxs[fr.callno]->owner->nativeformats = orignative;
 								ast_mutex_unlock(&iaxs[fr.callno]->owner->lock);
 							}
@@ -4962,9 +4962,9 @@ retryowner2:
 						if (iaxs[fr.callno] && iaxs[fr.callno]->owner) {
 							/* Setup read/write formats properly. */
 							if (iaxs[fr.callno]->owner->writeformat)
-								ast_set_write_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->writeformat);	
+								ast_set_write_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->writeformat, 0);	
 							if (iaxs[fr.callno]->owner->readformat)
-								ast_set_read_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->readformat);	
+								ast_set_read_format(iaxs[fr.callno]->owner, iaxs[fr.callno]->owner->readformat, 0);	
 							ast_mutex_unlock(&iaxs[fr.callno]->owner->lock);
 						}
 					}
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index 49b24dba14b094f80233ae4da119adc0740ec36c..375515914e245aa296d5a1ad5fcc21416cb17853 100755
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -1015,8 +1015,8 @@ static struct ast_frame *mgcp_rtp_read(struct mgcp_subchannel *sub)
 			if (f->subclass != sub->owner->nativeformats) {
 				ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
 				sub->owner->nativeformats = f->subclass;
-				ast_set_read_format(sub->owner, sub->owner->readformat);
-				ast_set_write_format(sub->owner, sub->owner->writeformat);
+				ast_set_read_format(sub->owner, sub->owner->readformat, 0);
+				ast_set_write_format(sub->owner, sub->owner->writeformat, 0);
 			}
             /* Courtesy fearnor aka alex@pilosoft.com */
             if (sub->parent->dtmfinband) {
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index ac736345417676a50521eb132573ed2106c1a749..e4abd936a5fca3ba3160402ce6b0a9deb446a946 100755
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -1675,8 +1675,8 @@ static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p
 			if (f->subclass != p->owner->nativeformats) {
 				ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
 				p->owner->nativeformats = f->subclass;
-				ast_set_read_format(p->owner, p->owner->readformat);
-				ast_set_write_format(p->owner, p->owner->writeformat);
+				ast_set_read_format(p->owner, p->owner->readformat, 0);
+				ast_set_write_format(p->owner, p->owner->writeformat, 0);
 			}
             if (p->dtmfmode & SIP_DTMF_INBAND) {
                    f = ast_dsp_process(p->owner,p->vad,f,0);
@@ -2132,8 +2132,8 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req)
 		if (!(p->owner->nativeformats & p->jointcapability)) {
 			ast_log(LOG_DEBUG, "Oooh, we need to change our formats since our peer supports only %d and not %d\n", p->jointcapability, p->owner->nativeformats);
 			p->owner->nativeformats = sip_codec_choose(p->jointcapability);
-			ast_set_read_format(p->owner, p->owner->readformat);
-			ast_set_write_format(p->owner, p->owner->writeformat);
+			ast_set_read_format(p->owner, p->owner->readformat, 0);
+			ast_set_write_format(p->owner, p->owner->writeformat, 0);
 		}
 		if (p->owner->bridge) {
 			/* Turn on/off music on hold if we are holding/unholding */
diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index cccb8429cd740e3c3e1b6272b54010c06316b850..344de37e2590762b2d2c82cc0427c76e4b933199 100755
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -1602,8 +1602,8 @@ static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
 			if (f->subclass != sub->owner->nativeformats) {
 				ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
 				sub->owner->nativeformats = f->subclass;
-				ast_set_read_format(sub->owner, sub->owner->readformat);
-				ast_set_write_format(sub->owner, sub->owner->writeformat);
+				ast_set_read_format(sub->owner, sub->owner->readformat, 0);
+				ast_set_write_format(sub->owner, sub->owner->writeformat, 0);
 			}
 		}
 	}
diff --git a/file.c b/file.c
index 9cdaec57567a6e05a2e6eff92b788afdd479fc9b..578b1febef5e85dacdd84853734c0d6a8dcf1e3a 100755
--- a/file.c
+++ b/file.c
@@ -166,7 +166,7 @@ int ast_stopstream(struct ast_channel *tmp)
 		ast_closestream(tmp->vstream);
 	if (tmp->stream) {
 		ast_closestream(tmp->stream);
-		if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
+		if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat, 1))
 			ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
 	}
 	return 0;
@@ -464,7 +464,7 @@ struct ast_filestream *ast_openstream(struct ast_channel *chan, char *filename,
 	}
 	chan->oldwriteformat = chan->writeformat;
 	/* Set the channel to a format we can work with */
-	res = ast_set_write_format(chan, fmts);
+	res = ast_set_write_format(chan, fmts, 1);
 	
  	fd = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN);
 	if(fd >= 0)
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index c0cd6cd2375e8d219e39ebc8a253ab148d3ac54d..dea3708faffea7e625f4066302dc1f4b6e54bf39 100755
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -572,7 +572,7 @@ int ast_prod(struct ast_channel *chan);
  * Set read format for channel to whichever component of "format" is best. 
  * Returns 0 on success, -1 on failure
  */
-int ast_set_read_format(struct ast_channel *chan, int format);
+int ast_set_read_format(struct ast_channel *chan, int format, int needlock);
 
 //! Sets write format on channel chan
 /*! 
@@ -581,7 +581,7 @@ int ast_set_read_format(struct ast_channel *chan, int format);
  * Set write format for channel to whichever compoent of "format" is best. 
  * Returns 0 on success, -1 on failure
  */
-int ast_set_write_format(struct ast_channel *chan, int format);
+int ast_set_write_format(struct ast_channel *chan, int format, int needlock);
 
 //! Sends text to a channel
 /*! 
diff --git a/indications.c b/indications.c
index fefec9153b2b69939e5a1442116c7363cf637fd7..5a6a8b913c12a38bd027777be22e42a7ba4d0dc8 100755
--- a/indications.c
+++ b/indications.c
@@ -58,7 +58,7 @@ static void playtones_release(struct ast_channel *chan, void *params)
 {
 	struct playtones_state *ps = params;
 	if (chan) {
-		ast_set_write_format(chan, ps->origwfmt);
+		ast_set_write_format(chan, ps->origwfmt, 0);
 	}
 	if (ps->items) free(ps->items);
 	free(ps);
@@ -72,7 +72,7 @@ static void * playtones_alloc(struct ast_channel *chan, void *params)
 		return NULL;
 	memset(ps, 0, sizeof(struct playtones_state));
 	ps->origwfmt = chan->writeformat;
-	if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
+	if (ast_set_write_format(chan, AST_FORMAT_SLINEAR, 1)) {
 		ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
 		playtones_release(NULL, ps);
 		ps = NULL;
diff --git a/res/res_adsi.c b/res/res_adsi.c
index c581e7a4fbf3b230b35af37f24abf27fc8e252f6..e237189479d1cc9f480de4dc92c99da118a521d1 100755
--- a/res/res_adsi.c
+++ b/res/res_adsi.c
@@ -367,15 +367,15 @@ int adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msgl
 
 	ast_stopstream(chan);
 
-	if (ast_set_write_format(chan, AST_FORMAT_ULAW)) {
+	if (ast_set_write_format(chan, AST_FORMAT_ULAW, 1)) {
 		ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
 		return -1;
 	}
 
-	if (ast_set_read_format(chan, AST_FORMAT_ULAW)) {
+	if (ast_set_read_format(chan, AST_FORMAT_ULAW, 1)) {
 		ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
 		if (writeformat) {
-			if (ast_set_write_format(chan, writeformat)) 
+			if (ast_set_write_format(chan, writeformat, 1)) 
 				ast_log(LOG_WARNING, "Unable to restore write format to %d\n", writeformat);
 		}
 		return -1;
@@ -385,9 +385,9 @@ int adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msgl
 		chan->adsicpe = (chan->adsicpe & ~ADSI_FLAG_DATAMODE) | newdatamode;
 
 	if (writeformat)
-		ast_set_write_format(chan, writeformat);
+		ast_set_write_format(chan, writeformat, 1);
 	if (readformat)
-		ast_set_read_format(chan, readformat);
+		ast_set_read_format(chan, readformat, 1);
 
 	return res;
 }
diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c
index d0c1b640dbef5a8b6604716fc26d71ceed0ae85c..febdaaa04d75734ad3219ec02320229b843cfa1b 100755
--- a/res/res_musiconhold.c
+++ b/res/res_musiconhold.c
@@ -397,7 +397,7 @@ static void moh_release(struct ast_channel *chan, void *data)
 	oldwfmt = moh->origwfmt;
 	free(moh);
 	if (chan) {
-		if (oldwfmt && ast_set_write_format(chan, oldwfmt)) 
+		if (oldwfmt && ast_set_write_format(chan, oldwfmt, 0)) 
 			ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(oldwfmt));
 		if (option_verbose > 2)
 			ast_verbose(VERBOSE_PREFIX_3 "Stopped music on hold on %s\n", chan->name);
@@ -420,7 +420,7 @@ static void *moh_alloc(struct ast_channel *chan, void *params)
 	ast_mutex_unlock(&moh_lock);
 	if (res) {
 		res->origwfmt = chan->writeformat;
-		if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
+		if (ast_set_write_format(chan, AST_FORMAT_SLINEAR, 1)) {
 			ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format\n", chan->name);
 			moh_release(NULL, res);
 			res = NULL;