I'm trying to create a node interface for a c++ media player. Upon decoding of a frame, there is an event which allows me to access the frame data, which I'm trying to funnel into node. But I can't seem to figure out how to get that kind of functionality to work with the functions available in the node api. My approach, for the time being, is to figure out a push mechanism to get the data from c++ to javascript where all i need is to initialize a callback in javascript, since it seems more elegant. If that fails I could create a polling loop in js to check if there is new frame data, but it seems less efficient.
I've tried with napi_create_async_work, by creating a lambda function in the execute parameter function, which would allow me to call napi_make_callback for every frame callback, but then I get the following error :
Fatal error in HandleScope::HandleScope
Entering the V8 API without proper locking in place
I'm likely approaching this incorrectly, its the first time I use n-api.
Any help is welcome, thank you!
The issue is mainly pertaining to the fact you can’t access V8 (JavaScript) memory outside the event-loop’s main thread. If you're creating an async thread, by default you're also creating a new memory stack.
Fortunately, a fix is on the way which should allow thread safe access with
napi_create_threadsafe_function (example here)
Until then
There is a header only C++ package which integrates great with the C++ N-API wrapper
Napi-addon-api is update. These is a good way that use the Napi::ThreadSafeFunction.
Doc and example.
Related
Could there be any reason why sendARAnimationObject should stop working without any change in the code? Does rendering of bitmaps in a timer depend on any external state like battery level or sensor state etc?
Another issue is that if I use sendARAnimationObjectWithCallback the callback method in the listener onResultSendAnimationObject is never called as stated in the documentation. Could there be any other dependency causing this callback method not called at all?
It works much better with the official SDK v1.0
Now I belive I found the main reason behind. I was not calling disableARAnimationRequest anywhere in my app. Now I am calling it in onPause and it works much better next time I start the app and call enableARAnimationRequest. But I would need someone at sony to confirm this kind of behaviour. Maybe the disable method should be called in some SDK method without putting that burden on the developer. Or some kind of cleanup in SDK when you start your app and enable animation request.
I am creating a user interface using (Qt) and I am attaching it to my C/C++ motion application using shared memory as my form of Inter Process Communication.
I currently have a class which I created in my motion application that has many members. Most of these members are used to update data on the UI and some of them get updated about 20 to 50 times a second, so it is pretty fast (the reason being because it is tracking motion). My problem is that the data is not getting updated on the UI frequently. It gets updated every few seconds. I was able to get it work using other variables made in structures from my application by using "volatile" however it does not seem to be working for members of my class. I know that the problem is not on the UI (Qt) side, because I saw that the actual member data was not being updated in my application, even though I have commands every cycle to update the data.
I was pretty sure the problem is that some optimization is occurring since I do not have my members declared as volatile as in my structures, but when I made them volatile it still did not work. I found that when I through a comment to print out in the function that updates my motion data within my motion application, the UI updates much more frequently as if the command to print out the comment deters the compiler form optimizing out some stuff.
Has anyone experienced this problem or have a possible solution?
Your help is greatly appreciated. Thanks ahead of time!
EDIT:
The interface does not freeze completely. I just updates every few seconds instead of continuously as I intended for it to do. Using various tests I know that the problem is not on the GUI or shared memory side. The problem lies strictly on the motion application side. The function that I am calling is below: int
`motionUpdate(MOTION_STAT * stat)
{
positionUpdate(&stat->traj);
}
`
where
positionUpdate(){stat->Position = motStatus.pos_fb;}
Position is a class member that contains x, y, and z. The function does not seem to update the position values unless I put a printed out comment before positionUpdate(). I don't track the change in shared memory to update the UI, but instead just update the UI every cycle.
Especially Given you are using Qt, I would strongly advise not using "native" shared memory, but to use signals instead. Concurrency using message-passing (signals/slots is one such way) is much, much easier to reason about and debug than trying to share memory.
I would expect your problem with updating is that the UI isn't being called enough of the time, so there is a backlog of updating to do.
Try putting in some code that throws away updates if they happen less than 0.3 seconds apart and see if that helps. You may wish to tune that number but start at the larger end.
Secondly, make sure there aren't any "notspots" in your app, in which the GUI thread is not being given the chance to run. If there are, consider putting code into another thread or, alternatively, calling processEvents() within that part of the code.
If the above really isn't what's happening, I would suggest adding more info about the architecture of your program.
I want create timer in my program so that I can cause it to rerun every minute and I don't know how to do it in a C++ Application. In C# I could just create a timer but I'm struggling here now...
sleep(); is not an option because as far as I know it makes your program inactive for X seconds, I need my app to be active and working, calculating all the time. This is because my code is used to constantly input information into a MS Access table. I was able to create the necessary components of my code to connect and perform the insert/update to the table but this is just on of the many components to the code that I am creating. Please help me with this little (or big?) problem, I'm very new to C++ and learning ATM, but I am developing a fast learning curve. Thanks
I suppose you work on Windows, since you mentioned C#. So take a look at SetTimer, and if it is a MFC app, then look at CWnd::SetTimer.
Every platform provides api for creating a timer, which will give you a callback usually after timer expires. You can just search for the api available on your platform.
If you are using windows use setTimer function.
If you're using C++ .NET, you can use the same Timer class(es) as C#, just use the C++ syntax (using gcnew instead of new, use the ^ for GC references).
Otherwise you could just have a loop:
while (should_keep_looping) {
// do what you need to do
// if necessary:
sleep(1);
}
See here: http://www.cplusplus.com/forum/beginner/317/
There is on built in "timer" in C++, and you are correct about the behavior of sleep(). The above thread describes a custom implementation.
if you have a window you can use its message queue, as suggested by Als and Marius in their answers
you can use some task dispatcher and some timer to register a callback, e.g. functionality provided by Boost.Asio and its deadline_timer (example)
you can check if timer expired between your tasks manually as proposed in BlackJack's link
or you can create separate thread and make it call your callback when time came. Pros: you can use sleep() there and you callback will be called in parallel with your main thread. Cons: worry about synchronization
I have some native code and want to update the progressbar from the native code. There isn't no return value because it is a long duration task.
I found a small example http://developer.android.com/reference/android/widget/ProgressBar.html but when I move the update part in a extra method I get a NullPointerException.
It seems that this part must be in the thread of the progressbar.
I tried another way by using the AsyncTask as nested class, but I haven't access on the method publishProgress from anywhere outside the class itself.
Is there any possibility to get it working?
If it is possible to break your long task into multiple incremental calls then I'd recommend doing that. Then you can make those calls from a loop inside AsyncTask.doInBackground(), just like in the SDK examples.
If that's not possible, you'll need a progress variable in your native code that can safely be accessed from multiple threads: Write to it from your worker code and read it from a new "getProgress()" JNI function, with the appropriate synchronisation done in native code. You would then be able to call your getProgress() function from AsyncTask.doInBackground(), or whatever UI scheme you choose to use.
I am receiving messages from the network on a non-GUI thread and need to use wxEvtHandler::AddPendingEvent to tell the GUI to update accordingly. I also need to pass data to my GUI code so that it can act apropriately.
I believe I have to create a custom event, but haven't found a straightforward implementation. This closest thing that I've found is The wxWiki on Creating a Custom Event, which is a partial example.
If you are receiving messages from a different thread, then you explicitily can not use AddPendingEvent. You must instead use wxEvtHandler::QueueEvent.
Second, there are a couple of good examples for creating custom event classes: the old way, the new way.
With the old way, you can also use the Connect method and leave off the event table, but it's not illustrated in that example. The new way has the much preferred Bind method... but as you can see in my question, I'm having my own problems with it.