From 39b123ded49238e930b955cf922f777a75c1921c Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>
Date: Mon, 23 Sep 2013 17:23:45 -0400
Subject: [PATCH] [ #30495 ] Fix video codec selection

---
 src/CMakeLists.txt      |  2 +
 src/account.cpp         | 25 -------------
 src/account.h           |  4 --
 src/videocodec.cpp      | 83 +++++++++++++++++++++++++++++++++++++++++
 src/videocodec.h        | 78 ++++++++++++++++++++++++++++++++++++++
 src/videocodecmodel.cpp | 53 +++-----------------------
 src/videocodecmodel.h   | 35 ++---------------
 src/videorenderer.cpp   |  2 +-
 8 files changed, 173 insertions(+), 109 deletions(-)
 create mode 100644 src/videocodec.cpp
 create mode 100644 src/videocodec.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 95966046..2c41cd34 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -48,6 +48,7 @@ set( qtsflphone_LIB_SRCS
   videorenderer.cpp
   videodevice.cpp
   phonenumber.cpp
+  videocodec.cpp
 
   #Models
   accountlistmodel.cpp
@@ -106,6 +107,7 @@ set( qtsflphone_LIB_HDRS
   phonedirectorymodel.h
   historytimecategorymodel.h
   numbercategorymodel.h
+  videocodec.h
 )
 
 set( qtsflphone_extra_LIB_HDRS
diff --git a/src/account.cpp b/src/account.cpp
index 8762d41b..67b17a3a 100644
--- a/src/account.cpp
+++ b/src/account.cpp
@@ -1214,29 +1214,4 @@ bool Account::operator==(const Account& a)const
  *                                   Video                                   *
  *                                                                           *
  ****************************************************************************/
-#ifdef ENABLE_VIDEO
-///Save active video codecs
-void Account::setActiveVideoCodecList(const QList<VideoCodec*>& codecs)
-{
-   Q_UNUSED(codecs)
-//    QStringList codecs2;
-//    VideoInterface& interface = DBus::VideoManager::instance();
-//    foreach(VideoCodec* codec,codecs) {
-//       codecs2 << codec->name();
-//    }
-//    interface.setCodecs(m_AccountId,codecs2);
-}
-
-///Return the list of active video dodecs
-QList<VideoCodec*> Account::activeVideoCodecList()
-{
-   QList<VideoCodec*> codecs;
-//    VideoInterface& interface = DBus::VideoManager::instance();
-//    const QStringList activeCodecList = interface.getCodecs(m_AccountId);
-//    foreach (const QString& codec, activeCodecList) {
-//       codecs << VideoCodec::Codec(codec);
-//    }
-   return codecs;
-}
 
-#endif
diff --git a/src/account.h b/src/account.h
index 784452f0..d83c0d3c 100644
--- a/src/account.h
+++ b/src/account.h
@@ -286,10 +286,6 @@ class LIB_EXPORT Account : public QObject {
 
       //Setters
       void setId      (const QString& id);
-      #ifdef ENABLE_VIDEO
-      void setActiveVideoCodecList(const QList<VideoCodec*>& codecs);
-      QList<VideoCodec*> activeVideoCodecList();
-      #endif
       void setAlias                         (const QString& detail);
       void setAccountType                   (const QString& detail);
       void setHostname                      (const QString& detail);
diff --git a/src/videocodec.cpp b/src/videocodec.cpp
new file mode 100644
index 00000000..d1710532
--- /dev/null
+++ b/src/videocodec.cpp
@@ -0,0 +1,83 @@
+/****************************************************************************
+ *   Copyright (C) 2012-2013 by Savoir-Faire Linux                          *
+ *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
+ *                                                                          *
+ *   This library 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.     *
+ *                                                                          *
+ *   This library 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 General Public License      *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
+ ***************************************************************************/
+#include "videocodec.h"
+
+#include "account.h"
+
+QHash<QString,VideoCodec*> VideoCodec::m_slCodecs;
+bool VideoCodec::m_sInit = false;
+
+///Private constructor
+VideoCodec::VideoCodec(QString codecName, uint bitRate, bool enabled) : QObject(),
+m_Name(codecName),m_Bitrate(bitRate),m_Enabled(enabled)
+{
+   setObjectName("VideoCodec"+codecName);
+}
+
+///Get the current codec name
+QString VideoCodec::name() const
+{
+   return m_Name;
+}
+
+///Get the current codec id
+uint VideoCodec::bitrate() const
+{
+   return m_Bitrate;
+}
+
+///Get the current codec id
+bool VideoCodec::enabled() const
+{
+   return m_Enabled;
+}
+
+///Set the codec bitrate
+void VideoCodec::setBitrate(const uint bitrate)
+{
+   m_Bitrate = bitrate;
+}
+
+///Set if the codec is enabled
+void VideoCodec::setEnabled(const bool enabled)
+{
+   m_Enabled = enabled;
+}
+
+///Set codec parameters
+void VideoCodec::setParamaters(const QString& params )
+{
+   m_Parameters = params;
+}
+
+///Get codec parameters
+QString VideoCodec::parameters() const
+{
+   return m_Parameters;
+}
+
+///Generate a deamon compatible codec representation
+QMap<QString,QString> VideoCodec::toMap() const
+{
+   QMap<QString,QString> ret;
+   ret[CodecFields::ENABLED    ] = enabled   ()?"true":"false";
+   ret[CodecFields::BITRATE    ] = QString::number(bitrate());
+   ret[CodecFields::NAME       ] = name      ();
+   ret[CodecFields::PARAMETERS ] = parameters();
+   return ret;
+}
diff --git a/src/videocodec.h b/src/videocodec.h
new file mode 100644
index 00000000..e613d52a
--- /dev/null
+++ b/src/videocodec.h
@@ -0,0 +1,78 @@
+/****************************************************************************
+ *   Copyright (C) 2012-2013 by Savoir-Faire Linux                          *
+ *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
+ *                                                                          *
+ *   This library 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.     *
+ *                                                                          *
+ *   This library 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 General Public License      *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
+ ***************************************************************************/
+#ifndef VIDEO_CODEC_H
+#define VIDEO_CODEC_H
+
+#include "typedefs.h"
+#include <QtCore/QObject>
+
+class Account;
+class VideoCodec;
+
+typedef QHash<QString,VideoCodec*> CodecHash;
+
+///VideoCodec: Codecs used for video calls
+class LIB_EXPORT VideoCodec : public QObject {
+   Q_OBJECT
+   friend class VideoCodecModel;
+   public:
+      //Properties
+      Q_PROPERTY(QString name       READ name                          )
+      Q_PROPERTY(uint    bitrate    READ bitrate    WRITE setBitrate   )
+      Q_PROPERTY(bool    enabled    READ enabled    WRITE setEnabled   )
+      Q_PROPERTY(QString parameters READ parameters WRITE setParamaters)
+
+      //Consts
+      class CodecFields {
+      public:
+         constexpr static const char* PARAMETERS = "parameters";
+         constexpr static const char* ENABLED    = "enabled"   ;
+         constexpr static const char* BITRATE    = "bitrate"   ;
+         constexpr static const char* NAME       = "name"      ;
+      };
+
+      //Static setters
+      static void setActiveCodecList(Account* account, QStringList codecs);
+
+      //Getters
+      QString name      () const;
+      uint    bitrate   () const;
+      bool    enabled   () const;
+      QString parameters() const;
+      QMap<QString,QString> toMap() const;
+
+      //Setters
+      void setBitrate   (const uint     bitrate );
+      void setEnabled   (const bool     enabled );
+      void setParamaters(const QString& params  );
+
+   private:
+      //Constructor
+      VideoCodec(QString codecName, uint bitRate, bool enabled);
+      ~VideoCodec(){};
+
+      //Attributes
+      static CodecHash m_slCodecs;
+      QString          m_Name;
+      uint             m_Bitrate;
+      bool             m_Enabled;
+      static bool      m_sInit;
+      QString          m_Parameters;
+};
+
+#endif
\ No newline at end of file
diff --git a/src/videocodecmodel.cpp b/src/videocodecmodel.cpp
index f1ac5a4c..ff92ff73 100644
--- a/src/videocodecmodel.cpp
+++ b/src/videocodecmodel.cpp
@@ -18,6 +18,7 @@
 #include "videocodecmodel.h"
 #include "call.h"
 #include "account.h"
+#include "videocodec.h"
 #include "dbus/videomanager.h"
 
 #include <QtCore/QCoreApplication>
@@ -84,7 +85,10 @@ void VideoCodecModel::reload()
    VideoInterface& interface = DBus::VideoManager::instance();
    const VectorMapStringString codecs =  interface.getCodecs(m_pAccount->id());
    foreach(const MapStringString& h,codecs) {
-      VideoCodec* c = new VideoCodec(h["name"],h["bitrate"].toInt(),h["enabled"]=="true");
+      VideoCodec* c = new VideoCodec(h[VideoCodec::CodecFields::NAME],
+                                     h[VideoCodec::CodecFields::BITRATE].toInt(),
+                                     h[VideoCodec::CodecFields::ENABLED]=="true");
+      c->setParamaters(h[VideoCodec::CodecFields::PARAMETERS]);
       m_lCodecs << c;
    }
    emit dataChanged(index(0,0), index(m_lCodecs.size()-1,0));
@@ -96,11 +100,7 @@ void VideoCodecModel::save()
    VideoInterface& interface = DBus::VideoManager::instance();
    VectorMapStringString toSave;
    foreach(VideoCodec* vc,m_lCodecs) {
-      MapStringString details;
-      details[ "name"    ] = vc->name   ();
-      details[ "bitrate" ] = QString::number(vc->bitrate());
-      details[ "enabled" ] = vc->enabled()?"true":"false";
-      toSave << details;
+      toSave << vc->toMap();
    }
    interface.setCodecs(m_pAccount->id(),toSave);
 }
@@ -130,44 +130,3 @@ bool VideoCodecModel::moveDown(QModelIndex idx)
    }
    return false;
 }
-
-
-QHash<QString,VideoCodec*> VideoCodec::m_slCodecs;
-bool VideoCodec::m_sInit = false;
-
-///Private constructor
-VideoCodec::VideoCodec(QString codecName, uint bitRate, bool enabled) :
-m_Name(codecName),m_Bitrate(bitRate),m_Enabled(enabled)
-{
-
-}
-
-///Get the current codec name
-QString VideoCodec::name() const
-{
-   return m_Name;
-}
-
-///Get the current codec id
-uint VideoCodec::bitrate() const
-{
-   return m_Bitrate;
-}
-
-///Get the current codec id
-bool VideoCodec::enabled() const
-{
-   return m_Enabled;
-}
-
-///Set the codec bitrate
-void VideoCodec::setBitrate(const uint bitrate)
-{
-   m_Bitrate = bitrate;
-}
-
-///Set if the codec is enabled
-void VideoCodec::setEnabled(const bool enabled)
-{
-   m_Enabled = enabled;
-}
diff --git a/src/videocodecmodel.h b/src/videocodecmodel.h
index f65dfd88..ae3b79b4 100644
--- a/src/videocodecmodel.h
+++ b/src/videocodecmodel.h
@@ -15,8 +15,8 @@
  *   You should have received a copy of the GNU General Public License      *
  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
  ***************************************************************************/
-#ifndef VIDEO_CODEC_H
-#define VIDEO_CODEC_H
+#ifndef VIDEOCODECMODEL_H
+#define VIDEOCODECMODEL_H
 
 #include "typedefs.h"
 #include <QtCore/QAbstractListModel>
@@ -50,7 +50,7 @@ public:
    int           rowCount ( const QModelIndex& parent = QModelIndex()                ) const;
    Qt::ItemFlags flags    ( const QModelIndex& index                                 ) const;
    virtual bool  setData  ( const QModelIndex& index, const QVariant &value, int role)      ;
-   
+
    void reload();
    void save();
    bool moveUp  (QModelIndex idx);
@@ -62,33 +62,4 @@ private:
    Account*           m_pAccount;
 };
 Q_DECLARE_METATYPE(VideoCodecModel*)
