sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_ |
| 12 | #define SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_ |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 13 | |
| 14 | #include <jni.h> |
| 15 | #include <deque> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/video_codecs/video_encoder.h" |
| 20 | #include "common_video/h264/h264_bitstream_parser.h" |
| 21 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
| 22 | #include "rtc_base/task_queue.h" |
| 23 | #include "sdk/android/src/jni/jni_helpers.h" |
| 24 | #include "sdk/android/src/jni/native_handle_impl.h" |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace jni { |
| 28 | |
Magnus Jedvert | 0371e10 | 2017-11-07 14:23:44 +0100 | [diff] [blame] | 29 | // Wraps a Java encoder and delegates all calls to it. |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 30 | class VideoEncoderWrapper : public VideoEncoder { |
| 31 | public: |
| 32 | VideoEncoderWrapper(JNIEnv* jni, jobject j_encoder); |
| 33 | |
| 34 | int32_t InitEncode(const VideoCodec* codec_settings, |
| 35 | int32_t number_of_cores, |
| 36 | size_t max_payload_size) override; |
| 37 | |
| 38 | int32_t RegisterEncodeCompleteCallback( |
| 39 | EncodedImageCallback* callback) override; |
| 40 | |
| 41 | int32_t Release() override; |
| 42 | |
| 43 | int32_t Encode(const VideoFrame& frame, |
| 44 | const CodecSpecificInfo* codec_specific_info, |
| 45 | const std::vector<FrameType>* frame_types) override; |
| 46 | |
| 47 | int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
| 48 | |
| 49 | int32_t SetRateAllocation(const BitrateAllocation& allocation, |
| 50 | uint32_t framerate) override; |
| 51 | |
| 52 | ScalingSettings GetScalingSettings() const override; |
| 53 | |
| 54 | bool SupportsNativeHandle() const override { return true; } |
| 55 | |
| 56 | // Should only be called by JNI. |
| 57 | void OnEncodedFrame(JNIEnv* jni, |
Magnus Jedvert | 18ce0f0 | 2017-11-07 17:34:38 +0100 | [diff] [blame] | 58 | jobject j_caller, |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 59 | jobject j_buffer, |
| 60 | jint encoded_width, |
| 61 | jint encoded_height, |
| 62 | jlong capture_time_ms, |
| 63 | jint frame_type, |
| 64 | jint rotation, |
| 65 | jboolean complete_frame, |
| 66 | jobject j_qp); |
| 67 | |
| 68 | const char* ImplementationName() const override; |
| 69 | |
| 70 | private: |
| 71 | struct FrameExtraInfo { |
| 72 | uint64_t capture_time_ns; // Used as an identifier of the frame. |
| 73 | |
| 74 | uint32_t timestamp_rtp; |
| 75 | }; |
| 76 | |
| 77 | int32_t InitEncodeInternal(JNIEnv* jni); |
| 78 | |
| 79 | // Takes Java VideoCodecStatus, handles it and returns WEBRTC_VIDEO_CODEC_* |
| 80 | // status code. |
| 81 | int32_t HandleReturnCode(JNIEnv* jni, jobject code); |
| 82 | |
| 83 | RTPFragmentationHeader ParseFragmentationHeader( |
| 84 | const std::vector<uint8_t>& buffer); |
| 85 | int ParseQp(const std::vector<uint8_t>& buffer); |
| 86 | CodecSpecificInfo ParseCodecSpecificInfo(const EncodedImage& frame); |
| 87 | jobject ToJavaBitrateAllocation(JNIEnv* jni, |
| 88 | const BitrateAllocation& allocation); |
| 89 | std::string GetImplementationName(JNIEnv* jni) const; |
| 90 | |
| 91 | const ScopedGlobalRef<jobject> encoder_; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 92 | const ScopedGlobalRef<jclass> frame_type_class_; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 93 | const ScopedGlobalRef<jclass> int_array_class_; |
| 94 | |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 95 | std::string implementation_name_; |
| 96 | |
| 97 | rtc::TaskQueue* encoder_queue_; |
| 98 | JavaVideoFrameFactory video_frame_factory_; |
| 99 | std::deque<FrameExtraInfo> frame_extra_infos_; |
| 100 | EncodedImageCallback* callback_; |
| 101 | bool initialized_; |
| 102 | int num_resets_; |
| 103 | int number_of_cores_; |
| 104 | VideoCodec codec_settings_; |
| 105 | H264BitstreamParser h264_bitstream_parser_; |
| 106 | |
| 107 | // RTP state. |
| 108 | uint16_t picture_id_; |
| 109 | uint8_t tl0_pic_idx_; |
| 110 | |
| 111 | // VP9 variables to populate codec specific structure. |
| 112 | GofInfoVP9 gof_; // Contains each frame's temporal information for |
| 113 | // non-flexible VP9 mode. |
| 114 | size_t gof_idx_; |
| 115 | }; |
| 116 | |
| 117 | } // namespace jni |
| 118 | } // namespace webrtc |
| 119 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 120 | #endif // SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_ |