掘り起こし手直し版 ofxZmqとofxTurboJpegで画像配信と受信
(編集中)
ofApp.h
#pragma once #include "ofMain.h" #include "ofxTurboJpeg.h" class ofxZmqSubscriber; class ofxZmqPublisher; class ofApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed(int key); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void mouseEntered(int x, int y); void mouseExited(int x, int y); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); ofxZmqSubscriber* subscriber; ofxZmqPublisher* publisher; ofVideoGrabber cap; ofxTurboJpeg turbo; ofImage frame; ofBuffer send_buf; int quality; ofImage image; ofBuffer recv_buf; protected: ofFbo fbo; ofPixels pix; bool flag_display; ofVec2f src_size, dst_size; };
ofApp.cpp
#include "ofApp.h" #include "ofxZmq.h" #pragma comment(lib, "C:\\vcpkg\\installed\\x64-windows\\lib\\libzmq-mt-4_3_4.lib") //-------------------------------------------------------------- void ofApp::setup(){ ofSetFrameRate(60); cap.setVerbose(true); cap.setDeviceID(2); flag_display = true; src_size = ofVec2f(/*3840, 1920*/1920,1080); dst_size = src_size; // ofVec2f(2560, 1440); if (cap.initGrabber(src_size.x, src_size.y)) { //frame.allocate(cap.getWidth(), cap.getHeight(), OF_IMAGE_COLOR); quality = 95; // start server publisher = new ofxZmqPublisher(); publisher->setHighWaterMark(1); publisher->bind("tcp://*:9999"); } // start client subscriber = new ofxZmqSubscriber(); subscriber->setHighWaterMark(1); // subscriber->connect("tcp://localhost:9999"); fbo.allocate(dst_size.x, dst_size.y, GL_RGB, 4); } //-------------------------------------------------------------- void ofApp::update(){ while (subscriber->hasWaitingMessage()) { subscriber->getNextMessage(recv_buf); turbo.load(image, recv_buf); cout << "received data: " << recv_buf.size() << endl; } cap.update(); if (cap.isFrameNew()) { if (src_size != dst_size) { // TODO: using FBO for image size adjustment fbo.begin(); cap.draw(0, 0, fbo.getWidth(), fbo.getHeight()); fbo.end(); fbo.readToPixels(pix); turbo.save(send_buf, pix, quality); } else { turbo.save(send_buf, cap.getPixelsRef(), quality); } publisher->send(send_buf); } } //-------------------------------------------------------------- void ofApp::draw(){ if (!flag_display) return; if (src_size != dst_size) { // resized fbo ofPushMatrix(); ofScale(0.5); fbo.draw(0, 0, fbo.getWidth(), fbo.getHeight()); ofPopMatrix(); //image.draw(cap.getWidth(), 0); } else { cap.draw(0, 0); //image.draw(cap.getWidth(), 0); } } //-------------------------------------------------------------- void ofApp::keyPressed(int key) { if (key == OF_KEY_RETURN) { flag_display = !flag_display; std::cout << "display " << flag_display << std::endl; } if (key == OF_KEY_UP) { quality = (quality < 100) ? quality + 1 : quality; std::cout << "quality " << quality << std::endl; } if (key == OF_KEY_DOWN) { quality = (quality > 0) ? quality - 1 : quality; std::cout << "quality " << quality << std::endl; } } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseEntered(int x, int y){ } //-------------------------------------------------------------- void ofApp::mouseExited(int x, int y){ } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }