From 8f7c2f9ed92acbdca22c7a233e27a57099c52736 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Wed, 17 Jun 2015 09:42:35 +0800 Subject: [PATCH] FS-7517 FS-7519 add H264 STAP-A packetizing support so it would work with FireFox --- src/mod/applications/mod_av/avcodec.c | 30 ++++++++++++++++++++ src/mod/codecs/mod_openh264/mod_openh264.cpp | 30 ++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/mod/applications/mod_av/avcodec.c b/src/mod/applications/mod_av/avcodec.c index f2b62a1eee..9537ae43db 100644 --- a/src/mod/applications/mod_av/avcodec.c +++ b/src/mod/applications/mod_av/avcodec.c @@ -187,6 +187,36 @@ static switch_status_t buffer_h264_nalu(h264_codec_context_t *context, switch_fr } switch_buffer_write(buffer, (void *)(data + 2), frame->datalen - 2); + } else if (nalu_type == 24) { // 0x18 STAP-A + uint16_t nalu_size; + int left = frame->datalen - 1; + + data++; + + again: + if (left > 2) { + nalu_size = ntohs(*(uint16_t *)data); + data += 2; + left -= 2; + + if (nalu_size > left) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID PACKET\n"); + context->got_pps = 0; + switch_buffer_zero(buffer); + return SWITCH_STATUS_FALSE; + } + + nalu_hdr = *data; + nalu_type = nalu_hdr & 0x1f; + + if (context->got_pps <= 0 && nalu_type == 7) context->got_pps = 1; + + switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); + switch_buffer_write(buffer, (void *)data, nalu_size); + data += nalu_size; + left -= nalu_size; + goto again; + } } else { switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); switch_buffer_write(buffer, frame->data, frame->datalen); diff --git a/src/mod/codecs/mod_openh264/mod_openh264.cpp b/src/mod/codecs/mod_openh264/mod_openh264.cpp index fa2ddfaf1e..c2e0fc8eee 100644 --- a/src/mod/codecs/mod_openh264/mod_openh264.cpp +++ b/src/mod/codecs/mod_openh264/mod_openh264.cpp @@ -246,6 +246,36 @@ static switch_size_t buffer_h264_nalu(h264_codec_context_t *context, switch_fram } size = switch_buffer_write(buffer, (void *)(data + 2), frame->datalen - 2); + } else if (nalu_type == 24) { // 0x18 STAP-A + uint16_t nalu_size; + int left = frame->datalen - 1; + + data++; + + again: + if (left > 2) { + nalu_size = ntohs(*(uint16_t *)data); + data += 2; + left -= 2; + + if (nalu_size > left) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID PACKET\n"); + context->got_sps = 0; + switch_buffer_zero(buffer); + return SWITCH_STATUS_FALSE; + } + + nalu_hdr = *data; + nalu_type = nalu_hdr & 0x1f; + + if (context->got_sps <= 0 && nalu_type == 7) context->got_sps = 1; + + size += switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); + size += switch_buffer_write(buffer, (void *)data, nalu_size); + data += nalu_size; + left -= nalu_size; + goto again; + } } else { size = switch_buffer_write(buffer, sync_bytes, sizeof(sync_bytes)); size = switch_buffer_write(buffer, frame->data, frame->datalen);