From 6ac8a5dbb5b3c9f3d320678e18d9fdceb1991ad2 Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Fri, 27 Jun 2014 10:27:04 -0400
Subject: [PATCH] daemon: (video) replace anonymous namespace by static keyword

Also minor code cleanup.

Rationale:
The C++ standard has un-deprecated the use of the static keyword.
But the superiority of anonymous namespaces is mainly for type
declarations (including structures and classes).

For variables and functions, the static keyword is functionally
equivalent to using an anonymous namespace.

But there is a major drawback with the latter!
Name mangling for anonymous namespaces is such that it's awkward
to type/find a local symbol declared in them when using a debugger
such as gdb.

Refs #50143

Change-Id: I925af61d14db08fafa37bfbfeedde95feadd0948
---
 daemon/src/video/libav_utils.cpp              | 37 ++++-----
 daemon/src/video/socket_pair.cpp              | 23 +++---
 daemon/src/video/test/test_shm.cpp            | 21 +++--
 .../src/video/v4l2/video_device_monitor.cpp   | 81 ++++++++++---------
 daemon/src/video/v4l2/video_v4l2.cpp          | 19 ++---
 daemon/src/video/video_encoder.cpp            |  5 +-
 6 files changed, 86 insertions(+), 100 deletions(-)

diff --git a/daemon/src/video/libav_utils.cpp b/daemon/src/video/libav_utils.cpp
index d461e90064..8c9c04cdc2 100644
--- a/daemon/src/video/libav_utils.cpp
+++ b/daemon/src/video/libav_utils.cpp
@@ -39,18 +39,13 @@
 #include <string>
 #include <iostream>
 
+std::map<std::string, std::string> encoders_;
+std::vector<std::string> installed_video_codecs_;
 
-namespace {
-using std::string;
-using std::map;
-using std::vector;
-
-map<string, string> encoders_;
-vector<string> installed_video_codecs_;
-
-void findInstalledVideoCodecs()
+static void
+findInstalledVideoCodecs()
 {
-    vector<string> libav_codecs;
+    std::vector<std::string> libav_codecs;
     AVCodec *p = NULL;
     while ((p = av_codec_next(p)))
         if (p->type == AVMEDIA_TYPE_VIDEO)
@@ -64,20 +59,18 @@ void findInstalledVideoCodecs()
     }
 }
 
