#include <chrono>
#include <exception>
#include <fstream>
#include <future>
#include <iostream>
#include <string>
#include <vector>
#include <cvb/block.hpp>
#include <cvb/global.hpp>
#include <cvb/rtpstreaming/rtpstreaming.hpp>
#include <cvb/rtpstreaming/stream.hpp>
#include <cvb/rtpstreaming/stream_config.hpp>
#include "DeviceSimulator.hpp"
std::atomic<bool> cancelFlag(false);
void CheckForCancel() {
std::cin.get();
cancelFlag.store(true);
}
int main() {
try {
auto config = Cvb::RTPStreaming::StreamConfig::Create();
config->SetDstAddr("127.0.0.1");
config->SetDstPort(6005);
auto codecFile =
Cvb::String(CVB_LIT(
"tutorial\\RTPStreaming\\Images\\h264.json"));
std::ifstream codecFileStream(codecFile);
if (!codecFileStream)
throw std::runtime_error("h264.json file not found");
std::stringstream buffer;
buffer << codecFileStream.rdbuf();
config->SetupCodecFromJSON(buffer.str());
auto sdp = config->ReadSessionDescriptionProtocol();
std::ofstream sdpFile("h264.sdp");
sdpFile << sdp;
sdpFile.close();
std::cout << "Wrote h264.sdp file to the current working directory"
<< std::endl;
auto deviceSim = DeviceSimulator(100);
std::cout.clear();
std::cout << "Stream successfully set up. You should now start ffplay to "
"receive the stream images."
<< std::endl;
std::cout << "ffplay should be started via the command line like this: "
<< std::endl;
std::cout << "ffplay -protocol_whitelist rtp,file,udp -i h264.sdp"
<< std::endl;
std::cout << "Press 'Enter' once ffplay is running to continue"
<< std::endl;
std::cin.get();
std::cout << "Streaming images... press 'Enter' to stop streaming"
<< std::endl;
std::thread cancelThread(CheckForCancel);
auto streamTask = std::async(std::launch::async, [&]() {
while (!cancelFlag.load()) {
auto start = std::chrono::high_resolution_clock::now();
stream->PushImage(*deviceSim.GetNextImage());
auto end = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(40) -
(end - start));
}
});
cancelThread.join();
streamTask.wait();
} catch (const std::exception &e) {
std::string msg = e.what();
if (msg.find("avutil") != std::string::npos)
std::cout << "FFmpeg not found. Make sure you have installed FFmpeg and "
"added it to your PATH environment variable. Error message: "
<< msg << std::endl;
else
std::cout << msg << std::endl;
}
return 0;
}
static std::unique_ptr< Stream > Create(const StreamConfig &streamConfig)