-
-///VideoCodec: Codecs used for video calls
-class LIB_EXPORT VideoCodec {
-   friend class VideoCodecModel;
-   public:
-      //Static setters
-      static void setActiveCodecList(Account* account, QStringList codecs);
-
-      //Getters
-      QString name   () const;
-      uint    bitrate() const;
-      bool    enabled() const;
-
-      //Setters
-      void setBitrate(const uint bitrate);
-      void setEnabled(const bool enabled);
-      
-   private:
-      //Constructor
-      VideoCodec(QString codecName, uint bitRate, bool enabled);
-      ~VideoCodec(){};
-
-      //Attributes
-      static CodecHash m_slCodecs;
-      QString     m_Name;
-      uint        m_Bitrate;
-      bool        m_Enabled;
-      static bool m_sInit;
-};
 #endif
diff --git a/src/videorenderer.cpp b/src/videorenderer.cpp
index f91f7182..d7a93e08 100644
--- a/src/videorenderer.cpp
+++ b/src/videorenderer.cpp
@@ -182,7 +182,7 @@ bool VideoRenderer::resizeShm()
 
       shmUnlock();
       if (munmap(m_pShmArea, m_ShmAreaLen)) {
-            qDebug() << "Could not unmap shared area:%s" << strerror(errno);
+            qDebug() << "Could not unmap shared area:" << strerror(errno);
             return false;
       }
 
-- 
GitLab