*libRTMP study

来自个人维基
2020年4月11日 (六) 19:10free6d1823讨论 | 贡献的版本

跳转至: 导航搜索

目录

rtmpdump

source code download:

git clone git://git.ffmpeg.org/rtmpdump

Calling flow:
1. Init

   RTMP_LogSetLevel(level);
   RTMP_LogSetCallback(rtmp_log);
     static void rtmp_log(int level, const char *fmt, va_list args)
   RTMP *r = {};
   RTMP_Init(r); //init RTMP. BufferMS=30000,

filename = " "+ 
app=   
tcUrl= 
pageUrl=
swfUrl=
flashVer=
conn=
live=1
subscribe=
buffer=
swfUrl=
swfVfy=1
  

2. Setup

   RTMP_SetupURL(r, filename)
   RTMP_EnableWrite(r); //r->Link.protocol |= RTMP_FEATURE_WRITE;
   RTMP_Connect(r, NULL)
   RTMP_ConnectStream(r, 0)
   //
   setsockopt()

3. write

   RTMP_Write(r, buf, size);

4. read

   RTMP_Read(r, buf, size);
   RTMP_Pause(r, pause);
   RTMP_SendSeek(r, timestamp);

5. close

  RTMP_Close(r)
  RTMP_Free(RTMP *r) // free(r)

FFMPEG default use

ffmpeg/libavformat/rtmpproto.c

