blob: 7717eb8e43115e544c8e0bd170d0096c61d45d90 [file] [log] [blame]
sakal07a3bd72017-09-04 03:57:21 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_
12#define SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_
sakal07a3bd72017-09-04 03:57:21 -070013
14#include <jni.h>
15#include <deque>
16#include <string>
17#include <vector>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#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"
sakal07a3bd72017-09-04 03:57:21 -070025
26namespace webrtc {
27namespace jni {
28
Magnus Jedvert0371e102017-11-07 14:23:44 +010029// Wraps a Java encoder and delegates all calls to it.
sakal07a3bd72017-09-04 03:57:21 -070030class 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,
58 jobject j_buffer,
59 jint encoded_width,
60 jint encoded_height,
61 jlong capture_time_ms,
62 jint frame_type,
63 jint rotation,
64 jboolean complete_frame,
65 jobject j_qp);
66
67 const char* ImplementationName() const override;
68
69 private:
70 struct FrameExtraInfo {
71 uint64_t capture_time_ns; // Used as an identifier of the frame.
72
73 uint32_t timestamp_rtp;
74 };
75
76 int32_t InitEncodeInternal(JNIEnv* jni);
77
78 // Takes Java VideoCodecStatus, handles it and returns WEBRTC_VIDEO_CODEC_*
79 // status code.
80 int32_t HandleReturnCode(JNIEnv* jni, jobject code);
81
82 RTPFragmentationHeader ParseFragmentationHeader(
83 const std::vector<uint8_t>& buffer);
84 int ParseQp(const std::vector<uint8_t>& buffer);
85 CodecSpecificInfo ParseCodecSpecificInfo(const EncodedImage& frame);
86 jobject ToJavaBitrateAllocation(JNIEnv* jni,
87 const BitrateAllocation& allocation);
88 std::string GetImplementationName(JNIEnv* jni) const;
89
90 const ScopedGlobalRef<jobject> encoder_;
sakal07a3bd72017-09-04 03:57:21 -070091 const ScopedGlobalRef<jclass> frame_type_class_;
sakal07a3bd72017-09-04 03:57:21 -070092 const ScopedGlobalRef<jclass> int_array_class_;
93
sakal07a3bd72017-09-04 03:57:21 -070094 std::string implementation_name_;
95
96 rtc::TaskQueue* encoder_queue_;
97 JavaVideoFrameFactory video_frame_factory_;
98 std::deque<FrameExtraInfo> frame_extra_infos_;
99 EncodedImageCallback* callback_;
100 bool initialized_;
101 int num_resets_;
102 int number_of_cores_;
103 VideoCodec codec_settings_;
104 H264BitstreamParser h264_bitstream_parser_;
105
106 // RTP state.
107 uint16_t picture_id_;
108 uint8_t tl0_pic_idx_;
109
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} // namespace jni
117} // namespace webrtc
118
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200119#endif // SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_