1 /* 2 * Copyright 2017 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 #ifndef SDK_ANDROID_SRC_JNI_VIDEO_ENCODER_WRAPPER_H_ 12 #define SDK_ANDROID_SRC_JNI_VIDEO_ENCODER_WRAPPER_H_ 13 14 #include <jni.h> 15 16 #include <deque> 17 #include <memory> 18 #include <string> 19 #include <vector> 20 21 #include "absl/types/optional.h" 22 #include "api/video_codecs/video_encoder.h" 23 #include "common_video/h264/h264_bitstream_parser.h" 24 #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" 25 #include "modules/video_coding/svc/scalable_video_controller_no_layering.h" 26 #include "rtc_base/synchronization/mutex.h" 27 #include "sdk/android/src/jni/jni_helpers.h" 28 29 namespace webrtc { 30 namespace jni { 31 32 // Wraps a Java encoder and delegates all calls to it. 33 class VideoEncoderWrapper : public VideoEncoder { 34 public: 35 VideoEncoderWrapper(JNIEnv* jni, const JavaRef<jobject>& j_encoder); 36 ~VideoEncoderWrapper() override; 37 38 int32_t InitEncode(const VideoCodec* codec_settings, 39 const Settings& settings) override; 40 41 int32_t RegisterEncodeCompleteCallback( 42 EncodedImageCallback* callback) override; 43 44 int32_t Release() override; 45 46 int32_t Encode(const VideoFrame& frame, 47 const std::vector<VideoFrameType>* frame_types) override; 48 49 void SetRates(const RateControlParameters& rc_parameters) override; 50 51 EncoderInfo GetEncoderInfo() const override; 52 53 // Should only be called by JNI. 54 void OnEncodedFrame(JNIEnv* jni, 55 const JavaRef<jobject>& j_encoded_image); 56 57 private: 58 struct FrameExtraInfo { 59 int64_t capture_time_ns; // Used as an identifier of the frame. 60 61 uint32_t timestamp_rtp; 62 }; 63 64 int32_t InitEncodeInternal(JNIEnv* jni); 65 66 // Takes Java VideoCodecStatus, handles it and returns WEBRTC_VIDEO_CODEC_* 67 // status code. 68 int32_t HandleReturnCode(JNIEnv* jni, 69 const JavaRef<jobject>& j_value, 70 const char* method_name); 71 72 int ParseQp(rtc::ArrayView<const uint8_t> buffer); 73 74 CodecSpecificInfo ParseCodecSpecificInfo(const EncodedImage& frame); 75 76 ScopedJavaLocalRef<jobject> ToJavaBitrateAllocation( 77 JNIEnv* jni, 78 const VideoBitrateAllocation& allocation); 79 80 ScopedJavaLocalRef<jobject> ToJavaRateControlParameters( 81 JNIEnv* jni, 82 const VideoEncoder::RateControlParameters& rc_parameters); 83 84 void UpdateEncoderInfo(JNIEnv* jni); 85 86 ScalingSettings GetScalingSettingsInternal(JNIEnv* jni) const; 87 std::vector<ResolutionBitrateLimits> GetResolutionBitrateLimits( 88 JNIEnv* jni) const; 89 90 VideoEncoder::EncoderInfo GetEncoderInfoInternal(JNIEnv* jni) const; 91 92 const ScopedJavaGlobalRef<jobject> encoder_; 93 const ScopedJavaGlobalRef<jclass> int_array_class_; 94 95 // Modified both on the encoder thread and the callback thread. 96 Mutex frame_extra_infos_lock_; 97 std::deque<FrameExtraInfo> frame_extra_infos_ 98 RTC_GUARDED_BY(frame_extra_infos_lock_); 99 EncodedImageCallback* callback_; 100 bool initialized_; 101 int num_resets_; 102 absl::optional<VideoEncoder::Capabilities> capabilities_; 103 int number_of_cores_; 104 VideoCodec codec_settings_; 105 EncoderInfo encoder_info_; 106 H264BitstreamParser h264_bitstream_parser_; 107 108 // Fills frame dependencies in codec-agnostic format. 109 ScalableVideoControllerNoLayering svc_controller_; 110 // VP9 variables to populate codec specific structure. 111 GofInfoVP9 gof_; // Contains each frame's temporal information for 112 // non-flexible VP9 mode. 113 size_t gof_idx_; 114 }; 115 116 /* If the j_encoder is a wrapped native encoder, unwrap it. If it is not, 117 * wrap it in a VideoEncoderWrapper. 118 */ 119 std::unique_ptr<VideoEncoder> JavaToNativeVideoEncoder( 120 JNIEnv* jni, 121 const JavaRef<jobject>& j_encoder); 122 123 bool IsHardwareVideoEncoder(JNIEnv* jni, const JavaRef<jobject>& j_encoder); 124 125 std::vector<VideoEncoder::ResolutionBitrateLimits> 126 JavaToNativeResolutionBitrateLimits( 127 JNIEnv* jni, 128 const JavaRef<jobjectArray>& j_bitrate_limits_array); 129 130 } // namespace jni 131 } // namespace webrtc 132 133 #endif // SDK_ANDROID_SRC_JNI_VIDEO_ENCODER_WRAPPER_H_ 134