const URLProtocol ff_rtmp[e|s|t|te|ts]_protocol = {     \
    .name           = rtmp[e|s|t|te|ts],                   \
    .url_open2      = rtmp_open,                 \
    .url_read       = rtmp_read,                 \
    .url_read_seek  = rtmp_seek,                 \
    .url_read_pause = rtmp_pause,                \
    .url_write      = rtmp_write,                \
    .url_close      = rtmp_close,                \
    .priv_data_size = sizeof(RTMPContext),       \
    .flags          = URL_PROTOCOL_FLAG_NETWORK, \
    .priv_data_class= &rtmp[e|s|t|te|ts]_class,           \
};
#define RTMP_PROTOCOL(rtmp[e|s|t|te|ts]) 
static const AVClass rtmp[e|s|t|te|ts]##_class = {          \
    .class_name = #rtmp[e|s|t|te|ts],                       \
    .item_name  = av_default_item_name,          \
    .option     = rtmp_options,                  \
    .version    = LIBAVUTIL_VERSION_INT,         \
};  
static const AVOption rtmp_options[] = {
    {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
    {"rtmp_buffer", "Set buffer time in milliseconds. The default is 3000.", OFFSET(client_buffer_time), AV_OPT_TYPE_INT, {.i64 = 3000}, 0, INT_MAX, DEC|ENC},
    {"rtmp_conn", "Append arbitrary AMF data to the Connect message", OFFSET(conn), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
    {"rtmp_flashver", "Version of the Flash plugin used to run the SWF player.", OFFSET(flashver), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
    {"rtmp_flush_interval", "Number of packets flushed in the same request (RTMPT only).", OFFSET(flush_interval), AV_OPT_TYPE_INT, {.i64 = 10}, 0, INT_MAX, ENC},
    {"rtmp_live", "Specify that the media is a live stream.", OFFSET(live), AV_OPT_TYPE_INT, {.i64 = -2}, INT_MIN, INT_MAX, DEC, "rtmp_live"},
    {"any", "both", 0, AV_OPT_TYPE_CONST, {.i64 = -2}, 0, 0, DEC, "rtmp_live"},
    {"live", "live stream", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, DEC, "rtmp_live"},
    {"recorded", "recorded stream", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, DEC, "rtmp_live"},
    {"rtmp_pageurl", "URL of the web page in which the media was embedded. By default no value will be sent.", OFFSET(pageurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
    {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
    {"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
    {"rtmp_swfhash", "SHA256 hash of the decompressed SWF file (32 bytes).", OFFSET(swfhash), AV_OPT_TYPE_BINARY, .flags = DEC},
    {"rtmp_swfsize", "Size of the decompressed SWF file, required for SWFVerification.", OFFSET(swfsize), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC},
    {"rtmp_swfurl", "URL of the SWF player. By default no value will be sent", OFFSET(swfurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
    {"rtmp_swfverify", "URL to player swf file, compute hash/size automatically.", OFFSET(swfverify), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
    {"rtmp_tcurl", "URL of the target stream. Defaults to proto://host[:port]/app.", OFFSET(tcurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
    {"rtmp_listen", "Listen for incoming rtmp connections", OFFSET(listen), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtmp_listen" },
    {"listen",      "Listen for incoming rtmp connections", OFFSET(listen), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtmp_listen" },
    {"timeout", "Maximum timeout (in seconds) to wait for incoming connections. -1 is infinite. Implies -rtmp_listen 1",  OFFSET(listen_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC, "rtmp_listen" },
    { NULL },
};

declared in libformat/protocols.c

extern const URLProtocol ff_rtmp_protocol;
extern const URLProtocol ff_rtmpe_protocol;
extern const URLProtocol ff_rtmps_protocol;
extern const URLProtocol ff_rtmpt_protocol;
extern const URLProtocol ff_rtmpte_protocol;
extern const URLProtocol ff_rtmpts_protocol;

libavformat/protocol_list.c

static const URLProtocol *url_protocols[] = {
    &&ff_rtmp_protocol,
    ....
}

libavformat/makefile

OBJS-$(CONFIG_RTMP_PROTOCOL)  += rtmpproto.o rtmpdigest.o rtmppkt.o
OBJS-$(CONFIG_LIBRTMP_PROTOCOL)          += librtmp.o
OBJS-$(CONFIG_LIBRTMPE_PROTOCOL)         += librtmp.o
OBJS-$(CONFIG_LIBRTMPS_PROTOCOL)         += librtmp.o
OBJS-$(CONFIG_LIBRTMPT_PROTOCOL)         += librtmp.o
OBJS-$(CONFIG_LIBRTMPTE_PROTOCOL)        += librtmp.o
 
SKIPHEADERS-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += rtmpdh.h
OBJS-$(CONFIG_FFRTMPCRYPT_PROTOCOL)      += rtmpcrypt.o rtmpdigest.o rtmpdh.o
OBJS-$(CONFIG_FFRTMPHTTP_PROTOCOL)       += rtmphttp.o

ffmpeg/libavformat/rtmpproto.c

/**
 * Open RTMP connection and verify that the stream can be played.
 *
 * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
 */
static int rtmp_open(URLContext *s, const char *uri, int flags, AVDictionary **opts)

ffmpeg/config.h

#define CONFI_RTMP_PROTOCOL 1

config.mak

CONFIG_RTMP_PROTOCOL=yes

ffmpeg calling flow

avformat_network_init
ffmpeg_parse_options
transcode {
 treancode_init
 init_input_thread
 while(not terminate) {
 }
 free_input_threads
 flush_encoder
 exit
}

Log:
ffmpeg version N-94476-gf12e662 Copyright (c) 2000-2019 the FFmpeg developers

 built with gcc 5.4.0 (CJ 20200411)
 configuration: --disable-x86asm --enable-librtmp
 libavutil      56. 33.100 / 56. 33.100
 libavcodec     58. 55.100 / 58. 55.100
 libavformat    58. 30.100 / 58. 30.100
 libavdevice    58.  9.100 / 58.  9.100
 libavfilter     7. 58.100 /  7. 58.100
 libswscale      5.  6.100 /  5.  6.100
 libswresample   3.  6.100 /  3.  6.100

CJ ffmpeg ----
CJ open_input_file(/home/jammy/test.mp4)
CJ: before avformat_open_input(/home/jammy/test.mp4)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/jammy/test.mp4':

 Metadata:
   major_brand     : isom
   minor_version   : 512
   compatible_brands: isomiso2avc1mp41
   encoder         : Lavf56.15.102
 Duration: 00:08:00.05, start: 0.000000, bitrate: 4817 kb/s
   Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 4683 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
   Metadata:
     handler_name    : VideoHandler
   Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
   Metadata:
     handler_name    : SoundHandler

CJ open_output_file(rtmp://localhost/myapp/mystream)
CJ: new_video_stream 0
CJ: librtmp rtmp_open(rtmp://localhost/myapp/mystream, 2)
CJ ffmpeg: step2
CJ: ffmpeg step3: before transcode()
CJ transcode_init
Output #0, flv, to 'rtmp://localhost/myapp/mystream':

 Metadata:
   major_brand     : isom
   minor_version   : 512
   compatible_brands: isomiso2avc1mp41
   encoder         : Lavf58.30.100
   Stream #0:0(und): Video: h264 (High) ([7][0][0][0] / 0x0007), yuv420p, 1920x1080, q=2-31, 4683 kb/s, 25 fps, 25 tbr, 1k tbn, 12800 tbc (default)
   Metadata:
     handler_name    : VideoHandler
   Stream #0:1(und): Audio: aac (LC) ([10][0][0][0] / 0x000A), 44100 Hz, stereo, fltp, 128 kb/s (default)
   Metadata:
     handler_name    : SoundHandler

Stream mapping:

 Stream #0:0 -> #0:0 (copy)
 Stream #0:1 -> #0:1 (copy)

Press [q] to stop, [?] for help
frame= 15 fps=0.0 q=-1.0 size= 379kB time=00:00:00.51 bitrate=6079.8kbits/frame= 28 fps= 28 q=-1.0 size= 715kB time=00:00:01.02 bitrate=5735.0kbits/frame= 41 fps= 27 q=-1.0 size= 981kB time=00:00:01.52 bitrate=5285.2kbits/frame= 54 fps= 27 q=-1.0 size= 1280kB time=00:00:02.04 bitrate=5135.7kbits/frame= 66 fps= 26 q=-1.0 size= 1668kB time=00:00:02.53 bitrate=5399.6kbits/frame= 79 fps= 26 q=-1.0 size= 1907kB time=00:00:03.04 bitrate=5136.1kbits/frame= 91 fps= 26 q=-1.0 size= 2163kB time=00:00:03.55 bitrate=4988.2kbits/frame= 104 fps= 26 q=-1.0 size= 2430kB time=00:00:04.04 bitrate=4925.2kbits/[flv @ 0x36c8040] Failed to update header with correct duration.
[flv @ 0x36c8040] Failed to update header with correct filesize.
frame= 112 fps= 26 q=-1.0 Lsize= 2558kB time=00:00:04.36 bitrate=4805.9kbits/s speed=0.998x
video:2483kB audio:70kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.228437%
CJ: ffmpeg step 4: after transcode
CJ: ffmpeg step 5: before exit_program

How to use librtmp in ffmpeg

cd ffmpeg root ( I don't have asm)

./configure --disable-x86asm --enable-librtmp

configure exclusive include librtmp or rtmp to config.h
check config.h

#define CONFIG_LIBRTMP 1
#define CONFIG_LIBRTMP_PROTOCOL 1
#define CONFIG_RTMP_PROTOCOL 0

configure create protocol_list.c from PROTOCOL_LIST parsered "URLProtocol" from config.h and libavformat/protocols.c
check libavformat/protocol_lists.c, we have &ff_librtmp_protocol added.

make 
sudo make install