google / grafika

Grafika test app
Apache License 2.0
6.03k stars 1.46k forks source link

How to show front and back camera simultaneously? #120

Open Arslanmajeed270 opened 3 years ago

Arslanmajeed270 commented 3 years ago

Hi i want to use both front and back camera simultaneously and then i have to stream it using rtmp. Can someone help regarding this issue?

Thanks in advance.

Regards, Arslan Majeed

alexcohn commented 3 years ago

First of all, you need a compatible device. Many devices with Snapdragon SoC are capable, see https://www.qualcomm.com/news/onq/2015/10/30/snapdragon-dual-cameras-capture-photos-first-adjust-focus-later.

Then, camera2 API lets you open the two devices.

Finally, you can encode the video as a single combined stream (e.g. Picture in Picture), or as separate video streams.

There is nothing special that you must do to produce RTMP.

Arslanmajeed270 commented 3 years ago

First of all, you need a compatible device. Many devices with Snapdragon SoC are capable, see https://www.qualcomm.com/news/onq/2015/10/30/snapdragon-dual-cameras-capture-photos-first-adjust-focus-later.

Then, camera2 API lets you open the two devices.

Finally, you can encode the video as a single combined stream (e.g. Picture in Picture), or as separate video streams.

There is nothing special that you must do to produce RTMP.

@alexcohn thanks for the reply. Can you tell me how can i encode both stream into single stream so that i can stream through rtmp.

alexcohn commented 3 years ago

how can i encode both stream into single stream

First, you configure the two streams to be of the same width. Then, you create a stream of this width, and the height that is a sum of the two, and a pixel buffer that is enough to hold this. You copy the frame pixels of the back camera to the upper half, the pixels of the front camera to the lower part. When both parts are ready, you send the frame to the encoder (using MediaCodec). The encoded frame is sent to RTMP same as if you had a single camera.

Arslanmajeed270 commented 3 years ago

how can i encode both stream into single stream

First, you configure the two streams to be of the same width. Then, you create a stream of this width, and the height that is a sum of the two, and a pixel buffer that is enough to hold this. You copy the frame pixels of the back camera to the upper half, the pixels of the front camera to the lower part. When both parts are ready, you send the frame to the encoder (using MediaCodec). The encoded frame is sent to RTMP same as if you had a single camera.

I have did exact same but the problem is that i want back camera should be full width and front camera should be overlap on it so that it look like picture in picture.

alexcohn commented 3 years ago

So, you have no choice but to copy one frame over the other (Y plane separate from UV plane, line by line).