Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
WebStreaming/Cvb++/CppWebStreamingServer

This example program is located in your CVB installation under %CVB%Tutorial/WebStreaming/Cvb++/CppWebStreamingServer.

main.cpp:

// Example for sending JPEG compressed images via WebSockets.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
#include <iostream>
#include <thread>
#include <cvb/webstreaming/server.hpp>
#include <cvb/webstreaming/jpeg_converter.hpp>
#include <cvb/device_factory.hpp>
// must match the client
const Cvb::String ipAddress = CVB_LIT("127.0.0.1");
const int port = 1112;
int main(int /*argc*/, char* /*argv*/[])
{
try
{
// setup a streaming device
auto path = Cvb::InstallPath();
path += CVB_LIT("drivers/CVMock.vin");
auto device = Cvb::DeviceFactory::Open(path);
auto stream = device->Stream();
// setup the server
// [percent] (1-100)
jpegConverter->SetQuality(50);
// (1-16)
jpegConverter->SetScale(1);
auto server = Cvb::WebStreaming::Server::Create(ipAddress, port, jpegConverter);
bool running = true;
std::thread senderThread([&running, &server, stream]
{
try
{
stream->Start();
while (running)
{
try
{
auto waitResult = stream->WaitFor<Cvb::RingBufferImage>(std::chrono::milliseconds(500));
if (waitResult.Status != Cvb::WaitStatus::Ok)
throw std::runtime_error("wait status was not ok");
server->SendAsync(*waitResult.Image);
}
catch (const std::runtime_error& error)
{
std::cout << "sender loop error:" << std::endl;
std::cout << error.what() << std::endl;
std::cout << "retry after waiting for 500ms" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
stream->TryAbort();
}
catch (const std::runtime_error& error)
{
std::cout << "sender thread error:" << std::endl;
std::cout << error.what() << std::endl;
}
});
(void)getchar();
running = false;
if (senderThread.joinable())
senderThread.join();
}
catch (const std::runtime_error& error)
{
std::cout << "main error:" << std::endl;
std::cout << error.what() << std::endl;
}
}
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
static JPEGConverterPtr Create()
static ServerPtr Create(const String &ipAddress, int port, const ConverterPtr &converter)
std::string String
String InstallPath()