Захват видео с собственного программного источника¶
Разработка¶
В сборке Android SDK 1.1.0.26 добавлена возможность подключить собственную программную реализацию камеры для захвата изображения. Для этого необходимо:
-
Разработать Java класс, реализующий интерфейс CameraVideoCapturer
-
Импортировать в приложение модули
-
Подготовить объект
CustomCameraCapturerOptions
private CustomCameraCapturerOptions createCustomCameraCapturerOptions() { return new CustomCameraCapturerOptions() { private String cameraName; private CameraVideoCapturer.CameraEventsHandler eventsHandler; private boolean captureToTexture; @Override public Class<?>[] getCameraConstructorArgsTypes() { return new Class<?>[]{String.class, CameraVideoCapturer.CameraEventsHandler.class, boolean.class}; } @Override public Object[] getCameraConstructorArgs() { return new Object[]{cameraName, eventsHandler, captureToTexture}; } @Override public void setCameraName(String cameraName) { this.cameraName = cameraName; } @Override public void setEventsHandler(CameraVideoCapturer.CameraEventsHandler eventsHandler) { this.eventsHandler = eventsHandler; } @Override public void setCaptureToTexture(boolean captureToTexture) { this.captureToTexture = captureToTexture; } // Use your custom capturer class name here @Override public String getCameraClassName() { return your.custom.CameraCapturer; } @Override public Class<?>[] getEnumeratorConstructorArgsTypes() { return new Class[0]; } @Override public Object[] getEnumeratorConstructorArgs() { return new Object[0]; } // Use your custom capturer enumerator name here @Override public String getEnumeratorClassName() { return your.custom.CameraEnumerator; } }; }
-
В приложении перед публикацией потока выбрать собственную камеру
Пример использования¶
Используем собственную реализацию CameraVideoCapturer для доступа к вспышке:
-
Создание объекта
CustomCameraCapturerOptions
codeprivate CustomCameraCapturerOptions createCustomCameraCapturerOptions() { return new CustomCameraCapturerOptions() { private String cameraName; private CameraVideoCapturer.CameraEventsHandler eventsHandler; private boolean captureToTexture; @Override public Class<?>[] getCameraConstructorArgsTypes() { return new Class<?>[]{String.class, CameraVideoCapturer.CameraEventsHandler.class, boolean.class}; } @Override public Object[] getCameraConstructorArgs() { return new Object[]{cameraName, eventsHandler, captureToTexture}; } @Override public void setCameraName(String cameraName) { this.cameraName = cameraName; } @Override public void setEventsHandler(CameraVideoCapturer.CameraEventsHandler eventsHandler) { this.eventsHandler = eventsHandler; } @Override public void setCaptureToTexture(boolean captureToTexture) { this.captureToTexture = captureToTexture; } // Using org.webrtc.FlashlightCameraCapturer to access flashlight hidden controls. @Override public String getCameraClassName() { return CustomCameras.FLASHLIGHT_CAMERA_CAPTURER; } @Override public Class<?>[] getEnumeratorConstructorArgsTypes() { return new Class[0]; } @Override public Object[] getEnumeratorConstructorArgs() { return new Object[0]; } // Using org.webrtc.FlashlightCameraEnumerator to access flashlight hidden controls. @Override public String getEnumeratorClassName() { return CustomCameras.FLASHLIGHT_CAMERA_ENUMERATOR; } }; }
-
Выбор камеры
codeCameraCapturerFactory.getInstance().setCustomCameraCapturerOptions(createCustomCameraCapturerOptions()); mCameraCapturer = (LabelledSpinner) findViewById(R.id.camera_capturer); mCameraCapturer.setOnItemChosenListener(new LabelledSpinner.OnItemChosenListener() { @Override public void onItemChosen(View labelledSpinner, AdapterView<?> adapterView, View itemView, int position, long id) { String captureType = getResources().getStringArray(R.array.camera_capturer)[position]; switch (captureType) { case "flashlight": CameraCapturerFactory.getInstance().setCameraType(CameraCapturerFactory.CameraType.FLASHLIGHT_CAMERA); break; case "camera1capturer": CameraCapturerFactory.getInstance().setCameraType(CameraCapturerFactory.CameraType.CAMERA1CAPTURE); break; case "camera2capturer": CameraCapturerFactory.getInstance().setCameraType(CameraCapturerFactory.CameraType.CAMERA2CAPTURE); break; case "custom": CameraCapturerFactory.getInstance().setCameraType(CameraCapturerFactory.CameraType.CUSTOM); break; } mCameraSpinner.setItemsArray(Flashphoner.getMediaDevices().getVideoList()); } @Override public void onNothingChosen(View labelledSpinner, AdapterView<?> adapterView) { } });