From d19747e302d7938fe6be11baca8aefc5c77473d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafa=C3=ABl=20Carr=C3=A9?=
 <rafael.carre@savoirfairelinux.com>
Date: Mon, 4 Jul 2011 16:35:45 -0400
Subject: [PATCH] test_video_rtp: use VideoRtpSession instead of RtpFactory

VideoRtpSession and VideoRtpFactory now take a std::map in ctor
---
 sflphone-common/src/video/test/Makefile.am    |  2 +-
 .../src/video/test/test_video_rtp.cpp         | 28 +++++----
 .../src/video/test/test_video_rtp.h           | 39 ------------
 .../src/video/video_receive_thread.cpp        |  7 ++-
 .../src/video/video_rtp_factory.cpp           | 16 ++++-
 sflphone-common/src/video/video_rtp_factory.h |  2 +-
 .../src/video/video_rtp_session.cpp           | 59 ++++---------------
 sflphone-common/src/video/video_rtp_session.h |  9 +--
 8 files changed, 52 insertions(+), 110 deletions(-)
 delete mode 100644 sflphone-common/src/video/test/test_video_rtp.h

diff --git a/sflphone-common/src/video/test/Makefile.am b/sflphone-common/src/video/test/Makefile.am
index 16163bf9f2..8c685e8cc7 100644
--- a/sflphone-common/src/video/test/Makefile.am
+++ b/sflphone-common/src/video/test/Makefile.am
@@ -5,7 +5,7 @@ check_PROGRAMS=test_video_endpoint test_video_rtp test_thread test_video_preview
 test_video_endpoint_SOURCES=test_video_endpoint.cpp test_video_endpoint.h
 test_video_endpoint_LDADD=$(top_builddir)/src/video/libvideo.la
 
-test_video_rtp_SOURCES=test_video_rtp.cpp test_video_rtp.h
+test_video_rtp_SOURCES=test_video_rtp.cpp
 test_video_rtp_LDADD=$(top_builddir)/src/libsflphone.la
 
 test_video_preview_SOURCES=test_video_preview.cpp test_video_preview.h
diff --git a/sflphone-common/src/video/test/test_video_rtp.cpp b/sflphone-common/src/video/test/test_video_rtp.cpp
index ed8fa5c0ff..d96092ec3c 100644
--- a/sflphone-common/src/video/test/test_video_rtp.cpp
+++ b/sflphone-common/src/video/test/test_video_rtp.cpp
@@ -31,20 +31,26 @@
 #include <iostream>
 #include <cassert>
 #include <unistd.h> // for sleep
-#include "test_video_rtp.h"
-#include "video_rtp_factory.h"
+#include <map>
+#include <string>
+#include "video_rtp_session.h"
 
-void VideoRtpTest::testRTPSession()
+int main (int argc, char* argv[])
 {
-    sfl_video::VideoRtpFactory videortp;
-    videortp.start();
+    std::map<std::string, std::string> args;
+    args["input"]       = "/dev/video0";
+    args["codec"]       = "mpeg4";
+    args["bitrate"]     = "1000000";
+    args["destination"] = "rtp://127.0.0.1:5000";
+    args["format"]      = "rgb24";
+    args["width"]       = "640";
+    args["height"]      = "480";
+
+    sfl_video::VideoRtpSession videosend(args);
+    //sfl_video::VideoRtpSession videorecv("rtp://127.0.0.1:5000", "libx264", 1000000, "rtp://127.0.0.1:5000", "rgb24");
+    videosend.start();
     sleep(10);
-    videortp.stop();
-}
+    videosend.stop();
 
-int main (int argc, char* argv[])
-{
-    VideoRtpTest test;
-    test.testRTPSession();
     return 0;
 }
