Skip to content

Commit 846827f

Browse files
committed
Getting rid of overcautious asserts on empty RTSP requests, cleaning
macros, tweaking compat quirks (for upcoming V4L2 support on non-ARM or -MIPS platforms)
1 parent b1c7803 commit 846827f

5 files changed

Lines changed: 41 additions & 34 deletions

File tree

src/compat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ float __expf_finite(float x) { return expf(x); }
5555
int __fgetc_unlocked(FILE *stream) { return fgetc(stream); }
5656
double __log_finite(double x) { return log(x); }
5757

58-
#if !defined(__riscv) && !defined(__riscv__)
58+
#if defined(__arm__) || defined(__aarch64__) || defined(__mips__)
5959
void *mmap(void *start, size_t len, int prot, int flags, int fd, uint32_t off) {
6060
return (void*)syscall(SYS_mmap2, start, len, prot, flags, fd, off >> 12);
6161
}

src/hal/macros.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
fprintf(stderr, "\033[0m"); \
5454
} while (0)
5555

56+
#ifndef ABS
57+
#define ABS(x) ((x) < 0 ? -(x) : (x))
58+
#endif
5659
#ifndef ALIGN_BACK
5760
#define ALIGN_BACK(x, a) (((x) / (a)) * (a))
5861
#endif
@@ -74,6 +77,7 @@
7477
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
7578
#endif
7679

80+
#define CLEAR(x) memset(&(x), 0, sizeof(x))
7781
#define CONTAINS(a, b) strstr(a, b)
7882
#define EMPTY(x) (x[0] == '\0')
7983
#define ENDS_WITH(a, b) \

src/rtsp/rtsp.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ static void __method_describe(struct connection_item_t *p, rtsp_handle h)
168168
case 96: strncpy(audioRtpfmt, "MPEG4-GENERIC", 16 - 1); break;
169169
default: strncpy(audioRtpfmt, "UNKNOWN", 16 - 1); break;
170170
}
171-
sprintf(audioRtp,
171+
sprintf(audioRtp,
172172
"\r\n"
173173
"m=audio 0 RTP/AVP %d\r\n"
174174
"a=control:track=1\r\n"
175175
"a=rtpmap:%d %s/90000\r\n",
176176
h->audioPt, h->audioPt, audioRtpfmt, h->audioPt);
177177
}
178178

