Browse Source

Compile support on Windows with mingw-w64

Murat Dogan 5 years ago
parent
commit
93e153398f
8 changed files with 118 additions and 2 deletions
  1. 20 2
      CMakeLists.txt
  2. 6 0
      src/candidate.cpp
  3. 5 0
      src/icetransport.cpp
  4. 2 0
      src/sctptransport.cpp
  5. 5 0
      src/sctptransport.hpp
  6. 24 0
      test/main.cpp
  7. 28 0
      test/p2p/answerer.cpp
  8. 28 0
      test/p2p/offerer.cpp

+ 20 - 2
CMakeLists.txt

@@ -16,6 +16,12 @@ endif()
 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
 
+if(WIN32)
+	if (MSYS OR MINGW)
+		add_definitions(-DSCTP_STDINT_INCLUDE=<stdint.h>)
+	endif()
+endif()
+
 set(LIBDATACHANNEL_SOURCES
 	${CMAKE_CURRENT_SOURCE_DIR}/src/candidate.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/certificate.cpp
@@ -59,9 +65,15 @@ target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inclu
 target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
 target_include_directories(datachannel PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/deps/plog/include)
 target_link_libraries(datachannel
-						Threads::Threads
-						Usrsctp::UsrsctpStatic
+					Threads::Threads
+					Usrsctp::UsrsctpStatic
+					)
+if(WIN32)
+	target_link_libraries(datachannel
+						"wsock32"   #winsock2
+    					"ws2_32"    #winsock2
 						)
+endif()
 
 add_library(datachannel-static STATIC EXCLUDE_FROM_ALL ${LIBDATACHANNEL_SOURCES})
 set_target_properties(datachannel-static PROPERTIES
@@ -76,6 +88,12 @@ target_link_libraries(datachannel-static
 						Threads::Threads
 						Usrsctp::UsrsctpStatic
 						)
+if(WIN32)
+	target_link_libraries(datachannel-static
+						"wsock32"   #winsock2
+    					"ws2_32"    #winsock2
+						)
+endif()
 
 if (USE_GNUTLS)
 	find_package(GnuTLS REQUIRED)

+ 6 - 0
src/candidate.cpp

@@ -22,8 +22,14 @@
 #include <array>
 #include <sstream>
 
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#elif __linux__
 #include <netdb.h>
 #include <sys/socket.h>
+#endif
+
 #include <sys/types.h>
 
 using std::array;

+ 5 - 0
src/icetransport.cpp

@@ -19,9 +19,14 @@
 #include "icetransport.hpp"
 #include "configuration.hpp"
 
+#ifdef _WIN32
+#include <winsock2.h>
+#elif __linux__
 #include <netdb.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
+#endif
+
 #include <sys/types.h>
 
 #include <iostream>

+ 2 - 0
src/sctptransport.cpp

@@ -23,7 +23,9 @@
 #include <iostream>
 #include <vector>
 
+#ifdef __linux__
 #include <arpa/inet.h>
+#endif
 
 using namespace std::chrono_literals;
 using namespace std::chrono;

+ 5 - 0
src/sctptransport.hpp

@@ -29,7 +29,12 @@
 #include <map>
 #include <mutex>
 
+#ifdef _WIN32
+#include <winsock2.h>
+#elif __linux__
 #include <sys/socket.h>
+#endif
+
 #include <sys/types.h>
 
 #include "usrsctp.h"

+ 24 - 0
test/main.cpp

@@ -23,6 +23,10 @@
 #include <memory>
 #include <thread>
 
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
+
 using namespace rtc;
 using namespace std;
 
@@ -43,6 +47,20 @@ int main(int argc, char **argv) {
 	auto pc1 = std::make_shared<PeerConnection>(config);
 	auto pc2 = std::make_shared<PeerConnection>(config);
 
+#ifdef _WIN32
+	WSADATA wsaData;
+	int iResult;
+
+	// Initialize Winsock
+	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
+	if (iResult != 0) {
+		std::string err("WSAStartup failed. Error:");
+		err.append(WSAGetLastError() + "");
+		std::cout << err;
+		return -1;
+	}
+#endif
+
 	pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](const Description &sdp) {
 		auto pc2 = wpc2.lock();
 		if (!pc2)
@@ -127,9 +145,15 @@ int main(int argc, char **argv) {
 		pc2->close();
 
 		cout << "Success" << endl;
+#ifdef _WIN32
+		WSACleanup();
+#endif
 		return 0;
 	} else {
 		cout << "Failure" << endl;
+#ifdef _WIN32
+		WSACleanup();
+#endif
 		return 1;
 	}
 }

+ 28 - 0
test/p2p/answerer.cpp

@@ -22,6 +22,10 @@
 #include <iostream>
 #include <memory>
 
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
+
 using namespace rtc;
 using namespace std;
 
@@ -41,9 +45,26 @@ int main(int argc, char **argv) {
 
 	auto pc = std::make_shared<PeerConnection>(config);
 
+#ifdef _WIN32
+	WSADATA wsaData;
+	int iResult;
+
+	// Initialize Winsock
+	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
+	if (iResult != 0) {
+		std::string err("WSAStartup failed. Error:");
+		err.append(WSAGetLastError() + "");
+		std::cout << err;
+		return -1;
+	}
+#endif
+
 	pc->onLocalDescription([](const Description &sdp) {
 		std::string s(sdp);
 		std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
+#ifdef _WIN32
+		std::replace(s.begin(), s.end(), '\r', static_cast<char>(95));
+#endif
 		cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
 	});
 
@@ -102,6 +123,9 @@ int main(int argc, char **argv) {
 				getline(cin, sdp);
 
 			std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
+#ifdef _WIN32
+			std::replace(sdp.begin(), sdp.end(), static_cast<char>(95), '\r');
+#endif
 			descPtr = std::make_unique<Description>(sdp, Description::Type::Offer,
 			                                        Description::Role::Passive);
 			pc->setRemoteDescription(*descPtr);
@@ -141,4 +165,8 @@ int main(int argc, char **argv) {
 		dc->close();
 	if (pc)
 		pc->close();
+
+#ifdef _WIN32
+	WSACleanup();
+#endif
 }

+ 28 - 0
test/p2p/offerer.cpp

@@ -22,6 +22,10 @@
 #include <iostream>
 #include <memory>
 
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
+
 using namespace rtc;
 using namespace std;
 
@@ -41,9 +45,26 @@ int main(int argc, char **argv) {
 
 	auto pc = std::make_shared<PeerConnection>(config);
 
+#ifdef _WIN32
+	WSADATA wsaData;
+	int iResult;
+
+	// Initialize Winsock
+	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
+	if (iResult != 0) {
+		std::string err("WSAStartup failed. Error:");
+		err.append(WSAGetLastError() + "");
+		std::cout << err;
+		return -1;
+	}
+#endif
+
 	pc->onLocalDescription([](const Description &sdp) {
 		std::string s(sdp);
 		std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
+#ifdef _WIN32
+		std::replace(s.begin(), s.end(), '\r', static_cast<char>(95));
+#endif
 		cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
 	});
 
@@ -100,6 +121,9 @@ int main(int argc, char **argv) {
 				getline(cin, sdp);
 
 			std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
+#ifdef _WIN32
+			std::replace(sdp.begin(), sdp.end(), static_cast<char>(95), '\r');
+#endif
 			descPtr = std::make_unique<Description>(sdp);
 			pc->setRemoteDescription(*descPtr);
 			break;
@@ -138,4 +162,8 @@ int main(int argc, char **argv) {
 		dc->close();
 	if (pc)
 		pc->close();
+
+#ifdef _WIN32
+	WSACleanup();
+#endif
 }