Hi all,
I've seen a behaviour similar to http://jira.freeswitch.org/browse/FS-5398 and I have a proposed patch that fixes it.

Scenario:
- Caller offers, in order, AMR-WB, iLBC, SILK/12K, SILK/24K, SILK/8, speex/8
- FS has global_codec_prefs  set to SILK@24000h@20i,SILK@16000h@20i,SILK@8000h@20i,speex@16000h@20i,speex@8000h@20i,PCMU,PC    MA
- FS decides to accept with SILK/16K (not even offered): the caller BYEs immediately

FS logged:

7dc4b656-a7cf-46fe-93f0-b10b497a1f7d 2013-06-27 14:17:30.726808 [DEBUG] switch_core_media.c:2662 Audio Codec Compare [SILK:97:12000:20:0]/[SILK:120:24000:20:40000]
7dc4b656-a7cf-46fe-93f0-b10b497a1f7d 2013-06-27 14:17:30.726808 [DEBUG] switch_core_media.c:2662 Audio Codec Compare [SILK:97:12000:20:0]/[SILK:119:16000:20:30000]
7dc4b656-a7cf-46fe-93f0-b10b497a1f7d 2013-06-27 14:17:30.726808 [DEBUG] switch_core_media.c:1772 Set Codec sofia/internal/4656729@truphone.comSILK/16000 20 ms 320 samples 30000 bits

To fix this I've added the comparison of sample rates in switch_core_media.c, with this patch (applied on latest master):

diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index 9270b0b..4b926ed 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -2788,7 +2788,7 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
                                        if ((zstr(map->rm_encoding) || (smh->mparams->ndlb & SM_NDLB_ALLOW_BAD_IANANAME)) && map->rm_pt < 96) {
                                                match = (map->rm_pt == imp->ianacode) ? 1 : 0;
                                        } else {
-                                               match = strcasecmp(rm_encoding, imp->iananame) ? 0 : 1;
+                                               match = (!strcasecmp(rm_encoding, imp->iananame) && (map->rm_rate == codec_rate)) ? 1 : 0
                                        }

Hope this helps, even if a better solution exists.
Regards,
Giacomo