Skip to content

Commit

Permalink
Adding an HTTP endpoint to provide the PCM audio
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Jun 25, 2024
1 parent 72d412b commit 93d9806
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/media.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ int save_audio_stream(hal_audframe *frame) {
printf("[audio] data:%p - %02x %02x %02x %02x %02x %02x %02x %02x\n",
frame->data, frame->data[0][0], frame->data[0][1], frame->data[0][2], frame->data[0][3],
frame->data[0][4], frame->data[0][5], frame->data[0][6], frame->data[0][7]);
printf(" len:%d\n", frame->length);
printf(" len:%d\n", frame->length[0]);
printf(" seq:%d\n", frame->seq);
printf(" ts:%d\n", frame->timestamp);
#endif

send_pcm_to_client(frame);

return ret;
}

Expand Down
45 changes: 44 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

char keepRunning = 1;

enum StreamType { STREAM_H26X, STREAM_JPEG, STREAM_MJPEG, STREAM_MP4 };
enum StreamType {
STREAM_H26X,
STREAM_JPEG,
STREAM_MJPEG,
STREAM_MP4,
STREAM_PCM
};

struct Client {
int socket_fd;
Expand Down Expand Up @@ -189,6 +195,26 @@ void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) {
}
}

void send_pcm_to_client(hal_audframe *frame) {
pthread_mutex_lock(&client_fds_mutex);
for (unsigned int i = 0; i < MAX_CLIENTS; ++i) {
if (client_fds[i].socket_fd < 0)
continue;
if (client_fds[i].type != STREAM_PCM)
continue;

static char len_buf[50];
ssize_t len_size = sprintf(len_buf, "%zX\r\n", frame->length[0]);
if (send_to_client(i, len_buf, len_size) < 0)
continue; // send <SIZE>\r\n
if (send_to_client(i, frame->data[0], frame->length[0]) < 0)
continue; // send <DATA>
if (send_to_client(i, "\r\n", 2) < 0)
continue; // send \r\n
}
pthread_mutex_unlock(&client_fds_mutex);
}

void send_mjpeg(char index, char *buf, ssize_t size) {
static char prefix_buf[128];
ssize_t prefix_size = sprintf(
Expand Down Expand Up @@ -588,6 +614,23 @@ void *server_thread(void *vargp) {
continue;
}

if (app_config.audio_enable && equals(uri, "/audio.pcm")) {
int respLen = sprintf(
response, "HTTP/1.1 200 OK\r\nContent-Type: "
"audio/pcm\r\nTransfer-Encoding: "
"chunked\r\nConnection: keep-alive\r\n\r\n");
send_to_fd(client_fd, response, respLen);
pthread_mutex_lock(&client_fds_mutex);
for (uint32_t i = 0; i < MAX_CLIENTS; ++i)
if (client_fds[i].socket_fd < 0) {
client_fds[i].socket_fd = client_fd;
client_fds[i].type = STREAM_PCM;
break;
}
pthread_mutex_unlock(&client_fds_mutex);
continue;
}

if ((!app_config.mp4_codecH265 && equals(uri, "/video.264")) ||
(app_config.mp4_codecH265 && equals(uri, "/video.265"))) {
request_idr();
Expand Down
3 changes: 2 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ int stop_server();
void send_jpeg(char index, char *buf, ssize_t size);
void send_mjpeg(char index, char *buf, ssize_t size);
void send_h26x_to_client(char index, hal_vidstream *stream);
void send_mp4_to_client(char index, hal_vidstream *stream, char isH265);
void send_mp4_to_client(char index, hal_vidstream *stream, char isH265);
void send_pcm_to_client(hal_audframe *frame);

0 comments on commit 93d9806

Please sign in to comment.