Skip to content

Commit

Permalink
obs-outputs: Remove FLV byte array style multitrack audio support
Browse files Browse the repository at this point in the history
  • Loading branch information
palana authored and RytoEX committed May 8, 2024
1 parent 1c33b83 commit 2587c32
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 283 deletions.
248 changes: 0 additions & 248 deletions plugins/obs-outputs/flv-mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,251 +674,3 @@ void flv_packet_metadata(enum video_id_t codec_id, uint8_t **output,
*output = data.bytes.array;
*size = data.bytes.num;
}

/* ------------------------------------------------------------------------- */
/* stuff for additional media streams */

#define s_amf_conststring(s, str) \
do { \
const size_t len = sizeof(str) - 1; \
s_wb16(s, (uint16_t)len); \
serialize(s, str, len); \
} while (false)

#define s_amf_double(s, d) \
do { \
double d_val = d; \
uint64_t u_val = *(uint64_t *)&d_val; \
s_wb64(s, u_val); \
} while (false)

static void flv_build_additional_meta_data(uint8_t **data, size_t *size)
{
struct array_output_data out;
struct serializer s;

array_output_serializer_init(&s, &out);

s_w8(&s, AMF_STRING);
s_amf_conststring(&s, "@setDataFrame");

s_w8(&s, AMF_STRING);
s_amf_conststring(&s, "onExpectAdditionalMedia");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "processingIntents");

s_w8(&s, AMF_STRICT_ARRAY);
s_wb32(&s, 1);
{
s_w8(&s, AMF_STRING);
s_amf_conststring(&s, "ArchiveProgramNarrationAudio");
}

/* ---- */

s_amf_conststring(&s, "additionalMedia");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "stream0");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "type");

s_w8(&s, AMF_NUMBER);
s_amf_double(&s, RTMP_PACKET_TYPE_AUDIO);

/* ---- */

s_amf_conststring(&s, "mediaLabels");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "contentType");

s_w8(&s, AMF_STRING);
s_amf_conststring(&s, "PNAR");
}
s_wb24(&s, AMF_OBJECT_END);
}
s_wb24(&s, AMF_OBJECT_END);
}
s_wb24(&s, AMF_OBJECT_END);

/* ---- */

s_amf_conststring(&s, "defaultMedia");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "audio");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "mediaLabels");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "contentType");

s_w8(&s, AMF_STRING);
s_amf_conststring(&s, "PRM");
}
s_wb24(&s, AMF_OBJECT_END);
}
s_wb24(&s, AMF_OBJECT_END);
}
s_wb24(&s, AMF_OBJECT_END);
}
s_wb24(&s, AMF_OBJECT_END);

*data = out.bytes.array;
*size = out.bytes.num;
}

void flv_additional_meta_data(obs_output_t *context, uint8_t **data,
size_t *size)
{
UNUSED_PARAMETER(context);
struct array_output_data out;
struct serializer s;
uint8_t *meta_data = NULL;
size_t meta_data_size;

flv_build_additional_meta_data(&meta_data, &meta_data_size);

array_output_serializer_init(&s, &out);

s_w8(&s, RTMP_PACKET_TYPE_INFO); //18

s_wb24(&s, (uint32_t)meta_data_size);
s_wb32(&s, 0);
s_wb24(&s, 0);

s_write(&s, meta_data, meta_data_size);
bfree(meta_data);

write_previous_tag_size(&s);

*data = out.bytes.array;
*size = out.bytes.num;
}

static inline void s_u29(struct serializer *s, uint32_t val)
{
if (val <= 0x7F) {
s_w8(s, val);
} else if (val <= 0x3FFF) {
s_w8(s, 0x80 | (val >> 7));
s_w8(s, val & 0x7F);
} else if (val <= 0x1FFFFF) {
s_w8(s, 0x80 | (val >> 14));
s_w8(s, 0x80 | ((val >> 7) & 0x7F));
s_w8(s, val & 0x7F);
} else {
s_w8(s, 0x80 | (val >> 22));
s_w8(s, 0x80 | ((val >> 15) & 0x7F));
s_w8(s, 0x80 | ((val >> 8) & 0x7F));
s_w8(s, val & 0xFF);
}
}

static inline void s_u29b_value(struct serializer *s, uint32_t val)
{
s_u29(s, 1 | ((val & 0xFFFFFFF) << 1));
}

static void flv_build_additional_audio(uint8_t **data, size_t *size,
struct encoder_packet *packet,
bool is_header, size_t index)
{
UNUSED_PARAMETER(index);
struct array_output_data out;
struct serializer s;

array_output_serializer_init(&s, &out);

s_w8(&s, AMF_STRING);
s_amf_conststring(&s, "additionalMedia");

s_w8(&s, AMF_OBJECT);
{
s_amf_conststring(&s, "id");

s_w8(&s, AMF_STRING);
s_amf_conststring(&s, "stream0");

/* ----- */

s_amf_conststring(&s, "media");

s_w8(&s, AMF_AVMPLUS);
s_w8(&s, AMF3_BYTE_ARRAY);
s_u29b_value(&s, (uint32_t)packet->size + 2);
s_w8(&s, 0xaf);
s_w8(&s, is_header ? 0 : 1);
s_write(&s, packet->data, packet->size);
}
s_wb24(&s, AMF_OBJECT_END);

*data = out.bytes.array;
*size = out.bytes.num;
}

