[FFmpeg-cvslog] lavf: add samba protocol via libsmbclient

Lukasz Marek git at videolan.org
Sun Jul 13 20:44:15 CEST 2014


ffmpeg | branch: master | Lukasz Marek <lukasz.m.luki2 at gmail.com> | Sun Jul 13 00:54:23 2014 +0200| [4cc0f79a2c11f4ef53ee1b1199896832915a668e] | committer: Michael Niedermayer

lavf: add samba protocol via libsmbclient

Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=4cc0f79a2c11f4ef53ee1b1199896832915a668e
---

 Changelog                  |    1 +
 configure                  |    8 ++
 doc/general.texi           |    1 +
 doc/protocols.texi         |   29 +++++++
 libavformat/Makefile       |    1 +
 libavformat/allformats.c   |    1 +
 libavformat/libsmbclient.c |  207 ++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h      |    2 +-
 8 files changed, 249 insertions(+), 1 deletion(-)

diff --git a/Changelog b/Changelog
index f645feb..169819b 100644
--- a/Changelog
+++ b/Changelog
@@ -33,6 +33,7 @@ version <next>:
 - flanger filter
 - Image format auto-detection
 - LRC demuxer and muxer
+- Samba protocol (via libsmbclient)
 
 
 version 2.2:
diff --git a/configure b/configure
index b68e0db..51efc39 100755
--- a/configure
+++ b/configure
@@ -227,6 +227,7 @@ External library support:
   --enable-librtmp         enable RTMP[E] support via librtmp [no]
   --enable-libschroedinger enable Dirac de/encoding via libschroedinger [no]
   --enable-libshine        enable fixed-point MP3 encoding via libshine [no]
+  --enable-libsmbclient    enable Samba protocol via libsmbclient [no]
   --enable-libsoxr         enable Include libsoxr resampling [no]
   --enable-libspeex        enable Speex de/encoding via libspeex [no]
   --enable-libssh          enable SFTP protocol via libssh [no]
@@ -1349,6 +1350,7 @@ EXTERNAL_LIBRARY_LIST="
     librtmp
     libschroedinger
     libshine
+    libsmbclient
     libsoxr
     libspeex
     libssh
@@ -2472,6 +2474,7 @@ librtmpe_protocol_deps="librtmp"
 librtmps_protocol_deps="librtmp"
 librtmpt_protocol_deps="librtmp"
 librtmpte_protocol_deps="librtmp"
+libsmbclient_protocol_deps="libsmbclient gplv3"
 libssh_protocol_deps="libssh"
 mmsh_protocol_select="http_protocol"
 mmst_protocol_select="network"
@@ -4240,6 +4243,7 @@ die_license_disabled_gpl() {
 
 die_license_disabled gpl frei0r
 die_license_disabled gpl libcdio
+die_license_disabled gpl libsmbclient
 die_license_disabled gpl libutvideo
 die_license_disabled gpl libvidstab
 die_license_disabled gpl libx264
@@ -4256,6 +4260,7 @@ enabled gpl && die_license_disabled_gpl nonfree openssl
 
 die_license_disabled version3 libopencore_amrnb
 die_license_disabled version3 libopencore_amrwb
+die_license_disabled version3 libsmbclient
 die_license_disabled version3 libvo_aacenc
 die_license_disabled version3 libvo_amrwbenc
 
@@ -4744,6 +4749,9 @@ enabled libquvi           && require_pkg_config libquvi quvi/quvi.h quvi_init
 enabled librtmp           && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
 enabled libschroedinger   && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init
 enabled libshine          && require_pkg_config shine shine/layer3.h shine_encode_buffer
+enabled libsmbclient      && { { check_pkg_config smbclient libsmbclient.h smbc_init &&
+                                 require_pkg_config smbclient libsmbclient.h smbc_init; } ||
+                                 require smbclient libsmbclient.h smbc_init -lsmbclient; }
 enabled libsoxr           && require libsoxr soxr.h soxr_create -lsoxr
 enabled libssh            && require_pkg_config libssh libssh/sftp.h sftp_init
 enabled libspeex          && require libspeex speex/speex.h speex_decoder_init -lspeex
diff --git a/doc/general.texi b/doc/general.texi
index c008261..35db917 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -1065,6 +1065,7 @@ performance on systems without hardware floating point support).
 @item RTMPTE       @tab X
 @item RTMPTS       @tab X
 @item RTP          @tab X
+ at item SAMBA        @tab E
 @item SCTP         @tab X
 @item SFTP         @tab E
 @item TCP          @tab X
