diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/Makefile | 29 | ||||
| -rw-r--r-- | test/test_amrwb.cc | 155 | ||||
| -rw-r--r-- | test/test_connectivity.cc | 122 | ||||
| -rw-r--r-- | test/test_init.cc | 63 | ||||
| -rw-r--r-- | test/test_opus.cc | 272 | ||||
| -rw-r--r-- | test/test_raw.cc | 78 | ||||
| -rw-r--r-- | test/test_rtp.cc | 112 | ||||
| -rw-r--r-- | test/test_srtp.cc | 87 | 
8 files changed, 918 insertions, 0 deletions
| diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..49bcf0d --- /dev/null +++ b/test/Makefile @@ -0,0 +1,29 @@ +all: connectivity amrwb init rtp srtp opus raw + +connectivity: +	g++ -g test_$@.cc -L../src/.libs -llrtp -I../src -o test_$@ +	LD_LIBRARY_PATH=../src/.libs ./test_$@ + +amrwb: +	g++ -g test_$@.cc -L../src/.libs -llrtp -lm -I../src -o test_$@ +	LD_LIBRARY_PATH=../src/.libs ./test_$@ + +opus: +	g++ -g test_$@.cc -L../src/.libs -llrtp -lm -I../src -lopus -lao -o test_$@ +	LD_LIBRARY_PATH=../src/.libs ./test_$@ + +init: +	g++ -g test_$@.cc -L../src/.libs -llrtp -I../src -o test_$@ +	LD_LIBRARY_PATH=../src/.libs ./test_$@ + +raw: +	g++ -g test_$@.cc -L../src/.libs -llrtp -I../src -o test_$@ +	LD_LIBRARY_PATH=../src/.libs ./test_$@ + +rtp: +	g++ -g test_$@.cc ../src/rtp.cc -o test_$@ +	./test_$@ + +srtp: +	g++ -g -DUSE_CRYPTO test_$@.cc ../src/rtp.cc ../src/srtp.cc ../src/asc2bin.cc -lsrtp -o test_$@ +	./test_$@ diff --git a/test/test_amrwb.cc b/test/test_amrwb.cc new file mode 100644 index 0000000..cf5a5a6 --- /dev/null +++ b/test/test_amrwb.cc @@ -0,0 +1,155 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + *            test_init.cc + * + *  Mon Sep  2 14:02:16 CEST 2013 + *  Copyright 2013 Bent Bisballe Nyeng + *  deva@aasimon.org + ****************************************************************************/ + +/* + *  This file is part of lrtp. + * + * lrtp is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * lrtp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with lrtp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <lrtp.h> + +#include <stdio.h> + +#include <string> +#include <vector> + +#include <math.h> +#include <opus/opus.h> +#include <limits.h> +#include <ao/ao.h> + +#define KEY "123456789012345678901234567890123456789012345678901234567890" +#define SSRC 1234567890 + +#define FS 8000 + +#define F1 440 +#define AF1 0.3 + +#define F2 500 +#define AF2 0.7 + +void dump(const char *title, const char *buf, size_t size) +{ +  printf("%12s: ", title); +  for(int i = 0; i < size; i++) { +    if(i % 8 == 0) printf(" "); +    printf("%02x ", (unsigned char)*buf++); +  } +  printf("\n"); +} + +// Frame sizes based on frame type index: +static const int wb_frame_size[16] = { +  17, 23, 32, 36, 40, 46, 50, 58, +  60, 5, -1, -1, -1, -1, -1, 0 +}; + +int main() +{ +  std::vector<std::string> packets; +  unsigned int csrc = 42; +   +  int frame_type_index = 8; + +  printf("========== Encode ==========\n"); + +  { // Encode +    struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +    struct lrtp_profile_t *profile = +      lrtp_create_profile(lrtp, PROFILE_AMRWB, csrc, +                          OPTION_AMRWB_FRAME_TYPE_INDEX, frame_type_index, +                          OPTION_END); + +    char packet[16*1024]; +    size_t packetsize = sizeof(packet); + +    int cnt = 0; +     +    for(int i = 0; i < 10; i++) { + +      size_t num_frames = 10; +      char frame[wb_frame_size[frame_type_index] * num_frames]; +      size_t framesize = wb_frame_size[frame_type_index] * num_frames; + +      for(int i = 0; i < framesize; i++) frame[i] = cnt++; + +      int timestamp = 0; + +      int ret = lrtp_enqueue_frame(profile, frame, framesize); +      while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) != 0) { +        std::string p; +        p.append(packet, ret); +        packets.push_back(p); +      } +    } + +    lrtp_destroy_profile(lrtp, csrc); +    lrtp_close(lrtp); +  } + +  printf("========== Decode ==========\n"); + +  { // Decode +    struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +    struct lrtp_profile_t *profile = +      lrtp_create_profile(lrtp, PROFILE_AMRWB, csrc, +                          OPTION_AMRWB_FRAME_TYPE_INDEX, frame_type_index, +                          OPTION_END); + +    char frame[16*1024]; +    size_t framesize = sizeof(frame); +     +    int cnt = 0; + +    std::vector<std::string>::iterator i = packets.begin(); +    while(i != packets.end()) { +      size_t packetsize = i->size(); +      printf("unpack sz: %d\n", packetsize); +      const char *packet = i->data(); +      unsigned int ts; + +      framesize = sizeof(frame); + +      lrtp_unpack(lrtp, packet, packetsize); +      int ret; +      while((ret = lrtp_dequeue_frame(lrtp, frame, framesize, &csrc, &ts)) != 0) { +        printf("Got %d bytes, csrc %d, ts: %d\n", ret, csrc, ts); +         +        printf("cnt:\n"); +        for(int i = 0; i < ret; i++) { +          printf("%02x ", (unsigned char)frame[i]); +        } +        printf("\n"); +      } + +      i++; +    } + +    lrtp_destroy_profile(lrtp, csrc); +    lrtp_close(lrtp); +  } + +  return 0; +} diff --git a/test/test_connectivity.cc b/test/test_connectivity.cc new file mode 100644 index 0000000..e115748 --- /dev/null +++ b/test/test_connectivity.cc @@ -0,0 +1,122 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + *            test_init.cc + * + *  Mon Sep  2 14:02:16 CEST 2013 + *  Copyright 2013 Bent Bisballe Nyeng + *  deva@aasimon.org + ****************************************************************************/ + +/* + *  This file is part of lrtp. + * + * lrtp is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * lrtp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with lrtp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <lrtp.h> + +#include <stdio.h> + +#include <string> +#include <vector> + +#define KEY "123456789012345678901234567890123456789012345678901234567890" +#define SSRC 1234567890 + +void dump(const char *title, const char *buf, size_t size) +{ +  printf("%12s: ", title); +  for(int i = 0; i < size; i++) { +    if(i % 8 == 0) printf(" "); +    printf("%02x ", (unsigned char)*buf++); +  } +  printf("\n"); +} + +int main() +{ +  char frame[5]; +  int pkg_size = 4; + +  std::vector<std::string> packets; +  unsigned int csrc = 42; + +  { // Encode +    struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +    struct lrtp_profile_t *profile = +      lrtp_create_profile(lrtp, PROFILE_RAW, csrc, +                          OPTION_RAW_PKG_SIZE, pkg_size, +                          OPTION_END); + +    char packet[16*1024]; +    size_t size = sizeof(packet); + +    for(unsigned int ts = 0; ts < 8; ts++) { +      int ret = 0; +      ret = lrtp_enqueue_frame(profile, frame, sizeof(frame)); +      while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) != 0) { +        std::string p; +        p.append(packet, ret); +        packets.push_back(p); +        dump("pkg", packet, ret); +      } +    } +     +    lrtp_destroy_profile(lrtp, csrc); +     +    lrtp_close(lrtp); +  } + +  { // Decode +    struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +    struct lrtp_profile_t *profile = +      lrtp_create_profile(lrtp, PROFILE_RAW, csrc, +                          OPTION_RAW_PKG_SIZE, pkg_size, +                          OPTION_END); + +    char frame[16*1024]; +    size_t framesize = sizeof(frame); +     +    int cnt = 0; + +    std::vector<std::string>::iterator i = packets.begin(); +    while(i != packets.end()) { +      size_t packetsize = i->size(); +      printf("unpack sz: %d\n", packetsize); +      const char *packet = i->data(); +      unsigned int ts; + +      framesize = sizeof(frame); + +      lrtp_unpack(lrtp, packet, packetsize); +      int ret; +      while((ret = lrtp_dequeue_frame(lrtp, frame, framesize, &csrc, &ts)) +            != 0) { +        printf("Got %d bytes, csrc %d, ts: %d\n", ret, csrc, ts); +        dump("pkg", frame, ret); +      } + +      i++; +    } + +    lrtp_destroy_profile(lrtp, csrc); +    lrtp_close(lrtp); +  } + +  return 0; +} + diff --git a/test/test_init.cc b/test/test_init.cc new file mode 100644 index 0000000..97ecd30 --- /dev/null +++ b/test/test_init.cc @@ -0,0 +1,63 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + *            test_init.cc + * + *  Mon Sep  2 14:02:16 CEST 2013 + *  Copyright 2013 Bent Bisballe Nyeng + *  deva@aasimon.org + ****************************************************************************/ + +/* + *  This file is part of lrtp. + * + * lrtp is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * lrtp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with lrtp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <lrtp.h> + +#include <stdio.h> + +#define KEY "123456789012345678901234567890123456789012345678901234567890" +#define SSRC 1234567890 + +void dump(const char *title, const char *buf, size_t size) +{ +  printf("%12s: ", title); +  for(int i = 0; i < size; i++) { +    if(i % 8 == 0) printf(" "); +    printf("%02x ", (unsigned char)*buf++); +  } +  printf("\n"); +} + +int main() +{ +  char frame[] = "foo"; + +  struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +  unsigned int csrc = 42; +  struct lrtp_profile_t *profile = +    lrtp_create_profile(lrtp, PROFILE_RAW, csrc, +                        OPTION_RAW_PKG_SIZE, sizeof(frame), +                        OPTION_END); + +  lrtp_destroy_profile(lrtp, csrc); + +  lrtp_close(lrtp); + +  return 0; +} + diff --git a/test/test_opus.cc b/test/test_opus.cc new file mode 100644 index 0000000..68ea0f4 --- /dev/null +++ b/test/test_opus.cc @@ -0,0 +1,272 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + *            test_init.cc + * + *  Mon Sep  2 14:02:16 CEST 2013 + *  Copyright 2013 Bent Bisballe Nyeng + *  deva@aasimon.org + ****************************************************************************/ + +/* + *  This file is part of lrtp. + * + * lrtp is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * lrtp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with lrtp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <lrtp.h> + +#include <stdio.h> + +#include <string> +#include <vector> + +#include <math.h> +#include <opus/opus.h> +#include <limits.h> +#include <ao/ao.h> + +#define KEY "123456789012345678901234567890123456789012345678901234567890" +#define SSRC 1234567890 + +#define FS 8000 + +#define F1 440 +#define AF1 0.3 + +#define F2 500 +#define AF2 0.7 + +void dump(const char *title, const char *buf, size_t size) +{ +  printf("%12s: ", title); +  for(int i = 0; i < size; i++) { +    if(i % 8 == 0) printf(" "); +    printf("%02x ", (unsigned char)*buf++); +  } +  printf("\n"); +} + +class Audio { +public: +  Audio() { +    ao_initialize(); + +    device = NULL; +    ao_sample_format format; +     +    int default_driver = ao_default_driver_id(); +    if(default_driver == -1) { +      printf("Error could not default driver.\n"); +      return; +    }    +    printf("Default driver: %d\n", default_driver); + +    format.bits = 16; +    format.channels = 2; +    format.rate = FS; +    format.byte_format = AO_FMT_LITTLE; +				 +    device = ao_open_live(default_driver, &format, NULL); +    if(device == NULL) { +      printf("Error opening device.\n"); +      return; +    } +  } + +  ~Audio() { +    if(device) ao_close(device); +    ao_shutdown(); +  } + +  void play(char *pcm, size_t size) { +    ao_play(device, pcm, size); +  } + +private: +  ao_device *device; +}; + +int main() +{ +  size_t channels = 2; +  size_t ms[] = { 120, 240, 480, 960, 1920, 2880 }; + +  std::vector<std::string> packets; +  unsigned int csrc = 42; +   +  double sin_x = 0; +  size_t ts = 0; + +  printf("========== Encode ==========\n"); + +  { // Encode +    struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +    struct lrtp_profile_t *profile = +      lrtp_create_profile(lrtp, PROFILE_OPUS, csrc, +                          //OPTION_RAW_PKG_SIZE, pkg_size, +                          OPTION_END); + +    char packet[16*1024]; +    size_t packetsize = sizeof(packet); +     +    int err; +    OpusEncoder *opus = opus_encoder_create(FS, channels, +                                            OPUS_APPLICATION_AUDIO, &err); +    printf("Opus create err: %d\n", err); + +    opus_encoder_ctl(opus, OPUS_SET_BITRATE(64000));// [500;512000] +    opus_encoder_ctl(opus, OPUS_SET_COMPLEXITY(10)); // [0;10] +    opus_encoder_ctl(opus, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));	 + +    int cnt = 0; +    size_t timestamp = 0; +    for(unsigned int ts = 0; ts < 120; ts++) { +      printf("packet #%d\n", ts); + +      size_t idx = rand() % (sizeof(ms)/sizeof(size_t)); +      printf("idx: %d\n", idx); +      size_t pcmsize = ms[idx] / (48000.0 / FS); // Number of samples pr channel +      short *pcm = new short[100000/*pcmsize * channels*/]; +      for(int i = 0 ; i < pcmsize; i++) { +        sin_x++; +         +        double amp1 = sin((2*M_PI/(double)FS)*(double)sin_x * AF1) * SHRT_MAX; +        double amp2 = sin((2*M_PI/(double)FS)*(double)sin_x * AF2) * SHRT_MAX; +         +        pcm[i*2]   = (short)(sin(2*M_PI/FS*(double)sin_x * F1) * amp1); +        pcm[i*2+1] = (short)(sin(2*M_PI/FS*(double)sin_x * F2) * amp2); +      } + +      // Master timestamp is sample number in 48kHz (Opus RFC states this) +      timestamp += pcmsize * 48000 / FS; + +      //      size_t pcmsize = pcmsize * channels * sizeof(short); + +      char frame[pcmsize]; +      int framesize = sizeof(frame); +      framesize = opus_encode(opus, pcm, pcmsize, +                              (unsigned char*)frame, framesize); + +      if(framesize < 0) { +        printf("Opus error: %s\n", opus_strerror(framesize));	 +      } + +      printf("Opus Packet: %d bytes from %d bytes\n", pcmsize, framesize); + +      int ret = lrtp_enqueue_frame(profile, frame, framesize); +      while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) != 0) { +        std::string p; +        p.append(packet, ret); +        packets.push_back(p); +      } + +      delete[] pcm; +    } + +    opus_encoder_destroy(opus); +    lrtp_destroy_profile(lrtp, csrc); +    lrtp_close(lrtp); +  } + +  printf("========== Decode ==========\n"); + +  { // Decode +    struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +    struct lrtp_profile_t *profile = +      lrtp_create_profile(lrtp, PROFILE_OPUS, csrc, +                          //OPTION_RAW_PKG_SIZE, pkg_size, +                          OPTION_END); + +    int err; +    OpusDecoder *opus = opus_decoder_create(FS, channels, &err); +    printf("Opus create err: %d\n", err); + +    int idx = (sizeof(ms)/sizeof(size_t)) - 1; +    printf("idx: %d\n", idx); + +    Audio audio; + +    char frame[16*1024]; +     +    int cnt = 0; +    std::vector<std::string>::iterator i = packets.begin(); +    while(i != packets.end()) { +      size_t packetsize = i->size(); +      const char *packet = i->data(); +      unsigned int ts; + +      printf("unpack sz: %d - %p\n", packetsize, packet); + +      lrtp_unpack(lrtp, packet, packetsize); +      int ret; +      while((ret = lrtp_dequeue_frame(lrtp, frame, sizeof(frame), &csrc, &ts)) +            != 0) { +        size_t pcmsize = 16*1024;//ms[idx] / (48000 / FS); +        short *pcm = new short[pcmsize * channels]; +        printf("pcmsize %d\n", pcmsize); fflush(stdout); +        int res = opus_decode(opus, (const unsigned char*)frame, ret, +                              pcm, pcmsize, 0); + +        printf("Decompressed %d bytes\n", res); +        //      pcmsize = res * channels * sizeof(short); + +        audio.play((char *)pcm, res * channels * sizeof(short)); + +        delete[] pcm; +      } + +      i++; +    } + + +    /* + +    std::vector<std::string>::iterator i = packets.begin(); +    while(i != packets.end()) { +      size_t packetsize = i->size(); +      printf("unpack sz: %d\n", packetsize); +      const char *packet = i->data(); +      unsigned int ts; + +      framesize = sizeof(frame); + +      lrtp_unpack(lrtp, packet, packetsize, frame, &framesize, &csrc, &ts); +      printf("Got %d bytes, csrc %d, ts: %d\n", framesize, csrc, ts); + +      size_t pcmsize = 16*1024;//ms[idx] / (48000 / FS); +      short *pcm = new short[pcmsize * channels]; +      printf("pcmsize %d\n", pcmsize); fflush(stdout); +      int res = opus_decode(opus, (const unsigned char*)frame, framesize, +                            pcm, pcmsize, 0); +      framesize = sizeof(frame); + +      printf("Decompressed %d bytes\n", res); +      //      pcmsize = res * channels * sizeof(short); + +			audio.play((char *)pcm, res * channels * sizeof(short)); + +      delete[] pcm; +      i++; +    } +    */ +    opus_decoder_destroy(opus); +    lrtp_destroy_profile(lrtp, csrc); +    lrtp_close(lrtp); +  } + +  return 0; +} diff --git a/test/test_raw.cc b/test/test_raw.cc new file mode 100644 index 0000000..c3dcb71 --- /dev/null +++ b/test/test_raw.cc @@ -0,0 +1,78 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + *            test_raw.cc + * + *  Mon Sep  2 14:02:16 CEST 2013 + *  Copyright 2013 Bent Bisballe Nyeng + *  deva@aasimon.org + ****************************************************************************/ + +/* + *  This file is part of lrtp. + * + * lrtp is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * lrtp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with lrtp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <lrtp.h> + +#include <stdio.h> + +#define KEY "123456789012345678901234567890123456789012345678901234567890" +#define SSRC 1234567890 + +void dump(const char *title, const char *buf, size_t size) +{ +  printf("%12s: ", title); +  for(int i = 0; i < size; i++) { +    if(i % 8 == 0) printf(" "); +    printf("%02x ", (unsigned char)*buf++); +  } +  printf("\n"); +} + +int main() +{ +  const char frame[] = "foo"; + +  struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); + +  unsigned int csrc = 42; +  struct lrtp_profile_t *profile = +    lrtp_create_profile(lrtp, PROFILE_RAW, csrc, +                        OPTION_RAW_PKG_SIZE, 4, +                        OPTION_END); +  if(profile == NULL) { +    printf("Could not create profile!\n"); +    return 1; +  } + +  char packet[16*1024]; +  size_t size = sizeof(packet); + +  for(unsigned int ts = 0; ts < 8; ts++) { +    int ret = 0; +    ret = lrtp_enqueue_frame(profile, frame, sizeof(frame)); +    while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) != 0) { +      dump("pkg", packet, ret); +    } +  } + +  lrtp_destroy_profile(lrtp, csrc); + +  lrtp_close(lrtp); + +  return 0; +} + diff --git a/test/test_rtp.cc b/test/test_rtp.cc new file mode 100644 index 0000000..b3ef80d --- /dev/null +++ b/test/test_rtp.cc @@ -0,0 +1,112 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + *            test_rtp.cc + * + *  Mon Sep  2 14:02:16 CEST 2013 + *  Copyright 2013 Bent Bisballe Nyeng + *  deva@aasimon.org + ****************************************************************************/ + +/* + *  This file is part of lrtp. + * + * lrtp is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * lrtp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with lrtp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "../src/rtp.h" + +#include <stdio.h> + +void dump(RTP &rtp, const char *title) +{ +  char buf[16 * 1024]; + +  unsigned char *p = (unsigned char *)buf; +  size_t s = rtp.packet(buf, sizeof(buf)); + +  printf("%12s: ", title); +  for(int i = 0; i < s; i++) { +    if(i % 8 == 0) printf(" "); +    printf("%02x ", *p++); +  } +  printf("\n"); +} + +int main() +{ +  RTP rtp; +  dump(rtp, "Clean"); + +  rtp.setMarker(true); +  dump(rtp, "mark"); + +  char payload[] = { 0xff, 0xff, 0xff }; +  rtp.setPayload(payload, sizeof(payload)); +  dump(rtp, "Payload[6*f]"); + +  rtp.setPadding(5); +  dump(rtp, "Padding(5)"); + +  rtp.setPadding(0); +  dump(rtp, "Padding(0)"); + +  rtp.setPadding(3); +  dump(rtp, "Padding(3)"); + +  rtp.setMarker(true); +  dump(rtp, "mark"); + +  rtp.setPayloadType(1); +  dump(rtp, "pt(1)"); + +  rtp.setPayloadType(0x7f); +  dump(rtp, "pt(7f)"); + +  rtp.setPayloadType(2); +  dump(rtp, "pt(2)"); + +  rtp.addCSrc(1); +  dump(rtp, "CSrc[1]"); + +  rtp.removeCSrc(1); +  dump(rtp, "CSrc[-]"); + +  rtp.addCSrc(2); +  rtp.addCSrc(4); +  dump(rtp, "CSrc[2,4]"); + +  rtp.removeCSrc(2); +  dump(rtp, "CSrc[2]"); + +  rtp.setSeq(0x0102); +  dump(rtp, "seq"); + +  rtp.setTimestamp(0x03040506); +  dump(rtp, "ts"); + +  rtp.setSSrc(0x0708090a); +  dump(rtp, "ssrc"); + +  char buf[MAX_RTP_PACKET_SIZE]; +  size_t sz = rtp.packet(buf, sizeof(buf)); + +  RTP rtp2; +  rtp2.fromPacket(buf, sz); + +  dump(rtp2, "fromPacket"); + +  return 0; +} + diff --git a/test/test_srtp.cc b/test/test_srtp.cc new file mode 100644 index 0000000..962c18f --- /dev/null +++ b/test/test_srtp.cc @@ -0,0 +1,87 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + *            test_srtp.cc + * + *  Mon Sep  2 14:02:16 CEST 2013 + *  Copyright 2013 Bent Bisballe Nyeng + *  deva@aasimon.org + ****************************************************************************/ + +/* + *  This file is part of lrtp. + * + * lrtp is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * lrtp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with lrtp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <stdio.h> + +#include "../src/rtp.h" +#include "../src/srtp.h" + +#define KEY "123456789012345678901234567890123456789012345678901234567890" +#define SSRC 1234567890 + +void dump(const char *title, const char *buf, size_t size) +{ +  printf("%12s: ", title); +  for(int i = 0; i < size; i++) { +    if(i % 8 == 0) printf(" "); +    printf("%02x ", (unsigned char)*buf++); +  } +  printf("\n"); +} + +int main() +{ +  RTP rtp; +  rtp.setSSrc(SSRC); + +  char payload[] = { 0xde, 0xad, 0xbe, 0xef }; +  rtp.setPayload(payload, sizeof(payload)); + +  char buf[MAX_RTP_PACKET_SIZE]; +  size_t sz = rtp.packet(buf, sizeof(buf)); + +  dump("Vanilla", buf, sz); + +  { +    SRTP srtp(KEY, rtp.SSrc()); +    sz = srtp.encrypt(buf, sz); +  } + +  dump("Encrypted", buf, sz); + +  { +    SRTP srtp(KEY, rtp.SSrc()); +    sz = srtp.decrypt(buf, sz); +  } + + +  dump("Decrypted", buf, sz); + +  printf("Compare:\n"); +  char buf0[MAX_RTP_PACKET_SIZE]; +  size_t sz0 = rtp.packet(buf0, sizeof(buf0)); +  if(sz0 != sz) printf("Sizes differ (%d %d)...\n", sz0, sz); +  unsigned int sum = 0; +  for(int i = 0; i < sz0; i++) { +    sum += abs(buf0[i] - buf[i]); +  } +  if(sum) printf("NOT EQUAL! diff = %d\n", sum); +  else printf("Vanilla == Decrypted\n"); + +  return 0; +} + | 
