librtmp: use AVBPrint instead of char *

This avoids having to do one pass to calculate the full length to allocate
followed by a second pass to actually append values.

Signed-off-by: Martin Storsjö <martin@martin.st>
This commit is contained in:
Tristan Matthews 2022-04-15 07:53:01 -04:00 committed by Martin Storsjö
parent b9e75c1862
commit 25d3f96db7
1 changed files with 37 additions and 95 deletions

View File

@ -25,6 +25,7 @@
*/ */
#include "libavutil/avstring.h" #include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/mathematics.h" #include "libavutil/mathematics.h"
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "avformat.h" #include "avformat.h"
@ -38,6 +39,7 @@
typedef struct LibRTMPContext { typedef struct LibRTMPContext {
const AVClass *class; const AVClass *class;
AVBPrint filename;
RTMP rtmp; RTMP rtmp;
char *app; char *app;
char *conn; char *conn;
@ -50,7 +52,6 @@ typedef struct LibRTMPContext {
char *pageurl; char *pageurl;
char *client_buffer_time; char *client_buffer_time;
int live; int live;
char *temp_filename;
int buffer_size; int buffer_size;
} LibRTMPContext; } LibRTMPContext;
@ -76,7 +77,7 @@ static int rtmp_close(URLContext *s)
RTMP *r = &ctx->rtmp; RTMP *r = &ctx->rtmp;
RTMP_Close(r); RTMP_Close(r);
av_freep(&ctx->temp_filename); av_bprint_finalize(&ctx->filename, NULL);
return 0; return 0;
} }
@ -97,8 +98,8 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
LibRTMPContext *ctx = s->priv_data; LibRTMPContext *ctx = s->priv_data;
RTMP *r = &ctx->rtmp; RTMP *r = &ctx->rtmp;
int rc = 0, level; int rc = 0, level;
char *filename = s->filename; /* This needs to stay allocated for as long as the RTMP context exists. */
int len = strlen(s->filename) + 1; av_bprint_init(&ctx->filename, 0, AV_BPRINT_SIZE_UNLIMITED);
switch (av_log_get_level()) { switch (av_log_get_level()) {
default: default:
@ -112,84 +113,28 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
RTMP_LogSetLevel(level); RTMP_LogSetLevel(level);
RTMP_LogSetCallback(rtmp_log); RTMP_LogSetCallback(rtmp_log);
if (ctx->app) len += strlen(ctx->app) + sizeof(" app="); av_bprintf(&ctx->filename, "%s", s->filename);
if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl="); if (ctx->app)
if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl="); av_bprintf(&ctx->filename, " app=%s", ctx->app);
if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver="); if (ctx->tcurl)
av_bprintf(&ctx->filename, " tcUrl=%s", ctx->tcurl);
if (ctx->conn) { if (ctx->pageurl)
char *sep, *p = ctx->conn; av_bprintf(&ctx->filename, " pageUrl=%s", ctx->pageurl);
int options = 0; if (ctx->swfurl)
av_bprintf(&ctx->filename, " swfUrl=%s", ctx->swfurl);
while (p) { if (ctx->flashver)
options++; av_bprintf(&ctx->filename, " flashVer=%s", ctx->flashver);
p += strspn(p, " ");
if (!*p)
break;
sep = strchr(p, ' ');
if (sep)
p = sep + 1;
else
break;
}
len += options * sizeof(" conn=");
len += strlen(ctx->conn);
}
if (ctx->playpath)
len += strlen(ctx->playpath) + sizeof(" playpath=");
if (ctx->live)
len += sizeof(" live=1");
if (ctx->subscribe)
len += strlen(ctx->subscribe) + sizeof(" subscribe=");
if (ctx->client_buffer_time)
len += strlen(ctx->client_buffer_time) + sizeof(" buffer=");
if (ctx->swfurl || ctx->swfverify) {
len += sizeof(" swfUrl=");
if (ctx->swfverify)
len += strlen(ctx->swfverify) + sizeof(" swfVfy=1");
else
len += strlen(ctx->swfurl);
}
if (!(ctx->temp_filename = filename = av_malloc(len)))
return AVERROR(ENOMEM);
av_strlcpy(filename, s->filename, len);
if (ctx->app) {
av_strlcat(filename, " app=", len);
av_strlcat(filename, ctx->app, len);
}
if (ctx->tcurl) {
av_strlcat(filename, " tcUrl=", len);
av_strlcat(filename, ctx->tcurl, len);
}
if (ctx->pageurl) {
av_strlcat(filename, " pageUrl=", len);
av_strlcat(filename, ctx->pageurl, len);
}
if (ctx->swfurl) {
av_strlcat(filename, " swfUrl=", len);
av_strlcat(filename, ctx->swfurl, len);
}
if (ctx->flashver) {
av_strlcat(filename, " flashVer=", len);
av_strlcat(filename, ctx->flashver, len);
}
if (ctx->conn) { if (ctx->conn) {
char *sep, *p = ctx->conn; char *sep, *p = ctx->conn;
while (p) { while (p) {
av_strlcat(filename, " conn=", len); av_bprintf(&ctx->filename, " conn=");
p += strspn(p, " "); p += strspn(p, " ");
if (!*p) if (!*p)
break; break;
sep = strchr(p, ' '); sep = strchr(p, ' ');
if (sep) if (sep)
*sep = '\0'; *sep = '\0';
av_strlcat(filename, p, len); av_bprintf(&ctx->filename, "%s", p);
if (sep) if (sep)
p = sep + 1; p = sep + 1;
@ -197,33 +142,29 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
break; break;
} }
} }
if (ctx->playpath) { if (ctx->playpath)
av_strlcat(filename, " playpath=", len); av_bprintf(&ctx->filename, " playpath=%s", ctx->playpath);
av_strlcat(filename, ctx->playpath, len);
}
if (ctx->live) if (ctx->live)
av_strlcat(filename, " live=1", len); av_bprintf(&ctx->filename, " live=1");
if (ctx->subscribe) { if (ctx->subscribe)
av_strlcat(filename, " subscribe=", len); av_bprintf(&ctx->filename, " subscribe=%s", ctx->subscribe);
av_strlcat(filename, ctx->subscribe, len); if (ctx->client_buffer_time)
} av_bprintf(&ctx->filename, " buffer=%s", ctx->client_buffer_time);
if (ctx->client_buffer_time) {
av_strlcat(filename, " buffer=", len);
av_strlcat(filename, ctx->client_buffer_time, len);
}
if (ctx->swfurl || ctx->swfverify) { if (ctx->swfurl || ctx->swfverify) {
av_strlcat(filename, " swfUrl=", len); if (ctx->swfverify)
av_bprintf(&ctx->filename, " swfUrl=%s swfVfy=1", ctx->swfverify);
if (ctx->swfverify) { else
av_strlcat(filename, ctx->swfverify, len); av_bprintf(&ctx->filename, " swfUrl=%s", ctx->swfurl);
av_strlcat(filename, " swfVfy=1", len);
} else {
av_strlcat(filename, ctx->swfurl, len);
} }
if (!av_bprint_is_complete(&ctx->filename)) {
av_bprint_finalize(&ctx->filename, NULL);
return AVERROR(ENOMEM);
} }
RTMP_Init(r); RTMP_Init(r);
if (!RTMP_SetupURL(r, filename)) { /* This will modify filename by null terminating the URL portion */
if (!RTMP_SetupURL(r, ctx->filename.str)) {
rc = AVERROR_UNKNOWN; rc = AVERROR_UNKNOWN;
goto fail; goto fail;
} }
@ -249,9 +190,10 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
s->is_streamed = 1; s->is_streamed = 1;
return 0; return 0;
fail: fail:
av_freep(&ctx->temp_filename);
if (rc) if (rc)
RTMP_Close(r); RTMP_Close(r);
av_bprint_finalize(&ctx->filename, NULL);
return rc; return rc;
} }