Skip to content

Commit

Permalink
Merge branch 'logger'
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Srajer committed Oct 13, 2016
2 parents e12a769 + d30faf6 commit f12ced3
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ add_executable(LoggerRealSense
CameraInterface.h
RealSenseInterface.cpp
RealSenseInterface.h
Logger.cpp
Logger.h
)

target_link_libraries(LoggerRealSense
Expand Down
6 changes: 6 additions & 0 deletions CameraInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
class CameraInterface
{
public:
CameraInterface(int width,int height,int fps)
: width(width),height(height),fps(fps)
{
}

virtual ~CameraInterface() {}

virtual bool ok() = 0;
virtual std::string error() = 0;
virtual float depthScale() = 0;

const int width,height,fps;
static const int numBuffers = 10;
std::atomic<int> latestDepthIndex;
std::pair<std::pair<uint8_t *,uint8_t *>,int64_t> frameBuffers[numBuffers];
Expand Down
90 changes: 90 additions & 0 deletions Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "Logger.h"

#include <iostream>

using std::cout;

Logger::Logger(string outDir,std::shared_ptr<CameraInterface> cam)
:
doWrite(false),
outDir(outDir),
fileId(0),
writeThread(nullptr),
cam(cam)
{
}

Logger::~Logger()
{
if(doWrite)
{
stopWriting();
}
}

void Logger::startWriting()
{
cout << "Starting writing.\n";
doWrite = true;
nFrames = 0;

string fn = outDir + "/seq" + std::to_string(fileId++) + ".klg";
file.open(fn,std::ios::binary);
if(!file.is_open())
{
cout << "Could not open file: " << fn << "\n";
doWrite = false;
return;
}

file.write((char*)&nFrames,sizeof(int32_t));
writeThread = new std::thread(&Logger::write,this);
}

void Logger::stopWriting()
{
cout << "Stopping writing.\n";
doWrite = false;
writeThread->join();
delete writeThread;
writeThread = nullptr;

file.seekp(0);
file.write((char*)&nFrames,sizeof(int32_t));

file.close();
file.clear();
}

void Logger::write()
{
static int lastWrittenBufferIdx = -1;
while(doWrite)
{
std::this_thread::sleep_for(std::chrono::microseconds(1000));

int lastDepth = cam->latestDepthIndex;
if(lastDepth == -1)
continue;

int bufferIdx = lastDepth % CameraInterface::numBuffers;
if(bufferIdx == lastWrittenBufferIdx)
continue;

const void *depthData = cam->frameBuffers[bufferIdx].first.first;
const void *rgbData = cam->frameBuffers[bufferIdx].first.second;

int64_t currTimestamp = cam->frameBuffers[bufferIdx].second;
int32_t depthSize = cam->width * cam->height * sizeof(uint16_t);
int32_t rgbSize = cam->width * cam->height * 3 * sizeof(uint8_t);

file.write((char*)&currTimestamp,sizeof(int64_t));
file.write((char*)&depthSize,sizeof(int32_t));
file.write((char*)&rgbSize,sizeof(int32_t));
file.write((char*)depthData,depthSize);
file.write((char*)rgbData,rgbSize);

lastWrittenBufferIdx = bufferIdx;
nFrames++;
}
}
50 changes: 50 additions & 0 deletions Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <string>
#include <atomic>
#include <memory>
#include <cstdint>
#include <fstream>
#include <thread>

#include "CameraInterface.h"

using std::string;

/**
* Format is:
* int32_t at file beginning for frame count
*
* For each frame:
* int64_t: timestamp
* int32_t: depthSize
* int32_t: imageSize
* depthSize * unsigned char: depth_compress_buf
* imageSize * unsigned char: encodedImage->data.ptr
*/

class Logger
{
public:
Logger(string outDir,std::shared_ptr<CameraInterface> cam);
~Logger();

bool isWriting() const
{
return doWrite;
}

void startWriting();
void stopWriting();

private:
void write();

std::atomic<bool> doWrite;
string outDir;
int32_t nFrames;
int fileId;
std::ofstream file;
std::thread *writeThread;
std::shared_ptr<CameraInterface> cam;
};
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
How to run:
Press button R to start recording and press it again to stop.

Dependencies:

* librealsense
Expand Down
5 changes: 2 additions & 3 deletions RealSenseInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#include <functional>

RealSenseInterface::RealSenseInterface(int inWidth,int inHeight,int inFps)
: width(inWidth),
height(inHeight),
fps(inFps),
:
CameraInterface(inWidth,inHeight,inFps),
dev(nullptr),
initSuccessful(true)
{
Expand Down
2 changes: 0 additions & 2 deletions RealSenseInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class RealSenseInterface : public CameraInterface
RealSenseInterface(int width = 640,int height = 480,int fps = 30);
virtual ~RealSenseInterface();

const int width,height,fps;

virtual bool ok()
{
return initSuccessful;
Expand Down
30 changes: 21 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <cstdint>

#include "GL/glut.h"
Expand All @@ -9,14 +9,16 @@

#include "CameraInterface.h"
#include "RealSenseInterface.h"
#include "Logger.h"

using std::cout;
using std::vector;
using std::string;

const static int width = 640;
const static int height = 480;
const static int fps = 30;
std::unique_ptr<CameraInterface> cam;
std::shared_ptr<CameraInterface> cam;
std::unique_ptr<Logger> logger;

void displayCallback()
{
Expand Down Expand Up @@ -52,6 +54,10 @@ void idleCallback()
void endApp()
{
glutLeaveMainLoop();
if(logger->isWriting())
{
logger->stopWriting();
}
}

void keyboardCallback(unsigned char keyPressed,int mouseX,int mouseY)
Expand All @@ -61,19 +67,25 @@ void keyboardCallback(unsigned char keyPressed,int mouseX,int mouseY)
case 27:
endApp();
break;
/*case 'r':
if(!lastPressStateKeyR)
restartGame();
lastPressStateKeyR = true;
break;*/
case 'r':
if(logger->isWriting())
logger->stopWriting();
else
logger->startWriting();
break;
default:
break;
}
}

int main(int argc,char *argv[])
{
cam = std::make_unique<RealSenseInterface>(width,height,fps);
string outDir = "";
if(argc > 1)
outDir = argv[1];

cam = std::make_shared<RealSenseInterface>(width,height,fps);
logger = std::make_unique<Logger>(outDir,cam);

glutInit(&argc,argv);

Expand Down

0 comments on commit f12ced3

Please sign in to comment.