2025-12-18 23:07:14 +08:00
|
|
|
#include "NetworkSender.h"
|
|
|
|
|
#include "ScreenCapture.h"
|
|
|
|
|
#include "VideoEncoder.h"
|
|
|
|
|
#include <iostream>
|
2025-12-19 15:28:38 +08:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <vector>
|
2025-12-18 23:07:14 +08:00
|
|
|
#include <thread>
|
|
|
|
|
#include <chrono>
|
2025-12-18 23:36:08 +08:00
|
|
|
#include <fstream>
|
2025-12-18 23:07:14 +08:00
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2025-12-19 15:28:38 +08:00
|
|
|
std::string ipStr = "127.0.0.1";
|
2025-12-18 23:07:14 +08:00
|
|
|
int port = 8888;
|
2025-12-18 23:36:08 +08:00
|
|
|
std::string outputFileName = "";
|
2025-12-18 23:07:14 +08:00
|
|
|
|
2025-12-19 15:28:38 +08:00
|
|
|
if (argc > 1) ipStr = argv[1];
|
2025-12-18 23:07:14 +08:00
|
|
|
if (argc > 2) port = std::stoi(argv[2]);
|
2025-12-18 23:36:08 +08:00
|
|
|
if (argc > 3) outputFileName = argv[3];
|
2025-12-18 23:07:14 +08:00
|
|
|
|
2025-12-19 15:28:38 +08:00
|
|
|
// 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");
|
|
|
|
|
|
2025-12-18 23:07:14 +08:00
|
|
|
std::cout << "Starting Windows Sender Demo..." << std::endl;
|
2025-12-19 15:28:38 +08:00
|
|
|
std::cout << "Targets: ";
|
|
|
|
|
for (const auto& ip : ips) std::cout << ip << " ";
|
|
|
|
|
std::cout << ", Port: " << port << std::endl;
|
2025-12-18 23:36:08 +08:00
|
|
|
if (!outputFileName.empty()) {
|
|
|
|
|
std::cout << "Output File: " << outputFileName << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Debug: Open file to save H.264 stream if filename is provided
|
|
|
|
|
std::ofstream outFile;
|
|
|
|
|
if (!outputFileName.empty()) {
|
|
|
|
|
outFile.open(outputFileName, std::ios::binary);
|
|
|
|
|
if (outFile.is_open()) {
|
|
|
|
|
std::cout << "Debug: Saving video stream to '" << outputFileName << "'" << std::endl;
|
|
|
|
|
} else {
|
|
|
|
|
std::cerr << "Warning: Failed to open output file '" << outputFileName << "'" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-18 23:07:14 +08:00
|
|
|
|
|
|
|
|
ScreenCapture capture;
|
|
|
|
|
if (!capture.Initialize()) {
|
|
|
|
|
std::cerr << "Failed to initialize Screen Capture" << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get screen size
|
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
|
|
|
|
ComPtr<ID3D11Texture2D> frame;
|
|
|
|
|
// Capture one frame to get size
|
|
|
|
|
std::cout << "Waiting for first frame..." << std::endl;
|
|
|
|
|
while (!capture.CaptureFrame(frame)) {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
|
}
|
|
|
|
|
frame->GetDesc(&desc);
|
|
|
|
|
capture.ReleaseFrame();
|
|
|
|
|
|
|
|
|
|
int width = desc.Width;
|
|
|
|
|
int height = desc.Height;
|
|
|
|
|
std::cout << "Screen Size: " << width << "x" << height << std::endl;
|
|
|
|
|
|
|
|
|
|
VideoEncoder encoder;
|
|
|
|
|
if (!encoder.Initialize(capture.GetDevice(), width, height, 60, 4000000)) { // 4Mbps
|
|
|
|
|
std::cerr << "Failed to initialize Video Encoder" << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetworkSender sender;
|
2025-12-19 15:28:38 +08:00
|
|
|
if (!sender.Initialize(ips, port)) {
|
2025-12-18 23:07:14 +08:00
|
|
|
std::cerr << "Failed to initialize Network Sender" << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << "Streaming started. Press Ctrl+C to stop." << std::endl;
|
|
|
|
|
|
|
|
|
|
int frameCount = 0;
|
|
|
|
|
auto lastTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
ComPtr<ID3D11Texture2D> texture;
|
|
|
|
|
if (capture.CaptureFrame(texture)) {
|
|
|
|
|
std::vector<uint8_t> encodedData;
|
|
|
|
|
bool isKeyFrame = false;
|
|
|
|
|
|
|
|
|
|
if (encoder.EncodeFrame(texture.Get(), encodedData, isKeyFrame)) {
|
|
|
|
|
if (!encodedData.empty()) {
|
|
|
|
|
// Current timestamp in ms
|
|
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
|
|
|
uint64_t timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
|
|
|
|
|
|
|
|
|
sender.SendFrame(encodedData, timestamp, width, height, isKeyFrame);
|
|
|
|
|
// std::cout << "Sent frame: " << encodedData.size() << " bytes, Key: " << isKeyFrame << std::endl;
|
2025-12-18 23:36:08 +08:00
|
|
|
|
|
|
|
|
if (outFile.is_open()) {
|
|
|
|
|
outFile.write(reinterpret_cast<const char*>(encodedData.data()), encodedData.size());
|
|
|
|
|
}
|
2025-12-18 23:07:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
capture.ReleaseFrame();
|
|
|
|
|
|
|
|
|
|
frameCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
|
|
|
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastTime).count() >= 1) {
|
|
|
|
|
std::cout << "FPS: " << frameCount << std::endl;
|
|
|
|
|
frameCount = 0;
|
|
|
|
|
lastTime = now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't sleep too much, CaptureFrame waits.
|
|
|
|
|
// But if CaptureFrame returns immediately (high refresh rate), we might want to cap it?
|
|
|
|
|
// Desktop Duplication limits itself to screen refresh rate usually.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|