MediaProcessors
utests_video_settings.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Rafael Antoniello
3  *
4  * This file is part of MediaProcessors.
5  *
6  * MediaProcessors is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * MediaProcessors is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MediaProcessors. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 #include <UnitTest++/UnitTest++.h>
27 
28 extern "C" {
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <string.h>
35 
36 #include <libcjson/cJSON.h>
37 #include <libmediaprocsutils/log.h>
38 #include <libmediaprocsutils/stat_codes.h>
39 #include <libmediaprocs/proc_if.h>
40 #include "../src/video_settings.h"
41 }
42 
43 SUITE(UTESTS_VIDEO_SETTINGS_CTX)
44 {
45  TEST(UTESTS_VIDEO_SETTINGS_CTX_T)
46  {
47  int ret_code;
48  video_settings_enc_ctx_t *video_settings_enc_ctx= NULL;
49  video_settings_enc_ctx_t *video_settings_enc_ctx2= NULL;
50  std::string settings_cppstr;
51  cJSON *cjson_rest= NULL;
52  char *rest_response_str= NULL;
53 
54  /* Allocate structure */
55  video_settings_enc_ctx= video_settings_enc_ctx_allocate();
56  CHECK(video_settings_enc_ctx!= NULL);
57  if(video_settings_enc_ctx== NULL)
58  goto end;
59  video_settings_enc_ctx2= video_settings_enc_ctx_allocate();
60  CHECK(video_settings_enc_ctx2!= NULL);
61  if(video_settings_enc_ctx2== NULL)
62  goto end;
63 
64  /* Initialize */
65  ret_code= video_settings_enc_ctx_init(video_settings_enc_ctx);
66  CHECK(ret_code== STAT_SUCCESS);
67 
68  /* Copy structure '1' to '2' */
69  ret_code= video_settings_enc_ctx_cpy(video_settings_enc_ctx,
70  video_settings_enc_ctx2);
71  CHECK(ret_code== STAT_SUCCESS);
72  CHECK(video_settings_enc_ctx->bit_rate_output==
73  video_settings_enc_ctx2->bit_rate_output);
74  CHECK(video_settings_enc_ctx->frame_rate_output==
75  video_settings_enc_ctx2->frame_rate_output);
76  CHECK(video_settings_enc_ctx->width_output==
77  video_settings_enc_ctx2->width_output);
78  CHECK(video_settings_enc_ctx->height_output==
79  video_settings_enc_ctx2->height_output);
80  CHECK(video_settings_enc_ctx->gop_size==
81  video_settings_enc_ctx2->gop_size);
82  CHECK(strncmp(video_settings_enc_ctx->conf_preset,
83  video_settings_enc_ctx2->conf_preset, sizeof(
84  video_settings_enc_ctx2->conf_preset))== 0);
85 
86  /* Put some settings via query string.
87  * NOTE: query string passed already omits '?' character at the
88  * beginning.
89  */
90  settings_cppstr= (std::string)"bit_rate_output=1234&"
91  "frame_rate_output=60&width_output=720&height_output=576&"
92  "gop_size=123&conf_preset=ultrafast";
93  ret_code= video_settings_enc_ctx_restful_put(video_settings_enc_ctx,
94  settings_cppstr.c_str(), NULL);
95  CHECK(ret_code== STAT_SUCCESS);
96  CHECK(video_settings_enc_ctx->bit_rate_output== 1234);
97  CHECK(video_settings_enc_ctx->frame_rate_output== 60);
98  CHECK(video_settings_enc_ctx->width_output== 720);
99  CHECK(video_settings_enc_ctx->height_output== 576);
100  CHECK(video_settings_enc_ctx->gop_size== 123);
101  CHECK(strncmp(video_settings_enc_ctx->conf_preset, "ultrafast",
102  sizeof(video_settings_enc_ctx->conf_preset))== 0);
103 
104  /* Put settings via JSON */
105  settings_cppstr= (std::string)"{"
106  "\"bit_rate_output\":4321,"
107  "\"frame_rate_output\":61,"
108  "\"width_output\":1920,"
109  "\"height_output\":1080,"
110  "\"gop_size\":321,"
111  "\"conf_preset\":\"veryfast\""
112  "}";
113  ret_code= video_settings_enc_ctx_restful_put(video_settings_enc_ctx,
114  settings_cppstr.c_str(), NULL);
115  CHECK(ret_code== STAT_SUCCESS);
116  CHECK(video_settings_enc_ctx->bit_rate_output== 4321);
117  CHECK(video_settings_enc_ctx->frame_rate_output== 61);
118  CHECK(video_settings_enc_ctx->width_output== 1920);
119  CHECK(video_settings_enc_ctx->height_output== 1080);
120  CHECK(video_settings_enc_ctx->gop_size== 321);
121  CHECK(strncmp(video_settings_enc_ctx->conf_preset, "veryfast",
122  sizeof(video_settings_enc_ctx->conf_preset))== 0);
123 
124  /* Get RESTful char string */
125  ret_code= video_settings_enc_ctx_restful_get(video_settings_enc_ctx,
126  &cjson_rest, NULL);
127  CHECK(ret_code== STAT_SUCCESS && cjson_rest!= NULL);
128  if(cjson_rest== NULL)
129  goto end;
130 
131  /* Print cJSON structure data to char string */
132  rest_response_str= cJSON_PrintUnformatted(cjson_rest);
133  CHECK(rest_response_str!= NULL && strlen(rest_response_str)> 0);
134  if(rest_response_str== NULL)
135  goto end;
136  CHECK(strcmp(settings_cppstr.c_str(), rest_response_str)== 0);
137 
138 end:
139  if(video_settings_enc_ctx!= NULL)
140  video_settings_enc_ctx_deinit(video_settings_enc_ctx);
141  if(cjson_rest!= NULL)
142  cJSON_Delete(cjson_rest);
143  if(rest_response_str!= NULL)
144  free(rest_response_str);
145  video_settings_enc_ctx_release(&video_settings_enc_ctx);
146  video_settings_enc_ctx_release(&video_settings_enc_ctx2);
147  }
148 }
int video_settings_enc_ctx_init(volatile video_settings_enc_ctx_t *video_settings_enc_ctx)
SUITE(UTESTS_LIVE555_RTSP)
int video_settings_enc_ctx_restful_put(volatile video_settings_enc_ctx_t *video_settings_enc_ctx, const char *str, log_ctx_t *log_ctx)
void video_settings_enc_ctx_deinit(volatile video_settings_enc_ctx_t *video_settings_enc_ctx)
void video_settings_enc_ctx_release(video_settings_enc_ctx_t **ref_video_settings_enc_ctx)
int video_settings_enc_ctx_cpy(const video_settings_enc_ctx_t *video_settings_enc_ctx_src, video_settings_enc_ctx_t *video_settings_enc_ctx_dst)
int video_settings_enc_ctx_restful_get(volatile video_settings_enc_ctx_t *video_settings_enc_ctx, cJSON **ref_cjson_rest, log_ctx_t *log_ctx)
video_settings_enc_ctx_t * video_settings_enc_ctx_allocate()