Can an arbitrary target fps range be specified with Camera2 API? - android

In our mobile application, camera capture on the Android device is sent as a video stream to a remote server.
I need to automatically adapt my camera fps to the network speed. Basically, if I detect that the network is slow, I need to reduce the fps and keep reducing it until a balance is reached.
I obtain available fps ranges using the field CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES on Camera2 API. I set the target fps using CONTROL_AE_TARGET_FPS_RANGE field.
Let's say the possible list of ranges, for example, is (30, 30) and (15, 30).
I started by setting the target with the highest fps ((30, 30) in our case). Once I detected that the network is slow, I reduced the fps range to (15, 30). However, what I noticed is that the device continued to generate about 29 fps.
As an experiment, I forced the target fps value to be (15, 15). This seems to have done the trick. The system started to generate 15 fps, a value that I was expecting.
However, this makes me wonder what really is the relationship between CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES and CONTROL_AE_TARGET_FPS_RANGE. My impression was that the target
range that is set on the camera has to be one of the values received from CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES. This would be either (30, 30) or (15, 30) in my case. However, if (15, 15) is also being accepted as a valid target fps, I wonder if I can specify any range inside a valid range. For example, I would like to set the fps to (29, 29), (28, 28), and so on until a balance is reached. Is this allowed?

Generally speaking, the answer is NO.
The contract requires that all supported FPS ranges be published in CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES. Furthermore, the behavior of the device is undefined when you choose an unsupported camera parameter, e.g. frame rate or preview size. Some device will throw a RuntimeException, another could keep the current setting, yet another will choose something 'as close as possible to what you ask'.
Some devices don't publish all supported FPS ranges, but also the API is not always possible to implement to the letter. For example, consider a camera that can deliver full HD 1920x1080 frames at 30 FPS, but for smaller 1280x720 frames supports 60 FPS? Which supported FPS ranges should it publish? What if these setting depend on some other choices, like night scene mode, or on focus distance?
And I have not yet spoken about the bugs that often happen on less polished devices. It isn't uncommon to see a device that declares some supported FPS or size is supported, but actually fails to set it (again, with a variety of results).
If your application or library intends to cover millions of users with a wide variety of hardware, you have no choice but to keep certain white-lists and black-lists for device features that may and may not be used, that take into account the manufacturer, the device model, and sometimes even the system version (e.g. I have seen over-the-air upgrades that broke certain, admittedly marginal, camera features).
Another note is that floating FPS ranges cannot be used for video recording or transmission. If you choose (15, 30) range, you will have problems with many video players, the audio will never be in sync with video, and you will still have no control of the bitrate.
TL;NR: in your specific case, there is no need to bother with unsupported undocumented (15, 15) FPS range. You can easily drop every second frame, and pass 15 FPS to the network, still keeping the supported (30, 30) range. If you need an arbitrary uniform rate of, say, 20 FPS, you are less lucky. There are ways to delay delivery of next frame a bit, but nobody will guarantee that exactly 50000000 nanoseconds will pass between these frames.

Related

Does sampling rates of sensors change when update the API version on the same device?

As in the title, I am wondering whether the pre-defined sampling rates of sensors could change when I updated the android version on my device from API 26 to API 28, for example?
In detail, assume I put the sampling rate for the accelerometer and gyroscope sensors at 100 Hz when the device Galaxy Samsung S8's android version has API 26. Then if I update the Android version to API 28, would that change or affect the sampling rates of the two sensors?
I searched a lot but could not find an official answer or resources.
Please, if could answer or share some useful links, that would be highly appreciated.
In short -- No, an API Level Change won't influence your sensor sampling rate. Nothing has changed in that field since API Level 3 (esp. adding the possibility to specifiy your own sampling rate). The main link for this topic is on Monitoring Sensor Events in the official Android Dev Docs.
A bit extensive quoting for your interest:
The data delay (or sampling rate) controls the interval at which sensor events are sent to your application via the onSensorChanged() callback method. The default data delay is suitable for monitoring typical screen orientation changes and uses a delay of 200,000 microseconds. You can specify other data delays, such as SENSOR_DELAY_GAME (20,000 microsecond delay), SENSOR_DELAY_UI (60,000 microsecond delay), or SENSOR_DELAY_FASTEST (0 microsecond delay). As of Android 3.0 (API Level 11) you can also specify the delay as an absolute value (in microseconds).
But pay attention, that specifying 100 Hz (so a delay of 10 microseconds) doesn't guarantee that rate anyway:
The delay that you specify is only a suggested delay. The Android system and other applications can alter this delay. As a best practice, you should specify the largest delay that you can because the system typically uses a smaller delay than the one you specify (that is, you should choose the slowest sampling rate that still meets the needs of your application). Using a larger delay imposes a lower load on the processor and therefore uses less power.
Of course the question arises, what rate do one get:
There is no public method for determining the rate at which the sensor framework is sending sensor events to your application; however, you can use the timestamps that are associated with each sensor event to calculate the sampling rate over several events.
Therefore you can write a small test program or use an existing one, there are plenty of them on Github.
But the mere update of the API Level shouldn't change anything.

