Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <err.h> |
| 6 | |
| 7 | #include <osmocom/core/talloc.h> |
| 8 | #include <osmocom/core/application.h> |
| 9 | |
| 10 | #include <openbsc/debug.h> |
| 11 | #include <openbsc/gsm_data.h> |
| 12 | #include <openbsc/mgcp.h> |
| 13 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 0c97688 | 2014-07-04 20:55:20 +0200 | [diff] [blame] | 14 | #include <openbsc/rtp.h> |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 15 | |
| 16 | #include "bscconfig.h" |
| 17 | #ifndef BUILD_MGCP_TRANSCODING |
| 18 | #error "Requires MGCP transcoding enabled (see --enable-mgcp-transcoding)" |
| 19 | #endif |
| 20 | |
Jacob Erlbeck | 535173a | 2014-05-08 14:08:37 +0200 | [diff] [blame] | 21 | #include "openbsc/mgcp_transcode.h" |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 22 | |
| 23 | uint8_t *audio_frame_l16[] = { |
| 24 | }; |
| 25 | |
| 26 | struct rtp_packets { |
| 27 | float t; |
| 28 | int len; |
| 29 | char *data; |
| 30 | }; |
| 31 | |
| 32 | struct rtp_packets audio_packets_l16[] = { |
| 33 | /* RTP: SeqNo=1, TS=160 */ |
| 34 | {0.020000, 332, |
| 35 | "\x80\x0B\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 36 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 37 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 38 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 39 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 40 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 41 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 42 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 43 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 44 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 45 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 46 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 47 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 48 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 49 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 50 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 51 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 52 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 53 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 54 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 55 | "\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED" |
| 56 | }, |
| 57 | }; |
| 58 | |
| 59 | struct rtp_packets audio_packets_gsm[] = { |
| 60 | /* RTP: SeqNo=1, TS=160 */ |
| 61 | {0.020000, 45, |
| 62 | "\x80\x03\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 63 | "\xD4\x7C\xE3\xE9\x62\x50\x39\xF0\xF8\xB4\x68\xEA\x6C\x0E\x81\x1B" |
| 64 | "\x56\x2A\xD5\xBC\x69\x9C\xD1\xF0\x66\x7A\xEC\x49\x7A\x33\x3D\x0A" |
| 65 | "\xDE" |
| 66 | }, |
| 67 | }; |
| 68 | |
| 69 | struct rtp_packets audio_packets_gsm_invalid_size[] = { |
| 70 | /* RTP: SeqNo=1, TS=160 */ |
| 71 | {0.020000, 41, |
| 72 | "\x80\x03\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 73 | "\xD4\x7C\xE3\xE9\x62\x50\x39\xF0\xF8\xB4\x68\xEA\x6C\x0E\x81\x1B" |
| 74 | "\x56\x2A\xD5\xBC\x69\x9C\xD1\xF0\x66\x7A\xEC\x49\x7A\x33\x3D\x0A" |
| 75 | "\xDE" |
| 76 | }, |
| 77 | }; |
| 78 | |
| 79 | struct rtp_packets audio_packets_gsm_invalid_data[] = { |
| 80 | /* RTP: SeqNo=1, TS=160 */ |
| 81 | {0.020000, 45, |
| 82 | "\x80\x03\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 83 | "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE" |
| 84 | "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE" |
| 85 | "\xEE" |
| 86 | }, |
| 87 | }; |
| 88 | |
| 89 | struct rtp_packets audio_packets_gsm_invalid_ptype[] = { |
| 90 | /* RTP: SeqNo=1, TS=160 */ |
| 91 | {0.020000, 45, |
| 92 | "\x80\x08\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 93 | "\xD4\x7C\xE3\xE9\x62\x50\x39\xF0\xF8\xB4\x68\xEA\x6C\x0E\x81\x1B" |
| 94 | "\x56\x2A\xD5\xBC\x69\x9C\xD1\xF0\x66\x7A\xEC\x49\x7A\x33\x3D\x0A" |
| 95 | "\xDE" |
| 96 | }, |
| 97 | }; |
| 98 | |
| 99 | struct rtp_packets audio_packets_g729[] = { |
| 100 | /* RTP: SeqNo=1, TS=160 */ |
| 101 | {0.020000, 32, |
| 102 | "\x80\x12\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 103 | "\xAF\xC2\x81\x40\x00\xFA\xCE\xA4\x21\x7C\xC5\xC3\x4F\xA5\x98\xF5" |
| 104 | "\xB2\x95\xC4\xAD" |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | struct rtp_packets audio_packets_pcma[] = { |
| 109 | /* RTP: SeqNo=1, TS=160 */ |
| 110 | {0.020000, 172, |
| 111 | "\x80\x08\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44" |
| 112 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 113 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 114 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 115 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 116 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 117 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 118 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 119 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 120 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 121 | "\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25" |
| 122 | }, |
Holger Hans Peter Freyther | 5bdb0be | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 123 | /* RTP: SeqNo=26527, TS=232640 */ |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 124 | {0.020000, 92, |
Holger Hans Peter Freyther | 5bdb0be | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 125 | "\x80\x08\x67\x9f\x00\x03\x8c\xc0\x04\xaa\x67\x9f\xd5\xd5\xd5\xd5" |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 126 | "\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5" |
| 127 | "\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5" |
| 128 | "\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5" |
| 129 | "\xd5\xd5\xd5\xd5\xd5\xd5\x55\x55\xd5\xd5\x55\x55\xd5\xd5\x55\x55" |
| 130 | "\xd5\xd5\xd5\x55\x55\xd5\xd5\xd5\x55\x55\xd5\xd5" |
| 131 | }, |
Holger Hans Peter Freyther | 5bdb0be | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 132 | /* RTP: SeqNo=26528, TS=232720 */ |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 133 | {0.020000, 92, |
Holger Hans Peter Freyther | 5bdb0be | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 134 | "\x80\x08\x67\xa0\x00\x03\x8d\x10\x04\xaa\x67\x9f\x55\xd5\xd5\x55" |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 135 | "\xd5\x55\xd5\xd5\xd5\x55\xd5\x55\xd5\xd5\x55\xd5\x55\xd5\x55\xd5" |
| 136 | "\x55\x55\xd5\x55\xd5\xd5\x55\x55\x55\x55\x55\xd5\xd5\x55\xd5\xd5" |
| 137 | "\xd5\x55\xd5\xd5\xd5\x55\x54\x55\xd5\xd5\x55\xd5\xd5\xd5\xd5\x55" |
| 138 | "\x54\x55\xd5\x55\xd5\x55\x55\x55\x55\x55\xd5\xd5\xd5\xd5\xd5\xd4" |
| 139 | "\xd5\x54\x55\xd5\xd4\xd5\x54\xd5\x55\xd5\xd5\xd5" |
| 140 | }, |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | |
| 144 | |
| 145 | static int audio_name_to_type(const char *name) |
| 146 | { |
| 147 | if (!strcasecmp(name, "gsm")) |
| 148 | return 3; |
| 149 | #ifdef HAVE_BCG729 |
| 150 | else if (!strcasecmp(name, "g729")) |
| 151 | return 18; |
| 152 | #endif |
| 153 | else if (!strcasecmp(name, "pcma")) |
| 154 | return 8; |
| 155 | else if (!strcasecmp(name, "l16")) |
| 156 | return 11; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | int mgcp_get_trans_frame_size(void *state_, int nsamples, int dst); |
| 161 | |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 162 | static int given_configured_endpoint(int in_samples, int out_samples, |
| 163 | const char *srcfmt, const char *dstfmt, |
| 164 | void **out_ctx, struct mgcp_endpoint **out_endp) |
| 165 | { |
| 166 | int rc; |
| 167 | struct mgcp_rtp_end *dst_end; |
| 168 | struct mgcp_rtp_end *src_end; |
| 169 | struct mgcp_config *cfg; |
| 170 | struct mgcp_trunk_config *tcfg; |
| 171 | struct mgcp_endpoint *endp; |
| 172 | |
| 173 | cfg = mgcp_config_alloc(); |
| 174 | tcfg = talloc_zero(cfg, struct mgcp_trunk_config); |
| 175 | endp = talloc_zero(tcfg, struct mgcp_endpoint); |
| 176 | |
Holger Hans Peter Freyther | 2add776 | 2014-09-01 22:20:57 +0200 | [diff] [blame] | 177 | cfg->setup_rtp_processing_cb = mgcp_transcoding_setup; |
| 178 | cfg->rtp_processing_cb = mgcp_transcoding_process_rtp; |
| 179 | cfg->get_net_downlink_format_cb = mgcp_transcoding_net_downlink_format; |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 180 | |
| 181 | tcfg->endpoints = endp; |
| 182 | tcfg->number_endpoints = 1; |
| 183 | tcfg->cfg = cfg; |
| 184 | endp->tcfg = tcfg; |
| 185 | endp->cfg = cfg; |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 186 | mgcp_initialize_endp(endp); |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 187 | |
| 188 | dst_end = &endp->bts_end; |
Holger Hans Peter Freyther | 4046126 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 189 | dst_end->codec.payload_type = audio_name_to_type(dstfmt); |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 190 | |
| 191 | src_end = &endp->net_end; |
Holger Hans Peter Freyther | 4046126 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 192 | src_end->codec.payload_type = audio_name_to_type(srcfmt); |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 193 | |
| 194 | if (out_samples) { |
Holger Hans Peter Freyther | 4046126 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 195 | dst_end->codec.frame_duration_den = dst_end->codec.rate; |
| 196 | dst_end->codec.frame_duration_num = out_samples; |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 197 | dst_end->frames_per_packet = 1; |
| 198 | dst_end->force_output_ptime = 1; |
| 199 | } |
| 200 | |
| 201 | rc = mgcp_transcoding_setup(endp, dst_end, src_end); |
Holger Hans Peter Freyther | ec7c151 | 2014-06-28 13:24:36 +0200 | [diff] [blame] | 202 | if (rc < 0) { |
| 203 | printf("setup failed: %s", strerror(-rc)); |
| 204 | abort(); |
| 205 | } |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 206 | |
| 207 | *out_ctx = cfg; |
| 208 | *out_endp = endp; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 213 | static int transcode_test(const char *srcfmt, const char *dstfmt, |
| 214 | uint8_t *src_pkts, size_t src_pkt_size) |
| 215 | { |
| 216 | char buf[4096] = {0x80, 0}; |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 217 | void *ctx; |
| 218 | |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 219 | struct mgcp_rtp_end *dst_end; |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 220 | struct mgcp_process_rtp_state *state; |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 221 | struct mgcp_endpoint *endp; |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 222 | int in_size; |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 223 | const int in_samples = 160; |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 224 | int len, cont; |
| 225 | |
| 226 | printf("== Transcoding test ==\n"); |
| 227 | printf("converting %s -> %s\n", srcfmt, dstfmt); |
| 228 | |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 229 | given_configured_endpoint(in_samples, 0, srcfmt, dstfmt, &ctx, &endp); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 230 | |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 231 | dst_end = &endp->bts_end; |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 232 | state = dst_end->rtp_process_data; |
| 233 | OSMO_ASSERT(state != NULL); |
| 234 | |
| 235 | in_size = mgcp_transcoding_get_frame_size(state, in_samples, 0); |
| 236 | OSMO_ASSERT(sizeof(buf) >= in_size + 12); |
| 237 | |
| 238 | memcpy(buf, src_pkts, src_pkt_size); |
| 239 | |
| 240 | len = src_pkt_size; |
| 241 | |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 242 | cont = mgcp_transcoding_process_rtp(endp, dst_end, |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 243 | buf, &len, sizeof(buf)); |
Holger Hans Peter Freyther | ec7c151 | 2014-06-28 13:24:36 +0200 | [diff] [blame] | 244 | if (cont < 0) { |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 245 | printf("Nothing encoded due: %s\n", strerror(-cont)); |
| 246 | talloc_free(ctx); |
| 247 | return -1; |
Holger Hans Peter Freyther | ec7c151 | 2014-06-28 13:24:36 +0200 | [diff] [blame] | 248 | } |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 249 | |
| 250 | if (len < 24) { |
| 251 | printf("encoded: %s\n", osmo_hexdump((unsigned char *)buf, len)); |
| 252 | } else { |
| 253 | const char *str = osmo_hexdump((unsigned char *)buf, len); |
| 254 | int i = 0; |
| 255 | const int prefix = 4; |
| 256 | const int cutlen = 48; |
| 257 | int nchars = 0; |
| 258 | |
| 259 | printf("encoded:\n"); |
| 260 | do { |
| 261 | nchars = printf("%*s%-.*s", prefix, "", cutlen, str + i); |
| 262 | i += nchars - prefix; |
| 263 | printf("\n"); |
| 264 | } while (nchars - prefix >= cutlen); |
| 265 | } |
Holger Hans Peter Freyther | b7f1ddb | 2014-06-28 00:10:10 +0200 | [diff] [blame] | 266 | printf("counted: %d\n", cont); |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 267 | talloc_free(ctx); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Holger Hans Peter Freyther | 0c97688 | 2014-07-04 20:55:20 +0200 | [diff] [blame] | 271 | static void test_rtp_seq_state(void) |
| 272 | { |
| 273 | char buf[4096]; |
| 274 | int len; |
| 275 | int cont; |
| 276 | void *ctx; |
| 277 | struct mgcp_endpoint *endp; |
| 278 | struct mgcp_process_rtp_state *state; |
| 279 | struct rtp_hdr *hdr; |
| 280 | uint32_t ts_no; |
| 281 | uint16_t seq_no; |
| 282 | |
| 283 | given_configured_endpoint(160, 0, "pcma", "l16", &ctx, &endp); |
| 284 | state = endp->bts_end.rtp_process_data; |
| 285 | OSMO_ASSERT(!state->is_running); |
| 286 | OSMO_ASSERT(state->next_seq == 0); |
| 287 | OSMO_ASSERT(state->next_time == 0); |
| 288 | |
| 289 | /* initialize packet */ |
| 290 | len = audio_packets_pcma[0].len; |
| 291 | memcpy(buf, audio_packets_pcma[0].data, len); |
| 292 | cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, len); |
| 293 | OSMO_ASSERT(cont >= 0); |
| 294 | OSMO_ASSERT(state->is_running); |
| 295 | OSMO_ASSERT(state->next_seq == 2); |
| 296 | OSMO_ASSERT(state->next_time = 240); |
| 297 | |
| 298 | /* verify that the right timestamp was written */ |
| 299 | OSMO_ASSERT(len == audio_packets_pcma[0].len); |
| 300 | hdr = (struct rtp_hdr *) &buf[0]; |
| 301 | |
| 302 | memcpy(&ts_no, &hdr->timestamp, sizeof(ts_no)); |
| 303 | OSMO_ASSERT(htonl(ts_no) == 160); |
| 304 | memcpy(&seq_no, &hdr->sequence, sizeof(seq_no)); |
| 305 | OSMO_ASSERT(htons(seq_no) == 1); |
| 306 | /* Check the right sequence number is written */ |
| 307 | state->next_seq = 1234; |
| 308 | len = audio_packets_pcma[0].len; |
| 309 | memcpy(buf, audio_packets_pcma[0].data, len); |
| 310 | cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, len); |
| 311 | OSMO_ASSERT(cont >= 0); |
| 312 | OSMO_ASSERT(len == audio_packets_pcma[0].len); |
| 313 | hdr = (struct rtp_hdr *) &buf[0]; |
| 314 | |
| 315 | memcpy(&seq_no, &hdr->sequence, sizeof(seq_no)); |
| 316 | OSMO_ASSERT(htons(seq_no) == 1234); |
| 317 | |
| 318 | talloc_free(ctx); |
| 319 | } |
| 320 | |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 321 | static void test_transcode_result(void) |
| 322 | { |
| 323 | char buf[4096]; |
| 324 | int len, res; |
| 325 | void *ctx; |
| 326 | struct mgcp_endpoint *endp; |
| 327 | struct mgcp_process_rtp_state *state; |
| 328 | |
| 329 | { |
| 330 | /* from GSM to PCMA and same ptime */ |
| 331 | given_configured_endpoint(160, 0, "gsm", "pcma", &ctx, &endp); |
| 332 | state = endp->bts_end.rtp_process_data; |
| 333 | |
| 334 | /* result */ |
| 335 | len = audio_packets_gsm[0].len; |
| 336 | memcpy(buf, audio_packets_gsm[0].data, len); |
| 337 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 338 | OSMO_ASSERT(res == sizeof(struct rtp_hdr)); |
| 339 | OSMO_ASSERT(state->sample_cnt == 0); |
| 340 | |
| 341 | len = res; |
| 342 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 343 | OSMO_ASSERT(res == -ENOMSG); |
| 344 | |
| 345 | talloc_free(ctx); |
| 346 | } |
| 347 | |
| 348 | { |
Holger Hans Peter Freyther | 322e54a | 2014-07-04 08:13:53 +0200 | [diff] [blame] | 349 | /* from GSM to PCMA and same ptime */ |
| 350 | given_configured_endpoint(160, 160, "gsm", "pcma", &ctx, &endp); |
| 351 | state = endp->bts_end.rtp_process_data; |
| 352 | |
| 353 | /* result */ |
| 354 | len = audio_packets_gsm[0].len; |
| 355 | memcpy(buf, audio_packets_gsm[0].data, len); |
| 356 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 357 | OSMO_ASSERT(res == sizeof(struct rtp_hdr)); |
| 358 | OSMO_ASSERT(state->sample_cnt == 0); |
| 359 | |
| 360 | len = res; |
| 361 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 362 | OSMO_ASSERT(res == -EAGAIN); |
| 363 | |
| 364 | talloc_free(ctx); |
| 365 | } |
| 366 | |
| 367 | { |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 368 | /* from PCMA to GSM and wrong different ptime */ |
| 369 | given_configured_endpoint(80, 160, "pcma", "gsm", &ctx, &endp); |
| 370 | state = endp->bts_end.rtp_process_data; |
| 371 | |
| 372 | /* Add the first sample */ |
| 373 | len = audio_packets_pcma[1].len; |
| 374 | memcpy(buf, audio_packets_pcma[1].data, len); |
| 375 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 376 | OSMO_ASSERT(state->sample_cnt == 80); |
Holger Hans Peter Freyther | 5bdb0be | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 377 | OSMO_ASSERT(state->next_time == 232640); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 378 | OSMO_ASSERT(res < 0); |
| 379 | |
| 380 | /* Add the second sample and it should be consumable */ |
| 381 | len = audio_packets_pcma[2].len; |
| 382 | memcpy(buf, audio_packets_pcma[2].data, len); |
| 383 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 384 | OSMO_ASSERT(state->sample_cnt == 0); |
Holger Hans Peter Freyther | 5bdb0be | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 385 | OSMO_ASSERT(state->next_time == 232640 + 80 + 160); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 386 | OSMO_ASSERT(res == sizeof(struct rtp_hdr)); |
| 387 | |
| 388 | talloc_free(ctx); |
| 389 | } |
Holger Hans Peter Freyther | 6be6c92 | 2014-07-04 20:55:20 +0200 | [diff] [blame] | 390 | |
| 391 | { |
| 392 | /* from PCMA to GSM with a big time jump */ |
| 393 | struct rtp_hdr *hdr; |
| 394 | uint32_t ts; |
| 395 | |
| 396 | given_configured_endpoint(80, 160, "pcma", "gsm", &ctx, &endp); |
| 397 | state = endp->bts_end.rtp_process_data; |
| 398 | |
| 399 | /* Add the first sample */ |
| 400 | len = audio_packets_pcma[1].len; |
| 401 | memcpy(buf, audio_packets_pcma[1].data, len); |
| 402 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 403 | OSMO_ASSERT(state->sample_cnt == 80); |
| 404 | OSMO_ASSERT(state->next_time == 232640); |
| 405 | OSMO_ASSERT(state->next_seq == 26527); |
| 406 | OSMO_ASSERT(res < 0); |
| 407 | |
| 408 | /* Add a skip to the packet to force a 'resync' */ |
| 409 | len = audio_packets_pcma[2].len; |
| 410 | memcpy(buf, audio_packets_pcma[2].data, len); |
| 411 | hdr = (struct rtp_hdr *) &buf[0]; |
| 412 | /* jump the time and add alignment error */ |
| 413 | ts = ntohl(hdr->timestamp) + 123 * 80 + 2; |
| 414 | hdr->timestamp = htonl(ts); |
| 415 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 416 | OSMO_ASSERT(res < 0); |
| 417 | OSMO_ASSERT(state->sample_cnt == 80); |
| 418 | OSMO_ASSERT(state->next_time == ts); |
| 419 | OSMO_ASSERT(state->next_seq == 26527); |
| 420 | /* TODO: this can create alignment errors */ |
| 421 | |
| 422 | |
| 423 | /* Now attempt to consume 160 samples */ |
| 424 | len = audio_packets_pcma[2].len; |
| 425 | memcpy(buf, audio_packets_pcma[2].data, len); |
| 426 | hdr = (struct rtp_hdr *) &buf[0]; |
| 427 | ts += 80; |
| 428 | hdr->timestamp = htonl(ts); |
| 429 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 430 | OSMO_ASSERT(res == 12); |
| 431 | OSMO_ASSERT(state->sample_cnt == 0); |
| 432 | OSMO_ASSERT(state->next_time == ts + 160); |
| 433 | OSMO_ASSERT(state->next_seq == 26528); |
| 434 | |
| 435 | talloc_free(ctx); |
| 436 | } |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Holger Hans Peter Freyther | 2add776 | 2014-09-01 22:20:57 +0200 | [diff] [blame] | 439 | static void test_transcode_change(void) |
| 440 | { |
| 441 | char buf[4096] = {0x80, 0}; |
| 442 | void *ctx; |
| 443 | |
| 444 | struct mgcp_endpoint *endp; |
| 445 | struct mgcp_process_rtp_state *state; |
| 446 | struct rtp_hdr *hdr; |
| 447 | |
| 448 | int len, res; |
| 449 | |
| 450 | { |
| 451 | /* from GSM to PCMA and same ptime */ |
| 452 | printf("Testing Initial G729->GSM, PCMA->GSM\n"); |
| 453 | given_configured_endpoint(160, 0, "g729", "gsm", &ctx, &endp); |
| 454 | endp->net_end.alt_codec = endp->net_end.codec; |
| 455 | endp->net_end.alt_codec.payload_type = audio_name_to_type("pcma"); |
| 456 | state = endp->bts_end.rtp_process_data; |
| 457 | |
| 458 | /* initial transcoding work */ |
| 459 | OSMO_ASSERT(state->src_fmt == AF_G729); |
| 460 | OSMO_ASSERT(state->dst_fmt == AF_GSM); |
| 461 | OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 8); |
| 462 | OSMO_ASSERT(endp->net_end.codec.payload_type == 18); |
| 463 | |
| 464 | /* result */ |
| 465 | len = audio_packets_pcma[0].len; |
| 466 | memcpy(buf, audio_packets_pcma[0].data, len); |
| 467 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 468 | OSMO_ASSERT(res == sizeof(struct rtp_hdr)); |
| 469 | OSMO_ASSERT(state->sample_cnt == 0); |
| 470 | OSMO_ASSERT(state->src_fmt == AF_PCMA); |
| 471 | OSMO_ASSERT(state->dst_fmt == AF_GSM); |
| 472 | OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 18); |
| 473 | OSMO_ASSERT(endp->net_end.codec.payload_type == 8); |
| 474 | |
| 475 | len = res; |
| 476 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 477 | OSMO_ASSERT(res == -ENOMSG); |
| 478 | |
| 479 | |
| 480 | /* now check that comfort noise doesn't change anything */ |
| 481 | len = audio_packets_pcma[1].len; |
| 482 | memcpy(buf, audio_packets_pcma[1].data, len); |
| 483 | hdr = (struct rtp_hdr *) buf; |
| 484 | hdr->payload_type = 11; |
| 485 | res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf)); |
| 486 | OSMO_ASSERT(state->sample_cnt == 80); |
| 487 | OSMO_ASSERT(state->src_fmt == AF_PCMA); |
| 488 | OSMO_ASSERT(state->dst_fmt == AF_GSM); |
| 489 | OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 18); |
| 490 | OSMO_ASSERT(endp->net_end.codec.payload_type == 8); |
| 491 | |
| 492 | talloc_free(ctx); |
| 493 | } |
| 494 | } |
| 495 | |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 496 | static int test_repacking(int in_samples, int out_samples, int no_transcode) |
| 497 | { |
| 498 | char buf[4096] = {0x80, 0}; |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 499 | int cc; |
| 500 | struct mgcp_endpoint *endp; |
| 501 | void *ctx; |
| 502 | |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 503 | struct mgcp_process_rtp_state *state; |
| 504 | int in_cnt; |
| 505 | int out_size; |
| 506 | int in_size; |
| 507 | uint32_t ts = 0; |
| 508 | uint16_t seq = 0; |
| 509 | const char *srcfmt = "pcma"; |
| 510 | const char *dstfmt = no_transcode ? "pcma" : "l16"; |
| 511 | |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 512 | printf("== Transcoding test ==\n"); |
| 513 | printf("converting %s -> %s\n", srcfmt, dstfmt); |
| 514 | |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 515 | given_configured_endpoint(in_samples, out_samples, srcfmt, dstfmt, &ctx, &endp); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 516 | |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 517 | state = endp->bts_end.rtp_process_data; |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 518 | OSMO_ASSERT(state != NULL); |
| 519 | |
| 520 | in_size = mgcp_transcoding_get_frame_size(state, in_samples, 0); |
| 521 | OSMO_ASSERT(sizeof(buf) >= in_size + 12); |
| 522 | |
| 523 | out_size = mgcp_transcoding_get_frame_size(state, -1, 1); |
| 524 | OSMO_ASSERT(sizeof(buf) >= out_size + 12); |
| 525 | |
Holger Hans Peter Freyther | 4046126 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 526 | buf[1] = endp->net_end.codec.payload_type; |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 527 | *(uint16_t*)(buf+2) = htons(1); |
| 528 | *(uint32_t*)(buf+4) = htonl(0); |
| 529 | *(uint32_t*)(buf+8) = htonl(0xaabbccdd); |
| 530 | |
| 531 | for (in_cnt = 0; in_cnt < 16; in_cnt++) { |
| 532 | int cont; |
| 533 | int len; |
| 534 | |
| 535 | /* fake PCMA data */ |
| 536 | printf("generating %d %s input samples\n", in_samples, srcfmt); |
| 537 | for (cc = 0; cc < in_samples; cc++) |
| 538 | buf[12+cc] = cc; |
| 539 | |
| 540 | *(uint16_t*)(buf+2) = htonl(seq); |
| 541 | *(uint32_t*)(buf+4) = htonl(ts); |
| 542 | |
| 543 | seq += 1; |
| 544 | ts += in_samples; |
| 545 | |
| 546 | cc += 12; /* include RTP header */ |
| 547 | |
| 548 | len = cc; |
| 549 | |
| 550 | do { |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 551 | cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end, |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 552 | buf, &len, sizeof(buf)); |
| 553 | if (cont == -EAGAIN) { |
| 554 | fprintf(stderr, "Got EAGAIN\n"); |
| 555 | break; |
| 556 | } |
| 557 | |
Holger Hans Peter Freyther | ec7c151 | 2014-06-28 13:24:36 +0200 | [diff] [blame] | 558 | if (cont < 0) { |
| 559 | printf("processing failed: %s", strerror(-cont)); |
| 560 | abort(); |
| 561 | } |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 562 | |
| 563 | len -= 12; /* ignore RTP header */ |
| 564 | |
Holger Hans Peter Freyther | b7f1ddb | 2014-06-28 00:10:10 +0200 | [diff] [blame] | 565 | printf("got %d %s output frames (%d octets) count=%d\n", |
| 566 | len / out_size, dstfmt, len, cont); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 567 | |
| 568 | len = cont; |
| 569 | } while (len > 0); |
| 570 | } |
Holger Hans Peter Freyther | a96adfd | 2014-06-22 21:55:50 +0200 | [diff] [blame] | 571 | |
| 572 | talloc_free(ctx); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | int main(int argc, char **argv) |
| 577 | { |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 578 | int rc; |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 579 | osmo_init_logging(&log_info); |
| 580 | |
| 581 | printf("=== Transcoding Good Cases ===\n"); |
| 582 | |
| 583 | transcode_test("l16", "l16", |
| 584 | (uint8_t *)audio_packets_l16[0].data, |
| 585 | audio_packets_l16[0].len); |
| 586 | transcode_test("l16", "gsm", |
| 587 | (uint8_t *)audio_packets_l16[0].data, |
| 588 | audio_packets_l16[0].len); |
| 589 | transcode_test("l16", "pcma", |
| 590 | (uint8_t *)audio_packets_l16[0].data, |
| 591 | audio_packets_l16[0].len); |
| 592 | transcode_test("gsm", "l16", |
| 593 | (uint8_t *)audio_packets_gsm[0].data, |
| 594 | audio_packets_gsm[0].len); |
| 595 | transcode_test("gsm", "gsm", |
| 596 | (uint8_t *)audio_packets_gsm[0].data, |
| 597 | audio_packets_gsm[0].len); |
| 598 | transcode_test("gsm", "pcma", |
| 599 | (uint8_t *)audio_packets_gsm[0].data, |
| 600 | audio_packets_gsm[0].len); |
| 601 | transcode_test("pcma", "l16", |
| 602 | (uint8_t *)audio_packets_pcma[0].data, |
| 603 | audio_packets_pcma[0].len); |
| 604 | transcode_test("pcma", "gsm", |
| 605 | (uint8_t *)audio_packets_pcma[0].data, |
| 606 | audio_packets_pcma[0].len); |
| 607 | transcode_test("pcma", "pcma", |
| 608 | (uint8_t *)audio_packets_pcma[0].data, |
| 609 | audio_packets_pcma[0].len); |
| 610 | |
| 611 | printf("=== Transcoding Bad Cases ===\n"); |
| 612 | |
| 613 | printf("Invalid size:\n"); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 614 | rc = transcode_test("gsm", "pcma", |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 615 | (uint8_t *)audio_packets_gsm_invalid_size[0].data, |
| 616 | audio_packets_gsm_invalid_size[0].len); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 617 | OSMO_ASSERT(rc < 0); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 618 | |
| 619 | printf("Invalid data:\n"); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 620 | rc = transcode_test("gsm", "pcma", |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 621 | (uint8_t *)audio_packets_gsm_invalid_data[0].data, |
| 622 | audio_packets_gsm_invalid_data[0].len); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 623 | OSMO_ASSERT(rc < 0); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 624 | |
| 625 | printf("Invalid payload type:\n"); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 626 | rc = transcode_test("gsm", "pcma", |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 627 | (uint8_t *)audio_packets_gsm_invalid_ptype[0].data, |
| 628 | audio_packets_gsm_invalid_ptype[0].len); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 629 | OSMO_ASSERT(rc == 0); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 630 | |
| 631 | printf("=== Repacking ===\n"); |
| 632 | |
| 633 | test_repacking(160, 160, 0); |
| 634 | test_repacking(160, 160, 1); |
| 635 | test_repacking(160, 80, 0); |
| 636 | test_repacking(160, 80, 1); |
| 637 | test_repacking(160, 320, 0); |
| 638 | test_repacking(160, 320, 1); |
| 639 | test_repacking(160, 240, 0); |
| 640 | test_repacking(160, 240, 1); |
| 641 | test_repacking(160, 100, 0); |
| 642 | test_repacking(160, 100, 1); |
Holger Hans Peter Freyther | 0c97688 | 2014-07-04 20:55:20 +0200 | [diff] [blame] | 643 | test_rtp_seq_state(); |
Holger Hans Peter Freyther | 6a4fdca | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 644 | test_transcode_result(); |
Holger Hans Peter Freyther | 2add776 | 2014-09-01 22:20:57 +0200 | [diff] [blame] | 645 | test_transcode_change(); |
Jacob Erlbeck | 497c2de | 2014-04-08 16:10:04 +0200 | [diff] [blame] | 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |