|
From: Lasse Kärkkäi. <tr...@us...> - 2010-07-18 06:38:18
|
Module: performous
Branch: portaudio
Commit: 25a9da624497f21e57032f086275670647e208d1
Author: Tapio Vierros <tap...@gm...>
Date: Sun Jun 20 18:38:23 2010 +0300
Experimental webcam video recording to file.
* No audio.
* Assumes camera captures 30 FPS (so video has most likely wrong "speed").
* Enabled/disabled via defining SAVE_WEBCAM_VIDEO macro.
* Initially enabled (in webcam.cc) only for testing purposes.
* Output to home directory, new recording overwrites previous.
---
game/webcam.cc | 18 +++++++++++++++++-
game/webcam.hh | 2 ++
2 files changed, 19 insertions(+), 1 deletions(-)
diff --git a/game/webcam.cc b/game/webcam.cc
index 0dd11e1..e29ca66 100644
--- a/game/webcam.cc
+++ b/game/webcam.cc
@@ -2,14 +2,17 @@
#include "webcam.hh"
#include "xtime.hh"
+#include "fs.hh"
#ifdef USE_OPENCV
#include <cv.h>
#include <highgui.h>
+
+#define SAVE_WEBCAM_VIDEO
#endif
Webcam::Webcam(int cam_id):
- m_thread(), m_capture(NULL), m_frameAvailable(false), m_running(false), m_quit(false)
+ m_thread(), m_capture(NULL), m_writer(NULL), m_frameAvailable(false), m_running(false), m_quit(false)
{
#ifdef USE_OPENCV
// Initialize the capture device
@@ -18,6 +21,17 @@ Webcam::Webcam(int cam_id):
std::cout << "Could not initialize webcam capturing!" << std::endl;
return;
}
+ // Initialize the video writer
+ #ifdef SAVE_WEBCAM_VIDEO
+ float fps = cvGetCaptureProperty(m_capture, CV_CAP_PROP_FPS);
+ int framew = cvGetCaptureProperty(m_capture, CV_CAP_PROP_FRAME_WIDTH);
+ int frameh = cvGetCaptureProperty(m_capture, CV_CAP_PROP_FRAME_HEIGHT);
+ int codec = CV_FOURCC('P','I','M','1'); // MPEG-1
+ std::string out_file((getHomeDir() / std::string("/performous-webcam_out.mpg")).string());
+ m_writer = cvCreateVideoWriter(out_file.c_str(), codec, fps > 0 ? fps : 30.0f, cvSize(framew,frameh));
+ if (!m_writer) std::cout << "Could not initialize webcam video saving!" << std::endl;
+ #endif
+ // Start thread
m_thread.reset(new boost::thread(boost::ref(*this)));
#else
++cam_id; // dummy
@@ -29,6 +43,7 @@ Webcam::~Webcam() {
#ifdef USE_OPENCV
if (m_thread) m_thread->join();
cvReleaseCapture(&m_capture);
+ cvReleaseVideoWriter(&m_writer);
#endif
}
@@ -40,6 +55,7 @@ void Webcam::operator()() {
// Try to get a new frame
if (m_running) frame = cvQueryFrame(m_capture);
if (frame) {
+ if (m_writer) cvWriteFrame(m_writer, frame);
boost::mutex::scoped_lock l(m_mutex);
// Copy the frame to storage
m_frame.width = frame->width;
diff --git a/game/webcam.hh b/game/webcam.hh
index 1091c91..f63087d 100644
--- a/game/webcam.hh
+++ b/game/webcam.hh
@@ -8,6 +8,7 @@
#include "surface.hh"
struct CvCapture;
+struct CvVideoWriter;
struct CamFrame {
int width;
@@ -39,6 +40,7 @@ class Webcam {
boost::scoped_ptr<boost::thread> m_thread;
mutable boost::mutex m_mutex;
CvCapture* m_capture;
+ CvVideoWriter* m_writer;
CamFrame m_frame;
Surface m_surface;
bool m_frameAvailable;
|