From patchwork Mon Apr 11 15:49:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tristan Matthews X-Patchwork-Id: 35260 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:671c:b0:7c:62c8:b2d1 with SMTP id q28csp1118627pzh; Mon, 11 Apr 2022 08:50:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyazTjTdP7KlK8nyKymXAEPeS+IJhT9TPM4WNbclDy541iU9JymvMnHiu+fVc5gGlDEUp4G X-Received: by 2002:a05:6402:34c4:b0:419:e00e:64c1 with SMTP id w4-20020a05640234c400b00419e00e64c1mr33985827edc.88.1649692230513; Mon, 11 Apr 2022 08:50:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649692230; cv=none; d=google.com; s=arc-20160816; b=ZC8xOakkLf15wHBXCldK7jqwTNA1Z16hn45Ff55p4Y4OxBklRD4BUs6aN6ax+w8cj0 /50t0VY85wHctV2mU9adr5hiSsiBv9cKTHChKDw0RPIU7toA46ijy0B0nvSNsUXc9GSW MqQ78UJv3VShH5QjnVG9QRJm2gknwF8ixIfjhIQ33O9fwhX4ZaNkB6T07ISc3jNBPw7z vG0wH0CCgbw04asSyES4uwjsV2xEQ9v7ldNBMW3anqROTw4vSjd4iDHrLdEdhADy00Bh OhLPMNYzCeHLPrD8Z7Dyz8m/KArl6ypTr2DiyWlpAy3rSRaGHlxD6seDbhPRr/WZXQN1 4g9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=tuVqai+mbdWxWdE6ZPclR58copx+YUlu2q+e6vQFPoU=; b=xYi+FNkl4BZJZnTUyJoCiKrL6Wlx9CFUta9YMJnzmvVd1alNVYcPyj9saUn5ozE7Qq zOtV1DLlIYhVWUTv+Lz7cr8HirRlo7QWc4vNtvN8YzF7PNCuyEMPxGut9cISpL0gq4Qh xmaKUgKMl/i0VPic/zPCk6KvKIKQXdjz8heADN+onmc4BGKlo5G4t5fh2K3WNqU0YtvM m29Ysq70rKwnbfRa1dviv3v91W6Eunxgxqv8zVMlaBiP8bRFZLm7vGA7vNqason/t7k1 j2GcPZKtgXQBlY4TJBqsUhYE4yH/VMCVzYlldL0PMVWuxRPVTM+V8rfdglIkphy4Ssqe hsFQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o12-20020a170906768c00b006e8ac17e983si131269ejm.385.2022.04.11.08.50.30; Mon, 11 Apr 2022 08:50:30 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3AA8568B2CC; Mon, 11 Apr 2022 18:50:28 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0D89C68B24D for ; Mon, 11 Apr 2022 18:50:22 +0300 (EEST) Received: by mail-qv1-f47.google.com with SMTP id x20so4279419qvl.10 for ; Mon, 11 Apr 2022 08:50:21 -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=MaX+0K/Mj9VVpknTc8pbU6gq6NXBiFmR9En3LWfA7OU=; b=Ff66vuTsmG7Wcg4Ow7nuNgi8wGzPK9yTbSnBPa0Dbcjf5XGCzb/lt1DifnbZfgqsAo TO1LFXgr/U60ZUpdTIPHErbvU9JJyWlhpHLI2AkXxEekymlgTfCilSmX11sLKRqULedN R7wFDf+s91FQvNedNQ7wPTuunTN1YKUVE1ESuuctX/gX/2Qb/6bzufOKbV8bKMwKMdSH hRjeUC1Z0JenzXuPz5bOcwPfwYYX9tvq1gBzrAJOHk1YTaRQCNofAeAdzDsI5PFqimrq sBwIDkh93Bkr5krCkUz8Ixdy2B62OsPsjE9nnzw+b8or1UUyLMBhKtC5R0BAB5WfBLij NcKg== X-Gm-Message-State: AOAM530GpC3DHgFaLJqGjrRLFAOiU+Up3YflwqLvL/aLCH43/Mb5WOo5 7Me7P3xjEu0/Dnnoa3qYXCVMCi1QtbKEug== X-Received: by 2002:a05:6214:1d23:b0:444:4d2d:e377 with SMTP id f3-20020a0562141d2300b004444d2de377mr2238663qvd.116.1649692220581; Mon, 11 Apr 2022 08:50:20 -0700 (PDT) Received: from localhost.localdomain ([107.159.99.23]) by smtp.gmail.com with ESMTPSA id l11-20020a05620a210b00b0069c0bc64bc5sm3944945qkl.128.2022.04.11.08.50.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Apr 2022 08:50:20 -0700 (PDT) From: Tristan Matthews To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Apr 2022 11:49:48 -0400 Message-Id: <20220411154948.3597680-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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: yrib0FZaOjtq 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 | 123 +++++++++++------------------------------- 1 file changed, 32 insertions(+), 91 deletions(-) diff --git a/libavformat/librtmp.c b/libavformat/librtmp.c index 43013e46e0..7b379e48ee 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" @@ -50,7 +51,6 @@ typedef struct LibRTMPContext { char *pageurl; char *client_buffer_time; int live; - char *temp_filename; int buffer_size; } LibRTMPContext; @@ -76,7 +76,6 @@ static int rtmp_close(URLContext *s) RTMP *r = &ctx->rtmp; RTMP_Close(r); - av_freep(&ctx->temp_filename); return 0; } @@ -94,11 +93,11 @@ static int rtmp_close(URLContext *s) */ static int rtmp_open(URLContext *s, const char *uri, int flags) { + AVBPrint filename; LibRTMPContext *ctx = s->priv_data; RTMP *r = &ctx->rtmp; int rc = 0, level; - char *filename = s->filename; - int len = strlen(s->filename) + 1; + av_bprint_init(&filename, 0, AV_BPRINT_SIZE_UNLIMITED); switch (av_log_get_level()) { default: @@ -112,118 +111,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(&filename, "%s", s->filename); + if (ctx->app) + av_bprintf(&filename, " app=%s", ctx->app); + if (ctx->tcurl) + av_bprintf(&filename, " tcUrl=%s", ctx->tcurl); + if (ctx->pageurl) + av_bprintf(&filename, " pageUrl=%s", ctx->pageurl); + if (ctx->swfurl) + av_bprintf(&filename, " swfUrl=%s", ctx->swfurl); + if (ctx->flashver) + av_bprintf(&filename, " flashVer=%s", ctx->flashver); if (ctx->conn) { char *sep, *p = ctx->conn; - int options = 0; - while (p) { - options++; + av_bprintf(&filename, " conn="); p += strspn(p, " "); if (!*p) break; sep = strchr(p, ' '); + if (sep) + *sep = '\0'; + av_bprintf(&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(&filename, " playpath=%s", ctx->playpath); if (ctx->live) - len += sizeof(" live=1"); + av_bprintf(&filename, " live=1"); if (ctx->subscribe) - len += strlen(ctx->subscribe) + sizeof(" subscribe="); - + av_bprintf(&filename, " subscribe=%s", ctx->subscribe); if (ctx->client_buffer_time) - len += strlen(ctx->client_buffer_time) + sizeof(" buffer="); - + av_bprintf(&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(&filename, " swfUrl=%s swfVfy=1", ctx->swfverify); else - len += strlen(ctx->swfurl); + av_bprintf(&filename, " swfUrl=%s", ctx->swfurl); } - if (!(ctx->temp_filename = filename = av_malloc(len))) + if (!av_bprint_is_complete(&filename)) { + av_bprint_finalize(&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, filename.str)) { rc = AVERROR_UNKNOWN; goto fail; } @@ -247,9 +186,11 @@ static int rtmp_open(URLContext *s, const char *uri, int flags) #endif s->is_streamed = 1; + av_bprint_finalize(&filename, NULL); return 0; fail: - av_freep(&ctx->temp_filename); + av_bprint_finalize(&filename, NULL); + if (rc) RTMP_Close(r);