[FFmpeg-devel] [PATCH] libavformat/rtmppkt.c ff_rtmp_packet_write write RTMP_PS_ONEBYTE chunks based on hypothesis than channel_id < 64.

Wentao Tang wentao.t at hotmail.com
Mon Jan 23 07:55:33 EET 2017


Although ffmpeg use internel defined video and audio channel ids that are than less than 64, I think it's better to patch ff_rtmp_packet_write write chunk logic for more clearness?

here is the patch(already tested by changing RTMP_VIDEO_CHANNEL >64 even >256):

diff --git a/libavformat/rtmppkt.c b/libavformat/rtmppkt.c
index cde0da7..51288ad 100644
--- a/libavformat/rtmppkt.c
+++ b/libavformat/rtmppkt.c
@@ -310,6 +310,18 @@ int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
     }
 }

+static void ff_rtmp_packet_write_chunk_basic_header(uint8_t **p, int mode, int channel_id) {
+    if (channel_id < 64) {
+        bytestream_put_byte(p, channel_id | (mode << 6));
+    } else if (channel_id < 64 + 256) {
+        bytestream_put_byte(p, 0          | (mode << 6));
+        bytestream_put_byte(p, channel_id - 64);
+    } else {
+        bytestream_put_byte(p, 1          | (mode << 6));
+        bytestream_put_le16(p, channel_id - 64);
+    }
+}
+
 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
                          int chunk_size, RTMPPacket **prev_pkt_ptr,
                          int *nb_prev_pkt)
@@ -354,15 +366,8 @@ int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
         }
     }

-    if (pkt->channel_id < 64) {
-        bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
-    } else if (pkt->channel_id < 64 + 256) {
-        bytestream_put_byte(&p, 0               | (mode << 6));
-        bytestream_put_byte(&p, pkt->channel_id - 64);
-    } else {
-        bytestream_put_byte(&p, 1               | (mode << 6));
-        bytestream_put_le16(&p, pkt->channel_id - 64);
-    }
+    ff_rtmp_packet_write_chunk_basic_header(&p, mode, pkt->channel_id);
+
     if (mode != RTMP_PS_ONEBYTE) {
         bytestream_put_be24(&p, pkt->ts_field);
         if (mode != RTMP_PS_FOURBYTES) {
@@ -391,10 +396,12 @@ int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
             return ret;
         off += towrite;
         if (off < pkt->size) {
-            uint8_t marker = 0xC0 | pkt->channel_id;
-            if ((ret = ffurl_write(h, &marker, 1)) < 0)
+            p = pkt_hdr;
+            ff_rtmp_packet_write_chunk_basic_header(&p, RTMP_PS_ONEBYTE, pkt->channel_id);
+            if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
                 return ret;
-            written++;
+            written += p - pkt_hdr;
+
             if (pkt->ts_field == 0xFFFFFF) {
                 uint8_t ts_header[4];
                 AV_WB32(ts_header, timestamp);



More information about the ffmpeg-devel mailing list