What is the purpose and how to use CaptureRequest.SENSOR_FRAME_DURATION in Camera2 API?

When setting manual controls in Android by using the Camera2 API, what is the purpose of CaptureRequest.SENSOR_FRAME_DURATION?
I have read several times the documentation but still can’t understand its purpose, and what value to set in relation to the exposure time and ISO.
I understand the CaptureRequest.SENSOR_EXPOSURE_TIME specifies how much light is the sensor letting in; also that the CaptureRequest.SENSOR_SENSITIVITY is the sensor sensitivity to light (ISO), but no idea about SENSOR_FRAME_DURATION and how it relates to the exposure time and sensor sensitivity.
For example, if I set a long exposure time of 1 second or 30 seconds, then what is the value that I should set in SENSOR_FRAME_DURATION? And how does it relate to the other sensor controls?
FRAME_DURATION is the same concept as output frame rate. That is, how often is an image read out from the image sensor? Frame rate is generally reported as frames per second, while FRAME_DURATION is the inverse of that - the duration of a single frame.
Since the camera2 API is all about per-frame control, having the duration as a per-frame property is appropriate.
FRAME_DURATION can't be shorter than EXPOSURE_TIME (since you can't read the image from the sensor until exposure is complete), but the API handles this for you - if you ask for a FRAME_DURATION that's too short compared to EXPOSURE_TIME, it gets automatically increased.
That said, often you may want consistent frame rate (such as 30fps for video recording), so you'd set your FRAME_DURATION to 1/30s = 33333333 ns, and then vary EXPOSURE_TIME for manual exposure control. As long as you keep EXPOSURE_TIME as less than 1/30s, you'll get steady frame rate and still have manual exposure control.
The minimum possible frame duration (and therefore the maximum frame rate) depends on the output resolution(s) and format(s) you've asked for in the camera capture session. Generally, bigger resolutions take longer to read out, putting a limit on minimum frame duration. Cameras that support the BURST_CAPTURE camera capability can handle at least 20fps for 8 MP captures, or better.
At the image sensor level, frame duration is implemented by adding in extra vertical blanking time so that EXPOSURE + VBLANK = FRAME_DURATION. The full picture is also more complicated in that typical CMOS image sensors can be exposing some rows of the image while others are being read out (rolling shutter) so the actual timing diagrams look more complicated. You don't generally have to care when just doing basic manual exposure control, however.
Most of the image sensors in a smart phone are using rolling shutter, which readout pixels line by line, the FRAME_DURATION = FRAME_READ_OUT_TIME + VBLANK.

How to fix the frame rate of camera in Android phone

