macosx: add build option for macosx

Gitlab: #18
Change-Id: I1372c1881b894fd0cde76f5ad94067bd22192315
diff --git a/HelloWorld/CenterCircleVideoSubscriber.cpp b/HelloWorld/CenterCircleVideoSubscriber.cpp
index 62babd0..1b96c23 100644
--- a/HelloWorld/CenterCircleVideoSubscriber.cpp
+++ b/HelloWorld/CenterCircleVideoSubscriber.cpp
@@ -25,6 +25,7 @@
 }
 #include <accel.h>
 #include <frameUtils.h>
+#include <frameScaler.h>
 #include <pluglog.h>
 #include <stdio.h>
 #include <opencv2/imgproc.hpp>
@@ -65,19 +66,15 @@
     // GET RAW FRAME
     // Use a non-const Frame
     // Convert input frame to RGB
-    int inputHeight = pluginFrame->height;
-    int inputWidth = pluginFrame->width;
-    AVFrame* temp = transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12);
-    AVFrame* bgrFrame = scaler.convertFormat(temp, AV_PIX_FMT_RGB24);
-    av_frame_unref(temp);
-    av_frame_free(&temp);
-    if (!bgrFrame)
+    uniqueFramePtr rgbFrame = {transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12), frameFree};
+    rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_RGB24));
+    if (!rgbFrame.get())
         return;
-    resultFrame = cv::Mat {bgrFrame->height,
-                           bgrFrame->width,
+    resultFrame = cv::Mat {rgbFrame->height,
+                           rgbFrame->width,
                            CV_8UC3,
-                           bgrFrame->data[0],
-                           static_cast<size_t>(bgrFrame->linesize[0])};
+                           rgbFrame->data[0],
+                           static_cast<size_t>(rgbFrame->linesize[0])};
 
     // First clone the frame as the original one is unusable because of
     // linespace
@@ -85,8 +82,8 @@
 
     if (firstRun) {
         // we set were the circle will be draw.
-        circlePos.y = static_cast<int>(inputHeight / 2);
-        circlePos.x = static_cast<int>(inputWidth / 2);
+        circlePos.y = static_cast<int>(pluginFrame->height / 2);
+        circlePos.x = static_cast<int>(pluginFrame->width / 2);
         int w = resultFrame.size().width;
         int h = resultFrame.size().height;
         radius = std::min(w, h) / 8;
@@ -94,23 +91,12 @@
     }
 
     drawCenterCircle();
-    copyByLine(bgrFrame->linesize[0]);
+    copyByLine(rgbFrame->linesize[0]);
 
     //======================================================================================
     // REPLACE AVFRAME DATA WITH FRAME DATA
-    if (bgrFrame->data[0]) {
-        uint8_t* frameData = bgrFrame->data[0];
-        if (angle == 90 || angle == -90) {
-            std::memmove(frameData,
-                         resultFrame.data,
-                         static_cast<size_t>(pluginFrame->width * pluginFrame->height * 3)
-                             * sizeof(uint8_t));
-        }
-
-        moveFrom(pluginFrame, bgrFrame);
-    }
-    av_frame_unref(bgrFrame);
-    av_frame_free(&bgrFrame);
+    rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_YUV420P));
+    moveFrom(pluginFrame, rgbFrame.get());
 }
 
 void
diff --git a/HelloWorld/CenterCircleVideoSubscriber.h b/HelloWorld/CenterCircleVideoSubscriber.h
index e13713f..29481f7 100644
--- a/HelloWorld/CenterCircleVideoSubscriber.h
+++ b/HelloWorld/CenterCircleVideoSubscriber.h
@@ -24,7 +24,6 @@
 #include <libavutil/frame.h>
 }
 #include <observer.h>
-#include <frameScaler.h>
 #include <opencv2/core.hpp>
 
 namespace jami {
@@ -49,7 +48,6 @@
 
     // Data
     std::string path_;
-    FrameScaler scaler;
 
     // Status variables of the processing
     bool firstRun {true};
diff --git a/HelloWorld/CoinCircleVideoSubscriber.cpp b/HelloWorld/CoinCircleVideoSubscriber.cpp
index 46e8b8c..b3e7720 100644
--- a/HelloWorld/CoinCircleVideoSubscriber.cpp
+++ b/HelloWorld/CoinCircleVideoSubscriber.cpp
@@ -25,6 +25,7 @@
 }
 #include <accel.h>
 #include <frameUtils.h>
