Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed crash on exit; made sure memory is reallocated/cleared correctl…

…y; removed an unused variable; encoder is now setup again when re/setting pixel source, this fixes bad record sizing
  • Loading branch information...
commit e5d55da6cfeb8a61f74e9bbbedc9fcfc34862ef9 1 parent c84de8b
@danomatika danomatika authored
Showing with 79 additions and 25 deletions.
  1. +73 −24 src/ofxMovieExporter.cpp
  2. +6 −1 src/ofxMovieExporter.h
View
97 src/ofxMovieExporter.cpp
@@ -38,8 +38,30 @@ namespace Apex
const string ofxMovieExporter::CONTAINER = "mp4";
ofxMovieExporter::ofxMovieExporter() {
- resetRecordingArea();
- resetPixelSource();
+ outputFormat = NULL;
+ formatCtx = NULL;
+ videoStream = NULL;
+
+ codec = NULL;
+ codecCtx = NULL;
+ convertCtx = NULL;
+
+ inPixels = NULL;
+ outPixels = NULL;
+ encodedBuf = NULL;
+
+ inFrame = NULL;
+ outFrame = NULL;
+
+ posX = 0;
+ posY = 0;
+ inW = ofGetWidth();
+ inH = ofGetHeight();
+ outW = ofGetWidth();
+ outH = ofGetHeight();
+
+ bool usePixelSource = false;
+ pixelSource = NULL;
}
void ofxMovieExporter::setup(
@@ -55,6 +77,7 @@ namespace Apex
this->outW = outW;
this->outH = outH;
this->frameRate = frameRate;
+ this->bitRate = bitRate;
this->codecId = codecId;
this->container = container;
@@ -71,7 +94,6 @@ namespace Apex
av_register_all();
convertCtx = sws_getContext(inW, inH, PIX_FMT_RGB24, outW, outH, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
- // allocate memory
allocateMemory();
}
@@ -79,24 +101,8 @@ namespace Apex
{
if (recording) finishRecord();
- // allocate input stuff
-#ifdef _THREAD_CAPTURE
stopThread(true);
- for (int i = 0; i < frameMem.size(); i++)
- {
- delete[] frameMem[i];
- }
- for (int i = 0; i < frameQueue.size(); i++)
- {
- delete[] frameQueue[i];
- }
-#else
- delete[] inPixels;
-#endif
- av_free(inFrame);
- av_free(outFrame);
- av_free(encodedBuf);
- av_free(outPixels);
+ clearMemory();
}
void ofxMovieExporter::record(string filePrefix, string folderPath)
@@ -168,6 +174,9 @@ namespace Apex
void ofxMovieExporter::setPixelSource(unsigned char* pixels, int w, int h)
{
+ if (isRecording())
+ stop();
+
if (pixels == NULL)
{
ofLog(OF_LOG_ERROR, "ofxMovieExporter: Could not set NULL pixel source");
@@ -177,6 +186,9 @@ namespace Apex
inW = w;
inH = h;
usePixelSource = true;
+
+ // resetup encoder etc
+ setup(outW, outH, bitRate, frameRate, codecId, container);
}
void ofxMovieExporter::resetPixelSource()
@@ -185,13 +197,18 @@ namespace Apex
pixelSource = NULL;
inW = ofGetViewportWidth();
inH = ofGetViewportHeight();
+
+ // resetup encoder etc
+ setup(outW, outH, bitRate, frameRate, codecId, container);
}
- int ofxMovieExporter::getNumCaptures() {
+ int ofxMovieExporter::getNumCaptures()
+ {
return numCaptures;
}
- void ofxMovieExporter::resetNumCaptures() {
+ void ofxMovieExporter::resetNumCaptures()
+ {
numCaptures = 0;
}
@@ -209,6 +226,7 @@ namespace Apex
av_freep(&formatCtx->streams[i]);
}
av_free(formatCtx);
+ formatCtx = NULL;
//url_fclose(formatCtx->pb);
}
@@ -266,7 +284,7 @@ namespace Apex
int screenY = screenHeight - posY;
screenY -= inH; // top, bottom issues
- glReadPixels(posX, screenY, inW, inH, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+ glReadPixels(posX, screenY, inW, inH, GL_RGB, GL_UNSIGNED_BYTE, pixels);
}
else
{
@@ -331,6 +349,10 @@ namespace Apex
void ofxMovieExporter::allocateMemory()
{
+ // clear if we need to reallocate
+ if(inPixels)
+ clearMemory();
+
// allocate input stuff
#ifdef _THREAD_CAPTURE
//unsigned char* initFrameMem = new unsigned char[inW * inH * 3 * INIT_QUEUE_SIZE];
@@ -352,6 +374,33 @@ namespace Apex
encodedBuf = (unsigned char*)av_malloc(ENCODED_FRAME_BUFFER_SIZE);
}
+ void ofxMovieExporter::clearMemory() {
+ // clear input stuff
+#ifdef _THREAD_CAPTURE
+ for (int i = 0; i < frameMem.size(); i++)
+ {
+ delete[] frameMem[i];
+ }
+ for (int i = 0; i < frameQueue.size(); i++)
+ {
+ delete[] frameQueue[i];
+ }
+#else
+ delete[] inPixels;
+#endif
+ inPixels = NULL;
+
+ av_free(inFrame);
+ av_free(outFrame);
+ av_free(encodedBuf);
+ av_free(outPixels);
+
+ inFrame = NULL;
+ outFrame = NULL;
+ encodedBuf = NULL;
+ outPixels = NULL;
+ }
+
void ofxMovieExporter::initEncoder()
{
/////////////////////////////////////////////////////////////
@@ -381,7 +430,7 @@ namespace Apex
/////////////////////////////////////////////////////////////
// init codec context
codecCtx = videoStream->codec;
- codecCtx->bit_rate = BIT_RATE;
+ codecCtx->bit_rate = bitRate;
codecCtx->width = outW;
codecCtx->height = outH;
View
7 src/ofxMovieExporter.h
@@ -105,6 +105,10 @@ namespace Apex
// reset the filename counter back to 0
void resetNumCaptures();
+
+ // get the recording size
+ inline int getRecordingWidth() {return outW;}
+ inline int getRecordingHeight() {return outH;}
private:
#ifdef _THREAD_CAPTURE
@@ -116,6 +120,7 @@ namespace Apex
#endif
void initEncoder();
void allocateMemory();
+ void clearMemory();
void checkFrame(ofEventArgs& args);
void encodeFrame();
@@ -127,6 +132,7 @@ namespace Apex
bool recording;
int numCaptures;
int frameRate;
+ int bitRate;
float frameInterval;
float lastFrameTime;
int frameNum;
@@ -147,7 +153,6 @@ namespace Apex
AVFrame* inFrame;
AVFrame* outFrame;
- AVFrame* encodedFrame;
int posX, posY;
int inW, inH;
Please sign in to comment.
Something went wrong with that request. Please try again.