diff --git a/sflphone-common/src/video/test/test_video_rtp.h b/sflphone-common/src/video/test/test_video_rtp.h
deleted file mode 100644
index 2c87a3cf54..0000000000
--- a/sflphone-common/src/video/test/test_video_rtp.h
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- *  Copyright (C) 2011 Savoir-Faire Linux Inc.
- *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program 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 General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- *  Additional permission under GNU GPL version 3 section 7:
- *
- *  If you modify this program, or any covered work, by linking or
- *  combining it with the OpenSSL project's OpenSSL library (or a
- *  modified version of that library), containing parts covered by the
- *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
- *  grants you additional permission to convey the resulting work.
- *  Corresponding Source for a non-source form of such a combination
- *  shall include the source code for the parts of OpenSSL used as well
- *  as that of the covered work.
- */
-
-#ifndef _VIDEO_RTP_TEST_
-#define _VIDEO_RTP_TEST_
-
-class VideoRtpTest {
-public:
-    void testRTPSession();
-};
-
-#endif // _VIDEO_RTP_TEST_
diff --git a/sflphone-common/src/video/video_receive_thread.cpp b/sflphone-common/src/video/video_receive_thread.cpp
index 819dfbdbf7..a65b323932 100644
--- a/sflphone-common/src/video/video_receive_thread.cpp
+++ b/sflphone-common/src/video/video_receive_thread.cpp
@@ -72,7 +72,7 @@ union semun
 };
 #endif
 
-int createSemSet(int shm_id, int shmKey, int *semKey)
+int createSemSet(int shmKey, int *semKey)
 {
     /* this variable will contain the semaphore set. */
     int sem_set_id;
@@ -138,6 +138,9 @@ int createShm(unsigned numBytes, int *shmKey)
     *shmKey = key;
     shm_id = shmget(key, numBytes, 0644 | IPC_CREAT);
 
+    if (shm_id == -1)
+        perror("shmget");
+
     return shm_id;
 }
 
@@ -288,7 +291,7 @@ void VideoReceiveThread::setup()
     // create shared memory segment and attach to it
     shmID_ = createShm(videoBufferSize_, &shmKey_);
     shmBuffer_  = attachShm(shmID_);
-    semSetID_ = createSemSet(shmID_, shmKey_, &semKey_);
+    semSetID_ = createSemSet(shmKey_, &semKey_);
     shmReady_.signal();
 
     // allocate video frame
diff --git a/sflphone-common/src/video/video_rtp_factory.cpp b/sflphone-common/src/video/video_rtp_factory.cpp
index d1d9296398..72d6ade6e9 100644
--- a/sflphone-common/src/video/video_rtp_factory.cpp
+++ b/sflphone-common/src/video/video_rtp_factory.cpp
@@ -30,14 +30,24 @@
 
 #include "video_rtp_factory.h"
 #include "video_rtp_session.h"
+#include <map>
+#include <string>
 
 namespace sfl_video 
 {
 
-VideoRtpFactory::VideoRtpFactory() :
-    session_(new VideoRtpSession("/dev/video0", "mpeg4", 1000000,
-                                 "rtp://127.0.0.1:5000"))
+VideoRtpFactory::VideoRtpFactory()
 {
+    std::map<std::string, std::string> args;
+    args["input"]       = "/dev/video0";
+    args["codec"]       = "mpeg4";
+    args["bitrate"]     = "1000000";
+    args["destination"] = "rtp://127.0.0.1:5000";
+    args["format"]      = "rgb24";
+    args["width"]       = "640";
+    args["height"]      = "480";
+
+    session_ = std::tr1::shared_ptr<VideoRtpSession>(new VideoRtpSession(args));
 }
 
 void VideoRtpFactory::start()
diff --git a/sflphone-common/src/video/video_rtp_factory.h b/sflphone-common/src/video/video_rtp_factory.h
index e3ae2ef481..6fa3d92ea5 100644
--- a/sflphone-common/src/video/video_rtp_factory.h
+++ b/sflphone-common/src/video/video_rtp_factory.h
@@ -57,4 +57,4 @@ class VideoRtpFactory {
 };
 }
 
