diff --git a/src/mod/codecs/mod_openh264/Makefile.am b/src/mod/codecs/mod_openh264/Makefile.am index c541e9563f..c9f6c743d7 100644 --- a/src/mod/codecs/mod_openh264/Makefile.am +++ b/src/mod/codecs/mod_openh264/Makefile.am @@ -8,3 +8,16 @@ mod_openh264_la_SOURCES = mod_openh264.cpp mod_openh264_la_CXXFLAGS = $(AM_CXXFLAGS) -I$(OPENH264_DIR)/include/wels mod_openh264_la_LIBADD = $(switch_builddir)/libfreeswitch.la mod_openh264_la_LDFLAGS = -L$(OPENH264_DIR)/lib/ -lopenh264 -avoid-version -module -no-undefined -shared + +noinst_LTLIBRARIES = libmodopenh264.la + +libmodopenh264_la_SOURCES = $(mod_openh264_la_SOURCES) +libmodopenh264_la_CXXFLAGS = $(AM_CXXFLAGS) -I$(OPENH264_DIR)/include/wels + +noinst_PROGRAMS = test/test_mod_openh264 +test_test_mod_openh264_SOURCES = test/test_mod_openh264.cpp +test_test_mod_openh264_CXXFLAGS = $(AM_CXXFLAGS) -I. +test_test_mod_openh264_LDFLAGS = -L$(OPENH264_DIR)/lib/ -lopenh264 -avoid-version -module +test_test_mod_openh264_LDADD = libmodopenh264.la $(switch_builddir)/libfreeswitch.la + +TESTS = $(noinst_PROGRAMS) diff --git a/src/mod/codecs/mod_openh264/mod_openh264.cpp b/src/mod/codecs/mod_openh264/mod_openh264.cpp index 841e9efcd7..8770a71818 100644 --- a/src/mod/codecs/mod_openh264/mod_openh264.cpp +++ b/src/mod/codecs/mod_openh264/mod_openh264.cpp @@ -243,8 +243,8 @@ static switch_size_t buffer_h264_nalu(h264_codec_context_t *context, switch_fram if (start) { //uint8_t nalu_idc = (nalu_hdr & 0x60) >> 5; nalu_type |= (nalu_idc << 5); - size = switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); - size = switch_buffer_write(buffer, &nalu_type, 1); + switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); + switch_buffer_write(buffer, &nalu_type, 1); context->nalu_28_start = 1; } @@ -273,14 +273,14 @@ static switch_size_t buffer_h264_nalu(h264_codec_context_t *context, switch_fram if (context->got_sps <= 0 && nalu_type == 7) context->got_sps = 1; - size += switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); - size += switch_buffer_write(buffer, (void *)data, nalu_size); + switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); + size = switch_buffer_write(buffer, (void *)data, nalu_size); data += nalu_size; left -= nalu_size; goto again; } } else { - size = switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); + switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); size = switch_buffer_write(buffer, frame->data, frame->datalen); context->nalu_28_start = 0; } diff --git a/src/mod/codecs/mod_openh264/test/conf/freeswitch.xml b/src/mod/codecs/mod_openh264/test/conf/freeswitch.xml new file mode 100644 index 0000000000..b0ba4da969 --- /dev/null +++ b/src/mod/codecs/mod_openh264/test/conf/freeswitch.xml @@ -0,0 +1,38 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
diff --git a/src/mod/codecs/mod_openh264/test/data/case1.packet1.264 b/src/mod/codecs/mod_openh264/test/data/case1.packet1.264 new file mode 100755 index 0000000000..4e076d8d91 --- /dev/null +++ b/src/mod/codecs/mod_openh264/test/data/case1.packet1.264 @@ -0,0 +1 @@ +'B X0{ \ No newline at end of file diff --git a/src/mod/codecs/mod_openh264/test/data/case1.packet2.264 b/src/mod/codecs/mod_openh264/test/data/case1.packet2.264 new file mode 100755 index 0000000000..d244d3b546 --- /dev/null +++ b/src/mod/codecs/mod_openh264/test/data/case1.packet2.264 @@ -0,0 +1 @@ +( \ No newline at end of file diff --git a/src/mod/codecs/mod_openh264/test/data/case1.packet3.264 b/src/mod/codecs/mod_openh264/test/data/case1.packet3.264 new file mode 100755 index 0000000000..b17dd27676 Binary files /dev/null and b/src/mod/codecs/mod_openh264/test/data/case1.packet3.264 differ diff --git a/src/mod/codecs/mod_openh264/test/data/case1.packet4.264 b/src/mod/codecs/mod_openh264/test/data/case1.packet4.264 new file mode 100755 index 0000000000..1b9c51c185 Binary files /dev/null and b/src/mod/codecs/mod_openh264/test/data/case1.packet4.264 differ diff --git a/src/mod/codecs/mod_openh264/test/data/case1.packet5.264 b/src/mod/codecs/mod_openh264/test/data/case1.packet5.264 new file mode 100755 index 0000000000..27264044f6 Binary files /dev/null and b/src/mod/codecs/mod_openh264/test/data/case1.packet5.264 differ diff --git a/src/mod/codecs/mod_openh264/test/data/case2.packet1.264 b/src/mod/codecs/mod_openh264/test/data/case2.packet1.264 new file mode 100755 index 0000000000..4e076d8d91 --- /dev/null +++ b/src/mod/codecs/mod_openh264/test/data/case2.packet1.264 @@ -0,0 +1 @@ +'B X0{ \ No newline at end of file diff --git a/src/mod/codecs/mod_openh264/test/data/case2.packet2.264 b/src/mod/codecs/mod_openh264/test/data/case2.packet2.264 new file mode 100755 index 0000000000..d244d3b546 --- /dev/null +++ b/src/mod/codecs/mod_openh264/test/data/case2.packet2.264 @@ -0,0 +1 @@ +( \ No newline at end of file diff --git a/src/mod/codecs/mod_openh264/test/data/case2.packet3.264 b/src/mod/codecs/mod_openh264/test/data/case2.packet3.264 new file mode 100755 index 0000000000..5157759b7f Binary files /dev/null and b/src/mod/codecs/mod_openh264/test/data/case2.packet3.264 differ diff --git a/src/mod/codecs/mod_openh264/test/data/case2.packet4.264 b/src/mod/codecs/mod_openh264/test/data/case2.packet4.264 new file mode 100755 index 0000000000..27264044f6 Binary files /dev/null and b/src/mod/codecs/mod_openh264/test/data/case2.packet4.264 differ diff --git a/src/mod/codecs/mod_openh264/test/test_mod_openh264.cpp b/src/mod/codecs/mod_openh264/test/test_mod_openh264.cpp new file mode 100644 index 0000000000..729f77be84 --- /dev/null +++ b/src/mod/codecs/mod_openh264/test/test_mod_openh264.cpp @@ -0,0 +1,220 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005-2018, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Seven Du + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pax Cao + * + * mod_openh264_test -- mod_openh264 tests + * + */ + +#include + +#define TEST_SIMU_RTP_MAX_LEN 4200 + +/* Add our command line options. */ +static fctcl_init_t my_cl_options[] = { + {"--disable-hw", /* long_opt */ + NULL, /* short_opt (optional) */ + FCTCL_STORE_TRUE , /* action */ + "disable hardware encoder" /* help */ + }, + + FCTCL_INIT_NULL /* Sentinel */ +}; + +FST_CORE_BEGIN("conf") +{ + fctcl_install(my_cl_options); + + FST_MODULE_BEGIN(mod_openh264, mod_openh264_test) + { + FST_SETUP_BEGIN() + { + fst_requires_module("mod_openh264"); + } + FST_SETUP_END() + + FST_TEST_BEGIN(decoder_testcase) + { + switch_status_t status; + switch_codec_t codec = { 0 }; + switch_codec_settings_t codec_settings = {{ 0 }}; + switch_image_t *img; + uint8_t buf[TEST_SIMU_RTP_MAX_LEN + 12] = {0}; + switch_frame_t frame = { 0 }; + int packets = 0; + switch_status_t decode_status; + FILE *fp = NULL; + long file_size = 0; + switch_bool_t fh_status = SWITCH_FALSE; + int file_count = 1; + switch_set_string(codec_settings.video.config_profile_name, "test-decoder-1"); + + if (!fctcl_is("--disable-hw")) { + codec_settings.video.try_hardware_encoder = 1; + } + + status = switch_core_codec_init(&codec, + "H264", + NULL, + NULL, + 0, + 0, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + &codec_settings, fst_pool); + fst_check(status == SWITCH_STATUS_SUCCESS); + + frame.packet = buf; + frame.packetlen = TEST_SIMU_RTP_MAX_LEN + 12; + frame.data = buf + 12; + frame.datalen = TEST_SIMU_RTP_MAX_LEN; + frame.payload = 96; + frame.m = SWITCH_FALSE; + frame.seq = 0; + frame.timestamp = 375233; + frame.img = (switch_image_t*)NULL; + do { + switch_size_t len = TEST_SIMU_RTP_MAX_LEN; + char file_name[128] = { 0 }; + frame.seq++; + switch_snprintf(file_name, 128, "./data/case1.packet%d.264", file_count); + + fp = fopen(file_name, "rb"); + if (!fp) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "read video file %s failed \n", file_name); + break; + } + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "file %s length: %ld \n", file_name, file_size); + fseek(fp, 0, SEEK_SET); + + frame.datalen = fread(frame.data, sizeof(char), file_size, fp); + fst_check(frame.datalen == file_size); + + if (4 == file_count ) { + frame.m = SWITCH_TRUE; + } + if (5 == file_count) { + frame.m = SWITCH_TRUE; + frame.timestamp = 380633; + } + decode_status = switch_core_codec_decode_video(&codec, &frame); + fst_check(decode_status == SWITCH_STATUS_SUCCESS || decode_status == SWITCH_STATUS_MORE_DATA); + + if (frame.img != NULL) { + // write down the decoded + status = switch_img_write_png(frame.img, (char *)"case1.output.qcif.png"); + fst_check(status == SWITCH_STATUS_SUCCESS); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "write output file done! \n"); + + } + fclose(fp); + file_count++; + } while (decode_status == SWITCH_STATUS_MORE_DATA && file_count < 6); + + switch_core_codec_destroy(&codec); + + file_count = 1; + status = switch_core_codec_init(&codec, + "H264", + NULL, + NULL, + 0, + 0, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + &codec_settings, fst_pool); + fst_check(status == SWITCH_STATUS_SUCCESS); + + frame.packet = buf; + frame.packetlen = TEST_SIMU_RTP_MAX_LEN + 12; + frame.data = buf + 12; + frame.datalen = TEST_SIMU_RTP_MAX_LEN; + frame.payload = 96; + frame.m = SWITCH_FALSE; + frame.seq = 0; + frame.timestamp = 375233; + frame.img = (switch_image_t*)NULL; + do { + switch_size_t len = TEST_SIMU_RTP_MAX_LEN; + char file_name[128] = { 0 }; + frame.seq++; + switch_snprintf(file_name, 128, "./data/case2.packet%d.264", file_count); + + fp = fopen(file_name, "rb"); + if (!fp) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "read video file %s failed \n", file_name); + break; + } + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "file %s length: %ld \n", file_name, file_size); + fseek(fp, 0, SEEK_SET); + + frame.datalen = fread(frame.data, sizeof(char), file_size, fp); + fst_check(frame.datalen == file_size); + + if (3 == file_count) { + frame.m = SWITCH_TRUE; + } + if (4 == file_count) { + frame.m = SWITCH_TRUE; + frame.timestamp = 380633; + } + decode_status = switch_core_codec_decode_video(&codec, &frame); + fst_check(decode_status == SWITCH_STATUS_SUCCESS || decode_status == SWITCH_STATUS_MORE_DATA); + if (frame.img != NULL) { + // write down the decoded + status = switch_img_write_png(frame.img, (char *)"case2.output.qcif.png"); + fst_check(status == SWITCH_STATUS_SUCCESS); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "write output file done! \n"); + + } + fclose(fp); + file_count++; + } while (decode_status == SWITCH_STATUS_MORE_DATA && file_count < 5); + + switch_core_codec_destroy(&codec); + + } + FST_TEST_END() + + + FST_TEARDOWN_BEGIN() + { + const char *err = NULL; + switch_sleep(1000000); + fst_check(switch_loadable_module_unload_module(SWITCH_GLOBAL_dirs.mod_dir, (char *)"mod_openh264", SWITCH_TRUE, &err) == SWITCH_STATUS_SUCCESS); + } + FST_TEARDOWN_END() + } + FST_MODULE_END() +} +FST_CORE_END()