From 2fc5ef5c4b2faa03d467ae18da03fc1ab4df716c Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 01:30:07 +0330 Subject: [PATCH 01/15] Add cmake. --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ed605b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs +/out/build/x64-debug +/temp From 5724840e8e7fe6e8cc24e1d39d4b2ec43b8a7c90 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 01:30:17 +0330 Subject: [PATCH 02/15] Add cmake modules. --- CMakeLists.txt | 38 ++++++++ CMakePresets.json | 101 +++++++++++++++++++++ cmake/Modules/FindOPAL.cmake | 161 ++++++++++++++++++++++++++++++++++ cmake/Modules/FindPTLib.cmake | 147 +++++++++++++++++++++++++++++++ src/channels.cpp | 2 + src/includes.h | 10 ++- src/main.cpp | 7 +- src/main.h | 14 +-- 8 files changed, 466 insertions(+), 14 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 CMakePresets.json create mode 100644 cmake/Modules/FindOPAL.cmake create mode 100644 cmake/Modules/FindPTLib.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8246382 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,38 @@ +# ITNOA +# CMakeList.txt : CMake project for SipCmd, include source and define +# project specific logic here. +# +cmake_minimum_required (VERSION 3.8) +# TODO: Correct path +set(CMAKE_TOOLCHAIN_FILE D:/Source/Repos/vcpkg/scripts/buildsystems/vcpkg.cmake) + +project ("SipCmd" VERSION 1.0 LANGUAGES CXX) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") + +set(PTLib_USE_STATIC_LIBS 1) +find_package(PTLib REQUIRED) + +set(OPAL_USE_STATIC_LIBS 1) +find_package(OPAL REQUIRED) + +# Add source to this project's library. +add_library(SipCmdLib "src/commands.cpp" "src/channels.cpp") + +target_include_directories(SipCmdLib PUBLIC ${PTLIB_INCLUDE_DIRS}) +target_link_libraries(SipCmdLib PRIVATE PTLib) + +target_include_directories(SipCmdLib PUBLIC ${OPAL_INCLUDE_DIRS}) +target_link_libraries(SipCmdLib PRIVATE OPAL) + +# Add source to this project's executable. +add_executable (SipCmd "src/main.cpp") + +target_link_libraries(SipCmd PRIVATE SipCmdLib) +message(${PTLIB_INCLUDE_DIRS}) + +if (CMAKE_VERSION VERSION_GREATER 3.12) + set_property(TARGET SipCmdLib PROPERTY CXX_STANDARD 14) + set_property(TARGET SipCmd PROPERTY CXX_STANDARD 14) +endif() + +# TODO: Add tests and install targets if needed. diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..f4bc98b --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,101 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "windows-base", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_C_COMPILER": "cl.exe", + "CMAKE_CXX_COMPILER": "cl.exe" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + } + }, + { + "name": "x64-debug", + "displayName": "x64 Debug", + "inherits": "windows-base", + "architecture": { + "value": "x64", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "x64-release", + "displayName": "x64 Release", + "inherits": "x64-debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "x86-debug", + "displayName": "x86 Debug", + "inherits": "windows-base", + "architecture": { + "value": "x86", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "x86-release", + "displayName": "x86 Release", + "inherits": "x86-debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "linux-debug", + "displayName": "Linux Debug", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Linux" + }, + "vendor": { + "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { + "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" + } + } + }, + { + "name": "macos-debug", + "displayName": "macOS Debug", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Darwin" + }, + "vendor": { + "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { + "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" + } + } + } + ] +} diff --git a/cmake/Modules/FindOPAL.cmake b/cmake/Modules/FindOPAL.cmake new file mode 100644 index 0000000..23c3346 --- /dev/null +++ b/cmake/Modules/FindOPAL.cmake @@ -0,0 +1,161 @@ +#------------------------------------------------------------------------------------ +# ITNOA +# +# This file is based on https://github.com/snikulov/cmake-modules +# +# Locate OPAL library (http://www.opalvoip.org/) +# This module defines +# OPAL_FOUND, if false, do not try to link to OPAL +# OPAL_LIBRARIES +# OPAL_INCLUDE_DIRS, where to find opal headers +# +# 2013/03/19 - aagenosov +# Module created +# 2013/03/21 - aagenosov +# Added macro to define version of OPAL +# 2015/08/13 - snikulov +# Added linux support +#------------------------------------------------------------------------------------- + +# get version macro +# first param - path to include +macro(opal_get_version _include_PATH version) + if (EXISTS "${_include_PATH}/opal/buildopts.h") + file(STRINGS "${_include_PATH}/opal/buildopts.h" _VER_STRING_AUX REGEX ".*#define[ ]+OPAL_VERSION[ ]+") + else() + file(STRINGS "${_include_PATH}/opal_config.h" _VER_STRING_AUX REGEX ".*#define[ ]+OPAL_VERSION[ ]+") + endif() + string(REGEX MATCHALL "[0-9]+[.][0-9]+[.][0-9]+" ${version} "${_VER_STRING_AUX}") +endmacro() + +find_package(PTLib REQUIRED) + +include(FindPkgConfig) +PKG_CHECK_MODULES(PC_OPAL "opal") + +find_path(OPAL_INCLUDE_DIRS opal.h + PATHS + ${PC_OPAL_INCLUDE_DIRS} + /usr/local/include + /usr/include + /opt/local/include + /opt/csw/include + /opt/include + $ENV{OPAL_DIR}/include + ${OPAL_DIR}/include + ) + +if(PC_OPAL_FOUND) + + set(OPAL_VERSION ${PC_OPAL_VERSION}) + set(OPAL_INCLUDE_DIRS ${PC_OPAL_INCLUDE_DIRS}) + + find_library(OPAL_LIBRARIES + NAMES ${PC_OPAL_LIBRARIES} + PATH ${PC_OPAL_LIBRARY_DIRS}) + +else() + + if(OPAL_USE_STATIC_LIBS) + set(opal_postfix "${opal_postfix}S") + endif() + + set(OPAL_NAME_RELEASE "opal${opal_postfix}") + set(OPAL_NAME_DEBUG "opal${opal_postfix}D") + set(OPAL64_NAME_RELEASE "opal64${opal_postfix}") + set(OPAL64_NAME_DEBUG "opal64${opal_postfix}D") + + + find_library(OPAL_LIBRARY_RELEASE + NAMES + ${OPAL_NAME_RELEASE} + ${OPAL64_NAME_RELEASE} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{OPAL_DIR}/lib + ${OPAL_DIR}/lib + NO_DEFAULT_PATH + ) + + find_library(OPAL_LIBRARY_DEBUG + NAMES + ${OPAL_NAME_DEBUG} + ${OPAL64_NAME_DEBUG} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{OPAL_DIR}/lib + ${OPAL_DIR}/lib + NO_DEFAULT_PATH + ) + + if(OPAL_INCLUDE_DIRS) + opal_get_version(${OPAL_INCLUDE_DIRS} OPAL_VERSION) + endif() + + if(OPAL_LIBRARY_DEBUG AND OPAL_LIBRARY_RELEASE) + set(OPAL_LIBRARIES + debug ${OPAL_LIBRARY_DEBUG} + optimized ${OPAL_LIBRARY_RELEASE} + CACHE STRING "OPAL Libraries") + endif() +endif() + +include(FindPackageHandleStandardArgs) + +# handle the QUIETLY and REQUIRED arguments and set OPAL_FOUND to TRUE if +# all listed variables are TRUE +FIND_PACKAGE_HANDLE_STANDARD_ARGS(OPAL DEFAULT_MSG OPAL_LIBRARIES OPAL_INCLUDE_DIRS) + +MARK_AS_ADVANCED(OPAL_INCLUDE_DIRS OPAL_LIBRARIES + OPAL_LIBRARY_DEBUG OPAL_LIBRARY_RELEASE) + +if(OPAL_FOUND) + message("-- OPAL version is: ${OPAL_VERSION}") + # short hack for install and copy + if(NOT OPAL_USE_STATIC_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Windows") + find_file(OPAL_DLL_RELEASE + NAMES + ${OPAL_NAME_RELEASE}.dll ${OPAL64_NAME_RELEASE}.dll + PATHS + $ENV{OPAL_DIR}/bin + ${OPAL_DIR}/bin + NO_DEFAULT_PATH + ) + + find_file(OPAL_DLL_DEBUG + NAMES + ${OPAL_NAME_DEBUG}.dll ${OPAL64_NAME_DEBUG}.dll + PATHS + $ENV{OPAL_DIR}/bin + ${OPAL_DIR}/bin + NO_DEFAULT_PATH + ) + get_filename_component(OPAL_RUNTIME_DIR ${OPAL_DLL_DEBUG} PATH) + MARK_AS_ADVANCED(OPAL_DLL_DEBUG OPAL_DLL_RELEASE OPAL_RUNTIME_DIR) + endif() + + if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + set(PATH_TO_OPAL_PLUGINS ${OPAL_RUNTIME_DIR}/plugins) + + if (EXISTS ${PATH_TO_OPAL_PLUGINS} AND IS_DIRECTORY ${PATH_TO_OPAL_PLUGINS}) + file(GLOB _opal_plugins_ "${PATH_TO_OPAL_PLUGINS}/*.dll") + endif() + + set(OPAL_PLUGINS) + foreach(_plugin ${_opal_plugins_}) + list(APPEND OPAL_PLUGINS ${_plugin}) + endforeach() + message("-- OPAL plugins: ${OPAL_PLUGINS}") + MARK_AS_ADVANCED(PATH_TO_OPAL_PLUGINS OPAL_PLUGINS) + endif() +endif() diff --git a/cmake/Modules/FindPTLib.cmake b/cmake/Modules/FindPTLib.cmake new file mode 100644 index 0000000..e0c5727 --- /dev/null +++ b/cmake/Modules/FindPTLib.cmake @@ -0,0 +1,147 @@ +#------------------------------------------------------------------------------------- +# ITNOA +# +# This file is based on https://github.com/snikulov/cmake-modules +# +# Locate PTLib library (http://www.opalvoip.org/) +# This module defines +# PTLIB_FOUND, if false, do not try to link to PTLib +# PTLIB_LIBRARIES +# PTLIB_INCLUDE_DIRS, where to find ptlib headers +# +# 2013/03/19 - aagenosov +# Module created +# 2013/03/21 - aagenosov +# Added macro to define version of PTLib +## 2015/08/13 - snikulov +# Added linux support +#------------------------------------------------------------------------------------- + +# get version macro +# first param - path to include +macro(ptlib_get_version _include_PATH version) + if(EXISTS "${_include_PATH}/ptbuildopts.h") + file(STRINGS "${_include_PATH}/ptbuildopts.h" _VER_STRING_AUX REGEX ".*#define[ ]+PTLIB_VERSION[ ]+") + else() + file(STRINGS "${_include_PATH}/ptlib_config.h" _VER_STRING_AUX REGEX ".*#define[ ]+PTLIB_VERSION[ ]+") + endif() + string(REGEX MATCHALL "[0-9]+[.][0-9]+[.][0-9]+" ${version} "${_VER_STRING_AUX}") +endmacro() + +include(FindPkgConfig) +PKG_CHECK_MODULES(PC_PTLIB "ptlib") + +find_path(PTLIB_INCLUDE_DIRS ptlib.h + PATHS + ${PC_PTLIB_INCLUDE_DIRS} + /usr/local/include + /usr/include + /opt/local/include + /opt/csw/include + /opt/include + $ENV{PTLIB_DIR}/include + ${PTLIB_DIR}/include + ) + +if(PC_PTLIB_FOUND) + + set(PTLIB_VERSION ${PC_PTLIB_VERSION}) + set(PTLIB_INCULDE_DIRS ${PC_PTLIB_INCLUDE_DIRS}) + + find_library(PTLIB_LIBRARIES + NAMES ${PC_PTLIB_LIBRARIES} + PATH ${PC_PTLIB_LIBRARY_DIRS}) + +else() + + if(PTLib_USE_STATIC_LIBS) + set(ptlib_postfix "${ptlib_postfix}S") + endif() + + set(PTLIB_NAME_RELEASE "ptlib${ptlib_postfix}") + set(PTLIB_NAME_DEBUG "ptlib${ptlib_postfix}D") + set(PTLIB64_NAME_RELEASE "ptlib64${ptlib_postfix}") + set(PTLIB64_NAME_DEBUG "ptlib64${ptlib_postfix}D") + + + find_library(PTLIB_LIBRARY_RELEASE + NAMES + ${PTLIB_NAME_RELEASE} + ${PTLIB64_NAME_RELEASE} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{PTLIB_DIR}/lib + ${PTLIB_DIR}/lib + NO_DEFAULT_PATH + ) + + find_library(PTLIB_LIBRARY_DEBUG + NAMES + ${PTLIB_NAME_DEBUG} + ${PTLIB64_NAME_DEBUG} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{PTLIB_DIR}/lib + ${PTLIB_DIR}/lib + NO_DEFAULT_PATH + ) + + if(PTLIB_INCLUDE_DIRS) + ptlib_get_version(${PTLIB_INCLUDE_DIRS} PTLIB_VERSION) + endif() + + if(PTLIB_LIBRARY_DEBUG AND PTLIB_LIBRARY_RELEASE) + set(PTLIB_LIBRARIES + debug ${PTLIB_LIBRARY_DEBUG} + optimized ${PTLIB_LIBRARY_RELEASE} + CACHE STRING "PTLib Libraries") + endif() +endif() + +include(FindPackageHandleStandardArgs) + +# handle the QUIETLY and REQUIRED arguments and set PTLIB_FOUND to TRUE if +# all listed variables are TRUE +FIND_PACKAGE_HANDLE_STANDARD_ARGS(PTLib DEFAULT_MSG PTLIB_LIBRARIES PTLIB_INCLUDE_DIRS) + +MARK_AS_ADVANCED(PTLIB_INCLUDE_DIRS PTLIB_LIBRARIES + PTLIB_LIBRARY_DEBUG PTLIB_LIBRARY_RELEASE PTLIB_VERSION) + +if(PTLIB_FOUND) + message("-- PTLib version is: ${PTLIB_VERSION}") + + # if we found the ptlib - and using dll's + # short hack for install and copy + if(NOT PTLib_USE_STATIC_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Windows") + find_file(PTLIB_DLL_RELEASE + NAMES + ${PTLIB_NAME_RELEASE}.dll ${PTLIB64_NAME_RELEASE}.dll + PATHS + $ENV{PTLIB_DIR}/bin + ${PTLIB_DIR}/bin + NO_DEFAULT_PATH + ) + + find_file(PTLIB_DLL_DEBUG + NAMES + ${PTLIB_NAME_DEBUG}.dll ${PTLIB64_NAME_DEBUG}.dll + PATHS + $ENV{PTLIB_DIR}/bin + ${PTLIB_DIR}/bin + NO_DEFAULT_PATH + ) + get_filename_component(PTLIB_RUNTIME_DIR ${PTLIB_DLL_DEBUG} PATH) + MARK_AS_ADVANCED(PTLIB_DLL_DEBUG PTLIB_DLL_RELEASE) + endif() + +endif() diff --git a/src/channels.cpp b/src/channels.cpp index 8b2fc80..25183c8 100644 --- a/src/channels.cpp +++ b/src/channels.cpp @@ -28,6 +28,8 @@ #include "main.h" #include "state.h" +using namespace std; + bool TestChanAudio::PlaybackAudio(const bool raw_rtp) { std::cout << __func__ << std::endl; diff --git a/src/includes.h b/src/includes.h index 58c4938..734bc23 100644 --- a/src/includes.h +++ b/src/includes.h @@ -23,12 +23,18 @@ #include #include #include + +#ifdef WIN32 +#include +#include +#else #include +#endif #ifdef DEBUG -#define debug cout +#define debug std::cout #else -#define debug 0 && cout +#define debug 0 && std::cout #endif #endif diff --git a/src/main.cpp b/src/main.cpp index 2b63bce..a5924e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -162,12 +162,15 @@ void signalHandler(int sig) { } void initSignalHandling() { + // FIXME + /* struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_flags = SA_RESTART; sa.sa_handler = signalHandler; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); + */ } TestProcess::TestProcess() : @@ -474,7 +477,7 @@ bool Manager::SendDTMF(const PString &dtmf) return ok; } -RTPSession::RTPSession(const Params& options) : RTP_UDP(options), m_audioformat(NULL) +RTPSession::RTPSession(const Init& options) : OpalRTPSession(options), m_audioformat(NULL) { std::cout << "RTP session created" << std::endl; } @@ -556,7 +559,7 @@ bool Manager::MakeCall(const PString &remoteParty) } // create rtp session - RTP_Session::Params p; + OpalRTPSession::Init p; p.id = OpalMediaType::Audio().GetDefinition()->GetDefaultSessionId(); p.encoding = OpalMediaType::Audio().GetDefinition()->GetRTPEncoding(); p.userData = new RTPUserData; diff --git a/src/main.h b/src/main.h index 1d9407b..09f82f2 100644 --- a/src/main.h +++ b/src/main.h @@ -22,6 +22,7 @@ #define CS_MANAGER_H #include "includes.h" +#include "rtp/rtp_session.h" class Manager; @@ -73,8 +74,8 @@ class LocalEndPoint : public OpalLocalEndPoint { }; -class RTPSession : public RTP_UDP { - PCLASSINFO(RTPSession, RTP_UDP); +class RTPSession : public OpalRTPSession { + PCLASSINFO(RTPSession, OpalRTPSession); public: enum Payload { @@ -83,7 +84,7 @@ class RTPSession : public RTP_UDP { G711_ULAW }; - RTPSession(const Params& options); + RTPSession(const Init& options); virtual SendReceiveStatus OnReceiveData( RTP_DataFrame &frame); @@ -102,13 +103,6 @@ class RTPSession : public RTP_UDP { // xxx }; -class RTPUserData : public RTP_UserData { - public: - RTPUserData() : RTP_UserData() {} - - virtual void OnTxStatistics(const RTP_Session &session); -}; - class Manager : public OpalManager { PCLASSINFO(Manager, OpalManager); From c53a22fe9222fe00062383e2d46b0c0a6d862eb5 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 01:35:57 +0330 Subject: [PATCH 03/15] Update gitignore. --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ed605b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs +/out/build/x64-debug +/temp From 4addd8386c23033c7864dd54e1a5d50785421f85 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 01:36:22 +0330 Subject: [PATCH 04/15] Removed nanosleep. (fixes #80) --- src/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2b63bce..c8367b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ #include #include +#include +#include #include #include "main.h" #include "commands.h" @@ -459,10 +461,8 @@ bool Manager::SendDTMF(const PString &dtmf) // sleep a while std::cout << "sent DTMF: [" << dtmf[i] << "]" << std::endl; - struct timespec tp; - tp.tv_sec = 0; - tp.tv_nsec = 500 * 1000 * 1000; // half a second - nanosleep (&tp, 0); + using namespace std::chrono_literals; + std::this_thread::sleep_for(500ms); } } ok = (i == dtmf.GetSize() - 1 ? true : false); From 63eb9d70cc56e72405bc3eaa688d578ccc6c3d6a Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 01:39:24 +0330 Subject: [PATCH 05/15] Update gitignore. --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ed605b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs +/out/build/x64-debug +/temp From 752f2f9a868069f900e0aed7e04989ec1e338eb4 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 10:37:05 +0330 Subject: [PATCH 06/15] Add cmake * Add cmake. (see #83) * Correct cmake dependencies. (fixes #83) --- .gitignore | 2 +- CMakeLists.txt | 36 ++++++++ CMakePresets.json | 101 +++++++++++++++++++++ cmake/Modules/FindOPAL.cmake | 161 ++++++++++++++++++++++++++++++++++ cmake/Modules/FindPTLib.cmake | 147 +++++++++++++++++++++++++++++++ src/commands.cpp | 2 +- 6 files changed, 447 insertions(+), 2 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 CMakePresets.json create mode 100644 cmake/Modules/FindOPAL.cmake create mode 100644 cmake/Modules/FindPTLib.cmake diff --git a/.gitignore b/.gitignore index 9ed605b..ed8b864 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ ################################################################################ /.vs -/out/build/x64-debug +/out /temp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..297d28b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,36 @@ +# ITNOA +# CMakeList.txt : CMake project for SipCmd, include source and define +# project specific logic here. +# +cmake_minimum_required (VERSION 3.8) + +project ("SipCmd" VERSION 1.0 LANGUAGES CXX) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") + +set(PTLib_USE_STATIC_LIBS 1) +find_package(PTLib REQUIRED) + +set(OPAL_USE_STATIC_LIBS 1) +find_package(OPAL REQUIRED) + +# Add source to this project's library. +add_library(SipCmdLib "src/commands.cpp" "src/channels.cpp") + +target_include_directories(SipCmdLib PUBLIC ${PTLIB_INCLUDE_DIRS}) +target_link_libraries(SipCmdLib PRIVATE ${PTLIB_LIBRARIES}) + +target_include_directories(SipCmdLib PUBLIC ${OPAL_INCLUDE_DIRS}) +target_link_libraries(SipCmdLib PRIVATE ${OPAL_LIBRARIES}) + +# Add source to this project's executable. +add_executable (SipCmd "src/main.cpp") + +target_link_libraries(SipCmd PRIVATE SipCmdLib) +message(${PTLIB_INCLUDE_DIRS}) + +if (CMAKE_VERSION VERSION_GREATER 3.10) + set_property(TARGET SipCmdLib PROPERTY CXX_STANDARD 14) + set_property(TARGET SipCmd PROPERTY CXX_STANDARD 14) +endif() + +# TODO: Add tests and install targets if needed. diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..f4bc98b --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,101 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "windows-base", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_C_COMPILER": "cl.exe", + "CMAKE_CXX_COMPILER": "cl.exe" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + } + }, + { + "name": "x64-debug", + "displayName": "x64 Debug", + "inherits": "windows-base", + "architecture": { + "value": "x64", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "x64-release", + "displayName": "x64 Release", + "inherits": "x64-debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "x86-debug", + "displayName": "x86 Debug", + "inherits": "windows-base", + "architecture": { + "value": "x86", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "x86-release", + "displayName": "x86 Release", + "inherits": "x86-debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "linux-debug", + "displayName": "Linux Debug", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Linux" + }, + "vendor": { + "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { + "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" + } + } + }, + { + "name": "macos-debug", + "displayName": "macOS Debug", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Darwin" + }, + "vendor": { + "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { + "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" + } + } + } + ] +} diff --git a/cmake/Modules/FindOPAL.cmake b/cmake/Modules/FindOPAL.cmake new file mode 100644 index 0000000..23c3346 --- /dev/null +++ b/cmake/Modules/FindOPAL.cmake @@ -0,0 +1,161 @@ +#------------------------------------------------------------------------------------ +# ITNOA +# +# This file is based on https://github.com/snikulov/cmake-modules +# +# Locate OPAL library (http://www.opalvoip.org/) +# This module defines +# OPAL_FOUND, if false, do not try to link to OPAL +# OPAL_LIBRARIES +# OPAL_INCLUDE_DIRS, where to find opal headers +# +# 2013/03/19 - aagenosov +# Module created +# 2013/03/21 - aagenosov +# Added macro to define version of OPAL +# 2015/08/13 - snikulov +# Added linux support +#------------------------------------------------------------------------------------- + +# get version macro +# first param - path to include +macro(opal_get_version _include_PATH version) + if (EXISTS "${_include_PATH}/opal/buildopts.h") + file(STRINGS "${_include_PATH}/opal/buildopts.h" _VER_STRING_AUX REGEX ".*#define[ ]+OPAL_VERSION[ ]+") + else() + file(STRINGS "${_include_PATH}/opal_config.h" _VER_STRING_AUX REGEX ".*#define[ ]+OPAL_VERSION[ ]+") + endif() + string(REGEX MATCHALL "[0-9]+[.][0-9]+[.][0-9]+" ${version} "${_VER_STRING_AUX}") +endmacro() + +find_package(PTLib REQUIRED) + +include(FindPkgConfig) +PKG_CHECK_MODULES(PC_OPAL "opal") + +find_path(OPAL_INCLUDE_DIRS opal.h + PATHS + ${PC_OPAL_INCLUDE_DIRS} + /usr/local/include + /usr/include + /opt/local/include + /opt/csw/include + /opt/include + $ENV{OPAL_DIR}/include + ${OPAL_DIR}/include + ) + +if(PC_OPAL_FOUND) + + set(OPAL_VERSION ${PC_OPAL_VERSION}) + set(OPAL_INCLUDE_DIRS ${PC_OPAL_INCLUDE_DIRS}) + + find_library(OPAL_LIBRARIES + NAMES ${PC_OPAL_LIBRARIES} + PATH ${PC_OPAL_LIBRARY_DIRS}) + +else() + + if(OPAL_USE_STATIC_LIBS) + set(opal_postfix "${opal_postfix}S") + endif() + + set(OPAL_NAME_RELEASE "opal${opal_postfix}") + set(OPAL_NAME_DEBUG "opal${opal_postfix}D") + set(OPAL64_NAME_RELEASE "opal64${opal_postfix}") + set(OPAL64_NAME_DEBUG "opal64${opal_postfix}D") + + + find_library(OPAL_LIBRARY_RELEASE + NAMES + ${OPAL_NAME_RELEASE} + ${OPAL64_NAME_RELEASE} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{OPAL_DIR}/lib + ${OPAL_DIR}/lib + NO_DEFAULT_PATH + ) + + find_library(OPAL_LIBRARY_DEBUG + NAMES + ${OPAL_NAME_DEBUG} + ${OPAL64_NAME_DEBUG} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{OPAL_DIR}/lib + ${OPAL_DIR}/lib + NO_DEFAULT_PATH + ) + + if(OPAL_INCLUDE_DIRS) + opal_get_version(${OPAL_INCLUDE_DIRS} OPAL_VERSION) + endif() + + if(OPAL_LIBRARY_DEBUG AND OPAL_LIBRARY_RELEASE) + set(OPAL_LIBRARIES + debug ${OPAL_LIBRARY_DEBUG} + optimized ${OPAL_LIBRARY_RELEASE} + CACHE STRING "OPAL Libraries") + endif() +endif() + +include(FindPackageHandleStandardArgs) + +# handle the QUIETLY and REQUIRED arguments and set OPAL_FOUND to TRUE if +# all listed variables are TRUE +FIND_PACKAGE_HANDLE_STANDARD_ARGS(OPAL DEFAULT_MSG OPAL_LIBRARIES OPAL_INCLUDE_DIRS) + +MARK_AS_ADVANCED(OPAL_INCLUDE_DIRS OPAL_LIBRARIES + OPAL_LIBRARY_DEBUG OPAL_LIBRARY_RELEASE) + +if(OPAL_FOUND) + message("-- OPAL version is: ${OPAL_VERSION}") + # short hack for install and copy + if(NOT OPAL_USE_STATIC_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Windows") + find_file(OPAL_DLL_RELEASE + NAMES + ${OPAL_NAME_RELEASE}.dll ${OPAL64_NAME_RELEASE}.dll + PATHS + $ENV{OPAL_DIR}/bin + ${OPAL_DIR}/bin + NO_DEFAULT_PATH + ) + + find_file(OPAL_DLL_DEBUG + NAMES + ${OPAL_NAME_DEBUG}.dll ${OPAL64_NAME_DEBUG}.dll + PATHS + $ENV{OPAL_DIR}/bin + ${OPAL_DIR}/bin + NO_DEFAULT_PATH + ) + get_filename_component(OPAL_RUNTIME_DIR ${OPAL_DLL_DEBUG} PATH) + MARK_AS_ADVANCED(OPAL_DLL_DEBUG OPAL_DLL_RELEASE OPAL_RUNTIME_DIR) + endif() + + if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + set(PATH_TO_OPAL_PLUGINS ${OPAL_RUNTIME_DIR}/plugins) + + if (EXISTS ${PATH_TO_OPAL_PLUGINS} AND IS_DIRECTORY ${PATH_TO_OPAL_PLUGINS}) + file(GLOB _opal_plugins_ "${PATH_TO_OPAL_PLUGINS}/*.dll") + endif() + + set(OPAL_PLUGINS) + foreach(_plugin ${_opal_plugins_}) + list(APPEND OPAL_PLUGINS ${_plugin}) + endforeach() + message("-- OPAL plugins: ${OPAL_PLUGINS}") + MARK_AS_ADVANCED(PATH_TO_OPAL_PLUGINS OPAL_PLUGINS) + endif() +endif() diff --git a/cmake/Modules/FindPTLib.cmake b/cmake/Modules/FindPTLib.cmake new file mode 100644 index 0000000..e0c5727 --- /dev/null +++ b/cmake/Modules/FindPTLib.cmake @@ -0,0 +1,147 @@ +#------------------------------------------------------------------------------------- +# ITNOA +# +# This file is based on https://github.com/snikulov/cmake-modules +# +# Locate PTLib library (http://www.opalvoip.org/) +# This module defines +# PTLIB_FOUND, if false, do not try to link to PTLib +# PTLIB_LIBRARIES +# PTLIB_INCLUDE_DIRS, where to find ptlib headers +# +# 2013/03/19 - aagenosov +# Module created +# 2013/03/21 - aagenosov +# Added macro to define version of PTLib +## 2015/08/13 - snikulov +# Added linux support +#------------------------------------------------------------------------------------- + +# get version macro +# first param - path to include +macro(ptlib_get_version _include_PATH version) + if(EXISTS "${_include_PATH}/ptbuildopts.h") + file(STRINGS "${_include_PATH}/ptbuildopts.h" _VER_STRING_AUX REGEX ".*#define[ ]+PTLIB_VERSION[ ]+") + else() + file(STRINGS "${_include_PATH}/ptlib_config.h" _VER_STRING_AUX REGEX ".*#define[ ]+PTLIB_VERSION[ ]+") + endif() + string(REGEX MATCHALL "[0-9]+[.][0-9]+[.][0-9]+" ${version} "${_VER_STRING_AUX}") +endmacro() + +include(FindPkgConfig) +PKG_CHECK_MODULES(PC_PTLIB "ptlib") + +find_path(PTLIB_INCLUDE_DIRS ptlib.h + PATHS + ${PC_PTLIB_INCLUDE_DIRS} + /usr/local/include + /usr/include + /opt/local/include + /opt/csw/include + /opt/include + $ENV{PTLIB_DIR}/include + ${PTLIB_DIR}/include + ) + +if(PC_PTLIB_FOUND) + + set(PTLIB_VERSION ${PC_PTLIB_VERSION}) + set(PTLIB_INCULDE_DIRS ${PC_PTLIB_INCLUDE_DIRS}) + + find_library(PTLIB_LIBRARIES + NAMES ${PC_PTLIB_LIBRARIES} + PATH ${PC_PTLIB_LIBRARY_DIRS}) + +else() + + if(PTLib_USE_STATIC_LIBS) + set(ptlib_postfix "${ptlib_postfix}S") + endif() + + set(PTLIB_NAME_RELEASE "ptlib${ptlib_postfix}") + set(PTLIB_NAME_DEBUG "ptlib${ptlib_postfix}D") + set(PTLIB64_NAME_RELEASE "ptlib64${ptlib_postfix}") + set(PTLIB64_NAME_DEBUG "ptlib64${ptlib_postfix}D") + + + find_library(PTLIB_LIBRARY_RELEASE + NAMES + ${PTLIB_NAME_RELEASE} + ${PTLIB64_NAME_RELEASE} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{PTLIB_DIR}/lib + ${PTLIB_DIR}/lib + NO_DEFAULT_PATH + ) + + find_library(PTLIB_LIBRARY_DEBUG + NAMES + ${PTLIB_NAME_DEBUG} + ${PTLIB64_NAME_DEBUG} + PATHS + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt + $ENV{PTLIB_DIR}/lib + ${PTLIB_DIR}/lib + NO_DEFAULT_PATH + ) + + if(PTLIB_INCLUDE_DIRS) + ptlib_get_version(${PTLIB_INCLUDE_DIRS} PTLIB_VERSION) + endif() + + if(PTLIB_LIBRARY_DEBUG AND PTLIB_LIBRARY_RELEASE) + set(PTLIB_LIBRARIES + debug ${PTLIB_LIBRARY_DEBUG} + optimized ${PTLIB_LIBRARY_RELEASE} + CACHE STRING "PTLib Libraries") + endif() +endif() + +include(FindPackageHandleStandardArgs) + +# handle the QUIETLY and REQUIRED arguments and set PTLIB_FOUND to TRUE if +# all listed variables are TRUE +FIND_PACKAGE_HANDLE_STANDARD_ARGS(PTLib DEFAULT_MSG PTLIB_LIBRARIES PTLIB_INCLUDE_DIRS) + +MARK_AS_ADVANCED(PTLIB_INCLUDE_DIRS PTLIB_LIBRARIES + PTLIB_LIBRARY_DEBUG PTLIB_LIBRARY_RELEASE PTLIB_VERSION) + +if(PTLIB_FOUND) + message("-- PTLib version is: ${PTLIB_VERSION}") + + # if we found the ptlib - and using dll's + # short hack for install and copy + if(NOT PTLib_USE_STATIC_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Windows") + find_file(PTLIB_DLL_RELEASE + NAMES + ${PTLIB_NAME_RELEASE}.dll ${PTLIB64_NAME_RELEASE}.dll + PATHS + $ENV{PTLIB_DIR}/bin + ${PTLIB_DIR}/bin + NO_DEFAULT_PATH + ) + + find_file(PTLIB_DLL_DEBUG + NAMES + ${PTLIB_NAME_DEBUG}.dll ${PTLIB64_NAME_DEBUG}.dll + PATHS + $ENV{PTLIB_DIR}/bin + ${PTLIB_DIR}/bin + NO_DEFAULT_PATH + ) + get_filename_component(PTLIB_RUNTIME_DIR ${PTLIB_DLL_DEBUG} PATH) + MARK_AS_ADVANCED(PTLIB_DLL_DEBUG PTLIB_DLL_RELEASE) + endif() + +endif() diff --git a/src/commands.cpp b/src/commands.cpp index 0a40a57..7addefd 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -465,7 +465,7 @@ bool Wait::ParseCommand( return false; } - sscanf(*cmds, "%u", &millis); + sscanf(*cmds, "%lu", &millis); *cmds = &((*cmds)[i]); sequence.push_back(this); return true; From 6cf8af687b008612ae81f78912a83c5230c32349 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 10:50:40 +0330 Subject: [PATCH 07/15] 80 standard time (#1) * Update gitignore. * Removed nanosleep. (fixes #80) --- src/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2b63bce..c8367b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ #include #include +#include +#include #include #include "main.h" #include "commands.h" @@ -459,10 +461,8 @@ bool Manager::SendDTMF(const PString &dtmf) // sleep a while std::cout << "sent DTMF: [" << dtmf[i] << "]" << std::endl; - struct timespec tp; - tp.tv_sec = 0; - tp.tv_nsec = 500 * 1000 * 1000; // half a second - nanosleep (&tp, 0); + using namespace std::chrono_literals; + std::this_thread::sleep_for(500ms); } } ok = (i == dtmf.GetSize() - 1 ? true : false); From 8c91cde2eea04262bc765c043b88eeca958b1886 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 10:51:54 +0330 Subject: [PATCH 08/15] Update .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9ed605b..ed8b864 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ ################################################################################ /.vs -/out/build/x64-debug +/out /temp From 05ae017c28e4389dff60f3ceb06f54b42a9c0152 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 12:42:42 +0330 Subject: [PATCH 09/15] Add vcpkg support. (#3) --- CMakePresets.json | 7 +++++++ bootstrap.sh | 2 ++ vcpkg.json | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 bootstrap.sh create mode 100644 vcpkg.json diff --git a/CMakePresets.json b/CMakePresets.json index f4bc98b..dd02e97 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -1,8 +1,14 @@ { "version": 3, "configurePresets": [ + { + "name": "all-base", + "hidden": true, + "toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + }, { "name": "windows-base", + "inherits": "all-base", "hidden": true, "generator": "Ninja", "binaryDir": "${sourceDir}/out/build/${presetName}", @@ -59,6 +65,7 @@ }, { "name": "linux-debug", + "inherits": "all-base", "displayName": "Linux Debug", "generator": "Ninja", "binaryDir": "${sourceDir}/out/build/${presetName}", diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100644 index 0000000..a8d0f6b --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,2 @@ +echo "export VCPKG_ROOT=~/vcpkg" >> ~/.env +source ~/.env \ No newline at end of file diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..a0ceefc --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", + "name": "sipcmd", + "version-string": "0.1.0", + "dependencies": [ + ] +} \ No newline at end of file From 020d98d6e21b421b9ac8fcd0820b2624c0cb6811 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 13:32:06 +0330 Subject: [PATCH 10/15] 85 cross platform time (#4) * Add date dependency * Add date as dependency. * Replace legacy time with chrono time. (fixes #85) --- CMakeLists.txt | 4 ++++ build.sh | 1 + src/commands.cpp | 17 +++++++++++------ vcpkg.json | 1 + 4 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 build.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 297d28b..5886c77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ find_package(PTLib REQUIRED) set(OPAL_USE_STATIC_LIBS 1) find_package(OPAL REQUIRED) +find_package(date CONFIG REQUIRED) + # Add source to this project's library. add_library(SipCmdLib "src/commands.cpp" "src/channels.cpp") @@ -22,6 +24,8 @@ target_link_libraries(SipCmdLib PRIVATE ${PTLIB_LIBRARIES}) target_include_directories(SipCmdLib PUBLIC ${OPAL_INCLUDE_DIRS}) target_link_libraries(SipCmdLib PRIVATE ${OPAL_LIBRARIES}) +target_link_libraries(SipCmdLib PRIVATE date::date date::date-tz) + # Add source to this project's executable. add_executable (SipCmd "src/main.cpp") diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..2f978a5 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=/home/ssoroosh/vcpkg/scripts/buildsystems/vcpkg.cmake \ No newline at end of file diff --git a/src/commands.cpp b/src/commands.cpp index 7addefd..a6c6df7 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -24,10 +24,16 @@ #include #include #include +#include + +#include #include "commands.h" #include "state.h" + using namespace date; + using namespace std::chrono; + //// // Command std::string Command::errorstring; @@ -157,12 +163,11 @@ bool Call::RunCommand(const std::string &loopsuffix) { // if one has been specified and there is no address for username PString rp = remoteparty; PString gw = TPState::Instance().GetGateway(); - char buf[256]; - time_t secsnow = time(NULL); + system_clock::time_point secsnow = system_clock::now(); if(rp.Find('@') == P_MAX_INDEX && !gw.IsEmpty()) { cout << "TestPhone::Main: calling \"" << rp << "\" using gateway \"" << gw << "\"" - << " at " << ctime_r(&secsnow, buf) << endl; + << " at " << secsnow << endl; switch (TPState::Instance().GetProtocol()) { case TPState::SIP: rp = "sip:" + rp + "@" + gw; break; @@ -176,7 +181,7 @@ bool Call::RunCommand(const std::string &loopsuffix) { } else { cout << "TestPhone::Main: calling \"" << rp << "\"" - << " at " << ctime_r(&secsnow, buf) << endl; + << " at " << secsnow << endl; } // dial out @@ -191,12 +196,12 @@ bool Call::RunCommand(const std::string &loopsuffix) { do { cout << "TestPhone::Main: calling \"" << rp << "\"" - << " for " << difftime(time(NULL), secsnow) << endl; + << " for " << duration_cast(system_clock::now() - secsnow).count() << endl; state = tpstate.WaitForStateChange(TPState::ESTABLISHED); if(state == TPState::TERMINATED) { errorstring = "Call: application terminated"; return false; - } else if (difftime(time(NULL), secsnow) > dialtimeout / 1000.0) { + } else if (duration_cast(system_clock::now() - secsnow).count() > dialtimeout / 1000.0) { errorstring = "Call: Dial timed out"; return false; } diff --git a/vcpkg.json b/vcpkg.json index a0ceefc..7edb91f 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -3,5 +3,6 @@ "name": "sipcmd", "version-string": "0.1.0", "dependencies": [ + "date" ] } \ No newline at end of file From ee13f2487b9746d5efca9071905a27682effd572 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 13:39:44 +0330 Subject: [PATCH 11/15] Remove ctime header (#5) * Add date dependency * Add date as dependency. * Replace legacy time with chrono time. (fixes #85) * Resolve some missing time function. (see #85) --- src/commands.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/commands.cpp b/src/commands.cpp index a6c6df7..a0b0849 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -240,9 +239,7 @@ bool Answer::ParseCommand( bool Answer::RunCommand(const std::string &loopsuffix) { std::cout << "## Answer ##" << std::endl; - char buf[256]; - time_t secsnow = time(NULL); - cout << "Answer: starting at " << ctime_r(&secsnow, buf) << endl; + cout << "Answer: starting at " << system_clock::now() << endl; // set up PString token; @@ -287,9 +284,7 @@ bool Hangup::ParseCommand( bool Hangup::RunCommand(const std::string &loopsuffix) { std::cout << "## Hangup ##" << std::endl; - char buf[256]; - time_t secsnow = time(NULL); - cout << "Hangup: at " << ctime_r(&secsnow, buf) << endl; + cout << "Hangup: at " << system_clock::now() << endl; TPState &tpstate = TPState::Instance(); // hangup @@ -597,12 +592,10 @@ bool Loop::RunCommand(const std::string &loopsuffix) { int timesleft = 0; do { - char buf[256]; - time_t secsnow = time(NULL); stringstream newsuffix; newsuffix << loopsuffix << "_" << timesleft; cout << "Loop: iteration \"" << newsuffix.str() - << "\" at " << ctime_r(&secsnow, buf) << endl; + << "\" at " << system_clock::now() << endl; if(!Command::Run(loopedsequence, newsuffix.str())) return false; From 721d47358fd294a06483e5c04fd7c9b8ac3508a7 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 13:49:36 +0330 Subject: [PATCH 12/15] remove another sleep. (see #80) --- src/commands.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/commands.cpp b/src/commands.cpp index 0a40a57..f650b3e 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -21,9 +21,11 @@ #include #include #include +#include #include #include #include +#include #include "commands.h" #include "state.h" @@ -497,7 +499,8 @@ bool Wait::RunCommand(const std::string &loopsuffix) { return true; } //cout << "Wait: usleep " << n << endl; - usleep(WAIT_SLEEP_ACCURACY * 1000); + using namespace std::chrono_literals; + std::this_thread::sleep_for((WAIT_SLEEP_ACCURACY * 1000)us); if(!closed && TPState::Instance().GetState() == TPState::TERMINATED) { errorstring = "Wait: application terminated"; From 06cbf4634a47df08acff5010649713db4558b43a Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 14:26:23 +0330 Subject: [PATCH 13/15] resolve compile error. --- src/commands.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/commands.cpp b/src/commands.cpp index f650b3e..8bad6ac 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -499,8 +499,7 @@ bool Wait::RunCommand(const std::string &loopsuffix) { return true; } //cout << "Wait: usleep " << n << endl; - using namespace std::chrono_literals; - std::this_thread::sleep_for((WAIT_SLEEP_ACCURACY * 1000)us); + std::this_thread::sleep_for(std::chrono::duration(WAIT_SLEEP_ACCURACY)); if(!closed && TPState::Instance().GetState() == TPState::TERMINATED) { errorstring = "Wait: application terminated"; From 3ff5c216c1b3b588acee5398afdb3804a60f12d1 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 14:36:20 +0330 Subject: [PATCH 14/15] Replace sscanf with sscanf_s --- src/commands.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commands.cpp b/src/commands.cpp index 19b1dbf..bae3b41 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -392,7 +392,7 @@ bool Record::ParseCommand( errorstring = "Record: No digits or invalid digits specified"; return false; } - sscanf(*cmds, "%u", &millis); + sscanf_s(*cmds, "%u", &millis); *cmds = &((*cmds)[i]); // filename for(i = 0U; (*cmds)[i] && (*cmds)[i] != ';'; i++); @@ -466,7 +466,7 @@ bool Wait::ParseCommand( return false; } - sscanf(*cmds, "%lu", &millis); + sscanf_s(*cmds, "%llu", &millis); *cmds = &((*cmds)[i]); sequence.push_back(this); return true; @@ -554,7 +554,7 @@ bool Loop::ParseCommand( size_t numdigits = strspn(*cmds, "0123456789"); if(numdigits && numdigits < 8) - sscanf(*cmds, "%d", &loops); + sscanf_s(*cmds, "%d", &loops); (*cmds) += numdigits; if(tolower(**cmds) == 'l') { From 20c3768c241f0dff4a65cf78e71db157140f032a Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Fri, 24 Feb 2023 18:32:35 +0330 Subject: [PATCH 15/15] Correct IsOpen --- src/channels.cpp | 4 ++-- src/main.cpp | 23 +++++++---------------- src/main.h | 6 +++--- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/channels.cpp b/src/channels.cpp index 25183c8..33bcd8f 100644 --- a/src/channels.cpp +++ b/src/channels.cpp @@ -401,7 +401,7 @@ OpalMediaStream *LocalConnection::CreateMediaStream( bool RawMediaStream::ReadData(BYTE *data, PINDEX size, PINDEX &length) { //cout << __func__ << endl; length = 0; - if (!isOpen) { + if (!IsOpen()) { cout << "channel not open!" << endl; return false; } @@ -432,7 +432,7 @@ bool RawMediaStream::WriteData(const BYTE *data, PINDEX length, PINDEX &written) { // cout << __func__ << endl; - if (!isOpen) { + if (!IsOpen()) { cout << "channel not open!" << endl; return false; } diff --git a/src/main.cpp b/src/main.cpp index 72570cc..a9b52a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -506,9 +506,8 @@ void RTPSession::SelectAudioFormat(const Payload payload) } } -RTP_Session::SendReceiveStatus RTPSession::OnReceiveData(RTP_DataFrame &frame) +OpalRTPSession::SendReceiveStatus RTPSession::OnReceiveData(RTP_DataFrame &frame) { - SendReceiveStatus ret = RTP_UDP::Internal_OnReceiveData(frame); #if 1 // master dump std::ostringstream os; frame.PrintOn(os); @@ -517,12 +516,11 @@ RTP_Session::SendReceiveStatus RTPSession::OnReceiveData(RTP_DataFrame &frame) TPState::Instance().GetRecordAudio().RecordFromBuffer( (const char*)frame.GetPayloadPtr(), frame.GetPayloadSize(), true); - return ret; + return OpalRTPSession::SendReceiveStatus::e_ProcessPacket; } -RTP_Session::SendReceiveStatus RTPSession::OnSendData(RTP_DataFrame &frame) +OpalRTPSession::SendReceiveStatus RTPSession::OnSendData(RTP_DataFrame &frame) { - SendReceiveStatus ret = RTP_UDP::Internal_OnSendData(frame); #if 1 // master dump std::ostringstream os; @@ -530,17 +528,16 @@ RTP_Session::SendReceiveStatus RTPSession::OnSendData(RTP_DataFrame &frame) std::cout << os.str() << std::endl; #endif - return ret; + return OpalRTPSession::SendReceiveStatus::e_IgnorePacket; } -RTP_Session::SendReceiveStatus RTPSession::OnReadTimeout(RTP_DataFrame &frame) { +OpalRTPSession::SendReceiveStatus RTPSession::OnReadTimeout(RTP_DataFrame &frame) { std::cout << __func__ << std::endl; TPState::Instance().GetRecordAudio().StopRecording(false); - return RTP_UDP::OnReadTimeout(frame); + return OpalRTPSession::SendReceiveStatus::e_AbortTransport; } - bool Manager::MakeCall(const PString &remoteParty) { cout << "Setting up a call to: " << remoteParty << endl; @@ -559,7 +556,7 @@ bool Manager::MakeCall(const PString &remoteParty) } // create rtp session - OpalRTPSession::Init p; + OpalRTPSession::Init p(); p.id = OpalMediaType::Audio().GetDefinition()->GetDefaultSessionId(); p.encoding = OpalMediaType::Audio().GetDefinition()->GetRTPEncoding(); p.userData = new RTPUserData; @@ -665,12 +662,6 @@ bool Manager::OnOpenMediaStream(OpalConnection &connection, return true; } -void RTPUserData::OnTxStatistics(const RTP_Session &session) -{ - cout << __func__ << endl; -} - - OpalConnection::AnswerCallResponse Manager::OnAnswerCall( OpalConnection &connection, const PString &caller) diff --git a/src/main.h b/src/main.h index 09f82f2..5445bfe 100644 --- a/src/main.h +++ b/src/main.h @@ -87,13 +87,13 @@ class RTPSession : public OpalRTPSession { RTPSession(const Init& options); virtual SendReceiveStatus OnReceiveData( - RTP_DataFrame &frame); + RTP_DataFrame &frame) override; virtual SendReceiveStatus OnSendData( - RTP_DataFrame &frame); + RTP_DataFrame &frame) override; virtual SendReceiveStatus OnReadTimeout( - RTP_DataFrame &frame); + RTP_DataFrame &frame) override; void SelectAudioFormat(const Payload p); OpalAudioFormat &GetAudioFormat() const { return *m_audioformat; }