增加windows多设备投屏

This commit is contained in:
huanglinhuan
2025-12-19 15:28:38 +08:00
parent 16a066ac94
commit 2b47c4c2f3
3 changed files with 38 additions and 15 deletions

View File

@@ -20,7 +20,7 @@ NetworkSender::~NetworkSender() {
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);
if (socket_ == INVALID_SOCKET) return false;
@@ -28,11 +28,16 @@ bool NetworkSender::Initialize(const std::string& ip, int port) {
int sndBuf = 1024 * 1024; // 1MB
setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, (char*)&sndBuf, sizeof(sndBuf));
destAddr_.sin_family = AF_INET;
destAddr_.sin_port = htons(port);
inet_pton(AF_INET, ip.c_str(), &destAddr_.sin_addr);
destAddrs_.clear();
for (const auto& ip : ips) {
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) {
@@ -75,10 +80,12 @@ bool NetworkSender::SendFrame(const std::vector<uint8_t>& data, uint64_t timesta
memcpy(packet.data(), &transHeader, HEADER_SIZE);
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_));
if (sent < 0) {
// std::cerr << "Send failed" << std::endl;
// Continue sending other fragments anyway
for (const auto& addr : destAddrs_) {
int sent = sendto(socket_, (const char*)packet.data(), (int)packet.size(), 0, (sockaddr*)&addr, sizeof(addr));
if (sent < 0) {
// std::cerr << "Send failed" << std::endl;
// Continue sending other fragments anyway
}
}
}

View File

@@ -21,10 +21,10 @@ public:
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);
private:
SOCKET socket_ = INVALID_SOCKET;
sockaddr_in destAddr_;
std::vector<sockaddr_in> destAddrs_;
};

View File

@@ -2,21 +2,37 @@
#include "ScreenCapture.h"
#include "VideoEncoder.h"
#include <iostream>
#include <sstream>
#include <vector>
#include <thread>
#include <chrono>
#include <fstream>
int main(int argc, char* argv[]) {
std::string ip = "127.0.0.1";
std::string ipStr = "127.0.0.1";
int port = 8888;
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 > 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 << "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()) {
std::cout << "Output File: " << outputFileName << std::endl;
}
@@ -60,7 +76,7 @@ int main(int argc, char* argv[]) {
}
NetworkSender sender;
if (!sender.Initialize(ip, port)) {
if (!sender.Initialize(ips, port)) {
std::cerr << "Failed to initialize Network Sender" << std::endl;
return 1;
}