179-
if (h->isH265 &&
179+
if (h->isH265 &&
180180
h->sprop_vps_b64 && h->sprop_sps_b64 && h->sprop_sps_b16 && h->sprop_pps_b64) {
181181
DASSERT(h->sprop_vps_b64->result, return);
182182
DASSERT(h->sprop_sps_b64->result, return);
@@ -201,7 +201,7 @@ static void __method_describe(struct connection_item_t *p, rtsp_handle h)
201201
h->sprop_sps_b64->result,
202202
h->sprop_pps_b64->result,
203203
audioRtp);
204-
} else if (!h->isH265 &&
204+
} else if (!h->isH265 &&
205205
h->sprop_sps_b64 && h->sprop_sps_b16 && h->sprop_pps_b64) {
206206
DASSERT(h->sprop_sps_b64->result, return);
207207
DASSERT(h->sprop_sps_b16->result, return);
@@ -270,7 +270,7 @@ static void __method_setup(struct connection_item_t *p, rtsp_handle h)
270270

271271
static void __method_pause(struct connection_item_t *p, rtsp_handle h)
272272
{
273-
fprintf(p->fp_tcp_write,
273+
fprintf(p->fp_tcp_write,
274274
"RTSP/1.0 "__RESPONCE_STR_METHODNOTALLOWED "\r\n");
275275
}
276276

@@ -302,8 +302,8 @@ static void __method_play(struct connection_item_t *p, rtsp_handle h)
302302
ASSERT(__bind_rtp(p) == SUCCESS, return);
303303
p->trans[p->track_id].rtp_timestamp = (millis() * 90) & UINT32_MAX;
304304
p->trans[p->track_id].rtp_seq = rand_r(&h->ctx);
305-
p->trans[p->track_id].rtcp_octet = 0;
306-
p->trans[p->track_id].rtcp_packet_cnt = 0;
305+
p->trans[p->track_id].rtcp_octet = 0;
306+
p->trans[p->track_id].rtcp_packet_cnt = 0;
307307
p->trans[p->track_id].rtcp_tick_org = 150; // TODO: must be variant
308308
p->trans[p->track_id].rtcp_tick = p->trans[p->track_id].rtcp_tick_org;
309309

@@ -377,7 +377,7 @@ static int __message_proc_sock(struct list_t *e, void *p)
377377
base64_encode(valid + 6, cred, strlen(cred));
378378
isAuthValid = !strncmp(buf + strlen(__STR_AUTH) + 2, valid, strlen(valid));
379379
} else if (SCMP(__STR_CSEQ, buf)) {
380-
ASSERT(tok = strtok_r(buf, ": ", &last), goto error);
380+
ASSERT(tok = strtok_r(buf, ": ", &last), goto error);
381381
ASSERT(tok = strtok_r(NULL, ": ", &last), goto error);
382382
ASSERT((con->cseq = atoi(tok)) > 0, goto error);
383383
con->parser_state = __PARSER_S_CSEQ;
@@ -392,7 +392,7 @@ static int __message_proc_sock(struct list_t *e, void *p)
392392
} else if (SCMP(__STR_TRANSPORT, buf)) {
393393
for (tok = strtok_r(buf, "; ", &last); tok != NULL; tok = strtok_r(NULL, "; ", &last)) {
394394
if (SCMP(__STR_CLIENTPORT, tok)) {
395-
ASSERT(sscanf(tok, __STR_CLIENTPORT "=%u-%u",
395+
ASSERT(sscanf(tok, __STR_CLIENTPORT "=%u-%u",
396396
&con->trans[con->track_id].client_port_rtp,
397397
&con->trans[con->track_id].client_port_rtcp) > 0,
398398
goto error);
@@ -421,18 +421,21 @@ static int __message_proc_sock(struct list_t *e, void *p)
421421
case __METHOD_PAUSE: __method_pause(con, h); break;
422422
case __METHOD_RECORDING: __method_record(con, h); break;
423423
case __METHOD_TEARDOWN: __method_teardown(con, h); break;
424-
case __METHOD_NONE:
424+
case __METHOD_NONE:
425425
/* state DISCONNECTED connections should be garbage collected immediately.
426426
but sending thread might watches the connection right now.
427427
so the connection might live at here */
428-
ASSERT(con->con_state == __CON_S_DISCONNECTED, return FAILURE);
428+
if (con->con_state != __CON_S_DISCONNECTED) {
429+
ERR("unexpected empty request, forcing disconnect\n");
430+
con->con_state = __CON_S_DISCONNECTED;
431+
}
429432
break;
430433
default: ERR("unexpected method state\n"); return FAILURE;
431434
}
432435
}
433436

434437
fflush(con->fp_tcp_write);
435-
}
438+
}
436439
return SUCCESS;
437440
}
438441

@@ -589,7 +592,7 @@ static inline int __bind_rtp(struct connection_item_t *con )
589592
addr.sin_port = htons(con->trans[con->track_id].server_port_rtp);
590593
addr.sin_addr.s_addr = htonl(INADDR_ANY);
591594
addr.sin_family = AF_INET;
592-
595+
593596
tmp = 1;
594597
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
595598

@@ -741,19 +744,19 @@ static void *rtspThrFxn(void *v)
741744
/* lock while tcp layer is done */
742745
rtsp_lock(rh);
743746

744-
ASSERT(__accept_proc_sock(rh, server_fd, &socks) == SUCCESS,
747+
ASSERT(__accept_proc_sock(rh, server_fd, &socks) == SUCCESS,
745748
({ rtsp_unlock(rh); goto error;}));
746749

747-
ASSERT(list_map_inline(&rh->con_list, __message_proc_sock, &socks) == SUCCESS,
750+
ASSERT(list_map_inline(&rh->con_list, __message_proc_sock, &socks) == SUCCESS,
748751
({ rtsp_unlock(rh); goto error;}));
749752

750-
MUST(list_sweep(&rh->con_list, __connection_is_dead) == SUCCESS,
753+
MUST(list_sweep(&rh->con_list, __connection_is_dead) == SUCCESS,
751754
({ rtsp_unlock(rh); goto error;}));
752755

753756
socks.nfds = max(server_fd, __find_fd_max(&rh->con_list)) + 1;
754757

755758
rtsp_unlock(rh);
756-
}
759+
}
757760
//bufpool_statistics(rh->con_pool);
758761
}
759762

@@ -833,7 +836,7 @@ rtsp_handle rtsp_create(unsigned char max_con, unsigned int port, int priority)
833836

834837
return nh;
835838

836-
error:
839+
error:
837840
rtsp_finish(nh);
838841
return NULL;
839842
}
@@ -859,4 +862,4 @@ int rtsp_tick(rtsp_handle h)
859862
return FAILURE;}));
860863

