Skip to content

Commit af5703b

Browse files
committed
implemented mp4 track selector
1 parent 9c88e12 commit af5703b

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

ngx_rtmp_flv_module.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ static ngx_int_t ngx_rtmp_flv_postconfiguration(ngx_conf_t *cf);
1212
static void ngx_rtmp_flv_read_meta(ngx_rtmp_session_t *s, ngx_file_t *f);
1313
static ngx_int_t ngx_rtmp_flv_timestamp_to_offset(ngx_rtmp_session_t *s,
1414
ngx_file_t *f, ngx_int_t timestamp);
15-
static ngx_int_t ngx_rtmp_flv_init(ngx_rtmp_session_t *s, ngx_file_t *f);
15+
static ngx_int_t ngx_rtmp_flv_init(ngx_rtmp_session_t *s, ngx_file_t *f,
16+
ngx_int_t aindex, ngx_int_t vindex);
1617
static ngx_int_t ngx_rtmp_flv_start(ngx_rtmp_session_t *s, ngx_file_t *f);
1718
static ngx_int_t ngx_rtmp_flv_seek(ngx_rtmp_session_t *s, ngx_file_t *f,
1819
ngx_uint_t offset);
@@ -548,7 +549,8 @@ ngx_rtmp_flv_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
548549

549550

550551
static ngx_int_t
551-
ngx_rtmp_flv_init(ngx_rtmp_session_t *s, ngx_file_t *f)
552+
ngx_rtmp_flv_init(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_int_t aindex,
553+
ngx_int_t vindex)
552554
{
553555
ngx_rtmp_flv_ctx_t *ctx;
554556

ngx_rtmp_mp4_module.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010

1111
static ngx_int_t ngx_rtmp_mp4_postconfiguration(ngx_conf_t *cf);
12-
static ngx_int_t ngx_rtmp_mp4_init(ngx_rtmp_session_t *s, ngx_file_t *f);
12+
static ngx_int_t ngx_rtmp_mp4_init(ngx_rtmp_session_t *s, ngx_file_t *f,
13+
ngx_int_t aindex, ngx_int_t vindex);
1314
static ngx_int_t ngx_rtmp_mp4_done(ngx_rtmp_session_t *s, ngx_file_t *f);
1415
static ngx_int_t ngx_rtmp_mp4_start(ngx_rtmp_session_t *s, ngx_file_t *f);
1516
static ngx_int_t ngx_rtmp_mp4_seek(ngx_rtmp_session_t *s, ngx_file_t *f,
@@ -173,6 +174,9 @@ typedef struct {
173174
ngx_uint_t sample_size;
174175
ngx_uint_t sample_rate;
175176

177+
ngx_int_t atracks, vtracks;
178+
ngx_int_t aindex, vindex;
179+
176180
uint32_t start_timestamp, epoch;
177181
} ngx_rtmp_mp4_ctx_t;
178182

@@ -368,6 +372,26 @@ ngx_rtmp_mp4_parse_trak(ngx_rtmp_session_t *s, u_char *pos, u_char *last)
368372
{
369373
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
370374
"mp4: adding track %ui", ctx->ntracks);
375+
376+
if (ctx->track->type == NGX_RTMP_MSG_AUDIO) {
377+
if (ctx->atracks++ != ctx->aindex) {
378+
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
379+
"mp4: skipping audio track %ui!=%ui",
380+
ctx->atracks - 1, ctx->aindex);
381+
ctx->track = NULL;
382+
return NGX_OK;
383+
}
384+
385+
} else {
386+
if (ctx->vtracks++ != ctx->vindex) {
387+
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
388+
"mp4: skipping video track %i!=%i",
389+
ctx->vtracks - 1, ctx->vindex);
390+
ctx->track = NULL;
391+
return NGX_OK;
392+
}
393+
}
394+
371395
++ctx->ntracks;
372396

373397
} else {
@@ -2168,7 +2192,8 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
21682192

21692193

21702194
static ngx_int_t
2171-
ngx_rtmp_mp4_init(ngx_rtmp_session_t *s, ngx_file_t *f)
2195+
ngx_rtmp_mp4_init(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_int_t aindex,
2196+
ngx_int_t vindex)
21722197
{
21732198
ngx_rtmp_mp4_ctx_t *ctx;
21742199
uint32_t hdr[2];
@@ -2189,6 +2214,9 @@ ngx_rtmp_mp4_init(ngx_rtmp_session_t *s, ngx_file_t *f)
21892214

21902215
ngx_memzero(ctx, sizeof(*ctx));
21912216

2217+
ctx->aindex = aindex;
2218+
ctx->vindex = vindex;
2219+
21922220
offset = 0;
21932221
size = 0;
21942222

ngx_rtmp_play_module.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ ngx_rtmp_play_do_init(ngx_rtmp_session_t *s)
208208
}
209209

210210
if (ctx->fmt && ctx->fmt->init &&
211-
ctx->fmt->init(s, &ctx->file) != NGX_OK)
211+
ctx->fmt->init(s, &ctx->file, ctx->aindex, ctx->vindex) != NGX_OK)
212212
{
213213
return NGX_ERROR;
214214
}
@@ -433,6 +433,33 @@ ngx_rtmp_play_pause(ngx_rtmp_session_t *s, ngx_rtmp_pause_t *v)
433433
}
434434

435435

436+
static ngx_int_t
437+
ngx_rtmp_play_parse_index(char type, u_char *args)
438+
{
439+
u_char *p, c;
440+
static u_char name[] = "xindex=";
441+
442+
name[0] = (u_char) type;
443+
444+
for ( ;; ) {
445+
p = (u_char *) ngx_strstr(args, name);
446+
if (p == NULL) {
447+
return 0;
448+
}
449+
450+
if (p != args) {
451+
c = *(p - 1);
452+
if (c != '?' && c != '&') {
453+
args = p + 1;
454+
continue;
455+
}
456+
}
457+
458+
return atoi((char *) p + (sizeof(name) - 1));
459+
}
460+
}
461+
462+
436463
static ngx_int_t
437464
ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
438465
{
@@ -487,6 +514,9 @@ ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
487514

488515
ngx_memzero(ctx, sizeof(*ctx));
489516

517+
ctx->aindex = ngx_rtmp_play_parse_index('a', v->args);
518+
ctx->vindex = ngx_rtmp_play_parse_index('v', v->args);
519+
490520
ctx->file.log = s->connection->log;
491521

492522
name.len = ngx_strlen(v->name);

ngx_rtmp_play_module.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
typedef ngx_int_t (*ngx_rtmp_play_init_pt) (ngx_rtmp_session_t *s,
14-
ngx_file_t *f);
14+
ngx_file_t *f, ngx_int_t aindex, ngx_int_t vindex);
1515
typedef ngx_int_t (*ngx_rtmp_play_done_pt) (ngx_rtmp_session_t *s,
1616
ngx_file_t *f);
1717
typedef ngx_int_t (*ngx_rtmp_play_start_pt) (ngx_rtmp_session_t *s,
@@ -45,6 +45,7 @@ typedef struct {
4545
unsigned playing:1;
4646
ngx_uint_t ncrs;
4747
ngx_str_t name;
48+
ngx_int_t aindex, vindex;
4849
} ngx_rtmp_play_ctx_t;
4950

5051

0 commit comments

Comments
 (0)