From 9b0888b2e56c1910efd7212cd36db74b775d2619 Mon Sep 17 00:00:00 2001 From: Josh Silverman Date: Mon, 30 Dec 2013 15:02:36 -0500 Subject: [PATCH] Adds network connectivity for frame sync and moves draw commands into separate thread --- src/ofxBMFrameSync.cpp | 199 ++++++++++++++++++++++++++++++++++++++++- src/ofxBMFrameSync.h | 28 +++++- 2 files changed, 224 insertions(+), 3 deletions(-) diff --git a/src/ofxBMFrameSync.cpp b/src/ofxBMFrameSync.cpp index 34913b1..dbd5c88 100644 --- a/src/ofxBMFrameSync.cpp +++ b/src/ofxBMFrameSync.cpp @@ -3,6 +3,8 @@ ofxBMFrameSync::ofxBMFrameSync(){ + isSlave = false; + setup(); IDeckLinkDisplayModeIterator* pDLDisplayModeIterator; @@ -48,6 +50,16 @@ ofxBMFrameSync::ofxBMFrameSync(){ uiTotalFrames = 0; + udp.Create(); + + if(isSlave) + udp.Bind(12345); + else + udp.Connect("192.168.0.19", 12345); + + exitThreadFlag = false; + startThread(); + } ofxBMFrameSync::~ofxBMFrameSync(){ @@ -55,6 +67,9 @@ ofxBMFrameSync::~ofxBMFrameSync(){ if (pDLOutput == NULL) return false; + exitThreadFlag = true; + waitForThread(); + pDLOutput->StopScheduledPlayback(0, NULL, 0); pDLOutput->DisableVideoOutput(); @@ -135,8 +150,188 @@ void ofxBMFrameSync::renderFrame(unsigned char* bytes, int length){ pDLVideoFrame->GetBytes((void**)&pFrame); memcpy(pFrame, bytes, fmin(length, uiFrameWidth*uiFrameHeight*4)); - - if (pDLOutput->DisplayVideoFrameSync(pDLVideoFrame)) + + HRESULT hr = pDLOutput->DisplayVideoFrameSync(pDLVideoFrame); + if (hr) uiTotalFrames++; +} + +void ofxBMFrameSync::threadedFunction(){ + + lock(); + CGLPixelFormatAttribute attribs[] = + { + kCGLPFAColorSize, (CGLPixelFormatAttribute)32, + kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8, + kCGLPFADepthSize, (CGLPixelFormatAttribute)16, + kCGLPFAAccelerated, + (CGLPixelFormatAttribute)0 + }; + CGLPixelFormatObj pixelFormatObj; + GLint numPixelFormats; + CGLChoosePixelFormat (attribs, &pixelFormatObj, &numPixelFormats); + CGLCreateContext (pixelFormatObj, 0, &contextObj); + CGLDestroyPixelFormat (pixelFormatObj); + + + + float positionAtLastPush = 2.0f; + + while(true){ + if(exitThreadFlag) + break; + + if(false){ + old_contextObj = CGLGetCurrentContext(); + CGLSetCurrentContext (contextObj); + + if(fbo.getWidth() != ofGetWidth() || fbo.getHeight()!=ofGetHeight()){ + fbo.allocate(ofGetWidth(),ofGetHeight(),GL_RGBA,8); + pix.allocate(ofGetWidth(),ofGetHeight(),ofGetImageTypeFromGLType(ofGetGLFormatFromInternal(fbo.getTextureReference().texData.glTypeInternal))); + } + } + + + + static int renderFr = 0; + + //msatimer.start(); + + //for(int fr = 0; fr < 10 ; fr++){ + + int whichQuarter = renderFr%4; + + + msa::Timer delayTimer; + delayTimer.start(); + + unlock(); + + float startingFramePos = getFramePosition(); + + static unsigned long long usStart = ofGetSystemTimeMicros(); + unsigned long long lastStart = usStart; + usStart = ofGetSystemTimeMicros(); + + if(!isSlave){ + udp.Send(ofToString(whichQuarter).c_str(), 2); + } + + while(getFramePosition() >= 0.5f){ + //printf("b %f\n",fsOut.getFramePosition()); + //sleep(2); + } + while(getFramePosition() < 0.5f){ + //printf("a %f\n",fsOut.getFramePosition()); + //sleep(2); + } + + if(isSlave){ + char buf[1000]; + while(udp.Receive(buf, 1000)){ + whichQuarter = buf[0]-'0'; + printf("whichQuarter = %i\n",whichQuarter); + } + } + + unsigned long long usEnd = ofGetSystemTimeMicros(); + + lock(); + + delayTimer.stop(); + + //float fps = 1.0f/msatimer.getSecondsSinceLastCall(); + int diff = usStart - lastStart; + float fps = 1000000.0f / diff ; + + if(fps < 58) + printf("Bad fps: %f\n",fps); + + + if(false){ + fbo.begin(); + + ofSetupScreen(); + + ofBackground(0,0,0); + ofDrawBitmapString(ofToString(fps,2), 20,20); + ofDrawBitmapString(ofToString(delayTimer.getSeconds()*1000.0f,2), 20,40); + ofDrawBitmapString(ofToString(startingFramePos,2), 20,60); + ofDrawBitmapString(ofToString((float)(usEnd-usStart)/1000.0f,2), 20,80); + ofEllipse(300.0f+200.0f*sin(0.02f*renderFr), 300.0f+200.0f*cos(0.02f*renderFr), 100, 100); + // ofPushMatrix(); + // ofTranslate(ofGetWidth()/2, ofGetHeight()/2); + // ofRotate(renderFr*3, 1, 1, 0.5); + // ofSetColor(255, 0, 0,100); + // ofBoxPrimitive(200,200,200).draw(); + // ofPopMatrix(); + fbo.end(); + fbo.draw(0,0); + + //if(delayTimer.getSeconds() > 0.009) + // printf("delay %f\n",delayTimer.getSeconds()); + + + if(ofGetFrameNum()<5) + sleep(1); + + + fbo.readToPixels(pix); + } + + + if(false){ + + int totalpix = ofGetWidth()*ofGetHeight(); + unsigned char c; + unsigned char* buf = pix.getPixels(); + + for(int i=0;i