+#include <frameScaler.h>
 #include <pluglog.h>
 #include <stdio.h>
 #include <opencv2/imgproc.hpp>
@@ -65,19 +66,15 @@
     // GET RAW FRAME
     // Use a non-const Frame
     // Convert input frame to RGB
-    int inputHeight = pluginFrame->height;
-    int inputWidth = pluginFrame->width;
-    AVFrame* temp = transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12);
-    AVFrame* bgrFrame = scaler.convertFormat(temp, AV_PIX_FMT_RGB24);
-    av_frame_unref(temp);
-    av_frame_free(&temp);
-    if (!bgrFrame)
+    uniqueFramePtr rgbFrame = {transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12), frameFree};
+    rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_RGB24));
+    if (!rgbFrame.get())
         return;
-    resultFrame = cv::Mat {bgrFrame->height,
-                           bgrFrame->width,
+    resultFrame = cv::Mat {rgbFrame->height,
+                           rgbFrame->width,
                            CV_8UC3,
-                           bgrFrame->data[0],
-                           static_cast<size_t>(bgrFrame->linesize[0])};
+                           rgbFrame->data[0],
+                           static_cast<size_t>(rgbFrame->linesize[0])};
 
     // First clone the frame as the original one is unusable because of
     // linespace
@@ -94,23 +91,12 @@
     }
 
     drawCoinCircle(angle);
-    copyByLine(bgrFrame->linesize[0]);
+    copyByLine(rgbFrame->linesize[0]);
 
     //======================================================================================
     // REPLACE AVFRAME DATA WITH FRAME DATA
-    if (bgrFrame->data[0]) {
-        uint8_t* frameData = bgrFrame->data[0];
-        if (angle == 90 || angle == -90) {
-            std::memmove(frameData,
-                         resultFrame.data,
-                         static_cast<size_t>(pluginFrame->width * pluginFrame->height * 3)
-                             * sizeof(uint8_t));
-        }
-
-        moveFrom(pluginFrame, bgrFrame);
-    }
-    av_frame_unref(bgrFrame);
-    av_frame_free(&bgrFrame);
+    rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_YUV420P));
+    moveFrom(pluginFrame, rgbFrame.get());
 }
 
 void
diff --git a/HelloWorld/CoinCircleVideoSubscriber.h b/HelloWorld/CoinCircleVideoSubscriber.h
index be1d466..373f0b8 100644
--- a/HelloWorld/CoinCircleVideoSubscriber.h
+++ b/HelloWorld/CoinCircleVideoSubscriber.h
@@ -24,7 +24,6 @@
 #include <libavutil/frame.h>
 }
 #include <observer.h>
