Skip to content

Commit

Permalink
Completing the MP4 audio track, manipulating MP4s in a thread-safe ma…
Browse files Browse the repository at this point in the history
…nner
  • Loading branch information
wberube committed Jul 8, 2024
1 parent 32ebb64 commit 74d7861
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/media.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "jpeg.h"
#include "server.h"

pthread_mutex_t mutex;
pthread_mutex_t chnMutex, mp4Mutex;
pthread_t audPid = 0;
pthread_t ispPid = 0;
pthread_t vidPid = 0;
Expand Down Expand Up @@ -48,7 +48,10 @@ int save_audio_stream(hal_audframe *frame) {
mp3Buf = shine_encode_buffer_interleaved(mp3Enc, pcmSrc, &ret);

send_mp3_to_client(mp3Buf, ret);

pthread_mutex_lock(&mp4Mutex);
mp4_ingest_audio(mp3Buf, ret);
pthread_mutex_unlock(&mp4Mutex);

pcmLen -= (pcmSamp - pcmPos);
pcmPos = 0;
Expand All @@ -68,7 +71,10 @@ int save_video_stream(char index, hal_vidstream *stream) {
case HAL_VIDCODEC_H264:
case HAL_VIDCODEC_H265:
if (app_config.mp4_enable) {
pthread_mutex_lock(&mp4Mutex);
send_mp4_to_client(index, stream, isH265);
pthread_mutex_unlock(&mp4Mutex);

send_h26x_to_client(index, stream);
}
if (app_config.rtsp_enable) {
Expand Down Expand Up @@ -128,7 +134,7 @@ int save_video_stream(char index, hal_vidstream *stream) {

void request_idr(void) {
signed char index = -1;
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&chnMutex);
for (int i = 0; i < chnCount; i++) {
if (!chnState[i].enable) continue;
if (chnState[i].payload != HAL_VIDCODEC_H264 &&
Expand All @@ -148,11 +154,11 @@ void request_idr(void) {
case HAL_PLATFORM_T31: t31_video_request_idr(index); break;
#endif
}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&chnMutex);
}

void set_grayscale(bool active) {
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&chnMutex);
switch (plat) {
#if defined(__arm__)
case HAL_PLATFORM_I6: i6_channel_grayscale(active); break;
Expand All @@ -164,20 +170,20 @@ void set_grayscale(bool active) {
case HAL_PLATFORM_T31: t31_channel_grayscale(active); break;
#endif
}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&chnMutex);
}

int take_next_free_channel(bool mainLoop) {
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&chnMutex);
for (int i = 0; i < chnCount; i++) {
if (!chnState[i].enable) {
chnState[i].enable = true;
chnState[i].mainLoop = mainLoop;
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&chnMutex);
return i;
}
}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&chnMutex);
return -1;
}

Expand Down
6 changes: 4 additions & 2 deletions src/mp4/mp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ enum BufError mp4_set_slice(const char *nal_data, const uint32_t nal_len,
buf_moof.offset = 0;
err = write_moof(
&buf_moof, 0, 0, 0, default_sample_size, samples_info,
samples_info_len, samples_info + 1, buf_aud.offset ? 1 : 0);
samples_info_len, samples_info + 1, buf_aud.offset >= 2304 ? 1 : 0);
chk_err;

buf_mdat.offset = 0;
err = write_mdat(&buf_mdat, nal_data, nal_len, buf_aud.buf, buf_aud.offset);
err = write_mdat(&buf_mdat, nal_data, nal_len,
buf_aud.buf, buf_aud.offset >= 2304 ? buf_aud.offset : 0);
chk_err;

buf_aud.offset = 0;

return BUF_OK;
Expand Down

0 comments on commit 74d7861

Please sign in to comment.