1 /* 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import org.webrtc.VideoFrame; 14 15 /** 16 * Used from native api and implements a simple VideoCapturer.CapturerObserver that feeds frames to 17 * a webrtc::jni::AndroidVideoTrackSource. 18 */ 19 class NativeCapturerObserver implements CapturerObserver { 20 private final NativeAndroidVideoTrackSource nativeAndroidVideoTrackSource; 21 22 @CalledByNative NativeCapturerObserver(long nativeSource)23 public NativeCapturerObserver(long nativeSource) { 24 this.nativeAndroidVideoTrackSource = new NativeAndroidVideoTrackSource(nativeSource); 25 } 26 27 @Override onCapturerStarted(boolean success)28 public void onCapturerStarted(boolean success) { 29 nativeAndroidVideoTrackSource.setState(success); 30 } 31 32 @Override onCapturerStopped()33 public void onCapturerStopped() { 34 nativeAndroidVideoTrackSource.setState(/* isLive= */ false); 35 } 36 37 @Override onFrameCaptured(VideoFrame frame)38 public void onFrameCaptured(VideoFrame frame) { 39 final VideoProcessor.FrameAdaptationParameters parameters = 40 nativeAndroidVideoTrackSource.adaptFrame(frame); 41 if (parameters == null) { 42 // Drop frame. 43 return; 44 } 45 46 final VideoFrame.Buffer adaptedBuffer = 47 frame.getBuffer().cropAndScale(parameters.cropX, parameters.cropY, parameters.cropWidth, 48 parameters.cropHeight, parameters.scaleWidth, parameters.scaleHeight); 49 nativeAndroidVideoTrackSource.onFrameCaptured( 50 new VideoFrame(adaptedBuffer, frame.getRotation(), parameters.timestampNs)); 51 adaptedBuffer.release(); 52 } 53 } 54