Android TextureView: onFrameAvailable not called - android

I have a TextureView in my xml:
<TextureView
android:id="#+id/textureView"
android:layout_width="300dp"
android:layout_height="300dp"
android:backgroundTint="#FF0000"
/>
I want to draw objects with openGL ES in this TextureView.
I do:
textureView = findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
#Override
public void onSurfaceTextureAvailable(#NonNull SurfaceTexture surface, int width, int height) {
Tools.logd("textureView onSurfaceTextureAvailable");
surface.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
#Override
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
Tools.logd("textureView onFrameAvailable");
}
});
}
#Override
public void onSurfaceTextureSizeChanged(#NonNull SurfaceTexture surface, int width, int height) {
Tools.logd("textureView onSurfaceTextureSizeChanged");
}
#Override
public boolean onSurfaceTextureDestroyed(#NonNull SurfaceTexture surface) {
Tools.logd("textureView onSurfaceTextureDestroyed");
return false;
}
#Override
public void onSurfaceTextureUpdated(#NonNull SurfaceTexture surface) {
Tools.logd("textureView onSurfaceTextureUpdated");
}
onSurfaceTextureAvailable is executed but then onFrameAvailable is never called (should be called 30 or 60 times per second).
How to solve this problem ?
Thanks.

Related

Android - TextureView + SurfaceHolder.Callback

I wanted to know if it is possible to use SurfaceHolder.Callback (and of course its callbacks) in combination with TextureView?
I mean sth. like the following:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private TextureView mTextureView;
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
}
Would that be working? The reason I ask is that whenever I see SurfaceHolder.Callback, they use SurfaceView.
No you can't. For TextureView you should use SurfaceTextureListener. In many senses these callbacks are similar to SurfaceHolder callbacks.
For example, instead of
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mPreviewSurface = holder.getSurface();
}
you can write
#Override
public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
mPreviewSurface = new Surface(texture);
}
See https://github.com/google/ExoPlayer/issues/98. Thanks to #BrentM.

How to live stream video from camera using texture view and not surfaceview?

I have seen many examples on how to live stream video from android camera to rtmp server using surfaceview. One is here : https://github.com/begeekmyfriend/yasea
But is it possible to stream the video from camera to rtmp using a textureview? If it is, how can we?
Textureview mTextureView;
// inside oncreate
mTextureView = (TextureView) findViewById(R.id.texture_view);
mTextureView.setSurfaceTextureListener(AircraftControlActivity.this);
// Outside OnCreate
#Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) {
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
}
#Override
public void onSurfaceTextureUpdated(final SurfaceTexture surface) {
}
What to do next?
Take a look at Texture View
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener
{
Camera camera;
TextureView textureView;
ImageButton button ; //ignore this one
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = (TextureView) findViewById(R.id.textureView);
button = (ImageButton)findViewById(R.id.imageButton);
textureView.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
camera = Camera.open();
try
{
camera.setPreviewTexture(surface);
camera.startPreview();
}
catch (IOException ioe)
{
// Something bad happened
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
{
// Ignored, Camera does all the work for us
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
{
camera.stopPreview();
camera.release();
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface)
{
// Invoked every time there's a new Camera preview frame
}
}

Get refrence to surface object assigned to window

Is it possible to get a reference to Surrface assigned to window of activity?
I know that you can do this by:
getWindow().takeSurface(new SurfaceHolder.Callback2() {
#Override
public void surfaceRedrawNeeded(SurfaceHolder surfaceHolder) {
}
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
// Draw view styff
Canvas c = surfaceHolder.lockCanvas();
c.drawRGB(0, 255, 255);
surfaceHolder.unlockCanvasAndPost(c);
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
});
But as stated in the documentation this basically disables the view hierarchy. Is there a way to get to this surface after view hierarchy has been drawn so I can draw some overlay over it?

How to add SurfaceView displaying camera preview with opacity

I want to add SurfaceView which displaying camera preview to my layout. It should be semi-transparent (alpha 0.5). I add View by addView() method.
Ho to do that. I tried animation but it seems to not works with camera.
My Surfave view looks like that:
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera theCamera;
public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}
My minAPI = 8.

Antialiasing in TextureView

I tried to play the same video with a SurfaceView and a TextureView and noticed that the image rendered with the TextureView is more aliased (less 'smooth') than with the SurfaceView.
What is the reason for this ? Is there any way to configure rendering of TextureView to look better ?
The TextureView is used like this:
TextureView textureView = new TextureView(this);
textureView.setSurfaceTextureListener(new SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Log.i("test", "onSurfaceTextureAvailable()");
MediaPlayer player = MediaPlayer.create(TestActivity.this, Uri.parse(VIDEO_URL));
Surface surface = new Surface(surfaceTexture);
player.setSurface(surface);
player.start();
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
Log.i("test", "onSurfaceTextureUpdated()");
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.i("test", "onSurfaceTextureSizeChanged()");
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.i("test", "onSurfaceTextureDestroyed()");
return false;
}
});
setContentView(textureView);
And for the SurfaceView:
SurfaceView surfaceView = new SurfaceView(this);
surfaceView.getHolder().addCallback(new Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("test", "surfaceCreated()");
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("test", "surfaceDestroyed()");
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.i("test", "surfaceChanged()");
MediaPlayer player = MediaPlayer.create(TestActivity.this, Uri.parse(VIDEO_URL));
player.setSurface(holder.getSurface());
player.start();
}
});
setContentView(surfaceView);
Well, it seems that applying a scaling (other than 1) on the TextureView does the 'smoothing' effect that I'm looking for.
textureView.setScaleX(1.00001f);
That sounds like a strange hack...but it works. Would be interesting to dig out what's done in the scaling that changes the rendering aspect...

Categories

Resources