onImageAvailable callback called but acquireLatestImage returns NO_BUFFER_AVAILABLE - android-ndk

I am working on Camera2 API to take pictures continuously in native side in C and it's working fine except that sometimes after receivinf onImageAvailable callback, when calling acquireLatestImage, return is NO_BUFFER_AVAILABLE.
As per Android documentation : https://developer.android.com/ndk/reference/struct/a-image-reader-image-listener#onimageavailable
Note that it is possible that calling AImageReader_acquireNextImage or AImageReader_acquireLatestImage returns AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE within this callback. For example, when there are multiple images and callbacks queued, if application called AImageReader_acquireLatestImage, some images will be returned to system before their corresponding callback is executed
Can someone please explain when actually this can happen and possible solution for this.

If you have multiple images already captured into the AImageReader, then calling AImageReader_acquireLatestImage will discard all of them except the newest, and then returns the newest one.
So you get a sequence like this:
onImageAvailable()
onImageAvailable()
onImageAvailable()
acquireLatestImage() -> OK
acquireLatestImage() -> NO_BUFFER_AVAILABLE
acquireLatestImage() -> NO_BUFFER_AVAILABLE
onImageAvailable()
acquireLatestImage() -> OK
The second call to acquireLatestImage() will get NO_BUFFER_AVAILABLE since the previous call discarded all other buffers, and no new images arrived before the second call.
If you want to always see all image buffers, then use acquireNextImage(), which does not discard older buffers and just returns the next one in the queue.
onImageAvailable()
onImageAvailable()
onImageAvailable()
acquireNextImage() -> OK
acquireNextImage() -> OK
acquireNextImage() -> OK
acquireNextImage() -> NO_BUFFER_AVAILABLE
onImageAvailable()
acquireNextImage() -> OK

Related

Capture Traces from AWS Lambda to Step Function to Lambda

I am experimenting with Lambdas and I am having a hard time passing traces from a Lambda to a Step Function which has a lambda within it.
So structure looks something like this:
Lambda Code call step function -> Step function -> Lambda.
Problem is that I am getting two different traces, instead of the desired one trace,
which effectively captures lambda -> step function -> lambda all under one trace id.
1st trace - Lambda A
2nd trace - Step Function -> Lambda B
Is this possible to unify the traces, so it looks like this?
Trace 3 - Lambda A -> Step Function -> Lambda B (TraceId: 1) ~ Something like that
And if so how would I go about doing that.
Thanks
Are you passing the trace header with value of Lambda A trace ID when invoking the State Machine? See https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html
traceHeader
Passes the AWS X-Ray trace header. The trace header can also be passed in the request payload.

What is the proper way to return a JSON object to Alexa Smart Home or end AWS Lambda in NodeJS?

I have seen three ways to return a JSON object or end a Lambda function. My trigger is Smart Home Alexa.
I am using now is context.succeed(response_JSON);This one works for me. Even if this instructions is inside a nested function. The whole Lambda ends and return the response_JSON to Smart Home Alexa.
I have seen in other blogs that say callback(response_error,response_JSON). This one did not work for me. It did not return anything to Smart Home.
Others just uses the return response_JSON. I have not used this one.
I am using now is context.succeed(response_JSON);This one works for me. Even if this instructions is inside a nested function. The whole Lambda ends and return the response_JSON to Smart Home Alexa.
context.succeed()/fail() causes the Lambda function to terminate immediately. However, I have not seen this documented in the context object docs, so it may get deprecated in later Node versions (?).
I have seen in other blogs that say callback(response_error,response_JSON). This one did not work for me. It did not return anything to Smart Home.
This one probably doesn't work for you because by default Node.js waits for the event loop to be empty before executing the callback statement. This may be due to open network/database connection. As per the doc, set the context.callbackWaitsForEmptyEventLoop variable to false to send the response right away.
Others just uses the return response_JSON. I have not used this one.
This should be used with async handlers. Read more about async and non-async handlers here: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

