]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[core] add audio channels limit when opening files, configurable through switch.conf... 268/head
authorDragos Oancea <dragos@signalwire.com>
Mon, 27 Jan 2020 15:39:59 +0000 (15:39 +0000)
committerDragos Oancea <dragos@signalwire.com>
Mon, 27 Jan 2020 23:35:19 +0000 (23:35 +0000)
conf/testing/autoload_configs/switch.conf.xml
conf/vanilla/autoload_configs/switch.conf.xml
src/include/private/switch_core_pvt.h
src/include/switch_core.h
src/switch_core.c
src/switch_core_file.c
tests/unit/switch_core_file.c

index 69783e62c5864691f49fb0028c4765c7ef49d22d..d07c1b2d6e2d2c4b4ea3f5aeb56c554c123b7f27 100644 (file)
 
     <!-- Allow multiple registrations to the same account in the central registration table -->
     <!-- <param name="multiple-registrations" value="true"/> -->
-
+    
+    <!-- <param name="max-audio-channels" value="2"/> -->
+   
   </settings>
 
 </configuration>
index 5b3930e796578cf3a1cfaa74d3e0d56580075118..714b62c54cc496ab88db36d1f1e40bd7fdd788c5 100644 (file)
     <!-- Allow multiple registrations to the same account in the central registration table -->
     <!-- <param name="multiple-registrations" value="true"/> -->
 
+    <!-- <param name="max-audio-channels" value="2"/> -->
+
   </settings>
 
 </configuration>
index 0f46b8471dc872c74a87698a7edf8f6daab1f469..c3478f109e79e3741c10c2ed5ec6387ceb419a09 100644 (file)
@@ -314,6 +314,7 @@ struct switch_runtime {
        int events_use_dispatch;
        uint32_t port_alloc_flags;
        char *event_channel_key_separator;
+       uint32_t max_audio_channels;
 };
 
 extern struct switch_runtime runtime;
index 8255e13c858bc5715f1a419b8c5244957ac587aa..cad1e0cb17f315f5cfaa0b399a91b4427f25ea38 100644 (file)
@@ -2702,6 +2702,7 @@ SWITCH_DECLARE(const char *) switch_core_banner(void);
 SWITCH_DECLARE(switch_bool_t) switch_core_session_in_thread(switch_core_session_t *session);
 SWITCH_DECLARE(uint32_t) switch_default_ptime(const char *name, uint32_t number);
 SWITCH_DECLARE(uint32_t) switch_default_rate(const char *name, uint32_t number);
+SWITCH_DECLARE(uint32_t) switch_core_max_audio_channels(uint32_t limit);
 
 /*!
  \brief Add user registration
index d3150835c01f82583b7e78fb9f58676d87272e4a..e7d478d2b8e0fc011dc4fa9d3fe7fac4cb0f5a84 100644 (file)
@@ -2383,6 +2383,8 @@ static void switch_load_core_config(const char *file)
                                        } else {
                                                switch_clear_flag((&runtime), SCF_EVENT_CHANNEL_LOG_UNDELIVERABLE_JSON);
                                        }
+                               } else if (!strcasecmp(var, "max-audio-channels") && !zstr(val)) {
+                                       switch_core_max_audio_channels(atoi(val));
                                }
                        }
                }
@@ -2638,6 +2640,15 @@ SWITCH_DECLARE(int32_t) switch_core_sessions_peak_fivemin(void)
        return runtime.sessions_peak_fivemin;
 }
 
+SWITCH_DECLARE(uint32_t) switch_core_max_audio_channels(uint32_t limit)
+{
+       if (limit) {
+               runtime.max_audio_channels = limit;
+       }
+
+       return runtime.max_audio_channels;
+}
+
 SWITCH_DECLARE(int32_t) switch_core_session_ctl(switch_session_ctl_t cmd, void *val)
 {
        int *intval = (int *) val;
index fb6f453a43e88fc5544e58a42870ea4d4f4562a8..2ef1523d0c686487d506e0fa7bf496c9536ddb90 100644 (file)
@@ -76,6 +76,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
        char *fp = NULL;
        int to = 0;
        int force_channels = 0;
+       uint32_t core_channel_limit;
 
        if (switch_test_flag(fh, SWITCH_FILE_OPEN)) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Handle already open\n");
@@ -361,6 +362,19 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
                goto fail;
        }
 
+       if (fh->channels > 2) {
+               /* just show a warning for more than 2 channels, no matter if we allow them or not */
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "File [%s] has more than 2 channels: [%u]\n", file_path, fh->channels);
+       }
+
+       core_channel_limit = switch_core_max_audio_channels(0);
+       if (core_channel_limit && fh->channels > core_channel_limit) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "File [%s] has more channels (%u) than limit (%u). Closing.\n", file_path, fh->channels, core_channel_limit);
+               fh->file_interface->file_close(fh);
+               UNPROTECT_INTERFACE(fh->file_interface);
+               switch_goto_status(SWITCH_STATUS_FALSE, fail);
+       }
+
        if (!force_channels && !fh->real_channels) {
                fh->real_channels = fh->channels;
 
index 3515e6787dd842e059b5a00e7e5379622d499e3c..c6aa74a24e2cc6f1330514b945fe72078766c576 100644 (file)
@@ -185,6 +185,45 @@ FST_CORE_BEGIN("./conf")
 
                }
                FST_TEST_END()
+               FST_TEST_BEGIN(test_switch_core_file_open_max_audio_channels)
+               {
+                       switch_file_handle_t fh = { 0 };
+                       switch_status_t status = SWITCH_STATUS_FALSE;
+                       static char filename[] = "/tmp/fs_unit_test.wav";
+                       static char hdr[] = /*simplest wav file, hardcoded 8 channels*/
+                               "\x52\x49\x46\x46"
+                               "\x24\x00\x00\x00"
+                               "\x57\x41\x56\x45"
+                               "\x66\x6d\x74\x20"
+                               "\x10\x00\x00\x00"
+                               "\x01\x00\x08\x00"
+                               "\x44\xac\x00\x00"
+                               "\x88\x58\x01\x00"
+                               "\x02\x00\x10\x00"
+                               "\x64\x61\x74\x61"
+                               "\x00\x00";
+                       FILE *f = NULL;
+
+                       f = fopen(filename, "w"); 
+                       fst_check(f != NULL);
+                       fwrite(hdr, 1, sizeof(hdr) ,f);
+                       fclose(f);
+
+                       switch_core_max_audio_channels(6);
+                       status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
+                       fst_check(status == SWITCH_STATUS_FALSE);
+
+                       switch_core_max_audio_channels(8);
+                       status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
+                       fst_check(status == SWITCH_STATUS_SUCCESS);
+
+                       status = switch_core_file_close(&fh);
+                       fst_check(status == SWITCH_STATUS_SUCCESS);
+
+                       unlink(filename);
+               }
+               FST_TEST_END()
+
        }
        FST_SUITE_END()
 }