From 303205a0cecee078e1c304ac3aee1b65798ae2c8 Mon Sep 17 00:00:00 2001 From: berger Date: Fri, 19 Jun 2015 03:38:22 -0700 Subject: [PATCH] * Added command line parameter to pass recording for playback --- .gitignore | 3 + CaroloCup-CameraPlayback.cpp | 143 ++++++++++++++++++++--------------- 2 files changed, 83 insertions(+), 63 deletions(-) diff --git a/.gitignore b/.gitignore index 17bf4c9..e8bd2ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ CaroloCup-CameraPlayback +*.o +*~ +.depend diff --git a/CaroloCup-CameraPlayback.cpp b/CaroloCup-CameraPlayback.cpp index 9256ccd..87cc693 100644 --- a/CaroloCup-CameraPlayback.cpp +++ b/CaroloCup-CameraPlayback.cpp @@ -19,6 +19,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include +#include #include #include @@ -42,91 +44,106 @@ using namespace core::wrapper; using namespace tools::player; int32_t main(int32_t argc, char **argv) { - // Location of the recording file. - URL url("file://example.rec"); + uint32_t retVal = 0; + if (argc != 2) { + cerr << "Insufficient command line parameters supplied. Use " << string(argv[0]) << " myRecording.rec" << endl; + retVal = 1; + } + else { + // Use command line parameter as file for playback; + string recordingFile(argv[1]); + stringstream recordingFileUrl; + recordingFileUrl << "file://" << recordingFile; - // Do we want to rewind the stream on EOF? - const bool AUTO_REWIND = false; + // Location of the recording file. + URL url(recordingFileUrl.str()); - // Size of the memory buffer that should fit at least the size of one frame. - const uint32_t MEMORY_SEGMENT_SIZE = 1024 * 768; + // Do we want to rewind the stream on EOF? + const bool AUTO_REWIND = false; - // Number of memory segments (one is enough as we are running sychronously). - const uint32_t NUMBER_OF_SEGMENTS = 1; + // Size of the memory buffer that should fit at least the size of one frame. + const uint32_t MEMORY_SEGMENT_SIZE = 1024 * 768; - // Run player in synchronous mode without data caching in background. - const bool THREADING = false; + // Number of memory segments (one is enough as we are running sychronously). + const uint32_t NUMBER_OF_SEGMENTS = 1; - // Construct the player. - Player player(url, AUTO_REWIND, MEMORY_SEGMENT_SIZE, NUMBER_OF_SEGMENTS, THREADING); + // Run player in synchronous mode without data caching in background. + const bool THREADING = false; - // The next container from the recording. - Container nextContainer; + // Construct the player. + Player player(url, AUTO_REWIND, MEMORY_SEGMENT_SIZE, NUMBER_OF_SEGMENTS, THREADING); - // Using OpenCV's IplImage data structure to simply playback the data. - IplImage *image = NULL; + // The next container from the recording. + Container nextContainer; - // Create the OpenCV playback window. - cvNamedWindow("CaroloCup-CameraPlayback", CV_WINDOW_AUTOSIZE); + // Using OpenCV's IplImage data structure to simply playback the data. + IplImage *image = NULL; - // This flag indicates whether we have attached already to the shared - // memory containing the sequence of captured images. - bool hasAttachedToSharedImageMemory = false; + // Create the OpenCV playback window. + cvNamedWindow("CaroloCup-CameraPlayback", CV_WINDOW_AUTOSIZE); - // Using this variable, we will access the captured images while - // also having convenient automated system resource management. - SharedPointer sharedImageMemory; + // This flag indicates whether we have attached already to the shared + // memory containing the sequence of captured images. + bool hasAttachedToSharedImageMemory = false; - // Main data processing loop. - while (player.hasMoreData()) { - // Read next entry from recording. - nextContainer = player.getNextContainerToBeSent(); + // Using this variable, we will access the captured images while + // also having convenient automated system resource management. + SharedPointer sharedImageMemory; - // Data type SHARED_IMAGE contains a SharedImage data structure that - // provides meta-information about the captured image. - if (nextContainer.getDataType() == Container::SHARED_IMAGE) { - // Read the data structure to retrieve information about the image. - SharedImage si = nextContainer.getData(); + // Main data processing loop. + while (player.hasMoreData()) { + // Read next entry from recording. + nextContainer = player.getNextContainerToBeSent(); - // Check if we have already attached to the shared memory. - if (!hasAttachedToSharedImageMemory) { - sharedImageMemory = SharedMemoryFactory::attachToSharedMemory(si.getName()); + // Data type SHARED_IMAGE contains a SharedImage data structure that + // provides meta-information about the captured image. + if (nextContainer.getDataType() == Container::SHARED_IMAGE) { + // Read the data structure to retrieve information about the image. + SharedImage si = nextContainer.getData(); - // Toggle the flag as we have now attached to the shared memory. - hasAttachedToSharedImageMemory = true; - } + // Check if we have already attached to the shared memory. + if (!hasAttachedToSharedImageMemory) { + sharedImageMemory = SharedMemoryFactory::attachToSharedMemory(si.getName()); - // Check if we could successfully attach to the shared memory. - if (sharedImageMemory->isValid()) { - // Lock the memory region to get exclusive access. - sharedImageMemory->lock(); - { - if (image == NULL) { - // Create the IplImage header data and access the shared memory for the actual image data. - image = cvCreateImageHeader(cvSize(si.getWidth(), si.getHeight()), IPL_DEPTH_8U, si.getBytesPerPixel()); - - // Let the IplImage point to the shared memory containing the captured image. - image->imageData = static_cast(sharedImageMemory->getSharedMemory()); - } + // Toggle the flag as we have now attached to the shared memory. + hasAttachedToSharedImageMemory = true; + } + + // Check if we could successfully attach to the shared memory. + if (sharedImageMemory->isValid()) { + // Lock the memory region to get exclusive access. + sharedImageMemory->lock(); + { + if (image == NULL) { + // Create the IplImage header data and access the shared memory for the actual image data. + image = cvCreateImageHeader(cvSize(si.getWidth(), si.getHeight()), IPL_DEPTH_8U, si.getBytesPerPixel()); - // Show the image using OpenCV. - cvShowImage("CaroloCup-CameraPlayback", image); + // Let the IplImage point to the shared memory containing the captured image. + image->imageData = static_cast(sharedImageMemory->getSharedMemory()); + } - // Let the image render before proceeding to the next image. - cvWaitKey(10); + // Show the image using OpenCV. + cvShowImage("CaroloCup-CameraPlayback", image); + + // Let the image render before proceeding to the next image. + cvWaitKey(10); + } + // Release the memory region so that the player can provide the next raw image data. + sharedImageMemory->unlock(); } - // Release the memory region so that the player can provide the next raw image data. - sharedImageMemory->unlock(); } } - } - // Release IplImage data structure. - cvReleaseImage(&image); + // Release IplImage data structure. + cvReleaseImage(&image); - // Close playback window. - cvDestroyWindow("CaroloCup-CameraPlayback"); + // Close playback window. + cvDestroyWindow("CaroloCup-CameraPlayback"); + + // The shared memory will be automatically released. + } - // The shared memory will be automatically released. + // Return error code. + return retVal; }