blob: afda8d2731610886c7b261b6319b918f0c14665b [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
11#ifndef WEBRTC_SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_
12#define WEBRTC_SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_
13
14#include <jni.h>
15#include <deque>
16#include <string>
17#include <vector>
18
19#include "webrtc/api/video_codecs/video_encoder.h"
20#include "webrtc/common_video/h264/h264_bitstream_parser.h"
21#include "webrtc/modules/video_coding/codecs/vp9/include/vp9_globals.h"
22#include "webrtc/rtc_base/task_queue.h"
23#include "webrtc/sdk/android/src/jni/jni_helpers.h"
24#include "webrtc/sdk/android/src/jni/native_handle_impl.h"
25
26namespace webrtc {
27namespace jni {
28
29// Wraps a Java decoder and delegates all calls to it. Passes
30// VideoEncoderWrapperCallback to the decoder on InitDecode. Wraps the received
31// frames to AndroidVideoBuffer.
32class VideoEncoderWrapper : public VideoEncoder {
33 public:
34 VideoEncoderWrapper(JNIEnv* jni, jobject j_encoder);
35
36 int32_t InitEncode(const VideoCodec* codec_settings,
37 int32_t number_of_cores,
38 size_t max_payload_size) override;
39
40 int32_t RegisterEncodeCompleteCallback(
41 EncodedImageCallback* callback) override;
42
43 int32_t Release() override;
44
45 int32_t Encode(const VideoFrame& frame,
46 const CodecSpecificInfo* codec_specific_info,
47 const std::vector<FrameType>* frame_types) override;
48
49 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
50
51 int32_t SetRateAllocation(const BitrateAllocation& allocation,
52 uint32_t framerate) override;
53
54 ScalingSettings GetScalingSettings() const override;
55
56 bool SupportsNativeHandle() const override { return true; }
57
58 // Should only be called by JNI.
59 void OnEncodedFrame(JNIEnv* jni,
60 jobject j_buffer,
61 jint encoded_width,
62 jint encoded_height,
63 jlong capture_time_ms,
64 jint frame_type,
65 jint rotation,
66 jboolean complete_frame,
67 jobject j_qp);
68
69 const char* ImplementationName() const override;
70
71 private:
72 struct FrameExtraInfo {
73 uint64_t capture_time_ns; // Used as an identifier of the frame.
74
75 uint32_t timestamp_rtp;
76 };
77
78 int32_t InitEncodeInternal(JNIEnv* jni);
79
80 // Takes Java VideoCodecStatus, handles it and returns WEBRTC_VIDEO_CODEC_*
81 // status code.
82 int32_t HandleReturnCode(JNIEnv* jni, jobject code);
83
84 RTPFragmentationHeader ParseFragmentationHeader(
85 const std::vector<uint8_t>& buffer);
86 int ParseQp(const std::vector<uint8_t>& buffer);
87 CodecSpecificInfo ParseCodecSpecificInfo(const EncodedImage& frame);
88 jobject ToJavaBitrateAllocation(JNIEnv* jni,
89 const BitrateAllocation& allocation);
90 std::string GetImplementationName(JNIEnv* jni) const;
91
92 const ScopedGlobalRef<jobject> encoder_;
93 const ScopedGlobalRef<jclass> settings_class_;
94 const ScopedGlobalRef<jclass> encode_info_class_;
95 const ScopedGlobalRef<jclass> frame_type_class_;
96 const ScopedGlobalRef<jclass> bitrate_allocation_class_;
97 const ScopedGlobalRef<jclass> int_array_class_;
98
99 jmethodID init_encode_method_;
100 jmethodID release_method_;
101 jmethodID encode_method_;
102 jmethodID set_channel_parameters_method_;
103 jmethodID set_rate_allocation_method_;
104 jmethodID get_scaling_settings_method_;
105 jmethodID get_implementation_name_method_;
106
107 jmethodID settings_constructor_;
108
109 jmethodID encode_info_constructor_;
110
111 jmethodID frame_type_from_native_method_;
112
113 jmethodID bitrate_allocation_constructor_;
114
115 jfieldID scaling_settings_on_field_;
116 jfieldID scaling_settings_low_field_;
117 jfieldID scaling_settings_high_field_;
118
119 jmethodID get_number_method_;
120
121 jmethodID int_value_method_;
122
123 std::string implementation_name_;
124
125 rtc::TaskQueue* encoder_queue_;
126 JavaVideoFrameFactory video_frame_factory_;
127 std::deque<FrameExtraInfo> frame_extra_infos_;
128 EncodedImageCallback* callback_;
129 bool initialized_;
130 int num_resets_;
131 int number_of_cores_;
132 VideoCodec codec_settings_;
133 H264BitstreamParser h264_bitstream_parser_;
134
135 // RTP state.
136 uint16_t picture_id_;
137 uint8_t tl0_pic_idx_;
138
139 // VP9 variables to populate codec specific structure.
140 GofInfoVP9 gof_; // Contains each frame's temporal information for
141 // non-flexible VP9 mode.
142 size_t gof_idx_;
143};
144
145} // namespace jni
146} // namespace webrtc
147
148#endif // WEBRTC_SDK_ANDROID_SRC_JNI_VIDEOENCODERWRAPPER_H_