Read in file with client-side clojurescript/re-frame app

I'm writing a client-side application which should read in a file, transform its content and then export the result. To do this, I decided on Re-Frame.
Now, I'm just starting to wrap my head around Re-Frame and cloujurescipt itself and got the following thing to work:
Somewhere in my view functions, I send this whenever a new file gets selected via a simple HTML input.
[:input {:class "file-input" :type "file"
:on-change #(re-frame/dispatch
[::events/file-name-change (-> % .-target .-value)])}]
What I get is something like C:\fakepath\file-name.txt, with fakepath actually being part of it.
My event handler currently only splits the name and saves the file name to which my input above is subscribed to display the selected file.
(re-frame/reg-event-db
::file-name-change
(fn [db [_ new-name]]
(assoc db :file-name (last (split new-name #"\\")))))
Additionally I want to read in the file to later process it locally. Assuming I'd just change my on-change action and the event handler to do this instead, how would I do it?
I've searched for a while but found next to nothing. The only things that came up where other frameworks and such, but I don't want to introduce a new dependency for each and every new problem.
I'm assuming you want to do everything in the client using HTML5 APIs (eg. no actual upload to a server).
This guide from MDN may come handy: https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications
It seems you can subscribe to the event triggered when the user selects the file(s), then you can obtain a list of said files, and inspect the files contents through the File API: https://developer.mozilla.org/en-US/docs/Web/API/File
In your case, you'll need to save a reference to the FileList object from the event somewhere, and re-use it later.

Is there a standard pattern for verifying an async request is still needed?

In mobile apps apps we can't (or should not) make network requests on the main thread. We normally get the results of the request back via a callback or a closure that is executed on the main thread when the result is available. Since the user may have moved on or the result may no longer be need, for example it may be an old request arriving out of order, we need to check that the action in the callback or closure should actually be executed based on the current state of the app.
In the case of iOS and swift I am planning on using closures so I am thinking of doing something like this for every request I make.
assume I have a method that looks something like this
func makeRequest(identifier: String, handler: (ident: String, result: ResultObject) -> Void) {
...
...
handler(identifier, result)
}
In addition to the handler that will be called when the result is available, I will pass in the value of an identifier, which in turn will be passed to the handler when it is called. The closure will capture a reference to the identifier when the request is created, so it be able to get the value that the reference holds at the time the handler is actually called. So it would look something like this, where ident is the value that commandIdentifier was when the request was made, and commandIdentifier inside the closure will be the value when the closure is actually executed.
commandIdentifer = "some unique identifier"
makeRequest(commandIdentifer) { ident, result in
if commandIdentifier == ident {
// do something
} else {
// do something else
}
}
I don't think there is anything special here, so my question is this:
Is this a general pattern, and if so where can I find any documentation on it?
I am particularly interested if there is some general way of creating the identifier and how to relate its reference in the main thread.
Also if I am total wrong and this not a good approach, I would like to hear that as well
I've used almost exactly that approach before. I use an integer identifier, and increment it when issuing a new request. That way if the pending request is superseded by a new one you can just drop the stale response on the floor.

Tkinter traffic-jamming

I have an expensive function that is called via a Tkinter callback:
def func: # called whenever there is a mouse press in the screen.
print("Busy? " + str(X.busy)) # X.busy is my own varaible and is initialized False.
X.busy = True
do_calculations() # do_calculations contains several tk.Canvas().update() calls
X.busy = False
When I click too quickly, the func()'s appear to pile up because the print gives "Busy? True", indicating that the function hasen't finished yet and we are starting it on another thread.
However, print(threading.current_thread()) always gives <_MainThread(MainThread, started 123...)>, the 123... is always the same each print for a given program run. How can the same thread be multiple threads?
It looks to me like you're running into recursive message processing. In particular, tk.Canvas().update() will process any pending messages, including extra button clicks. Further, it will do this on the same thread (at least on Windows).
So your thread ID is constant, but your stack trace will have multiple nested calls to func.

Resources