Skip to content

Commit 7ce6037

Browse files
committed
Added mjr metadata to (some) media containers when postprocessing recordings (see #1189)
1 parent 5a3cb89 commit 7ce6037

File tree

14 files changed

+96
-61
lines changed

14 files changed

+96
-61
lines changed

postprocessing/janus-pp-rec.c

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
#include <stdlib.h>
6262
#include <signal.h>
6363

64-
#include <glib.h>
6564
#include <jansson.h>
6665

6766
#include "../debug.h"
@@ -83,6 +82,7 @@ gboolean janus_log_timestamps = FALSE;
8382
gboolean janus_log_colors = TRUE;
8483

8584
static janus_pp_frame_packet *list = NULL, *last = NULL;
85+
static char *metadata = NULL;
8686
static int working = 0;
8787

8888
static int post_reset_trigger = 200;
@@ -126,6 +126,10 @@ int main(int argc, char *argv[])
126126
janus_log_level = val;
127127
JANUS_LOG(LOG_INFO, "Logging level: %d\n", janus_log_level);
128128
}
129+
if(g_getenv("JANUS_PPREC_METADATA") != NULL) {
130+
metadata = g_strdup(g_getenv("JANUS_PPREC_METADATA"));
131+
JANUS_LOG(LOG_INFO, "Metadata: %d\n", janus_log_level);
132+
}
129133
if(g_getenv("JANUS_PPREC_POSTRESETTRIGGER") != NULL) {
130134
int val = atoi(g_getenv("JANUS_PPREC_POSTRESETTRIGGER"));
131135
if(val >= 0)
@@ -204,8 +208,9 @@ int main(int argc, char *argv[])
204208
if(!jsonheader_only)
205209
JANUS_LOG(LOG_INFO, "Pre-parsing file to generate ordered index...\n");
206210
gboolean parsed_header = FALSE;
207-
int video = 0, data = 0;
208-
int opus = 0, g711 = 0, g722 = 0, vp8 = 0, vp9 = 0, h264 = 0;
211+
gboolean video = FALSE, data = FALSE;
212+
gboolean opus = FALSE, g711 = FALSE, g722 = FALSE,
213+
vp8 = FALSE, vp9 = FALSE, h264 = FALSE;
209214
gint64 c_time = 0, w_time = 0;
210215
int bytes = 0, skip = 0;
211216
long offset = 0;
@@ -246,26 +251,26 @@ int main(int argc, char *argv[])
246251
bytes = fread(prebuffer, sizeof(char), 5, file);
247252
if(prebuffer[0] == 'v') {
248253
JANUS_LOG(LOG_INFO, "This is a video recording, assuming VP8\n");
249-
video = 1;
250-
data = 0;
251-
vp8 = 1;
254+
video = TRUE;
255+
data = FALSE;
256+
vp8 = TRUE;
252257
if(extension && strcasecmp(extension, ".webm")) {
253258
JANUS_LOG(LOG_ERR, "VP8 RTP packets can only be converted to a .webm file\n");
254259
exit(1);
255260
}
256261
} else if(prebuffer[0] == 'a') {
257262
JANUS_LOG(LOG_INFO, "This is an audio recording, assuming Opus\n");
258-
video = 0;
259-
data = 0;
260-
opus = 1;
263+
video = FALSE;
264+
data = FALSE;
265+
opus = TRUE;
261266
if(extension && strcasecmp(extension, ".opus")) {
262267
JANUS_LOG(LOG_ERR, "Opus RTP packets can only be converted to an .opus file\n");
263268
exit(1);
264269
}
265270
} else if(prebuffer[0] == 'd') {
266271
JANUS_LOG(LOG_INFO, "This is a text data recording, assuming SRT\n");
267-
video = 0;
268-
data = 1;
272+
video = FALSE;
273+
data = TRUE;
269274
if(extension && strcasecmp(extension, ".srt")) {
270275
JANUS_LOG(LOG_ERR, "Data channel packets can only be converted to a .srt file\n");
271276
exit(1);
@@ -316,14 +321,14 @@ int main(int argc, char *argv[])
316321
}
317322
const char *t = json_string_value(type);
318323
if(!strcasecmp(t, "v")) {
319-
video = 1;
320-
data = 0;
324+
video = TRUE;
325+
data = FALSE;
321326
} else if(!strcasecmp(t, "a")) {
322-
video = 0;
323-
data = 0;
327+
video = FALSE;
328+
data = FALSE;
324329
} else if(!strcasecmp(t, "d")) {
325-
video = 0;
326-
data = 1;
330+
video = FALSE;
331+
data = TRUE;
327332
} else {
328333
JANUS_LOG(LOG_WARN, "Unsupported recording type '%s' in info header...\n", t);
329334
exit(1);
@@ -337,19 +342,19 @@ int main(int argc, char *argv[])
337342
const char *c = json_string_value(codec);
338343
if(video) {
339344
if(!strcasecmp(c, "vp8")) {
340-
vp8 = 1;
345+
vp8 = TRUE;
341346
if(extension && strcasecmp(extension, ".webm")) {
342347
JANUS_LOG(LOG_ERR, "VP8 RTP packets can only be converted to a .webm file\n");
343348
exit(1);
344349
}
345350
} else if(!strcasecmp(c, "vp9")) {
346-
vp9 = 1;
351+
vp9 = TRUE;
347352
if(extension && strcasecmp(extension, ".webm")) {
348353
JANUS_LOG(LOG_ERR, "VP9 RTP packets can only be converted to a .webm file\n");
349354
exit(1);
350355
}
351356
} else if(!strcasecmp(c, "h264")) {
352-
h264 = 1;
357+
h264 = TRUE;
353358
if(extension && strcasecmp(extension, ".mp4")) {
354359
JANUS_LOG(LOG_ERR, "H.264 RTP packets can only be converted to a .mp4 file\n");
355360
exit(1);
@@ -360,19 +365,19 @@ int main(int argc, char *argv[])
360365
}
361366
} else if(!video && !data) {
362367
if(!strcasecmp(c, "opus")) {
363-
opus = 1;
368+
opus = TRUE;
364369
if(extension && strcasecmp(extension, ".opus")) {
365370
JANUS_LOG(LOG_ERR, "Opus RTP packets can only be converted to a .opus file\n");
366371
exit(1);
367372
}
368373
} else if(!strcasecmp(c, "g711") || !strcasecmp(c, "pcmu") || !strcasecmp(c, "pcma")) {
369-
g711 = 1;
374+
g711 = TRUE;
370375
if(extension && strcasecmp(extension, ".wav")) {
371376
JANUS_LOG(LOG_ERR, "G.711 RTP packets can only be converted to a .wav file\n");
372377
exit(1);
373378
}
374379
} else if(!strcasecmp(c, "g722")) {
375-
g722 = 1;
380+
g722 = TRUE;
376381
if(extension && strcasecmp(extension, ".wav")) {
377382
JANUS_LOG(LOG_ERR, "G.722 RTP packets can only be converted to a .wav file\n");
378383
exit(1);
@@ -410,6 +415,10 @@ int main(int argc, char *argv[])
410415
JANUS_LOG(LOG_INFO, " -- Codec: %s\n", c);
411416
JANUS_LOG(LOG_INFO, " -- Created: %"SCNi64"\n", c_time);
412417
JANUS_LOG(LOG_INFO, " -- Written: %"SCNi64"\n", w_time);
418+
/* Save the original string as a metadata to save in the media container, if possible */
419+
if(metadata == NULL)
420+
metadata = g_strdup(prebuffer);
421+
json_decref(info);
413422
}
414423
} else {
415424
JANUS_LOG(LOG_ERR, "Invalid header...\n");
@@ -732,34 +741,34 @@ int main(int argc, char *argv[])
732741

733742
if(!video && !data) {
734743
if(opus) {
735-
if(janus_pp_opus_create(destination) < 0) {
744+
if(janus_pp_opus_create(destination, metadata) < 0) {
736745
JANUS_LOG(LOG_ERR, "Error creating .opus file...\n");
737746
exit(1);
738747
}
739748
} else if(g711) {
740-
if(janus_pp_g711_create(destination) < 0) {
749+
if(janus_pp_g711_create(destination, metadata) < 0) {
741750
JANUS_LOG(LOG_ERR, "Error creating .wav file...\n");
742751
exit(1);
743752
}
744753
} else if(g722) {
745-
if(janus_pp_g722_create(destination) < 0) {
754+
if(janus_pp_g722_create(destination, metadata) < 0) {
746755
JANUS_LOG(LOG_ERR, "Error creating .wav file...\n");
747756
exit(1);
748757
}
749758
}
750759
} else if(data) {
751-
if(janus_pp_srt_create(destination) < 0) {
760+
if(janus_pp_srt_create(destination, metadata) < 0) {
752761
JANUS_LOG(LOG_ERR, "Error creating .srt file...\n");
753762
exit(1);
754763
}
755764
} else {
756765
if(vp8 || vp9) {
757-
if(janus_pp_webm_create(destination, vp8) < 0) {
766+
if(janus_pp_webm_create(destination, metadata, vp8) < 0) {
758767
JANUS_LOG(LOG_ERR, "Error creating .webm file...\n");
759768
exit(1);
760769
}
761770
} else if(h264) {
762-
if(janus_pp_h264_create(destination) < 0) {
771+
if(janus_pp_h264_create(destination, metadata) < 0) {
763772
JANUS_LOG(LOG_ERR, "Error creating .mp4 file...\n");
764773
exit(1);
765774
}

postprocessing/pp-g711.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int16_t janus_pp_g711_alaw_table[256] =
118118

119119

120120
/* Processing methods */
121-
int janus_pp_g711_create(char *destination) {
121+
int janus_pp_g711_create(char *destination, char *metadata) {
122122
/* Create wav file */
123123
wav_file = fopen(destination, "wb");
124124
if(wav_file == NULL) {
@@ -142,6 +142,8 @@ int janus_pp_g711_create(char *destination) {
142142
{'d', 'a', 't', 'a'},
143143
0
144144
};
145+
/* Note: .wav files don't seem to support arbitrary comments
146+
* so there's nothing we can do with the provided metadata*/
145147
if(fwrite(&header, 1, sizeof(header), wav_file) != sizeof(header)) {
146148
JANUS_LOG(LOG_ERR, "Couldn't write WAV header, expect problems...\n");
147149
}

postprocessing/pp-g711.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* \brief Post-processing to generate .wav files (headers)
55
* \details Implementation of the post-processing code needed to
66
* generate raw .wav files out of G.711 (mu-law or a-law) RTP frames.
7-
*
7+
*
88
* \ingroup postprocessing
99
* \ref postprocessing
1010
*/
@@ -16,7 +16,7 @@
1616

1717
#include "pp-rtp.h"
1818

19-
int janus_pp_g711_create(char *destination);
19+
int janus_pp_g711_create(char *destination, char *metadata);
2020
int janus_pp_g711_process(FILE *file, janus_pp_frame_packet *list, int *working);
2121
void janus_pp_g711_close(void);
2222

postprocessing/pp-g722.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static FILE *wav_file = NULL;
6060

6161

6262
/* Processing methods */
63-
int janus_pp_g722_create(char *destination) {
63+
int janus_pp_g722_create(char *destination, char *metadata) {
6464
if(destination == NULL)
6565
return -1;
6666
/* Setup FFmpeg */
@@ -118,6 +118,8 @@ int janus_pp_g722_create(char *destination) {
118118
{'d', 'a', 't', 'a'},
119119
0
120120
};
121+
/* Note: .wav files don't seem to support arbitrary comments
122+
* so there's nothing we can do with the provided metadata*/
121123
if(fwrite(&header, 1, sizeof(header), wav_file) != sizeof(header)) {
122124
JANUS_LOG(LOG_ERR, "Couldn't write WAV header, expect problems...\n");
123125
}

postprocessing/pp-g722.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* \brief Post-processing to generate .wav files from G.722 (headers)
55
* \details Implementation of the post-processing code needed to
66
* generate raw .wav files out of G.722 RTP frames.
7-
*
7+
*
88
* \ingroup postprocessing
99
* \ref postprocessing
1010
*/
@@ -16,7 +16,7 @@
1616

1717
#include "pp-rtp.h"
1818

19-
int janus_pp_g722_create(char *destination);
19+
int janus_pp_g722_create(char *destination, char *metadata);
2020
int janus_pp_g722_process(FILE *file, janus_pp_frame_packet *list, int *working);
2121
void janus_pp_g722_close(void);
2222

postprocessing/pp-h264.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static AVCodecContext *vEncoder;
5858
static int max_width = 0, max_height = 0, fps = 0;
5959

6060

61-
int janus_pp_h264_create(char *destination) {
61+
int janus_pp_h264_create(char *destination, char *metadata) {
6262
if(destination == NULL)
6363
return -1;
6464
/* Setup FFmpeg */
@@ -76,6 +76,9 @@ int janus_pp_h264_create(char *destination) {
7676
JANUS_LOG(LOG_ERR, "Error allocating context\n");
7777
return -1;
7878
}
79+
/* We save the metadata part as a comment (see #1189) */
80+
if(metadata)
81+
av_dict_set(&fctx->metadata, "comment", metadata, 0);
7982
fctx->oformat = av_guess_format("mp4", NULL, NULL);
8083
if(fctx->oformat == NULL) {
8184
JANUS_LOG(LOG_ERR, "Error guessing format\n");

postprocessing/pp-h264.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* \brief Post-processing to generate .mp4 files (headers)
55
* \details Implementation of the post-processing code (based on FFmpeg)
66
* needed to generate .mp4 files out of H.264 RTP frames.
7-
*
7+
*
88
* \ingroup postprocessing
99
* \ref postprocessing
1010
*/
@@ -17,7 +17,7 @@
1717
#include "pp-rtp.h"
1818

1919
/* H.264 stuff */
20-
int janus_pp_h264_create(char *destination);
20+
int janus_pp_h264_create(char *destination, char *metadata);
2121
int janus_pp_h264_preprocess(FILE *file, janus_pp_frame_packet *list);
2222
int janus_pp_h264_process(FILE *file, janus_pp_frame_packet *list, int *working);
2323
void janus_pp_h264_close(void);

postprocessing/pp-opus.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "pp-opus.h"
2525
#include "pp-opus-silence.h"
2626
#include "../debug.h"
27+
#include "../version.h"
2728

2829

2930
/* OGG/Opus helpers */
@@ -33,14 +34,14 @@ ogg_stream_state *stream = NULL;
3334
void le32(unsigned char *p, int v);
3435
void le16(unsigned char *p, int v);
3536
ogg_packet *op_opushead(void);
36-
ogg_packet *op_opustags(void);
37+
ogg_packet *op_opustags(char *metadata);
3738
ogg_packet *op_from_pkt(const unsigned char *pkt, int len);
3839
void op_free(ogg_packet *op);
3940
int ogg_write(void);
4041
int ogg_flush(void);
4142

4243

43-
int janus_pp_opus_create(char *destination) {
44+
int janus_pp_opus_create(char *destination, char *metadata) {
4445
stream = g_malloc0(sizeof(ogg_stream_state));
4546
if(ogg_stream_init(stream, rand()) < 0) {
4647
JANUS_LOG(LOG_ERR, "Couldn't initialize Ogg stream state\n");
@@ -56,7 +57,7 @@ int janus_pp_opus_create(char *destination) {
5657
ogg_packet *op = op_opushead();
5758
ogg_stream_packetin(stream, op);
5859
op_free(op);
59-
op = op_opustags();
60+
op = op_opustags(metadata);
6061
ogg_stream_packetin(stream, op);
6162
op_free(op);
6263
ogg_flush();
@@ -123,7 +124,7 @@ int janus_pp_opus_process(FILE *file, janus_pp_frame_packet *list, int *working)
123124
steps++;
124125
}
125126
ogg_packet *op = op_from_pkt((const unsigned char *)buffer, bytes);
126-
pos = (tmp->ts - list->ts) / 48 / 20;
127+
pos = (tmp->ts - list->ts) / 48 / 20 + 1;
127128
JANUS_LOG(LOG_VERB, "pos: %06"SCNu64", writing %d bytes out of %d (seq=%"SCNu16", step=%"SCNu16", ts=%"SCNu64", time=%"SCNu64"s)\n",
128129
pos, bytes, tmp->len, tmp->seq, diff, tmp->ts, (tmp->ts-list->ts)/48000);
129130
op->granulepos = 960*(pos); /* FIXME: get this from the toc byte */
@@ -189,17 +190,30 @@ ogg_packet *op_opushead(void) {
189190
}
190191

191192
/* Manufacture a generic OpusTags packet */
192-
ogg_packet *op_opustags(void) {
193+
ogg_packet *op_opustags(char *metadata) {
193194
const char *identifier = "OpusTags";
194-
const char *vendor = "Janus post-processing";
195+
const char *desc = "DESCRIPTION=";
196+
char vendor[256];
197+
g_snprintf(vendor, sizeof(vendor), "Janus post-processor %s", janus_version_string);
195198
int size = strlen(identifier) + 4 + strlen(vendor) + 4;
199+
int dlen = strlen(desc), mlen = metadata ? strlen(metadata) : 0;
200+
if(mlen > 0)
201+
size += (4+dlen+mlen);
196202
unsigned char *data = g_malloc(size);
197203
ogg_packet *op = g_malloc(sizeof(*op));
198204

205+
/* Write down the tags */
199206
memcpy(data, identifier, 8);
200207
le32(data + 8, strlen(vendor));
201208
memcpy(data + 12, vendor, strlen(vendor));
202-
le32(data + 12 + strlen(vendor), 0);
209+
le32(data + 12 + strlen(vendor), mlen > 0 ? 1 : 0);
210+
/* Check if we have metadata to write down: we'll use the "DESCRIPTION" tag */
211+
if(metadata && strlen(metadata) > 0) {
212+
/* Add a single comment */
213+
le32(data + 12 + strlen(vendor) + 4, dlen+mlen);
214+
memcpy(data + 12 + strlen(vendor) + 8, desc, dlen);
215+
memcpy(data + 12 + strlen(vendor) + 8 + dlen, metadata, mlen);
216+
}
203217

204218
op->packet = data;
205219
op->bytes = size;

postprocessing/pp-opus.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* \brief Post-processing to generate .opus files (headers)
55
* \details Implementation of the post-processing code (based on libogg)
66
* needed to generate .opus files out of Opus RTP frames.
7-
*
7+
*
88
* \ingroup postprocessing
99
* \ref postprocessing
1010
*/
@@ -16,7 +16,7 @@
1616

1717
#include "pp-rtp.h"
1818

19-
int janus_pp_opus_create(char *destination);
19+
int janus_pp_opus_create(char *destination, char *metadata);
2020
int janus_pp_opus_process(FILE *file, janus_pp_frame_packet *list, int *working);
2121
void janus_pp_opus_close(void);
2222

0 commit comments

Comments
 (0)