diff --git a/doc/protocols.texi b/doc/protocols.texi
index 3e5a707..a8898d3 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -537,6 +537,35 @@ The Real-Time Messaging Protocol tunneled through HTTPS (RTMPTS) is used
 for streaming multimedia content within HTTPS requests to traverse
 firewalls.
 
+ at section libsmbclient
+
+libsmbclient permits to manipulate CIFS/SMB network resources.
+
+Following syntax is required.
+
+ at example
+smb://[[domain:]user[:password@@]]server[/share[/path[/file]]]
+ at end example
+
+This protocol accepts the following options.
+
+ at table @option
+ at item timeout
+Set timeout in miliseconds of socket I/O operations used by the underlying
+low level operation. By default it is set to -1, which means that the timeout
+is not specified.
+
+ at item truncate
+Truncate existing files on write, if set to 1. A value of 0 prevents
+truncating. Default value is 1.
+
+ at item workgroup
+Set the workgroup used for making connections. By default workgroup is not specified.
+
+ at end table
+
+For more information see: @url{http://www.samba.org/}.
+
 @section libssh
 
 Secure File Transfer Protocol via libssh
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 3e14ccc..a365463 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -459,6 +459,7 @@ OBJS-$(CONFIG_LIBNUT_MUXER)              += libnut.o
 OBJS-$(CONFIG_LIBQUVI_DEMUXER)           += libquvi.o
 OBJS-$(CONFIG_LIBRTMP)                   += librtmp.o
 OBJS-$(CONFIG_LIBSSH_PROTOCOL)           += libssh.o
+OBJS-$(CONFIG_LIBSMBCLIENT_PROTOCOL)     += libsmbclient.o
 
 # protocols I/O
 OBJS-$(CONFIG_APPLEHTTP_PROTOCOL)        += hlsproto.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index bc5e8fe..3e00cae 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -376,4 +376,5 @@ void av_register_all(void)
     REGISTER_PROTOCOL(LIBRTMPT,         librtmpt);
     REGISTER_PROTOCOL(LIBRTMPTE,        librtmpte);
     REGISTER_PROTOCOL(LIBSSH,           libssh);
+    REGISTER_PROTOCOL(LIBSMBCLIENT,     libsmbclient);
 }