I want to fix the frame rate of camera preview in Android, i.e., 20fps, or 30 fps. However, we find the frame rate is unstable.
In the android document, it is said that the frame rate is fluctuated between the minimum frame rate and the maximum one which are defined in getSupportedPreviewFpsRange.
https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getSupportedPreviewFpsRange%28%29
My questions are:
1) Which factors influence the frame rate? exposure time, white balance, frame resolution, background CPU loading, and etc.?
2) Is there any method to fix the frame rate by customised above factors?
3) In my project, higher frame rate is better. If the frame rate is unstable in the end. Can I increase the minimum frame rate? or fix the minimum frame rate?
4) It seems that the video taking is somewhat different with preview model, Can I fix the frame rate or minimum frame rate of video taking in Android?
Finally, we found that IOS can fix the frame rate using videoMinFrameDuration and
videoMaxFrameDuration.
Thanks.
First of all, please note that the camera API that you ask about was deprecated more than 3 years ago. The new camera2 API provides much more control over all aspects of capture, including frame rate.
Especially, if your goal is smooth video recording. Actually, the MediaRecorder performs its job decently on older devices, but I understand that this knowledge has little practical value if for some reason you cannot use the MediaRecorder.
Usually, the list of supported FPS ranges includes fixed ranges, e.g. 30 fps, intended exactly for video recording. Note that you are expected to choose a compliant (recommended) preview (video) resolution.
Two major factors cause frame rate variations within the declared range: exposure adjustments and focus adjustments. To achieve uniform rate, you should disable autofocus. If your camera supports exposure control, you should lock it, too. Refrain from using exotic "scenes" and "effects". SCENE_MODE_BARCODE and EFFECT_MONO don't seem to cause problems with frame rate. Whitebalance is OK, too.
There exist other factors that cause frame rate fluctuations that are completely under your control.
Make sure that your camera callbacks do not interfere with, and are not delayed by the Main (UI) thread. To achieve that, you must open the camera on a secondary HandlerThread. The new camera2 API makes thread management for camera callbacks easier.
Don't use setPreviewCallback() which automatically allocates pixel buffers for each frame. This is a significant burden for garbage collector, which may lock all threads once in a while for major cleanup. Instead, use setPreviewCallbackWithBuffer() and preallocate just enough pixel buffers to keep it always busy.
Don't perform heavy calculations in the context of your onPreviewFrame() callback. Pass all work to a different thread. Do your best to release the pixel buffer as early as possible.
Even for the old camera API, if the device lists a supported FPS range of (30, 30), then you should be able to select this range and get consistent, fixed video recording.
Unfortunately, some devices disregard your frame rate request once the scene conditions get too dark, and increase exposure times past 1/30s. For many applications, this is the preferable option, but such applications should simply be selecting a wider frame rate range like (15, 30).

Getting a stable frame rate from Android

I'm trying to get a stable frame rate from a simple camera Android application written based on the guide below.
http://developer.android.com/guide/topics/media/camera.html - Non intent version.
There is only preview and there is no image or video capture. Everytime onPreview (implementating previewcallback) is called, I check timestamp differences to measure frame rate. Though on average it meets the 15 FPS rate I set ( setPreviewFpsRange(15000, 15000 and verified that it is supported on the device using getSupportedPreviewFpsRange() ), the individual frame rate vary from 5 fps to 40 fps.
Is there a way to fix this. What are the reasons for it? Guess one reason is application process priority. It was observed that adding more applications reduced the fps. One solution is to increase priority of this camera application. Second reason could be garbage collection and slow buffer copies to preview. Third reason is that camera api (not the new camera2 api of Android L - my device is not supported yet) was not designed for streaming camera data.
Also the exposure time lock was enabled to fix frame rates.
It's most likely just how you are timestamping the frames. Try saving a sequence of preview frames pointed at a stopwatch to disk and viewing them using a YUV player (i.e http://www.yuvtoolkit.com/).

Forcing the camera in Android to deliver 30 FPS

I'm trying to use the camera for video processing that needs a high constant frame rate around 30 FPS.
I'm using the Camera class and setPreviewCallbackWithBuffer to receive the video frames. I have noticed that most camera does not support a FPS range of 30000 - 30000. However, when recording movies I assume the camera on those devices still delivers a frame rate around 30. Is there some other way to achieve a higher frame rates than with my current method?
Note that non-top devices with cheap cameras, especially front ones, don't support reliably fps you've requested. If you request 30, device can reply ok (will start capture, no crash, etc) but in real it will deliver frames with fps in range say... [4-30] depending on lighting conditions (less light needs longer exposition time) and may be something else too. Example of such camera is front camera on galaxy S3 mini
If you don't want to use static fps rate for all the devices, you can use getSupportedPreviewFpsRange () method to determine the available fps range for that particular device. This method will return minimum and maximum supported fps rate.
Now after getting the maximum supported fps rate, you can use your current method to set the fps rate.
Hope this will give you some hint about setting fps to its maxmimum.

Categories

Resources