Skip to content

Commit 13a5fe7

Browse files
committed
[dev] added mp3 support for HLS.
1 parent 01a825c commit 13a5fe7

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

hls/ngx_rtmp_hls_module.c

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,8 @@ ngx_rtmp_hls_audio(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
16951695
ngx_buf_t *b;
16961696
u_char *p;
16971697
ngx_uint_t objtype, srindex, chconf, size;
1698+
ngx_uint_t aud_codec_id, samples;
1699+
ngx_chain_t *cl;
16981700

16991701
hacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_hls_module);
17001702

@@ -1708,8 +1710,16 @@ ngx_rtmp_hls_audio(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
17081710
return NGX_OK;
17091711
}
17101712

1711-
if (codec_ctx->audio_codec_id != NGX_RTMP_AUDIO_AAC ||
1712-
codec_ctx->aac_header == NULL || ngx_rtmp_is_codec_header(in))
1713+
aud_codec_id = codec_ctx->audio_codec_id;
1714+
1715+
if (aud_codec_id != NGX_RTMP_AUDIO_AAC &&
1716+
aud_codec_id != NGX_RTMP_AUDIO_MP3)
1717+
{
1718+
return NGX_OK;
1719+
}
1720+
1721+
if ((aud_codec_id == NGX_RTMP_AUDIO_AAC &&
1722+
codec_ctx->aac_header == NULL) || ngx_rtmp_is_codec_header(in))
17131723
{
17141724
return NGX_OK;
17151725
}
@@ -1734,7 +1744,7 @@ ngx_rtmp_hls_audio(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
17341744
b->pos = b->last = b->start;
17351745
}
17361746

1737-
size = h->mlen - 2 + 7;
1747+
size = aud_codec_id == NGX_RTMP_AUDIO_AAC ? h->mlen - 2 + 7 : h->mlen - 1;
17381748
pts = (uint64_t) h->timestamp * 90;
17391749

17401750
if (b->start + size > b->end) {
@@ -1758,14 +1768,20 @@ ngx_rtmp_hls_audio(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
17581768
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
17591769
"hls: audio pts=%uL", pts);
17601770

1761-
if (b->last + 7 > b->end) {
1762-
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
1763-
"hls: not enough buffer for audio header");
1764-
return NGX_OK;
1765-
}
1766-
1771+
cl = in;
17671772
p = b->last;
1768-
b->last += 5;
1773+
1774+
if (aud_codec_id == NGX_RTMP_AUDIO_AAC) {
1775+
if (b->last + 7 > b->end) {
1776+
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
1777+
"hls: not enough buffer for audio header");
1778+
return NGX_OK;
1779+
}
1780+
1781+
b->last += 5;
1782+
} else {
1783+
in->buf->pos += 1;
1784+
}
17691785

17701786
/* copy payload */
17711787

@@ -1779,30 +1795,36 @@ ngx_rtmp_hls_audio(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
17791795
b->last = ngx_cpymem(b->last, in->buf->pos, bsize);
17801796
}
17811797

1798+
if (aud_codec_id == NGX_RTMP_AUDIO_MP3) {
1799+
cl->buf->pos -= 1;
1800+
}
1801+
17821802
/* make up ADTS header */
17831803

1784-
if (ngx_rtmp_hls_parse_aac_header(s, &objtype, &srindex, &chconf)
1785-
!= NGX_OK)
1786-
{
1787-
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1788-
"hls: aac header error");
1789-
return NGX_OK;
1790-
}
1804+
if (aud_codec_id == NGX_RTMP_AUDIO_AAC) {
1805+
if (ngx_rtmp_hls_parse_aac_header(s, &objtype, &srindex, &chconf)
1806+
!= NGX_OK)
1807+
{
1808+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1809+
"hls: aac header error");
1810+
return NGX_OK;
1811+
}
17911812

1792-
/* we have 5 free bytes + 2 bytes of RTMP frame header */
1813+
/* we have 5 free bytes + 2 bytes of RTMP frame header */
17931814

1794-
p[0] = 0xff;
1795-
p[1] = 0xf1;
1796-
p[2] = (u_char) (((objtype - 1) << 6) | (srindex << 2) |
1797-
((chconf & 0x04) >> 2));
1798-
p[3] = (u_char) (((chconf & 0x03) << 6) | ((size >> 11) & 0x03));
1799-
p[4] = (u_char) (size >> 3);
1800-
p[5] = (u_char) ((size << 5) | 0x1f);
1801-
p[6] = 0xfc;
1815+
p[0] = 0xff;
1816+
p[1] = 0xf1;
1817+
p[2] = (u_char) (((objtype - 1) << 6) | (srindex << 2) |
1818+
((chconf & 0x04) >> 2));
1819+
p[3] = (u_char) (((chconf & 0x03) << 6) | ((size >> 11) & 0x03));
1820+
p[4] = (u_char) (size >> 3);
1821+
p[5] = (u_char) ((size << 5) | 0x1f);
1822+
p[6] = 0xfc;
18021823

1803-
if (p != b->start) {
1804-
ctx->aframe_num++;
1805-
return NGX_OK;
1824+
if (p != b->start) {
1825+
ctx->aframe_num++;
1826+
return NGX_OK;
1827+
}
18061828
}
18071829

18081830
ctx->aframe_pts = pts;
@@ -1816,7 +1838,8 @@ ngx_rtmp_hls_audio(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
18161838
/* TODO: We assume here AAC frame size is 1024
18171839
* Need to handle AAC frames with frame size of 960 */
18181840

1819-
est_pts = ctx->aframe_base + ctx->aframe_num * 90000 * 1024 /
1841+
samples = aud_codec_id == NGX_RTMP_AUDIO_AAC ? 1024 : 1152;
1842+
est_pts = ctx->aframe_base + ctx->aframe_num * 90000 * samples /
18201843
codec_ctx->sample_rate;
18211844
dpts = (int64_t) (est_pts - pts);
18221845

hls/ngx_rtmp_mpegts.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ static u_char ngx_rtmp_mpegts_aac_header[] = {
8181
};
8282

8383

84+
static u_char ngx_rtmp_mpegts_mp3_header[] = {
85+
0x03, 0xe1, 0x01, 0xf0, 0x00
86+
};
87+
88+
8489
#define NGX_RTMP_MPEGTS_PMT_CRC_START_OFFSET 193
8590
#define NGX_RTMP_MPEGTS_PMT_SECTION_LENGTH_OFFSET 195
8691
#define NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET 205
@@ -202,8 +207,13 @@ ngx_rtmp_mpegts_write_header(ngx_rtmp_mpegts_file_t *file,
202207

203208
if (codec_ctx->audio_codec_id) {
204209
/* audio info */
205-
ngx_memcpy(buf + NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes,
206-
ngx_rtmp_mpegts_aac_header, NGX_RTMP_MPEGTS_STREAM_BYTES);
210+
if (codec_ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC) {
211+
ngx_memcpy(buf + NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes,
212+
ngx_rtmp_mpegts_aac_header, NGX_RTMP_MPEGTS_STREAM_BYTES);
213+
} else {
214+
ngx_memcpy(buf + NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes,
215+
ngx_rtmp_mpegts_mp3_header, NGX_RTMP_MPEGTS_STREAM_BYTES);
216+
}
207217

208218
stream_bytes += NGX_RTMP_MPEGTS_STREAM_BYTES;
209219
}

0 commit comments

Comments
 (0)