Search code examples
androidunity-game-engineagora.ioagora-implementation

Select second front camera on Android device with Agora.io in Unity


I am using an Android device with two front facing cameras and I need to use the Ultra Wide one (that is not the default one use by Agora) for the video chat. How can I accomplish this? It works just fine with the default front facing camera.

I tried following the documentation for using a custom source here: https://docs.agora.io/en/video-calling/develop/custom-video-and-audio?platform=unity but was unable to get it to work.


Solution

  • The documentation was missing a crucial part - getting the image from the camera and pushing that data to Agora server.

        private void Update()
        {
            if (_webCameraTexture == null || !_webCameraTexture.isPlaying) return;
    
            if (_texture2D == null)
                _texture2D = new Texture2D(_webCameraTexture.width, _webCameraTexture.height);
    
            Color[] pixels = _webCameraTexture.GetPixels();
            _texture2D.SetPixels(pixels);
            _texture2D.Apply();
    
            int width = _texture2D.width;
            int height = _texture2D.height;
    
            // Convert Texture2D to a byte array
            byte[] textureBytes = _texture2D.GetRawTextureData();
    
            // Create an external video frame
            ExternalVideoFrame externalVideoFrame = new ExternalVideoFrame();
            externalVideoFrame.type = VIDEO_BUFFER_TYPE.VIDEO_BUFFER_RAW_DATA;
            externalVideoFrame.format = VIDEO_PIXEL_FORMAT.VIDEO_PIXEL_RGBA;
            externalVideoFrame.buffer = textureBytes;
            externalVideoFrame.stride = width;
            externalVideoFrame.height = height;
            externalVideoFrame.timestamp = DateTime.Now.Ticks / 10000; // Convert to ms
    
            RtcEngine.PushVideoFrame(externalVideoFrame);
        }