obs-x264: Refactor tokenizing of options
We do a bad job of handling errors in user-supplied x264 options. I want to improve our error handling. To make my job easier, move the code for parsing the x264 options string into its own function. Also, add some tests for the functionality. Aside from a minor tweak to a log message for the opencl option, this commit should not change behavior.
This commit is contained in:
65
plugins/obs-x264/obs-x264-test.c
Normal file
65
plugins/obs-x264/obs-x264-test.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "obs-x264-options.h"
|
||||
|
||||
#define CHECK(condition) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
fprintf(stderr, "%s:%d: error: check failed: %s\n", \
|
||||
__FILE__, __LINE__, #condition); \
|
||||
exit(1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static void test_obs_x264_parse_options()
|
||||
{
|
||||
struct obs_x264_options options;
|
||||
|
||||
options = obs_x264_parse_options(NULL);
|
||||
CHECK(options.count == 0);
|
||||
obs_x264_free_options(options);
|
||||
|
||||
options = obs_x264_parse_options("");
|
||||
CHECK(options.count == 0);
|
||||
obs_x264_free_options(options);
|
||||
|
||||
options = obs_x264_parse_options("ref=3");
|
||||
CHECK(options.count == 1);
|
||||
CHECK(strcmp(options.options[0].name, "ref") == 0);
|
||||
CHECK(strcmp(options.options[0].value, "3") == 0);
|
||||
obs_x264_free_options(options);
|
||||
|
||||
options = obs_x264_parse_options("ref=3 bframes=8");
|
||||
CHECK(options.count == 2);
|
||||
CHECK(strcmp(options.options[0].name, "ref") == 0);
|
||||
CHECK(strcmp(options.options[0].value, "3") == 0);
|
||||
CHECK(strcmp(options.options[1].name, "bframes") == 0);
|
||||
CHECK(strcmp(options.options[1].value, "8") == 0);
|
||||
obs_x264_free_options(options);
|
||||
|
||||
// Invalid options are dropped.
|
||||
options = obs_x264_parse_options(
|
||||
"ref=3 option_with_no_equal_sign bframes=8");
|
||||
CHECK(options.count == 2);
|
||||
CHECK(strcmp(options.options[0].name, "ref") == 0);
|
||||
CHECK(strcmp(options.options[0].value, "3") == 0);
|
||||
CHECK(strcmp(options.options[1].name, "bframes") == 0);
|
||||
CHECK(strcmp(options.options[1].value, "8") == 0);
|
||||
obs_x264_free_options(options);
|
||||
|
||||
// Extra whitespace is ignored between and around options.
|
||||
options = obs_x264_parse_options(" ref=3 bframes=8 ");
|
||||
CHECK(options.count == 2);
|
||||
CHECK(strcmp(options.options[0].name, "ref") == 0);
|
||||
CHECK(strcmp(options.options[0].value, "3") == 0);
|
||||
CHECK(strcmp(options.options[1].name, "bframes") == 0);
|
||||
CHECK(strcmp(options.options[1].value, "8") == 0);
|
||||
obs_x264_free_options(options);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_obs_x264_parse_options();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user