Skip to content

Commit 5709771

Browse files
committed
Add an audio configuration API
1 parent 5281abc commit 5709771

File tree

2 files changed

+61
-41
lines changed

2 files changed

+61
-41
lines changed

src/media.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ int bind_channel(char index, char framerate, char jpeg);
3131
int unbind_channel(char index, char jpeg);
3232
int disable_video(char index, char jpeg);
3333

34-
int disable_mp4(void);
35-
int enable_mp4(void);
34+
void disable_audio(void);
35+
int enable_audio(void);
3636
int disable_mjpeg(void);
37-
int enable_mjpeg(void);
37+
int enable_mjpeg(void);
38+
int disable_mp4(void);
39+
int enable_mp4(void);

src/server.c

+56-38
Original file line numberDiff line numberDiff line change
@@ -410,29 +410,6 @@ int send_video_html(const int client_fd) {
410410
return 1;
411411
}
412412

413-
int send_image_html(const int client_fd) {
414-
char html[] = "<html>\n"
415-
" <head>\n"
416-
" <title>Snapshot</title>\n"
417-
" </head>\n"
418-
" <body>\n"
419-
" <center>\n"
420-
" <img src=\"image.jpg\"/>\n"
421-
" </center>\n"
422-
" </body>\n"
423-
"</html>";
424-
char buf[1024];
425-
int buf_len = sprintf(
426-
buf,
427-
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: "
428-
"%lu\r\nConnection: close\r\n\r\n%s",
429-
strlen(html), html);
430-
buf[buf_len++] = 0;
431-
send_to_fd(client_fd, buf, buf_len);
432-
close_socket_fd(client_fd);
433-
return 1;
434-
}
435-
436413
#define REQSIZE 512 * 1024
437414
char response[256];
438415
char *method, *payload, *prot, *request, *query, *uri;
@@ -616,16 +593,12 @@ void *server_thread(void *vargp) {
616593
break;
617594
}
618595

619-
if (equals(uri, "/image.html") &&
620-
app_config.jpeg_enable) {
621-
send_image_html(client_fd);
622-
continue;
623-
}
624596
if (equals(uri, "/mjpeg.html") &&
625597
app_config.mjpeg_enable) {
626598
send_mjpeg_html(client_fd);
627599
continue;
628600
}
601+
629602
if (equals(uri, "/video.html") &&
630603
app_config.mp4_enable) {
631604
send_video_html(client_fd);
@@ -768,19 +741,64 @@ void *server_thread(void *vargp) {
768741
size_t stacksize;
769742
pthread_attr_getstacksize(&thread_attr, &stacksize);
770743
size_t new_stacksize = 16 * 1024;
771-
if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) {
772-
printf("[jpeg] Can't set stack size %ld\n", new_stacksize);
773-
}
744+
if (pthread_attr_setstacksize(&thread_attr, new_stacksize))
745+
HAL_DANGER("jpeg", "Can't set stack size %zu\n", new_stacksize);
774746
pthread_create(
775747
&thread_id, &thread_attr, send_jpeg_thread, (void *)&task);
776-
if (pthread_attr_setstacksize(&thread_attr, stacksize)) {
777-
printf("[jpeg] Can't set stack size %ld\n", stacksize);
778-
}
748+
if (pthread_attr_setstacksize(&thread_attr, stacksize))
749+
HAL_DANGER("jpeg", "Can't set stack size %zu\n", stacksize);
779750
pthread_attr_destroy(&thread_attr);
780751
}
781752
continue;
782753
}
783754

755+
if (app_config.audio_enable && equals(uri, "/api/audio")) {
756+
int respLen;
757+
if (equals(method, "GET")) {
758+
if (!empty(query)) {
759+
char *remain;
760+
while (query) {
761+
char *value = split(&query, "&");
762+
if (!value || !*value) continue;
763+
unescape_uri(value);
764+
char *key = split(&value, "=");
765+
if (!key || !*key || !value || !*value) continue;
766+
if (equals(key, "bitrate")) {
767+
short result = strtol(value, &remain, 10);
768+
if (remain != value)
769+
app_config.audio_bitrate = result;
770+
} else if (equals(key, "srate")) {
771+
short result = strtol(value, &remain, 10);
772+
if (remain != value)
773+
app_config.audio_srate = result;
774+
}
775+
}
776+
}
777+
778+
disable_audio();
779+
enable_audio();
780+
781+
respLen = sprintf(response,
782+
"HTTP/1.1 200 OK\r\n" \
783+
"Content-Type: application/json;charset=UTF-8\r\n" \
784+
"Connection: close\r\n" \
785+
"\r\n" \
786+
"{\"bitrate\":%d,\"srate\":%d}",
787+
app_config.audio_bitrate, app_config.audio_srate);
788+
} else {
789+
respLen = sprintf(response,
790+
"HTTP/1.1 400 Bad Request\r\n" \
791+
"Content-Type: text/plain\r\n" \
792+
"Connection: close\r\n" \
793+
"\r\n" \
794+
"The server has no handler to the request.\r\n" \
795+
);
796+
}
797+
send_to_fd(client_fd, response, respLen);
798+
close_socket_fd(client_fd);
799+
continue;
800+
}
801+
784802
if (app_config.jpeg_enable && equals(uri, "/api/jpeg")) {
785803
int respLen;
786804
if (equals(method, "GET")) {
@@ -925,9 +943,9 @@ void *server_thread(void *vargp) {
925943
if (remain != value)
926944
app_config.mp4_bitrate = result;
927945
} else if (equals(key, "h265")) {
928-
if (equals(value, "true") || equals(value, "1"))
946+
if (equals_case(value, "true") || equals(value, "1"))
929947
app_config.mp4_codecH265 = 1;
930-
else if (equals(value, "false") || equals(value, "0"))
948+
else if (equals_case(value, "false") || equals(value, "0"))
931949
app_config.mp4_codecH265 = 0;
932950
} else if (equals(key, "mode")) {
933951
if (equals_case(value, "CBR"))
@@ -1005,9 +1023,9 @@ void *server_thread(void *vargp) {
10051023
char *key = split(&value, "=");
10061024
if (!key || !*key || !value || !*value) continue;
10071025
if (equals(key, "active")) {
1008-
if (equals_case(value, "true") || equals_case(value, "1"))
1026+
if (equals_case(value, "true") || equals(value, "1"))
10091027
set_night_mode(1);
1010-
else if (equals_case(value, "false") || equals_case(value, "0"))
1028+
else if (equals_case(value, "false") || equals(value, "0"))
10111029
set_night_mode(0);
10121030
}
10131031
}

0 commit comments

Comments
 (0)