Qt5 to stream two videos simultaneously - camera

I created a GUI to play two videos from two cameras, one camera is a USB camera, and another one is integrated in my laptop. My code is simple, I have three files:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraImageCapture>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QList <QCameraInfo> camList;
QCamera *camera1;
QCamera *camera2;
private slots:
void onCameraChanged(int);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
camera1 = NULL;
camera2 = NULL;
connect(ui->cameraComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,
&MainWindow::onCameraChanged);
camList = QCameraInfo::availableCameras();
for(QList <QCameraInfo>::iterator it = camList.begin();it!=camList.end();++it) {
ui->cameraComboBox->addItem(it->description());
}
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onCameraChanged(int idx) {
if(camera1 != NULL) {
camera1->stop();
}
qDebug() << idx;
camera1 = new QCamera(camList.at(idx),this);
camera1->setViewfinder(ui->mainView);
camera1->setCaptureMode(QCamera::CaptureStillImage);
camera1->start();
if(camera2 != NULL) {
camera2->stop();
}
camera2 = new QCamera(camList.at(idx+1),this);
camera2->setViewfinder(ui->submainView);
camera2->setCaptureMode(QCamera::CaptureStillImage);
camera2->start();
}
I have deleted irrelevant code. Of course I have this in the *.pro file:
QT += multimedia multimediawidgets
My code compiles correctly, but it can only stream one video (camera1), and the other one shows nothing. The error I got is
CameraBin error: "Error starting streaming on device '/dev/video1'."
CameraBin error: "Could not negotiate format"
Any help will be appreciated.

Related

Integrate boost-asio with file descriptor based socket api from KDB+

