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