From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id D55C842423 for ; Fri, 15 Apr 2022 11:53:40 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3A86068B4D4; Fri, 15 Apr 2022 14:53:39 +0300 (EEST) Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 57E8C68B2C2 for ; Fri, 15 Apr 2022 14:53:33 +0300 (EEST) Received: by mail-qk1-f179.google.com with SMTP id g11so6488365qke.1 for ; Fri, 15 Apr 2022 04:53:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=E+uIvQDC7QgKsVUdKFuh1+Df8anmgQgVKWnpbATnohc=; b=es5Yq+KHuIbDL2Bpt5uPSExI+ihyu4l/4fpioskz+4owr2Aqm65lvYIJWLrf3CK7Vh yCCHR1bSzZPPo8fZTTwxxjyKlAqeoFzfeNxK2AoWLHk4ZTNeQoJwxKb81jKR26zYx64B IlxvGouk3ytcZKmn+0k/FAEDgKPkC2wfYjqtLg1W6HC6jf7Bt0ZfpeqWv/u5zDtP5Pm0 2gGondnlXSuJkYBF/PrCd89/R4t+tNgJYn3sDwjLw352A3cOexXvsk9ZPBVnFXHzHN3N sIELqcjPoDdDC8kZMhP3MLVnEGZrcrDscLSYfbNM/rn7b48nmL9QeiJeP0rYAlFePsvG 8KhA== X-Gm-Message-State: AOAM531e20Daoz1PqkYOfnrJH2+bYZq8L+oF1saZpTYJ7AQpmPq9/zSR MUCZlFuVTkj59LChCBCBEkdWC1jtpxQIRA== X-Google-Smtp-Source: ABdhPJwoIxDcK1CMAL0zijxDF4XD7WogDI+wDYTetO4E6nB57TGXe/B60q0TDntSBW8ZcXcVZTTznQ== X-Received: by 2002:a05:620a:85e:b0:69b:e5f5:df1c with SMTP id u30-20020a05620a085e00b0069be5f5df1cmr5225514qku.272.1650023611736; Fri, 15 Apr 2022 04:53:31 -0700 (PDT) Received: from localhost.localdomain ([107.159.99.23]) by smtp.gmail.com with ESMTPSA id t15-20020a05622a148f00b002f1ec8e7e6bsm818623qtx.18.2022.04.15.04.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 04:53:31 -0700 (PDT) From: Tristan Matthews To: ffmpeg-devel@ffmpeg.org Date: Fri, 15 Apr 2022 07:53:01 -0400 Message-Id: <20220415115301.973416-1-tmatth@videolan.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/1] librtmp: use AVBPrint instead of char * X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Tristan Matthews Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: This avoids having to do one pass to calculate the full length to allocate followed by a second pass to actually append values. --- libavformat/librtmp.c | 124 +++++++++++------------------------------- 1 file changed, 33 insertions(+), 91 deletions(-) diff --git a/libavformat/librtmp.c b/libavformat/librtmp.c index 43013e46e0..b7e9fc81cf 100644 --- a/libavformat/librtmp.c +++ b/libavformat/librtmp.c @@ -25,6 +25,7 @@ */ #include "libavutil/avstring.h" +#include "libavutil/bprint.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" #include "avformat.h" @@ -38,6 +39,7 @@ typedef struct LibRTMPContext { const AVClass *class; + AVBPrint filename; RTMP rtmp; char *app; char *conn; @@ -50,7 +52,6 @@ typedef struct LibRTMPContext { char *pageurl; char *client_buffer_time; int live; - char *temp_filename; int buffer_size; } LibRTMPContext; @@ -76,7 +77,7 @@ static int rtmp_close(URLContext *s) RTMP *r = &ctx->rtmp; RTMP_Close(r); - av_freep(&ctx->temp_filename); + av_bprint_finalize(&ctx->filename, NULL); return 0; } @@ -97,8 +98,8 @@ static int rtmp_open(URLContext *s, const char *uri, int flags) LibRTMPContext *ctx = s->priv_data; RTMP *r = &ctx->rtmp; int rc = 0, level; - char *filename = s->filename; - int len = strlen(s->filename) + 1; + /* This needs to stay allocated for as long as the RTMP context exists. */ + av_bprint_init(&ctx->filename, 0, AV_BPRINT_SIZE_UNLIMITED); switch (av_log_get_level()) { default: @@ -112,118 +113,58 @@ static int rtmp_open(URLContext *s, const char *uri, int flags) RTMP_LogSetLevel(level); RTMP_LogSetCallback(rtmp_log); - if (ctx->app) len += strlen(ctx->app) + sizeof(" app="); - if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl="); - if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl="); - if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver="); - + av_bprintf(&ctx->filename, "%s", s->filename); + if (ctx->app) + av_bprintf(&ctx->filename, " app=%s", ctx->app); + if (ctx->tcurl) + av_bprintf(&ctx->filename, " tcUrl=%s", ctx->tcurl); + if (ctx->pageurl) + av_bprintf(&ctx->filename, " pageUrl=%s", ctx->pageurl); + if (ctx->swfurl) + av_bprintf(&ctx->filename, " swfUrl=%s", ctx->swfurl); + if (ctx->flashver) + av_bprintf(&ctx->filename, " flashVer=%s", ctx->flashver); if (ctx->conn) { char *sep, *p = ctx->conn; - int options = 0; - while (p) { - options++; + av_bprintf(&ctx->filename, " conn="); p += strspn(p, " "); if (!*p) break; sep = strchr(p, ' '); + if (sep) + *sep = '\0'; + av_bprintf(&ctx->filename, "%s", 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="); + av_bprintf(&ctx->filename, " playpath=%s", ctx->playpath); if (ctx->live) - len += sizeof(" live=1"); + av_bprintf(&ctx->filename, " live=1"); if (ctx->subscribe) - len += strlen(ctx->subscribe) + sizeof(" subscribe="); - + av_bprintf(&ctx->filename, " subscribe=%s", ctx->subscribe); if (ctx->client_buffer_time) - len += strlen(ctx->client_buffer_time) + sizeof(" buffer="); - + av_bprintf(&ctx->filename, " buffer=%s", ctx->client_buffer_time); if (ctx->swfurl || ctx->swfverify) { - len += sizeof(" swfUrl="); - if (ctx->swfverify) - len += strlen(ctx->swfverify) + sizeof(" swfVfy=1"); + av_bprintf(&ctx->filename, " swfUrl=%s swfVfy=1", ctx->swfverify); else - len += strlen(ctx->swfurl); + av_bprintf(&ctx->filename, " swfUrl=%s", ctx->swfurl); } - if (!(ctx->temp_filename = filename = av_malloc(len))) + if (!av_bprint_is_complete(&ctx->filename)) { + av_bprint_finalize(&ctx->filename, NULL); 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) { - char *sep, *p = ctx->conn; - while (p) { - av_strlcat(filename, " conn=", len); - p += strspn(p, " "); - if (!*p) - break; - sep = strchr(p, ' '); - if (sep) - *sep = '\0'; - av_strlcat(filename, p, len); - - if (sep) - p = sep + 1; - else - break; - } - } - if (ctx->playpath) { - av_strlcat(filename, " playpath=", len); - av_strlcat(filename, ctx->playpath, len); - } - if (ctx->live) - av_strlcat(filename, " live=1", len); - if (ctx->subscribe) { - av_strlcat(filename, " subscribe=", len); - av_strlcat(filename, ctx->subscribe, len); - } - if (ctx->client_buffer_time) { - av_strlcat(filename, " buffer=", len); - av_strlcat(filename, ctx->client_buffer_time, len); - } - if (ctx->swfurl || ctx->swfverify) { - av_strlcat(filename, " swfUrl=", len); - - if (ctx->swfverify) { - av_strlcat(filename, ctx->swfverify, len); - av_strlcat(filename, " swfVfy=1", len); - } else { - av_strlcat(filename, ctx->swfurl, len); - } } 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; goto fail; } @@ -249,9 +190,10 @@ static int rtmp_open(URLContext *s, const char *uri, int flags) s->is_streamed = 1; return 0; fail: - av_freep(&ctx->temp_filename); + if (rc) RTMP_Close(r); + av_bprint_finalize(&ctx->filename, NULL); return rc; } -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".