增加windows多设备投屏
This commit is contained in:
@@ -20,7 +20,7 @@ NetworkSender::~NetworkSender() {
|
|||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetworkSender::Initialize(const std::string& ip, int port) {
|
bool NetworkSender::Initialize(const std::vector<std::string>& ips, int port) {
|
||||||
socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (socket_ == INVALID_SOCKET) return false;
|
if (socket_ == INVALID_SOCKET) return false;
|
||||||
|
|
||||||
@@ -28,11 +28,16 @@ bool NetworkSender::Initialize(const std::string& ip, int port) {
|
|||||||
int sndBuf = 1024 * 1024; // 1MB
|
int sndBuf = 1024 * 1024; // 1MB
|
||||||
setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, (char*)&sndBuf, sizeof(sndBuf));
|
setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, (char*)&sndBuf, sizeof(sndBuf));
|
||||||
|
|
||||||
destAddr_.sin_family = AF_INET;
|
destAddrs_.clear();
|
||||||
destAddr_.sin_port = htons(port);
|
for (const auto& ip : ips) {
|
||||||
inet_pton(AF_INET, ip.c_str(), &destAddr_.sin_addr);
|
sockaddr_in addr = {};
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
inet_pton(AF_INET, ip.c_str(), &addr.sin_addr);
|
||||||
|
destAddrs_.push_back(addr);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return !destAddrs_.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetworkSender::SendFrame(const std::vector<uint8_t>& data, uint64_t timestamp, int width, int height, bool isKeyFrame) {
|
bool NetworkSender::SendFrame(const std::vector<uint8_t>& data, uint64_t timestamp, int width, int height, bool isKeyFrame) {
|
||||||
@@ -75,12 +80,14 @@ bool NetworkSender::SendFrame(const std::vector<uint8_t>& data, uint64_t timesta
|
|||||||
memcpy(packet.data(), &transHeader, HEADER_SIZE);
|
memcpy(packet.data(), &transHeader, HEADER_SIZE);
|
||||||
memcpy(packet.data() + HEADER_SIZE, buffer.data() + offset, chunkSize);
|
memcpy(packet.data() + HEADER_SIZE, buffer.data() + offset, chunkSize);
|
||||||
|
|
||||||
int sent = sendto(socket_, (const char*)packet.data(), (int)packet.size(), 0, (sockaddr*)&destAddr_, sizeof(destAddr_));
|
for (const auto& addr : destAddrs_) {
|
||||||
|
int sent = sendto(socket_, (const char*)packet.data(), (int)packet.size(), 0, (sockaddr*)&addr, sizeof(addr));
|
||||||
if (sent < 0) {
|
if (sent < 0) {
|
||||||
// std::cerr << "Send failed" << std::endl;
|
// std::cerr << "Send failed" << std::endl;
|
||||||
// Continue sending other fragments anyway
|
// Continue sending other fragments anyway
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ public:
|
|||||||
NetworkSender();
|
NetworkSender();
|
||||||
~NetworkSender();
|
~NetworkSender();
|
||||||
|
|
||||||
bool Initialize(const std::string& ip, int port);
|
bool Initialize(const std::vector<std::string>& ips, int port);
|
||||||
bool SendFrame(const std::vector<uint8_t>& data, uint64_t timestamp, int width, int height, bool isKeyFrame);
|
bool SendFrame(const std::vector<uint8_t>& data, uint64_t timestamp, int width, int height, bool isKeyFrame);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SOCKET socket_ = INVALID_SOCKET;
|
SOCKET socket_ = INVALID_SOCKET;
|
||||||
sockaddr_in destAddr_;
|
std::vector<sockaddr_in> destAddrs_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,21 +2,37 @@
|
|||||||
#include "ScreenCapture.h"
|
#include "ScreenCapture.h"
|
||||||
#include "VideoEncoder.h"
|
#include "VideoEncoder.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
std::string ip = "127.0.0.1";
|
std::string ipStr = "127.0.0.1";
|
||||||
int port = 8888;
|
int port = 8888;
|
||||||
std::string outputFileName = "";
|
std::string outputFileName = "";
|
||||||
|
|
||||||
if (argc > 1) ip = argv[1];
|
if (argc > 1) ipStr = argv[1];
|
||||||
if (argc > 2) port = std::stoi(argv[2]);
|
if (argc > 2) port = std::stoi(argv[2]);
|
||||||
if (argc > 3) outputFileName = argv[3];
|
if (argc > 3) outputFileName = argv[3];
|
||||||
|
|
||||||
|
// Parse IPs
|
||||||
|
std::vector<std::string> ips;
|
||||||
|
std::stringstream ss(ipStr);
|
||||||
|
std::string item;
|
||||||
|
while (std::getline(ss, item, ',')) {
|
||||||
|
// Trim spaces might be good but let's assume no spaces for simplicity or user should handle
|
||||||
|
if (!item.empty()) {
|
||||||
|
ips.push_back(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ips.empty()) ips.push_back("127.0.0.1");
|
||||||
|
|
||||||
std::cout << "Starting Windows Sender Demo..." << std::endl;
|
std::cout << "Starting Windows Sender Demo..." << std::endl;
|
||||||
std::cout << "Target: " << ip << ":" << port << std::endl;
|
std::cout << "Targets: ";
|
||||||
|
for (const auto& ip : ips) std::cout << ip << " ";
|
||||||
|
std::cout << ", Port: " << port << std::endl;
|
||||||
if (!outputFileName.empty()) {
|
if (!outputFileName.empty()) {
|
||||||
std::cout << "Output File: " << outputFileName << std::endl;
|
std::cout << "Output File: " << outputFileName << std::endl;
|
||||||
}
|
}
|
||||||
@@ -60,7 +76,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NetworkSender sender;
|
NetworkSender sender;
|
||||||
if (!sender.Initialize(ip, port)) {
|
if (!sender.Initialize(ips, port)) {
|
||||||
std::cerr << "Failed to initialize Network Sender" << std::endl;
|
std::cerr << "Failed to initialize Network Sender" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user