static void flv_additional_audio(struct serializer *s, int32_t dts_offset,
struct encoder_packet *packet, bool is_header,
size_t index)
{
int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
uint8_t *data;
size_t size;

if (!packet->data || !packet->size)
return;

flv_build_additional_audio(&data, &size, packet, is_header, index);

s_w8(s, RTMP_PACKET_TYPE_INFO); //18

#ifdef DEBUG_TIMESTAMPS
blog(LOG_DEBUG, "Audio2: %lu", time_ms);

if (last_time > time_ms)
blog(LOG_DEBUG, "Non-monotonic");

last_time = time_ms;
#endif

s_wb24(s, (uint32_t)size);
s_wb24(s, (uint32_t)time_ms);
s_w8(s, (time_ms >> 24) & 0x7F);
s_wb24(s, 0);

serialize(s, data, size);
bfree(data);

write_previous_tag_size(s);
}

void flv_additional_packet_mux(struct encoder_packet *packet,
int32_t dts_offset, uint8_t **data, size_t *size,
bool is_header, size_t index)
{
struct array_output_data out;
struct serializer s;

array_output_serializer_init(&s, &out);

if (packet->type == OBS_ENCODER_VIDEO) {
//currently unsupported
bcrash("who said you could output an additional video packet?");
} else {
flv_additional_audio(&s, dts_offset, packet, is_header, index);
}

*data = out.bytes.array;
*size = out.bytes.num;
}
6 changes: 0 additions & 6 deletions plugins/obs-outputs/flv-mux.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,8 @@ extern void write_file_info(FILE *file, int64_t duration_ms, int64_t size);

extern void flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
bool write_header);
extern void flv_additional_meta_data(obs_output_t *context, uint8_t **output,
size_t *size);
extern void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset,
uint8_t **output, size_t *size, bool is_header);
extern void flv_additional_packet_mux(struct encoder_packet *packet,
int32_t dts_offset, uint8_t **output,
size_t *size, bool is_header,
size_t index);
// Y2023 spec
extern void flv_packet_start(struct encoder_packet *packet,
enum video_id_t codec, uint8_t **output,
Expand Down
35 changes: 6 additions & 29 deletions plugins/obs-outputs/rtmp-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,25 +406,17 @@ static int handle_socket_read(struct rtmp_stream *stream)
}

static int send_packet(struct rtmp_stream *stream,
struct encoder_packet *packet, bool is_header,
size_t idx)
struct encoder_packet *packet, bool is_header)
{
uint8_t *data;
size_t size;
int ret = 0;

assert(idx < RTMP_MAX_STREAMS);
if (handle_socket_read(stream))
return -1;

if (idx > 0) {
flv_additional_packet_mux(
packet, is_header ? 0 : stream->start_dts_offset, &data,
&size, is_header, idx);
} else {
flv_packet_mux(packet, is_header ? 0 : stream->start_dts_offset,
&data, &size, is_header);
}
flv_packet_mux(packet, is_header ? 0 : stream->start_dts_offset, &data,
&size, is_header);

#ifdef TEST_FRAMEDROPS
droptest_cap_data_rate(stream, size);
Expand Down Expand Up @@ -712,8 +704,7 @@ static void *send_thread(void *data)
sent = send_audio_packet_ex(stream, &packet, false,
packet.track_idx);
} else {
sent = send_packet(stream, &packet, false,
packet.track_idx);
sent = send_packet(stream, &packet, false);
}

if (sent < 0) {
Expand Down Expand Up @@ -783,20 +774,6 @@ static void *send_thread(void *data)
return NULL;
}

static bool send_additional_meta_data(struct rtmp_stream *stream)
{
uint8_t *meta_data;
size_t meta_data_size;
bool success = true;

flv_additional_meta_data(stream->output, &meta_data, &meta_data_size);
success = RTMP_Write(&stream->rtmp, (char *)meta_data,
(int)meta_data_size, 0) >= 0;
bfree(meta_data);

return success;
}

static bool send_meta_data(struct rtmp_stream *stream)
{
uint8_t *meta_data;
Expand Down Expand Up @@ -829,7 +806,7 @@ static bool send_audio_header(struct rtmp_stream *stream, size_t idx,
if (obs_encoder_get_extra_data(aencoder, &header, &packet.size)) {
packet.data = bmemdup(header, packet.size);
if (idx == 0) {
return send_packet(stream, &packet, true, idx) >= 0;
return send_packet(stream, &packet, true) >= 0;
} else {
return send_audio_packet_ex(stream, &packet, true,
idx) >= 0;
Expand Down Expand Up @@ -866,7 +843,7 @@ static bool send_video_header(struct rtmp_stream *stream, size_t idx)
packet.size = obs_parse_avc_header(&packet.data, header, size);
// Always send H.264 on track 0 as old style for compatibility.
if (idx == 0) {
return send_packet(stream, &packet, true, idx) >= 0;
return send_packet(stream, &packet, true) >= 0;
} else {
return send_packet_ex(stream, &packet, true, false,
idx) >= 0;
Expand Down

0 comments on commit 2587c32

Please sign in to comment.