From d815fda870e63cd3ad72b937ca73e88f11ad5fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Mon, 12 Aug 2024 08:45:53 -0400 Subject: [PATCH] Trying to fix MP4 audio playback in-browser --- src/media.c | 2 +- src/mp4/moov.c | 8 ++++---- src/mp4/moov.h | 1 + src/mp4/mp4.c | 8 ++++++-- src/mp4/mp4.h | 4 ++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/media.c b/src/media.c index 184cf4b..dc6f7fb 100644 --- a/src/media.c +++ b/src/media.c @@ -490,7 +490,7 @@ int enable_mp4(void) { mp4_set_config(app_config.mp4_width, app_config.mp4_height, app_config.mp4_fps, app_config.audio_enable ? HAL_AUDCODEC_MP3 : HAL_AUDCODEC_UNSPEC, - app_config.audio_bitrate, app_config.audio_srate); + app_config.audio_bitrate, 1, app_config.audio_srate); } if (ret = bind_channel(index, app_config.mp4_fps, 0)) diff --git a/src/mp4/moov.c b/src/mp4/moov.c index 0e824a8..c442315 100644 --- a/src/mp4/moov.c +++ b/src/mp4/moov.c @@ -553,7 +553,7 @@ enum BufError write_btrt(struct BitBuf *ptr, const struct MoovInfo *moov_info) { chk_err; // 4 Buffer size err = put_u32_be(ptr, moov_info->audio_bitrate * 1000); chk_err; // 4 Max bitrate - err = put_u32_be(ptr, moov_info->audio_bitrate * 1000); + err = put_u32_be(ptr, moov_info->audio_bitrate * 1000 - 1); chk_err; // 4 Avg bitrate err = put_u32_be_to_offset(ptr, start_atom, ptr->offset - start_atom); chk_err; @@ -619,7 +619,7 @@ enum BufError write_DecoderConfig(struct BitBuf *ptr, const struct MoovInfo *moo chk_err; // 3 bufferSize err = put_u32_be(ptr, moov_info->audio_bitrate * 1000); chk_err; // 4 Max bitrate - err = put_u32_be(ptr, moov_info->audio_bitrate * 1000); + err = put_u32_be(ptr, moov_info->audio_bitrate * 1000 - 1); chk_err; // 4 Avg bitrate err = put_u32_le_to_offset(ptr, var_len, varint32(ptr->offset - var_len - 4)); chk_err; @@ -675,7 +675,7 @@ enum BufError write_TagAudioSpecificConfig( bitstream |= moov_info->audio_samplerate; used_bits += 24; } - int channels = 1; + int channels = moov_info->audio_channels; bitstream <<= 4; bitstream |= channels; used_bits += 4; @@ -768,7 +768,7 @@ enum BufError write_mp4a(struct BitBuf *ptr, const struct MoovInfo *moov_info) { chk_err; // 1 dataref index err = put_skip(ptr, 8); chk_err; // 8 reserved - err = put_u16_be(ptr, 2); + err = put_u16_be(ptr, moov_info->audio_channels); chk_err; // 2 channel count err = put_u16_be(ptr, 16); chk_err; // 2 sample size diff --git a/src/mp4/moov.h b/src/mp4/moov.h index 71f7900..83cc6eb 100644 --- a/src/mp4/moov.h +++ b/src/mp4/moov.h @@ -5,6 +5,7 @@ struct MoovInfo { char audio_codec; unsigned short audio_bitrate; + unsigned char audio_channels; unsigned int audio_samplerate; char is_h265; uint8_t profile_idc; diff --git a/src/mp4/mp4.c b/src/mp4/mp4.c index 2e8da79..f73bad5 100644 --- a/src/mp4/mp4.c +++ b/src/mp4/mp4.c @@ -8,6 +8,7 @@ uint32_t default_sample_size = 40000; unsigned int aud_samplerate = 0; unsigned short aud_bitrate = 0; +char aud_channels = 0; short vid_width = 1920, vid_height = 1080; char aud_codec = 0, vid_framerate = 30; @@ -35,6 +36,8 @@ enum BufError create_header(char is_h265) { struct MoovInfo moov_info; memset(&moov_info, 0, sizeof(struct MoovInfo)); moov_info.audio_codec = aud_codec; + moov_info.audio_bitrate = aud_bitrate; + moov_info.audio_channels = aud_channels; moov_info.audio_samplerate = aud_samplerate; moov_info.is_h265 = is_h265 & 1; moov_info.profile_idc = 100; @@ -59,13 +62,14 @@ enum BufError create_header(char is_h265) { chk_err return BUF_OK; } -void mp4_set_config(short width, short height, char framerate, - char acodec, unsigned short bitrate, unsigned int srate) { +void mp4_set_config(short width, short height, char framerate, char acodec, + unsigned short bitrate, char channels, unsigned int srate) { vid_width = width; vid_height = height; vid_framerate = framerate; aud_codec = acodec; aud_bitrate = bitrate; + aud_channels = channels; aud_samplerate = srate; } diff --git a/src/mp4/mp4.h b/src/mp4/mp4.h index f6e8d67..53615a8 100644 --- a/src/mp4/mp4.h +++ b/src/mp4/mp4.h @@ -25,8 +25,8 @@ struct Mp4State { uint32_t nals_count; }; -void mp4_set_config(short width, short height, char framerate, - char acodec, unsigned short bitrate, unsigned int srate); +void mp4_set_config(short width, short height, char framerate, char acodec, + unsigned short bitrate, char channels, unsigned int srate); void mp4_set_sps(const char *nal_data, const uint32_t nal_len, char is_h265); void mp4_set_pps(const char *nal_data, const uint32_t nal_len, char is_h265);