diff --git a/libavformat/libsmbclient.c b/libavformat/libsmbclient.c
new file mode 100644
index 0000000..892d2db
--- /dev/null
+++ b/libavformat/libsmbclient.c
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 2014 Lukasz Marek <lukasz.m.luki at gmail.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <libsmbclient.h>
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+#include "avformat.h"
+#include "internal.h"
+#include "url.h"
+
+typedef struct {
+    const AVClass *class;
+    SMBCCTX *ctx;
+    int fd;
+    int64_t filesize;
+    int trunc;
+    int timeout;
+    char *workgroup;
+} LIBSMBContext;
+
+static void libsmbc_get_auth_data(SMBCCTX *c, const char *server, const char *share,
+                                  char *workgroup, int workgroup_len,
+                                  char *username, int username_len,
+                                  char *password, int password_len)
+{
+    /* Do nothing yet. Credentials are passed via url.
+     * Callback must exists, there might be a segmentation fault otherwise. */
+}
+
+static av_cold int libsmbc_connect(URLContext *h)
+{
+    LIBSMBContext *libsmbc = h->priv_data;
+
+    libsmbc->ctx = smbc_new_context();
+    if (!libsmbc->ctx) {
+        av_log(h, AV_LOG_ERROR, "Cannot create context: %s.\n", strerror(errno));
+        return AVERROR(errno);
+    }
+    if (!smbc_init_context(libsmbc->ctx)) {
+        av_log(h, AV_LOG_ERROR, "Cannot initialize context: %s.\n", strerror(errno));
+        return AVERROR(errno);
+    }
+    smbc_set_context(libsmbc->ctx);
+
+    smbc_setOptionUserData(libsmbc->ctx, h);
+    smbc_setFunctionAuthDataWithContext(libsmbc->ctx, libsmbc_get_auth_data);
+
+    if (libsmbc->timeout != -1)
+        smbc_setTimeout(libsmbc->ctx, libsmbc->timeout);
+    if (libsmbc->workgroup)
+        smbc_setWorkgroup(libsmbc->ctx, libsmbc->workgroup);
+
+    if (smbc_init(NULL, 0) < 0) {
+        av_log(h, AV_LOG_ERROR, "Initialization failed: %s\n", strerror(errno));
+        return AVERROR(errno);
+    }
+    return 0;
+}
+
+static av_cold int libsmbc_close(URLContext *h)
+{
+    LIBSMBContext *libsmbc = h->priv_data;
+    if (libsmbc->fd >= 0) {
+        smbc_close(libsmbc->fd);
+        libsmbc->fd = -1;
+    }
+    if (libsmbc->ctx) {
+        smbc_free_context(libsmbc->ctx, 1);
+        libsmbc->ctx = NULL;
+    }
+    return 0;
+}
+
+static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
+{
+    LIBSMBContext *libsmbc = h->priv_data;
+    int access, ret;
+    struct stat st;
+
+    libsmbc->fd = -1;
+    libsmbc->filesize = -1;
+
+    if ((ret = libsmbc_connect(h)) < 0)
+        goto fail;
+
+    if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
+        access = O_CREAT | O_RDWR;
+        if (libsmbc->trunc)
+            access |= O_TRUNC;
+    } else if (flags & AVIO_FLAG_WRITE) {
+        access = O_CREAT | O_WRONLY;
+        if (libsmbc->trunc)
+            access |= O_TRUNC;
+    } else
+        access = O_RDONLY;
+
+    /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
+    if ((libsmbc->fd = smbc_open(url, access, 0666)) < 0) {
+        ret = AVERROR(errno);
+        av_log(h, AV_LOG_ERROR, "File open failed: %s\n", strerror(errno));
+        goto fail;
+    }
+
+    if (smbc_fstat(libsmbc->fd, &st) < 0)
+        av_log(h, AV_LOG_WARNING, "Cannot stat file: %s\n", strerror(errno));
+    else
+        libsmbc->filesize = st.st_size;
+
+    return 0;
+  fail:
+    libsmbc_close(h);
+    return ret;
+}
+
+static int64_t libsmbc_seek(URLContext *h, int64_t pos, int whence)
+{
+    LIBSMBContext *libsmbc = h->priv_data;
+    int64_t newpos;
+
+    if (whence == AVSEEK_SIZE) {
+        if (libsmbc->filesize == -1) {
+            av_log(h, AV_LOG_ERROR, "Error during seeking: filesize is unknown.\n");
+            return AVERROR(EIO);
+        } else
+            return libsmbc->filesize;
+    }
+
+    if ((newpos = smbc_lseek(libsmbc->fd, pos, whence)) < 0) {
+        int err = errno;
+        av_log(h, AV_LOG_ERROR, "Error during seeking: %s\n", strerror(err));
+        return AVERROR(err);
+    }
+
+    return newpos;
+}
+
+static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
+{
+    LIBSMBContext *libsmbc = h->priv_data;
+    int bytes_read;
+
+    if ((bytes_read = smbc_read(libsmbc->fd, buf, size)) < 0) {
+        av_log(h, AV_LOG_ERROR, "Read error: %s\n", strerror(errno));
+        return AVERROR(errno);
+    }
+
+    return bytes_read;
+}
+
+static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
+{
+    LIBSMBContext *libsmbc = h->priv_data;
+    int bytes_written;
+
+    if ((bytes_written = smbc_write(libsmbc->fd, buf, size)) < 0) {
+        av_log(h, AV_LOG_ERROR, "Write error: %s\n", strerror(errno));
+        return AVERROR(errno);
+    }
+
+    return bytes_written;
+}
+
+#define OFFSET(x) offsetof(LIBSMBContext, x)
+#define D AV_OPT_FLAG_DECODING_PARAM
+#define E AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    {"timeout",   "set timeout in ms of socket I/O operations",    OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
+    {"truncate",  "truncate existing files on write",              OFFSET(trunc),   AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
+    {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
+    {NULL}
+};
+
+static const AVClass libsmbclient_context_class = {
+    .class_name     = "libsmbc",
+    .item_name      = av_default_item_name,
+    .option         = options,
+    .version        = LIBAVUTIL_VERSION_INT,
+};
+
+URLProtocol ff_libsmbclient_protocol = {
+    .name                = "smb",
+    .url_open            = libsmbc_open,
+    .url_read            = libsmbc_read,
+    .url_write           = libsmbc_write,
+    .url_seek            = libsmbc_seek,
+    .url_close           = libsmbc_close,
+    .priv_data_size      = sizeof(LIBSMBContext),
+    .priv_data_class     = &libsmbclient_context_class,
+    .flags               = URL_PROTOCOL_FLAG_NETWORK,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index a5b24bf..5216e85 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 55
-#define LIBAVFORMAT_VERSION_MINOR 45
+#define LIBAVFORMAT_VERSION_MINOR 46
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list