]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[mod_openh264] Fix stap-a size issue and add unit test. 222/head
authorpaxc <pax.cao@icloud.com>
Sun, 12 Jan 2020 09:44:48 +0000 (13:44 +0400)
committerAndrey Volk <andywolk@gmail.com>
Fri, 15 May 2020 16:35:58 +0000 (20:35 +0400)
13 files changed:
src/mod/codecs/mod_openh264/Makefile.am
src/mod/codecs/mod_openh264/mod_openh264.cpp
src/mod/codecs/mod_openh264/test/conf/freeswitch.xml [new file with mode: 0644]
src/mod/codecs/mod_openh264/test/data/case1.packet1.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case1.packet2.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case1.packet3.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case1.packet4.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case1.packet5.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case2.packet1.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case2.packet2.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case2.packet3.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/data/case2.packet4.264 [new file with mode: 0755]
src/mod/codecs/mod_openh264/test/test_mod_openh264.cpp [new file with mode: 0644]

index c541e9563f518ebaac531bbb1281b77f63048688..c9f6c743d71ccf054c6b54448ae8615418a2222d 100644 (file)
@@ -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)
index 841e9efcd750c83b479d77f0631eb90b8728edc3..8770a718182a2ac41db6e052846a0310cbec4739 100644 (file)
@@ -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 (file)
index 0000000..b0ba4da
--- /dev/null
@@ -0,0 +1,38 @@
+<?xml version="1.0"?>
+<document type="freeswitch/xml">
+
+  <section name="configuration" description="Various Configuration">
+    <configuration name="modules.conf" description="Modules">
+      <modules>
+        <load module="mod_console"/>
+      </modules>
+    </configuration>
+
+    <configuration name="console.conf" description="Console Logger">
+      <mappings>
+        <map name="all" value="console,debug,info,notice,warning,err,crit,alert"/>
+      </mappings>
+      <settings>
+        <param name="colorize" value="true"/>
+        <param name="loglevel" value="debug"/>
+      </settings>
+    </configuration>
+
+    <configuration name="timezones.conf" description="Timezones">
+      <timezones>
+          <zone name="GMT" value="GMT0" />
+      </timezones>
+    </configuration>
+
+  </section>
+
+  <section name="dialplan" description="Regex/XML Dialplan">
+    <context name="default">
+      <extension name="sample">
+        <condition>
+          <action application="info"/>
+        </condition>
+      </extension>
+    </context>
+  </section>
+</document>
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 (executable)
index 0000000..4e076d8
--- /dev/null
@@ -0,0 +1 @@
+'Bð\v©\18X\9d\80µ\ 2\ 2\ 20­{ß\ 1
\ 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 (executable)
index 0000000..d244d3b
--- /dev/null
@@ -0,0 +1 @@
+(Þ     \88
\ 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 (executable)
index 0000000..b17dd27
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 (executable)
index 0000000..1b9c51c
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 (executable)
index 0000000..2726404
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 (executable)
index 0000000..4e076d8
--- /dev/null
@@ -0,0 +1 @@
+'Bð\v©\18X\9d\80µ\ 2\ 2\ 20­{ß\ 1
\ 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 (executable)
index 0000000..d244d3b
--- /dev/null
@@ -0,0 +1 @@
+(Þ     \88
\ 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 (executable)
index 0000000..5157759
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 (executable)
index 0000000..2726404
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 (file)
index 0000000..729f77b
--- /dev/null
@@ -0,0 +1,220 @@
+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2018, Anthony Minessale II <anthm@freeswitch.org>
+ *
+ * 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 <dujinfang@gmail.com>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Pax Cao <yiping.cao@msn.com>
+ *
+ * mod_openh264_test -- mod_openh264 tests
+ *
+ */
+
+#include <test/switch_test.h>
+
+#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()