-#endif // __VIDEO_RTP_SESSION_H__
+#endif // __VIDEO_RTP_FACTORY_H__
diff --git a/sflphone-common/src/video/video_rtp_session.cpp b/sflphone-common/src/video/video_rtp_session.cpp
index 207978b1d2..a58944628a 100644
--- a/sflphone-common/src/video/video_rtp_session.cpp
+++ b/sflphone-common/src/video/video_rtp_session.cpp
@@ -39,12 +39,8 @@
 
 namespace sfl_video {
 
-VideoRtpSession::VideoRtpSession(const std::string &input,
-        const std::string &codec,
-        int bitrate,
-        const std::string &destinationURI) :
-    input_(input), codec_(codec), bitrate_(bitrate),
-    destinationURI_(destinationURI)
+VideoRtpSession::VideoRtpSession(std::map<std::string,std::string> args) :
+    args_(args)
 {
 }
 
@@ -52,19 +48,8 @@ void VideoRtpSession::test()
 {
     assert(sendThread_.get() == 0);
     assert(receiveThread_.get() == 0);
-    std::cerr << "Capturing from " << input_ << ", encoding to " << codec_ <<
-        " at " << bitrate_ << " bps, sending to " << destinationURI_ <<
-        std::endl;
-    std::map<std::string, std::string> args;
-    args["input"] = input_;
-    args["codec"] = codec_;
-    std::stringstream bitstr;
-    bitstr << bitrate_;
-
-    args["bitrate"] = bitstr.str();
-    args["destination"] = destinationURI_;
-
-    sendThread_.reset(new VideoSendThread(args));
+
+    sendThread_.reset(new VideoSendThread(args_));
     sendThread_->start();
 
     /* block until SDP is ready */
@@ -75,25 +60,14 @@ void VideoRtpSession::test()
 void VideoRtpSession::test_loopback()
 {
     assert(sendThread_.get() == 0);
-    std::cerr << "Capturing from " << input_ << ", encoding to " << codec_ <<
-        " at " << bitrate_ << " bps, sending to " << destinationURI_ <<
-        std::endl;
-    std::map<std::string, std::string> args;
-    args["input"] = input_;
-    args["codec"] = codec_;
-    std::stringstream bitstr;
-    bitstr << bitrate_;
-
-    args["bitrate"] = bitstr.str();
-    args["destination"] = destinationURI_;
-
-    sendThread_.reset(new VideoSendThread(args));
+
+    sendThread_.reset(new VideoSendThread(args_));
     sendThread_->start();
 
     sendThread_->waitForSDP();
+    std::map<std::string, std::string> args(args_);
     args["input"] = "test.sdp";
-    std::cerr << "Receiving at " << destinationURI_ <<
-        ", writing to shared memory" << std::endl;
+
     receiveThread_.reset(new VideoReceiveThread(args));
     receiveThread_->start();
 }
@@ -102,21 +76,12 @@ void VideoRtpSession::start()
 {
     assert(sendThread_.get() == 0);
     assert(receiveThread_.get() == 0);
-    std::cerr << "Capturing from " << input_ << ", encoding to " << codec_ <<
-        " at " << bitrate_ << " bps, sending to " << destinationURI_ <<
-        std::endl;
-    std::map<std::string, std::string> args;
-    args["input"] = input_;
-    args["codec"] = codec_;
-    std::stringstream bitstr;
-    bitstr << bitrate_;
-
-    args["bitrate"] = bitstr.str();
-    args["destination"] = destinationURI_;
-
-    sendThread_.reset(new VideoSendThread(args));
+
+    sendThread_.reset(new VideoSendThread(args_));
     sendThread_->start();
 
+    sendThread_->waitForSDP();
+    std::map<std::string, std::string> args(args_);
     args["input"] = "test.sdp";
     receiveThread_.reset(new VideoReceiveThread(args));
     receiveThread_->start();
diff --git a/sflphone-common/src/video/video_rtp_session.h b/sflphone-common/src/video/video_rtp_session.h
index 6d353071d1..703c5b9abe 100644
--- a/sflphone-common/src/video/video_rtp_session.h
+++ b/sflphone-common/src/video/video_rtp_session.h
@@ -32,6 +32,7 @@
 #define __VIDEO_RTP_SESSION_H__
 
 #include <string>
+#include <map>
 #include <tr1/memory>
 
 namespace sfl_video {
@@ -41,20 +42,16 @@ class VideoReceiveThread;
 
 class VideoRtpSession {
     public:
-        VideoRtpSession(const std::string &input, const std::string &codec,
-                int bitrate, const std::string &destinationURI);
+        VideoRtpSession(std::map<std::string,std::string> args);
 
         void start();
         void test();
         void test_loopback();
         void stop();
     private:
-        const std::string input_;
-        const std::string codec_;
-        const int bitrate_;
-        const std::string destinationURI_;
         std::tr1::shared_ptr<VideoSendThread> sendThread_;
         std::tr1::shared_ptr<VideoReceiveThread> receiveThread_;
+        std::map<std::string,std::string> args_;
 };
 
 }
-- 
GitLab