I want to use this python module: https://python-omxplayer-wrapper.readthedocs.io/en/latest/ on my c++ app.
It is a omxplayer python wrapper and has some callback events that I want to link to my c++ class function member.
I did it with success using a static function like this:
void foo(py::object player) {
std::cout << "pause event callback" << std::endl;
}
py::object mod = py::import("omxplayer.player");
OMXPlayer::pyOMXPlayer = mod.attr("OMXPlayer")(_file, args, NULL, NULL, _dbus_name, _pause);
OMXPlayer::pyOMXPlayer.attr("pauseEvent") = py::make_function( &foo );
Where OMXPlayer is my c++ class.
I tried to use boost::bind and boost::function without success.
How can use OMXPlayer::onPause() function instead of static foo function?
Edit with example:
OMXPlayer.cpp
#include "OMXPlayer.h"
OMXPlayer::OMXPlayer(std::string _file,
std::vector<std::string> _args,
bool _pause,
std::string _dbus_name){
try{
Py_Initialize();
py::object mod = py::import("omxplayer.player");
py::list args;
for(auto const& value: _args) {
args.append(value);
}
OMXPlayer::pyOMXPlayer = mod.attr("OMXPlayer")(_file, args, NULL, NULL, _dbus_name, _pause);
pyOMXPlayer.attr("pauseEvent" ) = py::make_function(&OMXPlayer::onPause);
OMXPlayer::active = false;
}
catch(py::error_already_set){
PyErr_Print();
}
}
void OMXPlayer::onPause(){
std::cout << "onPause" << std::endl;
}
void OMXPlayer::pause(){
try{
OMXPlayer::pyOMXPlayer.attr("pause")();
}
catch(py::error_already_set){
PyErr_Print();
}
}
OMXPlayer.h
#include <boost/python.hpp>
#include <boost/function.hpp>
#include <vector>
#include <iostream>
#include <atomic>
namespace py = boost::python;
class OMXPlayer{
public:
OMXPlayer(std::string _file,
std::vector<std::string> _args,
bool _pause = false,
std::string _dbus_name = "");
void pause();
void onPause();
};
main.cpp function:
#include "OMXPlayer.h"
int main(){
OMXPlayer player1("/root/Setteventi.mp4", std::vector<std::string> {"--loop"}, false, "org.mpris.MediaPlayer2.omxplayer1");
player1.pause();
}
You can see the python Class from here: https://python-omxplayer-wrapper.readthedocs.io/en/latest/_modules/omxplayer/player/
When eventPause on python side is called it throw this:
Traceback (most recent call last):
File "<decorator-gen-56>", line 2, in pause
File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 48, in wrapped
return fn(self, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 550, in pause
self.pauseEvent(self)
Boost.Python.ArgumentError: Python argument types in
None.None(OMXPlayer)
did not match C++ signature:
None(OMXPlayer {lvalue})
Traceback (most recent call last):
File "<decorator-gen-56>", line 2, in pause
File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 48, in wrapped
return fn(self, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 550, in pause
self.pauseEvent(self)
Boost.Python.ArgumentError: Python argument types in
None.None(OMXPlayer)
did not match C++ signature:
None(OMXPlayer {lvalue})
&OMXPLayer::onPause? What is the result? Show the complete example.selfthat is a differentOMXPlayerclass. And if youboost::bindorstd::bindthethispointer, what is the error?pyOMXPlayer.attr("pauseEvent" ) = py::make_function( boost::bind(&OMXPlayer::onPause, this) );It will not compile with error:/usr/include/boost/python/make_function.hpp:104:57: error: no matching function for call to ‘get_signature(boost::_bi::bind_t<void, boost::_mfi::mf0<void, OMXPlayer>, boost::_bi::list1<boost::_bi::value<OMXPlayer*> > >&)’ f,default_call_policies(), detail::get_signature(f));make_function require a function and boost:bind return a object.py::make_function(boost::bind(&OMXPlayer::onPause, this));Or maybe ratherpy::make_function(boost::bind(&OMXPlayer::onPause, this, _1));and the signature:void onPause(py::object player)because python calls it with one argument. Or with standard library:py::make_function(std::bind(&OMXPlayer::onPause, this, std::placeholders::_1));Sorry for not being more precise, today I don't have any good environment to try your code.