-} // end anon namespace
-
 namespace libav_utils {
 
-vector<string> getVideoCodecList()
+std::vector<std::string> getVideoCodecList()
 {
     if (installed_video_codecs_.empty())
         findInstalledVideoCodecs();
     return installed_video_codecs_;
 }
 
-namespace {
 // protect libav/ffmpeg access with pthreads
-int avcodecManageMutex(void **data, enum AVLockOp op)
+static int
+avcodecManageMutex(void **data, enum AVLockOp op)
 {
     pthread_mutex_t **mutex = reinterpret_cast<pthread_mutex_t**>(data);
     int ret = 0;
@@ -108,7 +101,12 @@ int avcodecManageMutex(void **data, enum AVLockOp op)
     return AVERROR(ret);
 }
 
-void init_once()
+std::map<std::string, std::string> encodersMap()
+{
+    return encoders_;
+}
+
+static void init_once()
 {
     av_register_all();
     avdevice_register_all();
@@ -141,13 +139,6 @@ void init_once()
     findInstalledVideoCodecs();
 }
 
-}
-
-map<string, string> encodersMap()
-{
-    return encoders_;
-}
-
 static pthread_once_t already_called = PTHREAD_ONCE_INIT;
 
 void sfl_avcodec_init()
diff --git a/daemon/src/video/socket_pair.cpp b/daemon/src/video/socket_pair.cpp
index f542652723..dafd7199f5 100644
--- a/daemon/src/video/socket_pair.cpp
+++ b/daemon/src/video/socket_pair.cpp
@@ -48,12 +48,10 @@
 # define SOCK_NONBLOCK O_NONBLOCK
 #endif
 
-namespace {
-
 static const int NET_POLL_TIMEOUT = 100; /* poll() timeout in ms */
 
-
-int ff_network_wait_fd(int fd)
+static int
+ff_network_wait_fd(int fd)
 {
     struct pollfd p = { fd, POLLOUT, 0 };
     int ret;
@@ -61,7 +59,8 @@ int ff_network_wait_fd(int fd)
     return ret < 0 ? errno : p.revents & (POLLOUT | POLLERR | POLLHUP) ? 0 : -EAGAIN;
 }
 
-struct addrinfo* udp_resolve_host(const char *node, int service)
+static struct
+addrinfo* udp_resolve_host(const char *node, int service)
 {
     struct addrinfo hints, *res = 0;
     int error;
@@ -81,8 +80,8 @@ struct addrinfo* udp_resolve_host(const char *node, int service)
     return res;
 }
 
-unsigned udp_set_url(struct sockaddr_storage *addr,
-            const char *hostname, int port)
+static unsigned
+udp_set_url(struct sockaddr_storage *addr, const char *hostname, int port)
 {
     struct addrinfo *res0;
     int addr_len;
@@ -96,8 +95,8 @@ unsigned udp_set_url(struct sockaddr_storage *addr,
     return addr_len;
 }
 
-int udp_socket_create(sockaddr_storage *addr, socklen_t *addr_len,
-                  int local_port)
+static int
+udp_socket_create(sockaddr_storage *addr, socklen_t *addr_len, int local_port)
 {
     int udp_fd = -1;
     struct addrinfo *res0 = NULL, *res = NULL;
@@ -135,12 +134,10 @@ int udp_socket_create(sockaddr_storage *addr, socklen_t *addr_len,
     return udp_fd;
 }
 
-const int RTP_BUFFER_SIZE = 1472;
-
-}
-
 namespace sfl_video {
 
+static const int RTP_BUFFER_SIZE = 1472;
+
 SocketPair::SocketPair(const char *uri, int localPort) :
            rtcpWriteMutex_(),
            rtpHandle_(0),
diff --git a/daemon/src/video/test/test_shm.cpp b/daemon/src/video/test/test_shm.cpp
index 5eb73d7113..312571024d 100644
--- a/daemon/src/video/test/test_shm.cpp
+++ b/daemon/src/video/test/test_shm.cpp
@@ -39,18 +39,18 @@
 #include <cstring>
 #include <cassert>
 
-namespace {
+static std::atomic<bool> done(false);
 
-std::atomic<bool> done(false);
-
-void signal_handler(int /*sig*/)
+static void
+signal_handler(int /*sig*/)
 {
     done = true;
 }
 
-const char test_data[] = "abcdefghijklmnopqrstuvwxyz";
+static const char test_data[] = "abcdefghijklmnopqrstuvwxyz";
 
-void sink_thread()
+static void
+sink_thread()
 {
     sfl_video::SHMSink sink("bob");;
     if (!sink.start())
@@ -66,7 +66,8 @@ void sink_thread()
     std::cerr << "Exitting sink thread" << std::endl;
 }
 
-void run_client()
+static void
+run_client()
 {
     SHMSrc src("bob");;
     bool started = false;
@@ -92,7 +93,8 @@ void run_client()
     std::cerr << "Got characters, exitting client process" << std::endl;
 }
 
-void run_daemon()
+static void
+run_daemon()
 {
     std::thread bob(sink_thread);
     /* Wait for child process. */
@@ -116,9 +118,6 @@ void run_daemon()
     // wait for thread
     bob.join();
 }
-
-} // end anonymous namespace
-
 int main()
 {
     signal(SIGINT, signal_handler);
diff --git a/daemon/src/video/v4l2/video_device_monitor.cpp b/daemon/src/video/v4l2/video_device_monitor.cpp
index f490e777c4..261b6c9a93 100644
--- a/daemon/src/video/v4l2/video_device_monitor.cpp
+++ b/daemon/src/video/v4l2/video_device_monitor.cpp
@@ -186,53 +186,56 @@ void VideoDeviceMonitorImpl::start()
 
 namespace {
 
-    typedef std::vector<VideoV4l2Device> Devices;
-    struct DeviceComparator {
-        explicit DeviceComparator(const std::string &name) : name_(name) {}
-        inline bool operator()(const VideoV4l2Device &d) const { return d.name == name_; }
-        private:
-        const std::string name_;
-    };
-
-    int getNumber(const string &name, size_t *sharp)
-    {
-        size_t len = name.length();
-        // name is too short to be numbered
-        if (len < 3)
-            return -1;
-
-        for (size_t c = len; c; --c) {
-            if (name[c] == '#') {
-                unsigned i;
-                if (sscanf(name.substr(c).c_str(), "#%u", &i) != 1)
-                    return -1;
-                *sharp = c;
-                return i;
-            }
-        }
+typedef std::vector<VideoV4l2Device> Devices;
+struct DeviceComparator {
+    explicit DeviceComparator(const std::string &name) : name_(name) {}
+    inline bool operator()(const VideoV4l2Device &d) const { return d.name == name_; }
+private:
+    const std::string name_;
+};
 
+}
+
+static int
+getNumber(const string &name, size_t *sharp)
+{
+    size_t len = name.length();
+    // name is too short to be numbered
+    if (len < 3)
         return -1;
+
+    for (size_t c = len; c; --c) {
+        if (name[c] == '#') {
+            unsigned i;
+            if (sscanf(name.substr(c).c_str(), "#%u", &i) != 1)
+                return -1;
+            *sharp = c;
+            return i;
+        }
     }
 
-    void giveUniqueName(VideoV4l2Device &dev, const vector<VideoV4l2Device> &devices)
-    {
+    return -1;
+}
+
+static void
+giveUniqueName(VideoV4l2Device &dev, const vector<VideoV4l2Device> &devices)
+{
 start:
-        for (auto &item : devices) {
-            if (dev.name == item.name) {
-                size_t sharp;
-                int num = getNumber(dev.name, &sharp);
-                if (num < 0) // not numbered
-                    dev.name += " #0";
-                else {
-                    std::stringstream ss;
-                    ss  << num + 1;
-                    dev.name.replace(sharp + 1, ss.str().length(), ss.str());
-                }
-                goto start; // we changed the name, let's look again if it is unique
+    for (auto &item : devices) {
+        if (dev.name == item.name) {
+            size_t sharp;
+            int num = getNumber(dev.name, &sharp);
+            if (num < 0) // not numbered
+                dev.name += " #0";
+            else {
+                std::stringstream ss;
+                ss  << num + 1;
+                dev.name.replace(sharp + 1, ss.str().length(), ss.str());
             }
+            goto start; // we changed the name, let's look again if it is unique
         }
     }
-} // end anonymous namespace
+}
 
 VideoDeviceMonitorImpl::~VideoDeviceMonitorImpl()
 {
diff --git a/daemon/src/video/v4l2/video_v4l2.cpp b/daemon/src/video/v4l2/video_v4l2.cpp
index 55d474a44e..4bc7ba031c 100644
--- a/daemon/src/video/v4l2/video_v4l2.cpp
+++ b/daemon/src/video/v4l2/video_v4l2.cpp
@@ -122,8 +122,8 @@ static const unsigned pixelformats_supported[] = {
  *
  */
 
-namespace {
-unsigned int pixelformat_score(unsigned pixelformat)
+static unsigned int
+pixelformat_score(unsigned pixelformat)
 {
     for (const auto &item : pixelformats_supported)
         if (item == pixelformat)
@@ -131,7 +131,6 @@ unsigned int pixelformat_score(unsigned pixelformat)
 
     return UINT_MAX - 1;
 }
-}
 
 VideoV4l2Size::VideoV4l2Size(unsigned height, unsigned width) :
     height(height), width(width), rates_() {}
@@ -259,13 +258,12 @@ VideoV4l2Channel::getSizes(int fd, unsigned int pixelformat)
     return fmt.fmt.pix.pixelformat;
 }
 
-namespace {
-    bool isCIF(const VideoV4l2Size &size)
-    {
-        const unsigned CIF_WIDTH = 352;
-        const unsigned CIF_HEIGHT = 288;
-        return size.width == CIF_WIDTH and size.height == CIF_HEIGHT;
-    }
+static bool
+isCIF(const VideoV4l2Size &size)
+{
+    const unsigned CIF_WIDTH = 352;
+    const unsigned CIF_HEIGHT = 288;
+    return size.width == CIF_WIDTH and size.height == CIF_HEIGHT;
 }
 
 // Put CIF resolution (352x288) first in the list since it is more prevalent in
@@ -327,7 +325,6 @@ VideoV4l2Size VideoV4l2Channel::getSize(const string &name) const
     return sizes_.back();
 }
 
-
 VideoV4l2Device::VideoV4l2Device(const string &device) :
     device(device), name(), channels_()
 {
diff --git a/daemon/src/video/video_encoder.cpp b/daemon/src/video/video_encoder.cpp
index 0a531bbb54..bfbfa58b9c 100644
--- a/daemon/src/video/video_encoder.cpp
+++ b/daemon/src/video/video_encoder.cpp
@@ -210,14 +210,13 @@ VideoEncoder::startIO()
     av_dump_format(outputCtx_, 0, outputCtx_->filename, 1);
 }
 
-namespace {
-void print_averror(const char *funcname, int err)
+static void
+print_averror(const char *funcname, int err)
 {
     char errbuf[64];
     av_strerror(err, errbuf, sizeof(errbuf));
     ERROR("%s failed: %s", funcname, errbuf);
 }
-}
 
 int VideoEncoder::encode(VideoFrame &input, bool is_keyframe, int64_t frame_number)
 {
-- 
GitLab