wcWebCamClient lib  v0.8.2
media_test/main.cpp
/*===============================================================*/
/* This is an example of how to use the wcWebCamClient library. */
/* Simple class for parsing command line */
/* commandline_tools.h */
/* */
/* Part of wcWebCamClient library project */
/* */
/* Copyright 2022 Ilya Medvedkov */
/*===============================================================*/
#ifndef COMMLINE_TOOLS_H_INCLUDED
#define COMMLINE_TOOLS_H_INCLUDED
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
class InputParser{
public:
InputParser (int &argc, char **argv){
for (int i=1; i < argc; ++i)
this->tokens.push_back(std::string(argv[i]));
}
const std::string& getCmdOption(const std::string &option) const{
std::vector<std::string>::const_iterator itr;
itr = std::find(this->tokens.begin(), this->tokens.end(), option);
if (itr != this->tokens.end() && ++itr != this->tokens.end()){
return *itr;
}
static const std::string empty_string("");
return empty_string;
}
bool cmdOptionExists(const std::string &option) const{
return std::find(this->tokens.begin(), this->tokens.end(), option)
!= this->tokens.end();
}
const std::string& getHost() {
static const std::string CMD_HOST("--host");
static const std::string CMD_HOST_SYN("-u");
const std::string & res = getCmdOption(CMD_HOST);
if (res.empty()) {
return getCmdOption(CMD_HOST_SYN);
} else
return res;
}
const std::string& getProxy() {
static const std::string CMD_PROXY("--proxy");
static const std::string CMD_PROXY_SYN("-p");
const std::string & res = getCmdOption(CMD_PROXY);
if (res.empty()) {
return getCmdOption(CMD_PROXY_SYN);
} else
return res;
}
const std::string& getDevice() {
static const std::string CMD_DEVICE("--device");
static const std::string CMD_DEVICE_SYN("-d");
const std::string & res = getCmdOption(CMD_DEVICE);
if (res.empty()) {
return getCmdOption(CMD_DEVICE_SYN);
} else
return res;
}
const std::string& getUserName() {
static const std::string CMD_USER_NAME("--name");
static const std::string CMD_USER_NAME_SYN("-n");
const std::string & res = getCmdOption(CMD_USER_NAME);
if (res.empty()) {
return getCmdOption(CMD_USER_NAME_SYN);
} else
return res;
}
const std::string& getUserPassword() {
static const std::string CMD_USER_PWRD("--pwrd");
static const std::string CMD_USER_PWRD_SYN("-x");
const std::string & res = getCmdOption(CMD_USER_PWRD);
if (res.empty()) {
return getCmdOption(CMD_USER_PWRD_SYN);
} else
return res;
}
bool ignoreTLSCert() {
static const std::string OPT_TLS_IGNORE("-k");
return cmdOptionExists(OPT_TLS_IGNORE);
}
bool showHelp() {
static const std::string OPT_SHOW_HELP("-h");
return cmdOptionExists(OPT_SHOW_HELP);
}
virtual void printCommonHelp() {
if (tokens.size() > 0)
std::cout << "Usage:" << tokens.at(0) << " [options...]" << std::endl;
std::cout << " -u, --host URL for server host in format https://hostname:port" << std::endl;
std::cout << " -p, --proxy Proxy in format [[username][:password]@][proxyurl:port]" << std::endl;
std::cout << " -d, --device Device name" << std::endl;
std::cout << " -n, --name Login name" << std::endl;
std::cout << " -x, --pwrd Login password" << std::endl;
std::cout << " -k Ignore TLS certificate errors" << std::endl;
std::cout << " -h Show this help" << std::endl;
}
private:
std::vector <std::string> tokens;
};
#endif // COMMLINE_TOOLS_H_INCLUDED

This is an example of how to use the wcWebCamClient library. A simple test program to demonstrate working with media records.