-#include <frameScaler.h>
 #include <opencv2/core.hpp>
 
 namespace jami {
@@ -49,7 +48,6 @@
 
     // Data
     std::string path_;
-    FrameScaler scaler;
 
     // Status variables of the processing
     bool firstRun {true};
diff --git a/HelloWorld/build.sh b/HelloWorld/build.sh
index 953f7a3..75f14c1 100755
--- a/HelloWorld/build.sh
+++ b/HelloWorld/build.sh
@@ -2,7 +2,7 @@
 # Build the plugin for the project
 set -e
 export OSTYPE
-ARCH=$(arch)
+ARCH=$(uname -m)
 EXTRAPATH=''
 # Flags:
 
@@ -23,11 +23,19 @@
 CONTRIB_PATH="${DAEMON}/contrib"
 PLUGINS_LIB="../lib"
 LIBS_DIR="./../contrib/Libs"
+PLATFORM=$(uname)
 
-if [ -z "${PLATFORM}" ]; then
+if [ "${PLATFORM}" = "Linux" ]; then
     PLATFORM="linux-gnu"
-    echo "PLATFORM not provided, building with ${PLATFORM}"
-    echo "Other options: redhat-linux"
+    CONTRIB_PLATFORM_CURT=${ARCH}
+    echo "Building with ${PLATFORM}"
+elif [ "${PLATFORM}" = "Darwin" ]; then
+    PLATFORM="darwin"
+    SO_FILE_NAME="lib${PLUGIN_NAME}.dylib"
+    alias nproc='sysctl -n hw.logicalcpu'
+    CONTRIB_PLATFORM_CURT=${ARCH}-apple
+    CONTRIB_PLATFORM_EXTRA=$(uname -r)
+    echo "Building with ${PLATFORM}"
 fi
 
 while getopts t:c:p OPT; do
@@ -50,7 +58,6 @@
 then
     python3 ./../SDK/jplManipulation.py --preassemble --plugin=${PLUGIN_NAME}
 
-    CONTRIB_PLATFORM_CURT=${ARCH}
     CONTRIB_PLATFORM=${CONTRIB_PLATFORM_CURT}-${PLATFORM}
 
     # Compile
@@ -78,7 +85,55 @@
     -lopencv_imgproc \
     -lopencv_core \
     -lva \
-    -o "build-local/jpl/lib/${CONTRIB_PLATFORM_CURT}-linux-gnu/${SO_FILE_NAME}"
+    -o "build-local/jpl/lib/${CONTRIB_PLATFORM}/${SO_FILE_NAME}"
+
+elif [ "${PLATFORM}" = "darwin" ]
+then
+    python3 ./../SDK/jplManipulation.py --preassemble --plugin=${PLUGIN_NAME}
+
+    CONTRIB_PLATFORM=${CONTRIB_PLATFORM_CURT}-${PLATFORM}
+
+    # Compile
+    clang++ -std=c++17 -shared -fPIC \
+    -Wl,-no_compact_unwind -Wl,-framework,CoreFoundation \
+    -Wl,-framework,Security -Wl,-framework,VideoToolbox \
+    -Wl,-framework,CoreMedia -Wl,-framework,CoreVideo \
+    -Wl,-framework,OpenCl -Wl,-framework,Accelerate \
+    -Wl,-rpath,"\${ORIGIN}" \
+    -Wall -Wextra \
+    -Wno-unused-variable \
+    -Wno-unused-function \
+    -Wno-unused-parameter \
+    -I"." \
+    -I"${DAEMON_SRC}" \
+    -I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}${CONTRIB_PLATFORM_EXTRA}/include" \
+    -I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}${CONTRIB_PLATFORM_EXTRA}/include/opencv4" \
+    -I"${PLUGINS_LIB}" \
+    ./../lib/accel.cpp \
+    ./../lib/frameUtils.cpp \
+    CoinCircleMediaHandler.cpp \
+    CenterCircleVideoSubscriber.cpp \
+    CenterCircleMediaHandler.cpp \
+    CoinCircleVideoSubscriber.cpp \
+    main.cpp \
+    -L"${CONTRIB_PATH}/${CONTRIB_PLATFORM}${CONTRIB_PLATFORM_EXTRA}/lib/" \
+    -lswscale \
+    -lavutil \
+    -lopencv_imgproc \
+    -lopencv_core \
+    -lvpx -lx264 -lbz2 -liconv -lz -lspeex -lopus \
+    -o "build-local/jpl/lib/${CONTRIB_PLATFORM}/${SO_FILE_NAME}"
+
+    install_name_tool -id "@loader_path/${SO_FILE_NAME}" "build-local/jpl/lib/${CONTRIB_PLATFORM}/${SO_FILE_NAME}"
+
+    if [ -n "${APPLE_SIGN_CERTIFICATE}" ]; then
+      codesign --force --verify --timestamp -o runtime --sign "${APPLE_SIGN_CERTIFICATE}"  "build-local/jpl/lib/${CONTRIB_PLATFORM}/${SO_FILE_NAME}"
+      ditto -c -k --rsrc "build-local/jpl/lib/${CONTRIB_PLATFORM}/${SO_FILE_NAME}" "build-local/${SO_FILE_NAME}.zip"
+      LIBRARYNAME=${PLUGIN_NAME} sh ./../notarize.sh
+      cd ..
+      ditto -x -k "build-local/${SO_FILE_NAME}.zip" "build-local/notarized"
+      cp "build-local/notarized/${SO_FILE_NAME}" "build-local/jpl/lib/${CONTRIB_PLATFORM}/${SO_FILE_NAME}"
+    fi
 
 elif [ "${PLATFORM}" = "android" ]
 then