I'm relatively new to boost-asio and I'm wondering if it's possible to make it work with KDB+ api here.
I tried something like the below but it doesn't seem to work properly,
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#define KXVER 3
#include "kx/k.h"
using boost::asio::ip::tcp;
namespace posix = boost::asio::posix;
class Feedhandler
{
public:
Feedhandler(boost::asio::io_service &io_service) : m_qsvc(io_service) {
char host[] = "localhost";
int port = 6812;
m_fd = khpu(host, port, "user:pass");
m_qsvc.assign(m_fd);
start_operations();
K ret = k(m_fd, ".u.sub", ks(""), ks(""), (K)0);
}
void start_operations()
{
boost::asio::async_read(m_qsvc, boost::asio::null_buffers(),
boost::bind(&Feedhandler::handle_read, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(const boost::system::error_code& error, size_t size)
{
K data = k(m_fd,(S)0);
start_operations();
}
private:
int m_fd;
posix::stream_descriptor m_qsvc;
};
int main(int argc, char* argv[])
{
boost::asio::io_service io_service;
Feedhandler fh(io_service);
io_service.run();
return 0;
}
The handle_read method gets hit once and then subsequently there's no more call-backs.
Actually, it's better to use async_wait instead of async_read, like below,
m_qsvc.async_wait(boost::asio::posix::stream_descriptor::wait_read,
boost::bind(&Feedhandler::handle_read, this, boost::asio::placeholders::error) );

vkCreateInstance access violation at VKLayer_device_simulation.dll

i'm new to vulkan.
when i call vkCreateInstance() , it crash , but i can't figure out what's the problem.
vkenumerateExtensionProperties return 0 extensoin count,and only VKLayer_LUNARG_api_dump found , it is weird .
core code are following , irrelevant code are removed.
Instance_Vulkan
#pragma once
#ifndef INSTANCE_VULKAN_HPP
#define INSTANCE_VULKAN_HPP
#include <vulkan/vulkan.h>
#include <vector>
#include <string>
#include <exception>
#include <iostream>
using namespace std;
namespace LB
{
class Instance_Vulkan
{
public:
bool isCreated = false;
VkInstance instance;
VkInstanceCreateInfo createInfo;
std::vector<const char*> extensions;
std::vector<const char*> layers;
Instance_Vulkan()
{
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.flags = 0;
createInfo.pNext = NULL;
createInfo.pApplicationInfo = NULL;
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = NULL;
createInfo.enabledExtensionCount = 0;
createInfo.ppEnabledExtensionNames = NULL;
}
VkResult creat(const VkAllocationCallbacks* pAllocator=NULL)
{
if (isCreated)
return VK_SUCCESS;
else
{
VkResult result;
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
if(createInfo.enabledExtensionCount)
createInfo.ppEnabledExtensionNames = extensions.data();
createInfo.enabledLayerCount = static_cast<uint32_t>(layers.size());
if(createInfo.enabledLayerCount)
createInfo.ppEnabledLayerNames = layers.data();
if((result = vkCreateInstance(&createInfo, nullptr, &instance))!=VK_SUCCESS)
cout << "Error: fail to create Vulkan Instance ! " << endl;
if (result == VK_SUCCESS)
isCreated = true;
return result;
}
}
void destroy()
{
if (isCreated)
vkDestroyInstance(instance,nullptr);
isCreated = false;
}
};
}
#endif // !INSTANCE_VULKAN_HPP
following code invoke above class
#pragma once
#ifndef APPLICATION_HPP
#define APPLICATION_HPP
#include<vulkan/vulkan.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "Item.hpp"
#include"Window.hpp"
#include "Instance_Vulkan.hpp"
using namespace std;
namespace LB
{
class Application
{
public:
bool shouldClose = false;
virtual void mainLoop()
{
}
void init()
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API,GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
initVulkan();
}
void initVulkan()
{
// set the application info
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hellp Triangle";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName= "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
appInfo.pNext = NULL;
// set papplicatoinInfo
instance.createInfo.pApplicationInfo = &appInfo;
if (instance.creat()!= VK_SUCCESS)
throw std::runtime_error("Error: fail to create vulkan instance ");
}
void cleanUp()
{
glfwTerminate();
instance.destroy();
}
int run(int argc=0,char* argv[]=0)
{
init();
try {
for (;!shouldClose;)
mainLoop();
}
catch (const std::exception e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
cleanUp();
return EXIT_SUCCESS;
}
private:
Instance_Vulkan instance;
};
}
#endif
there are main function
#include "pch.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "Application.hpp"
#include <iostream>
#include <cstdlib>
using namespace LB;
using namespace std;
int main()
{
Application app;
return app.run();
}
when i run the code,it crash
'result image'
My Environment:
Win10 debug 64bit
vs2017
Gtx 940m driver: 416.94
i install lunarg sdk 1.1.85.0 , run the cube.exe and also compile the demo successfully

How can I stop the program inside the a parent or a child process?

I have this piece of code that I developed just to address a problem that I have in another large program that I am developing.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <limits.h>
#include <string>
#include <iostream>
using namespace std;
void processLine (char []);
void readLine(char []);
const int LIMIT = 512;
int main(int argc, char *argv[])
{
char oneLine[LINE_MAX];
readLine(oneLine);
return 0;
}
void readLine(char line[])
{
processLine(line);
//Otherstuff
------------
}
void processLine(char line[])
{
pid_t process;
int child_status;
string input;
cout << "Input: ";
cin >> input;
process = fork();
if(process == 0)
{ // do nothing
}
else
{
//parent
if(input == "quit")
{
printf("Quit command found ! \nExiting ");
for(int i = 0;i < 3;i++)
{
printf(".");
fflush(stdout);
sleep(1);
}
printf("\n");
exit(0);
}
else
{
wait(&child_status);
}
}
}
My goal is simple, When the user enter quit.
I will just display
Quit command found
Exiting ...
And there is a delay of one second between each of these three dots.
However the output that I get is
Quit command found
Exiting . other stuff ..
However, what seems to happen is that the parent process returns and then executes other stuff from the calling function before it continues to print the other two dots. How would I avoid the parent process from doing that ?
Use waitpid() like this:
pid_t childPid;
childPid = fork();
...
int returnStatus;
waitpid(childPid, &returnStatus, 0); // Parent process waits here for child to terminate.
Use it here
if(childPid == 0) // fork succeeded
{
// Do something
exit(0);
}
else // Main (parent) process after fork succeeds
{
int returnStatus;
waitpid(childPid, &returnStatus, 0);
}

Empty QComboBox with QSqlQueryModel

I am totally new to Qt and just want to play around a little. I want to try to fill a ComboBox with values from a DB
I have got the following code:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LOCALHOST");
db.setDatabaseName("rms32");
if(db.open())
{
QSqlQuery query;
query.prepare("select user_name from T_USER");
if(query.exec())
{
this->model = new QSqlQueryModel();
this->model->setQuery(query);
qDebug() << this->model->rowCount();
qDebug() << this->model->columnCount();
ui->_UsernameCB->setModel(this->model);
//ui->_UsernameCB->setModelColumn(0);
qDebug() << "Last error: " << db.lastError().text();
qDebug() << "Connection opened successfully";
}
db.close();
}
The rowCount and colCount gives me 1, corresponding to my DB, but the user_name is not displayed in the Combobox.
Can anyone point me to the error?
I cannot reproduce your problem. I have the very simply class below inheriting from QComboBox. I went for an sqlite database but otherwise most lines are pretty much exactly as in your case and it correctly displays the one entry in the DB.
If you replace the DB in the code with yours, do you get the entry correctly displayed?
combobox.h
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QComboBox>
QT_FORWARD_DECLARE_CLASS(QSqlQueryModel);
class ComboBox : public QComboBox
{
public:
ComboBox(QWidget *widget = 0);
QSqlQueryModel *model;
};
#endif
combobox.cpp
#include "combobox.h"
#include <QSqlDatabase>
#include <QSqlQueryModel>
#include <QSqlQuery>
ComboBox::ComboBox(QWidget *parent) :
QComboBox(parent)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("db.db");
if(db.open())
{
QSqlQuery query;
query.prepare("select * from TEST");
if(query.exec())
{
this->model = new QSqlQueryModel;
this->model->setQuery(query);
this->setModel(this->model);
}
db.close();
}
}
main.cpp
#include <QApplication>
#include <QDialog>
#include <QHBoxLayout>
#include "combobox.h"
int main(int argc, char** argv)
{
QApplication app(argc,argv);
QDialog dialog;
ComboBox box;
QHBoxLayout layout;
layout.addWidget(&box);
dialog.setLayout(&layout);
dialog.show();
return app.exec();
}

simple linux device driver open call crash

I am trying to learn how to write a device driver in linux, following some reference from google and ldd3. i am able to insert the module below but when i tried to open the device in an application the kernel crashed.
The code and build steps followed as below :
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/ioport.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/param.h>
#include <linux/fs.h>
/* =============== Constant Definitions ============ */
#define SERIAL_IRQ 4
/* =============== Variable Definitions ============ */
static int SER_MAJOR = 0;
int ser_open(struct inode *inode, struct file *filp);
int ser_release(struct inode *inode, struct file *filp);
irqreturn_t my_ser_dev_isr(int irq,void *ser_data,struct pt_regs * pt_reg_var)
{
printk("\n\n ------- INTR raised -----------\n\n");
return 0;
}
int ser_open(struct inode *inode, struct file *filp)
{
if(request_irq(SERIAL_IRQ,&my_ser_dev_isr,1,"my_ser_dev_intr",NULL))
{
printk("\n interrupt req failed\n");
}
else
{
enable_irq(SERIAL_IRQ);
printk("\n!!!! ..obtained the requested interrupt and enabled\n");
}
}
int ser_release(struct inode *inode, struct file *filp)
{
disable_irq(SERIAL_IRQ);
free_irq(SERIAL_IRQ,NULL) ;
}
static struct file_operations ser_fops = {
open: ser_open,
release: ser_release
};
void *p = NULL;
irqreturn_t my_ser_dev_isr (int, void *, struct pt_regs *);
static int __init hello_start(void)
{
int ret_val=-1;
int result;
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
result = register_chrdev(SER_MAJOR,"SER_DEV",&ser_fops);
if(result < 0)
{
printk(KERN_WARNING"Can't get major %d\n",SER_MAJOR);
return result;
}
if(SER_MAJOR == 0)
{
SER_MAJOR = result;
printk("SER DEV Major Number : %d",SER_MAJOR );
}
return 0;
}
static void __exit hello_end(void)
{
// free_irq(SERIAL_IRQ,NULL);
//release_region(0x0031,1);
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);
Makefile for module :
obj-m := hello.o
default:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
The application used for accesing is as follows :
#include <stdio.h> /* test.c */
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
static int dev;
int main(void)
{
char buff[40];
dev = open("/dev/my_ser_dev",O_RDONLY);
if(dev < 0)
{
printf( "Device Open ERROR!\n");
exit(1);
}
printf("Please push the GPIO_16 port!\n");
//read(dev,buff,40);
// scanf("%s",buff);
printf("%s\n",buff);
close(dev);
return 0;
}
insmod gave
[ 3837.312140] Loading hello module...
[ 3837.312147] Hello world
[ 3837.312218] SER DEV Major Number : 251
Then I created the special file using mknod /dev/my_ser_dev c 251 0
Executing the application caused kernel crash. I am using UBUNTU 3.2.0-23-generic-pae.
The function you are registering as your IRQ handler has the wrong prototype - it should be like
irqreturn_t irq_handler(int, void *);
Maybe you are referring to old documentation.