/*===============================================================*/
/* This is an example of how to use the wcWebCamClient library. */
/* In this example, a client is created, authorized on the */
/* server, uploads a media record and downloads it to disk. */
/* */
/* Part of wcWebCamClient library project */
/* */
/* Copyright 2022 Ilya Medvedkov */
/*===============================================================*/
#include <iostream>
#include <fstream>
#include <wcwebcamclient.h>
#include <chrono>
#include <thread>
#include <wcJSON.h>
#include "../commline_tools.h"
using namespace std;
static uint8_t running = 0;
static const string input_media_file("morti.png");
static const string cRID("rid");
static const string cMETA("meta");
static const string cTIMESTAMP("stamp");
static string metadata("");
static int rid;
/* Callback. CURL was initialized successfully. */
void onCURLinit(wcHandle self, wcTask task)
{
cout << "CURL initialized" << endl;
}
/* Callback. Authorization was successful. */
void onAuth(wcHandle self, wcTask task)
{
char * res = NULL;
/* Get a new session id and display it on the screen. */
if (wcClientGetStrValue(self, wcstSID, &res) == WC_OK) {
if (res) {
cout << res << endl;
free(res);
}
}
running = 2;
}
/* Callback. The client has been disabled. */
void onDisconnect(wcHandle self, wcTask task)
{
cout << "Client disconnected" << endl;
running = 5;
}
/* Callback. Connection state changed. */
void onConnChanged(wcHandle self, int state)
{
cout << "Client connection changed " << state << endl;
}
/* Callback. Log was changed. */
void onLog(wcHandle self, const char * str)
{
/* Display new log entry. */
cout << str << endl;
}
/* Callback. The list of media records was changed. */
void getRecords(wcHandle self, wcTask task, const char * jArr)
{
wcJSON jarr(jArr);
if (jarr.isArray()) {
int i = jarr.length() - 1;
if (i >= 0) {
wcJSON obj = jarr.getArrayItem(i);
if (obj.isObject()) {
wcJSON jrid = obj.getObjItem(cRID.c_str());
if (jrid.isNumber()) {
rid = jrid.valueInt();
running = 5;
return;
}
}
}
}
// error in server response
running = 10;
}
/* Callback. The media record metadata received. */
void getRecordMeta(wcHandle self, wcTask task, const char * jObj)
{
wcJSON j(jObj);
if (j.isValid()) {
wcJSON meta = j.getObjItem(cMETA.c_str());
if (meta.isString()) {
metadata = string(meta.valueString());
running = 6;
return;
}
}
// error in server response
running = 10;
}
/* Callback. The request to save the media record has been completed. The response has arrived. */
void afterSaveRecord(wcHandle self, wcTask task)
{
char * file_name = NULL;
if ((wcTaskGetUserData(task, (void**)&file_name) == WC_OK) && file_name) {
cout << "file " << file_name << " sended" << endl;
}
running = 3;
}
/* Callback. The request to get the media record has been completed. The response has arrived. */
void getRecordData(wcHandle client, wcTask tsk, void* data, size_t sz)
{
string output_file = "record" + to_string(rid) + "." + metadata;
ofstream outfile(output_file, ios::out | ios::trunc | ios::binary);
if (!outfile.is_open())
{
cerr << "Can't open output file. Wrong file name or the file is blocked: " << output_file << endl;
return;
}
outfile.write((const char*)data, sz);
outfile.close();
running = 10;
}
int main(int argc, char * argv[])
{
/* Parse command line */
InputParser input(argc, argv);
const std::string &host = input.getHost();
const std::string &proxy = input.getProxy();
const std::string &user = input.getUserName();
const std::string &pwrd = input.getUserPassword();
std::string device = input.getDevice();
if (device.empty()) device = "device_test001";
if (input.showHelp()) {
/* Show help doc */
input.printCommonHelp();
return 0;
}
if (!host.empty()){
/* Main part */
/* Configure certificate */
if (input.ignoreTLSCert())
/* Configure proxy */
if (!proxy.empty())
wcClientSetStrValue(client, wcstProxy, proxy.c_str());
/* Configure host */
wcClientSetStrValue(client, wcstHostName, host.c_str());
/* Configure callbacks */
wcSetNotifyCallback(client, wccbkInitCURL, onCURLinit);
wcSetNotifyCallback(client, wccbkDisconnect, onDisconnect);
wcSetConnCallback(client, onConnChanged);
/* Configure callbacks to work with media records */
wcSetTaskCallback(client, wccbkSuccessSaveRecord, afterSaveRecord);
/* Start client */
wcClientStart(client);
int time_out = 100;
while (running < 10)
{
switch (running)
{
case 0:
{
/* Authorize client */
wcClientSetStrValue(client, wcstDeviceName, device.c_str());
wcClientAuth(client, user.c_str(), pwrd.c_str());
running = 1;
break;
}
case 2:
{
/* Read input_media_file and send to server */
ifstream fp (input_media_file, ios::binary);
if (!fp.is_open()) {
cerr << input_media_file << ": error: " << errno << endl;
return 1;
}
fp.seekg (0, ios::end);
long fsize = fp.tellg();
fp.seekg (0, ios::beg);
char * mem = (char*)malloc(fsize);
fp.read (mem, fsize);
fp.close();
wcSaveRecord(client, mem, fsize, "PNG", (void*) input_media_file.c_str());
running = 4;
break;
}
case 3:
{
/* Update list of media records to get the rid of the last one */
running = 4;
break;
}
case 5:
{
/* Get the record metadata */
wcRequestRecordMeta(client, rid, NULL);
running = 4;
break;
}
case 6:
{
/* Get the record data */
wcRequestRecord(client, rid, NULL);
running = 4;
break;
}
default:
{
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(300));
/* Proceed client */
wcClientProceed(client);
time_out--;
if (time_out <= 0) {
cout << "timeout" << endl;
break;
}
}
/* Disconnect client */
/* Destroy client */
wcClientDestroy(client);
return 0;
} else {
cout << "should contain at least --host (host-name) option";
}
}
wcRCode DLLEXPORT wcSetTaskCallback(wcHandle client, wcCallback callbackId, TaskNotifyLibFunc func)
Set specified task notify callback for client.
wcRCode DLLEXPORT wcSetNotifyCallback(wcHandle client, wcCallback callbackId, NotifyEventLibFunc func)
Set specified notify callback for client.
wcRCode DLLEXPORT wcSetCStringCallback(wcHandle client, wcCallback callbackId, CStringNotifyLibFunc func)
Set specified notify callback for client to handling the C-style string values.
wcRCode DLLEXPORT wcSetAltDataCallback(wcHandle client, wcCallback callbackId, DataAltNotifyEventLibFunc func)
Set specified notify callback for client to handling the sized data values.
wcRCode DLLEXPORT wcSetJSONStrCallback(wcHandle client, wcCallback callbackId, JSONStrNotifyEventLibFunc func)
Set specified notify callback for client to process the JSON-formatted response results represented a...
wcRCode DLLEXPORT wcSetConnCallback(wcHandle client, ConnNotifyEventLibFunc func)
Set specified connection notify callback for client.
@ wccbkAddLog
Added new log entry.
Definition: wcwebcamclient.h:140
@ wccbkSuccessRequestRecord
The request to get the media record has been completed.
Definition: wcwebcamclient.h:150
@ wccbkSuccessRequestRecordMeta
The request to get metadata for the media record has been completed.
Definition: wcwebcamclient.h:158
@ wccbkSuccessSaveRecord
The request to save the media record has been completed.
Definition: wcwebcamclient.h:149
@ wccbkInitCURL
Successful initialization of the multiCURL handle.
Definition: wcwebcamclient.h:135
@ wccbkSuccessAuth
Successful authorization.
Definition: wcwebcamclient.h:136
@ wccbkSuccessUpdateRecords
The request to update list of records has been completed.
Definition: wcwebcamclient.h:153
@ wccbkDisconnect
Client has been disconnected.
Definition: wcwebcamclient.h:138
wcRCode DLLEXPORT wcClientGetStrValue(wcHandle client, wcStateId aStateId, char **aStateVal)
Get a C-style string value for the selected client state.
wcRCode DLLEXPORT wcClientInvalidateState(wcHandle client, wcStateId aStateId)
Reset the selected client state.
wcRCode DLLEXPORT wcClientSetStrValue(wcHandle client, wcStateId aStateId, const char *aStateVal)
Set a C-style string value to the selected client state.
wcRCode DLLEXPORT wcClientSetBoolState(wcHandle client, wcStateId aStateId, wcStateVal aStateVal)
Set the boolean value to the selected client state.
@ wcstDeviceName
Definition: wcwebcamclient.h:187
@ wcstSID
Definition: wcwebcamclient.h:188
@ wcstProxy
Definition: wcwebcamclient.h:190
@ wcstHostName
Definition: wcwebcamclient.h:189
@ wcstVerifyTLS
Definition: wcwebcamclient.h:175
@ wcstRecords
Definition: wcwebcamclient.h:181
wcHandle DLLEXPORT wcClientCreate()
Create client.
wcRCode DLLEXPORT wcClientTasksProceed(wcHandle client)
Call the synchronous client update stage.
wcRCode DLLEXPORT wcClientDisconnect(wcHandle client)
Disconnect client from the server host.
wcRCode DLLEXPORT wcClientProceed(wcHandle client)
Call the asynchronous client update stage.
wcRCode DLLEXPORT wcClientAuth(wcHandle client, const char *aLogin, const char *aPwrd)
Authorize client on the server host.
wcRCode DLLEXPORT wcClientDestroy(wcHandle client)
Destroy client.
wcRCode DLLEXPORT wcClientStart(wcHandle client)
Launch client.
wcRCode DLLEXPORT wcRequestRecord(wcHandle client, int rid, void *data)
Request specified media record from server for authorized client.
wcRCode DLLEXPORT wcRequestRecordMeta(wcHandle client, int rid, void *data)
Request metadata of specified media record from server for authorized client.
wcRCode DLLEXPORT wcSaveRecord(wcHandle client, const void *aBuf, size_t sz, const char *meta, void *data)
Send a media record to server from authorized client.
const wcRCode WC_OK
Definition: wcwebcamclient.h:74
const wcRCode WC_FALSE
Definition: wcwebcamclient.h:71
wcRCode DLLEXPORT wcTaskGetUserData(wcTask task, void **data)
Get the user data for the task.
int wcHandle
Client handle.
Definition: wcwebcamclient.h:99
wcCallbackTask wcTask
Pointer to task.
Definition: wcwebcamclient.h:102