Last active
November 24, 2021 14:31
-
-
Save Seanmatthews/261d1e3c59237fef7be3a148a5713992 to your computer and use it in GitHub Desktop.
GoPro Hero4 Black C++ Streaming With OpenCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/exception/exception.hpp> | |
#include <boost/thread.hpp> | |
#include <boost/chrono.hpp> | |
#include "opencv2/opencv.hpp" | |
#include "opencv2/highgui.hpp" | |
using namespace cv; | |
using namespace std; | |
bool newFrame = false; | |
VideoCapture* cap; | |
Mat frame; | |
boost::mutex mtx; | |
boost::condition_variable cond; | |
void captureFunc() | |
{ | |
cap = new VideoCapture("udp://@10.5.5.9:8554"); | |
while (cap->isOpened()) | |
{ | |
boost::this_thread::sleep_for(boost::chrono::milliseconds(20)); | |
{ | |
boost::lock_guard<boost::mutex> lock(mtx); | |
*cap >> frame; | |
newFrame = true; | |
} | |
cond.notify_one(); | |
} | |
return; | |
} | |
void sendToCB(const boost::system::error_code& ec) | |
{ | |
cerr << "Error " << ec.value() << endl; | |
} | |
void keepAlive() | |
{ | |
try | |
{ | |
boost::asio::io_service ioService; | |
boost::asio::ip::udp::resolver resolver(ioService); | |
boost::asio::ip::udp::endpoint dest(boost::asio::ip::address::from_string("10.5.5.9"), 8554); | |
boost::asio::ip::udp::socket sock(ioService, boost::asio::ip::udp::v4()); | |
for (;;) | |
{ | |
boost::this_thread::sleep_for(boost::chrono::milliseconds(2000)); | |
sock.async_send_to(boost::asio::buffer("_GPHD_:0:0:2:0.000000\n", 22), dest, | |
boost::bind(&sendToCB, boost::asio::placeholders::error)); | |
cout << "Sent stay alive\n"; | |
} | |
} | |
catch (boost::exception& e) | |
{ | |
cerr << "socket errored\n"; | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
namedWindow("frame"); | |
boost::thread capThread {captureFunc}; | |
boost::thread keepAliveThread {keepAlive}; | |
for (;;) | |
{ | |
boost::unique_lock<boost::mutex> lock(mtx); | |
while (!newFrame) | |
{ | |
cond.wait(lock); | |
} | |
imshow("frame", frame); | |
newFrame = false; | |
if (27 == waitKey(10)) break; // the thread sleeps for longer | |
} | |
keepAliveThread.interrupt(); | |
cap->release(); | |
capThread.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an ancient gist, compared to the speed of GoPro releases. I believe I wrote this code for either the Hero 3 or 4. If the code does not work for you, it most likely means that they've changed their interface.