861864
return __get_timestamp_offset(&h->stat, &tv);
862-
}
865+
}

src/server.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ pthread_mutex_t client_fds_mutex;
5656

5757
static bool is_local_address(const char *client_ip) {
5858
if (!client_ip) return false;
59-
59+
6060
if (!strcmp(client_ip, "127.0.0.1") ||
6161
!strncmp(client_ip, "127.", 4))
6262
return true;
63-
63+
6464
if (!strcmp(client_ip, "::1"))
6565
return true;
66-
66+
6767
if (!strncmp(client_ip, "::ffff:127.", 11))
6868
return true;
69-
69+
7070
return false;
7171
}
7272

@@ -108,7 +108,7 @@ int send_to_client(int i, char *buf, ssize_t size) {
108108
free_client(i);
109109
return EXIT_FAILURE;
110110
}
111-
111+
112112
return EXIT_SUCCESS;
113113
}
114114

@@ -121,22 +121,22 @@ void send_http_error(int fd, int code) {
121121
const char *desc = "\0", *msg = "Unspecified";
122122
char buffer[256];
123123
int len;
124-
124+
125125
for (int i = 0; i < sizeof(http_errors) / sizeof(*http_errors); i++) {
126126
if (http_errors[i].code == code) {
127127
desc = http_errors[i].desc;
128128
msg = http_errors[i].msg;
129129
break;
130130
}
131131
}
132-
132+
133133
len = snprintf(buffer, sizeof(buffer),
134134
"HTTP/1.1 %d %s\r\n"
135135
"Content-Type: text/plain\r\n"
136136
"Connection: close\r\n"
137137
"\r\n%s\r\n",
138138
code, msg, desc);
139-
139+
140140
send_and_close(fd, buffer, len);
141141
}
142142

