Publishing a stream using librtmp in C/C++

前端 未结 2 1869
情歌与酒
情歌与酒 2021-02-03 13:27

How to publish a stream using librtmp library? I read the librtmp man page and for publishing , RTMP_Write() is used.

I am doing like this.

//Code
//Ini         


        
相关标签:
2条回答
  • 2021-02-03 14:04

    In my own experience, streaming video data to an RTMP server is actually pretty simple on the librtmp side. The tricky part is to correctly packetize video/audio data and read it at the correct rate.

    Assuming you are using FLV video files, as long as you can correctly isolate each tag in the file and send each one using one RTMP_Write call, you don't even need to handle incoming packets.

    The tricky part is to understand how FLV files are made. The official specification is available here: http://www.adobe.com/devnet/f4v.html

    First, there's a header, that is made of 9 bytes. This header must not be sent to the server, but only read through in order to make sure the file is really FLV.

    Then there is a stream of tags. Each tag has a 11 bytes header that contains the tag type (video/audio/metadata), the body length, and the tag's timestamp, among other things.

    The tag header can be described using this structure:

    typedef struct __flv_tag {
      uint8       type;
      uint24_be   body_length; /* in bytes, total tag size minus 11 */
      uint24_be   timestamp; /* milli-seconds */
      uint8       timestamp_extended; /* timestamp extension */
      uint24_be   stream_id; /* reserved, must be "\0\0\0" */
      /* body comes next */
    } flv_tag;
    

    The body length and timestamp are presented as 24-bit big endian integers, with a supplementary byte to extend the timestamp to 32 bits if necessary (that's approximatively around the 4 hours mark).

    Once you have read the tag header, you can read the body itself as you now know its length (body_length).

    After that there is a 32-bit big endian integer value that contains the complete length of the tag (11 bytes + body_length).

    You must write the tag header + body + previous tag size in one RTMP_Write call (else it won't play).

    Also, be careful to send packets at the nominal frame rate of the video, else playback will suffer greatly.

    I have written a complete FLV file demuxer as part of my GPL project FLVmeta that you can use as reference.

    0 讨论(0)
  • 2021-02-03 14:21

    In fact, RTMP_Write() seems to require that you already have the RTMP packet formed in buf.

    RTMPPacket *pkt = &r->m_write;
    ...
    pkt->m_packetType = *buf++;
    

    So, you cannot just push the flv data there - you need to separate it to packets first.

    There is a nice function, RTMP_ReadPacket(), but it reads from the network socket.

    I have the same problem as you, hope to have a solution soon.

    Edit:

    There are certain bugs in RTMP_Write(). I've made a patch and now it works. I'm going to publish that.

    0 讨论(0)
提交回复
热议问题