@@ -195,7 +195,7 @@ void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) {
195195
printf("NAL: %s received in packet %d\n", nal_type_to_str(pack->nalu[j].type), i);
196196
printf(" starts at %p, ends at %p\n", pack_data + pack->nalu[j].offset, pack_data + pack->nalu[j].offset + pack->nalu[j].length);
197197
#endif
198-
if ((pack->nalu[j].type == NalUnitType_SPS || pack->nalu[j].type == NalUnitType_SPS_HEVC)
198+
if ((pack->nalu[j].type == NalUnitType_SPS || pack->nalu[j].type == NalUnitType_SPS_HEVC)
199199
&& pack->nalu[j].length >= 4 && pack->nalu[j].length <= UINT16_MAX)
200200
mp4_set_sps(pack_data + pack->nalu[j].offset + 4, pack->nalu[j].length - 4, isH265);
201201
else if ((pack->nalu[j].type == NalUnitType_PPS || pack->nalu[j].type == NalUnitType_PPS_HEVC)
@@ -1160,7 +1160,7 @@ void respond_request(http_request_t *req) {
11601160
"\r\n"
11611161
"{\"active\":%s,\"manual\":%s,\"grayscale\":%s,\"ircut\":%s,\"ircut_pin1\":%d,\"ircut_pin2\":%d,"
11621162
"\"irled\":%s,\"irled_pin\":%d,\"irsense_pin\":%d,\"adc_device\":\"%s\",\"adc_threshold\":%d}",
1163-
app_config.night_mode_enable ? "true" : "false", night_manual_on() ? "true" : "false",
1163+
app_config.night_mode_enable ? "true" : "false", night_manual_on() ? "true" : "false",
11641164
night_grayscale_on() ? "true" : "false",
11651165
night_ircut_on() ? "true" : "false", app_config.ir_cut_pin1, app_config.ir_cut_pin2,
11661166
night_irled_on() ? "true" : "false", app_config.ir_led_pin, app_config.ir_sensor_pin,
@@ -1192,7 +1192,7 @@ void respond_request(http_request_t *req) {
11921192

11931193
char path[32];
11941194

1195-
if (!memcmp(payloadb, "\x89\x50\x4E\x47\xD\xA\x1A\xA", 8))
1195+
if (!memcmp(payloadb, "\x89\x50\x4E\x47\xD\xA\x1A\xA", 8))
11961196
sprintf(path, "/tmp/osd%d.png", id);
11971197
else
11981198
sprintf(path, "/tmp/osd%d.bmp", id);
@@ -1350,7 +1350,7 @@ void respond_request(http_request_t *req) {
13501350
"{\"recording\":%s,\"start_time\":\"%s\",\"continuous\":%s,\"path\":\"%s\","
13511351
"\"filename\":\"%s\",\"segment_duration\":%d,\"segment_size\":%d}",
13521352
recordOn ? "true" : "false", start_time, app_config.record_continuous ? "true" : "false",
1353-
app_config.record_path, app_config.record_filename,
1353+
app_config.record_path, app_config.record_filename,
13541354
app_config.record_segment_duration, app_config.record_segment_size);
13551355
send_and_close(req->clntFd, response, respLen);
13561356
return;
@@ -1376,7 +1376,7 @@ void respond_request(http_request_t *req) {
13761376
"\r\n"
13771377
"{\"chip\":\"%s\",\"loadavg\":[%.2f,%.2f,%.2f],\"memory\":\"%s\","
13781378
"\"sensor\":\"%s\",\"temp\":\"%.1f\u00B0C\",\"uptime\":\"%s\"}",
1379-
chip, si.loads[0] / 65536.0, si.loads[1] / 65536.0, si.loads[2] / 65536.0,
1379+
chip, si.loads[0] / 65536.0, si.loads[1] / 65536.0, si.loads[2] / 65536.0,
13801380
memory, sensor, hal_temperature_read(), uptime);
13811381
send_and_close(req->clntFd, response, respLen);
13821382
return;
@@ -1497,4 +1497,4 @@ int stop_server() {
14971497
HAL_INFO("server", "Shutting down server...\n");
14981498

14991499
return EXIT_SUCCESS;
1500-
}
1500+
}

src/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ void send_mjpeg_to_client(char index, char *buf, ssize_t size);
3737
void send_h26x_to_client(char index, hal_vidstream *stream);
3838
void send_mp3_to_client(char *buf, ssize_t size);
3939
void send_mp4_to_client(char index, hal_vidstream *stream, char isH265);
40-
void send_pcm_to_client(hal_audframe *frame);
40+
void send_pcm_to_client(hal_audframe *frame);

0 commit comments

Comments
 (0)