blob: ceecda2bf2d564c0dd4cdb516fb5479557c7e293 [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#include "sdk/android/src/jni/videoencoderwrapper.h"
sakal07a3bd72017-09-04 03:57:21 -070012
13#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "common_video/h264/h264_common.h"
16#include "modules/include/module_common_types.h"
17#include "modules/video_coding/include/video_codec_interface.h"
18#include "modules/video_coding/include/video_error_codes.h"
19#include "modules/video_coding/utility/vp8_header_parser.h"
20#include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
21#include "rtc_base/logging.h"
22#include "rtc_base/random.h"
23#include "rtc_base/timeutils.h"
24#include "sdk/android/src/jni/classreferenceholder.h"
sakal07a3bd72017-09-04 03:57:21 -070025
26namespace webrtc {
27namespace jni {
28
29static const int kMaxJavaEncoderResets = 3;
30
31VideoEncoderWrapper::VideoEncoderWrapper(JNIEnv* jni, jobject j_encoder)
32 : encoder_(jni, j_encoder),
33 settings_class_(jni, FindClass(jni, "org/webrtc/VideoEncoder$Settings")),
34 encode_info_class_(jni,
35 FindClass(jni, "org/webrtc/VideoEncoder$EncodeInfo")),
36 frame_type_class_(jni,
37 FindClass(jni, "org/webrtc/EncodedImage$FrameType")),
38 bitrate_allocation_class_(
39 jni,
40 FindClass(jni, "org/webrtc/VideoEncoder$BitrateAllocation")),
41 int_array_class_(jni, jni->FindClass("[I")),
42 video_frame_factory_(jni) {
Magnus Jedvertf4810dd2017-09-27 11:56:45 +000043 jclass encoder_class = FindClass(jni, "org/webrtc/VideoEncoder");
44
45 init_encode_method_ =
46 jni->GetMethodID(encoder_class, "initEncode",
47 "(Lorg/webrtc/VideoEncoder$Settings;Lorg/webrtc/"
48 "VideoEncoder$Callback;)Lorg/webrtc/VideoCodecStatus;");
49 release_method_ = jni->GetMethodID(encoder_class, "release",
50 "()Lorg/webrtc/VideoCodecStatus;");
51 encode_method_ = jni->GetMethodID(
52 encoder_class, "encode",
53 "(Lorg/webrtc/VideoFrame;Lorg/webrtc/"
54 "VideoEncoder$EncodeInfo;)Lorg/webrtc/VideoCodecStatus;");
55 set_channel_parameters_method_ =
56 jni->GetMethodID(encoder_class, "setChannelParameters",
57 "(SJ)Lorg/webrtc/VideoCodecStatus;");
58 set_rate_allocation_method_ =
59 jni->GetMethodID(encoder_class, "setRateAllocation",
60 "(Lorg/webrtc/VideoEncoder$BitrateAllocation;I)Lorg/"
61 "webrtc/VideoCodecStatus;");
62 get_scaling_settings_method_ =
63 jni->GetMethodID(encoder_class, "getScalingSettings",
64 "()Lorg/webrtc/VideoEncoder$ScalingSettings;");
65 get_implementation_name_method_ = jni->GetMethodID(
66 encoder_class, "getImplementationName", "()Ljava/lang/String;");
67
sakal07a3bd72017-09-04 03:57:21 -070068 settings_constructor_ =
69 jni->GetMethodID(*settings_class_, "<init>", "(IIIIIZ)V");
70
71 encode_info_constructor_ = jni->GetMethodID(
72 *encode_info_class_, "<init>", "([Lorg/webrtc/EncodedImage$FrameType;)V");
73
74 frame_type_from_native_method_ =
75 jni->GetStaticMethodID(*frame_type_class_, "fromNative",
76 "(I)Lorg/webrtc/EncodedImage$FrameType;");
77
78 bitrate_allocation_constructor_ =
79 jni->GetMethodID(*bitrate_allocation_class_, "<init>", "([[I)V");
80
81 jclass video_codec_status_class =
82 FindClass(jni, "org/webrtc/VideoCodecStatus");
83 get_number_method_ =
84 jni->GetMethodID(video_codec_status_class, "getNumber", "()I");
85
86 jclass integer_class = jni->FindClass("java/lang/Integer");
87 int_value_method_ = jni->GetMethodID(integer_class, "intValue", "()I");
88
89 jclass scaling_settings_class =
90 FindClass(jni, "org/webrtc/VideoEncoder$ScalingSettings");
91 scaling_settings_on_field_ =
92 jni->GetFieldID(scaling_settings_class, "on", "Z");
93 scaling_settings_low_field_ =
94 jni->GetFieldID(scaling_settings_class, "low", "Ljava/lang/Integer;");
95 scaling_settings_high_field_ =
96 jni->GetFieldID(scaling_settings_class, "high", "Ljava/lang/Integer;");
97
98 implementation_name_ = GetImplementationName(jni);
99
100 encoder_queue_ = rtc::TaskQueue::Current();
101
102 initialized_ = false;
103 num_resets_ = 0;
104
105 Random random(rtc::TimeMicros());
106 picture_id_ = random.Rand<uint16_t>() & 0x7FFF;
107 tl0_pic_idx_ = random.Rand<uint8_t>();
108}
109
110int32_t VideoEncoderWrapper::InitEncode(const VideoCodec* codec_settings,
111 int32_t number_of_cores,
112 size_t max_payload_size) {
113 JNIEnv* jni = AttachCurrentThreadIfNeeded();
114 ScopedLocalRefFrame local_ref_frame(jni);
115
116 number_of_cores_ = number_of_cores;
117 codec_settings_ = *codec_settings;
118 num_resets_ = 0;
119
120 return InitEncodeInternal(jni);
121}
122
123int32_t VideoEncoderWrapper::InitEncodeInternal(JNIEnv* jni) {
124 bool automatic_resize_on;
125 switch (codec_settings_.codecType) {
126 case kVideoCodecVP8:
127 automatic_resize_on = codec_settings_.VP8()->automaticResizeOn;
128 break;
129 case kVideoCodecVP9:
130 automatic_resize_on = codec_settings_.VP9()->automaticResizeOn;
131 break;
132 default:
133 automatic_resize_on = true;
134 }
135
136 jobject settings =
137 jni->NewObject(*settings_class_, settings_constructor_, number_of_cores_,
138 codec_settings_.width, codec_settings_.height,
139 codec_settings_.startBitrate, codec_settings_.maxFramerate,
140 automatic_resize_on);
141
142 jclass callback_class =
143 FindClass(jni, "org/webrtc/VideoEncoderWrapperCallback");
144 jmethodID callback_constructor =
145 jni->GetMethodID(callback_class, "<init>", "(J)V");
146 jobject callback = jni->NewObject(callback_class, callback_constructor,
147 jlongFromPointer(this));
148
149 jobject ret =
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000150 jni->CallObjectMethod(*encoder_, init_encode_method_, settings, callback);
sakal07a3bd72017-09-04 03:57:21 -0700151 if (jni->CallIntMethod(ret, get_number_method_) == WEBRTC_VIDEO_CODEC_OK) {
152 initialized_ = true;
153 }
154
155 return HandleReturnCode(jni, ret);
156}
157
158int32_t VideoEncoderWrapper::RegisterEncodeCompleteCallback(
159 EncodedImageCallback* callback) {
160 callback_ = callback;
161 return WEBRTC_VIDEO_CODEC_OK;
162}
163
164int32_t VideoEncoderWrapper::Release() {
165 JNIEnv* jni = AttachCurrentThreadIfNeeded();
166 ScopedLocalRefFrame local_ref_frame(jni);
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000167 jobject ret = jni->CallObjectMethod(*encoder_, release_method_);
sakal07a3bd72017-09-04 03:57:21 -0700168 frame_extra_infos_.clear();
169 initialized_ = false;
170 return HandleReturnCode(jni, ret);
171}
172
173int32_t VideoEncoderWrapper::Encode(
174 const VideoFrame& frame,
175 const CodecSpecificInfo* /* codec_specific_info */,
176 const std::vector<FrameType>* frame_types) {
177 if (!initialized_) {
178 // Most likely initializing the codec failed.
179 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
180 }
181
182 JNIEnv* jni = AttachCurrentThreadIfNeeded();
183 ScopedLocalRefFrame local_ref_frame(jni);
184
185 // Construct encode info.
186 jobjectArray j_frame_types =
187 jni->NewObjectArray(frame_types->size(), *frame_type_class_, nullptr);
188 for (size_t i = 0; i < frame_types->size(); ++i) {
189 jobject j_frame_type = jni->CallStaticObjectMethod(
190 *frame_type_class_, frame_type_from_native_method_,
191 static_cast<jint>((*frame_types)[i]));
192 jni->SetObjectArrayElement(j_frame_types, i, j_frame_type);
193 }
194 jobject encode_info = jni->NewObject(*encode_info_class_,
195 encode_info_constructor_, j_frame_types);
196
197 FrameExtraInfo info;
198 info.capture_time_ns = frame.timestamp_us() * rtc::kNumNanosecsPerMicrosec;
199 info.timestamp_rtp = frame.timestamp();
200 frame_extra_infos_.push_back(info);
201
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000202 jobject ret = jni->CallObjectMethod(
203 *encoder_, encode_method_, video_frame_factory_.ToJavaFrame(jni, frame),
sakal07a3bd72017-09-04 03:57:21 -0700204 encode_info);
205 return HandleReturnCode(jni, ret);
206}
207
208int32_t VideoEncoderWrapper::SetChannelParameters(uint32_t packet_loss,
209 int64_t rtt) {
210 JNIEnv* jni = AttachCurrentThreadIfNeeded();
211 ScopedLocalRefFrame local_ref_frame(jni);
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000212 jobject ret = jni->CallObjectMethod(*encoder_, set_channel_parameters_method_,
213 (jshort)packet_loss, (jlong)rtt);
sakal07a3bd72017-09-04 03:57:21 -0700214 return HandleReturnCode(jni, ret);
215}
216
217int32_t VideoEncoderWrapper::SetRateAllocation(
218 const BitrateAllocation& allocation,
219 uint32_t framerate) {
220 JNIEnv* jni = AttachCurrentThreadIfNeeded();
221 ScopedLocalRefFrame local_ref_frame(jni);
222
223 jobject j_bitrate_allocation = ToJavaBitrateAllocation(jni, allocation);
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000224 jobject ret = jni->CallObjectMethod(*encoder_, set_rate_allocation_method_,
225 j_bitrate_allocation, (jint)framerate);
sakal07a3bd72017-09-04 03:57:21 -0700226 return HandleReturnCode(jni, ret);
227}
228
229VideoEncoderWrapper::ScalingSettings VideoEncoderWrapper::GetScalingSettings()
230 const {
231 JNIEnv* jni = AttachCurrentThreadIfNeeded();
232 ScopedLocalRefFrame local_ref_frame(jni);
233 jobject j_scaling_settings =
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000234 jni->CallObjectMethod(*encoder_, get_scaling_settings_method_);
sakal07a3bd72017-09-04 03:57:21 -0700235 bool on =
236 jni->GetBooleanField(j_scaling_settings, scaling_settings_on_field_);
237 jobject j_low =
238 jni->GetObjectField(j_scaling_settings, scaling_settings_low_field_);
239 jobject j_high =
240 jni->GetObjectField(j_scaling_settings, scaling_settings_high_field_);
241
242 if (j_low != nullptr || j_high != nullptr) {
243 RTC_DCHECK(j_low != nullptr);
244 RTC_DCHECK(j_high != nullptr);
245 int low = jni->CallIntMethod(j_low, int_value_method_);
246 int high = jni->CallIntMethod(j_high, int_value_method_);
247 return ScalingSettings(on, low, high);
248 } else {
249 return ScalingSettings(on);
250 }
251}
252
253const char* VideoEncoderWrapper::ImplementationName() const {
254 return implementation_name_.c_str();
255}
256
257void VideoEncoderWrapper::OnEncodedFrame(JNIEnv* jni,
258 jobject j_buffer,
259 jint encoded_width,
260 jint encoded_height,
261 jlong capture_time_ns,
262 jint frame_type,
263 jint rotation,
264 jboolean complete_frame,
265 jobject j_qp) {
266 const uint8_t* buffer =
267 static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_buffer));
268 const size_t buffer_size = jni->GetDirectBufferCapacity(j_buffer);
269
270 std::vector<uint8_t> buffer_copy(buffer_size);
271 memcpy(buffer_copy.data(), buffer, buffer_size);
272 int qp = -1;
273 if (j_qp != nullptr) {
274 qp = jni->CallIntMethod(j_qp, int_value_method_);
275 }
276
277 encoder_queue_->PostTask(
278 [
279 this, task_buffer = std::move(buffer_copy), qp, encoded_width,
280 encoded_height, capture_time_ns, frame_type, rotation, complete_frame
281 ]() {
282 FrameExtraInfo frame_extra_info;
283 do {
284 if (frame_extra_infos_.empty()) {
285 LOG(LS_WARNING)
286 << "Java encoder produced an unexpected frame with timestamp: "
287 << capture_time_ns;
288 return;
289 }
290
291 frame_extra_info = frame_extra_infos_.front();
292 frame_extra_infos_.pop_front();
293 // The encoder might drop frames so iterate through the queue until
294 // we find a matching timestamp.
295 } while (frame_extra_info.capture_time_ns != capture_time_ns);
296
297 RTPFragmentationHeader header = ParseFragmentationHeader(task_buffer);
298 EncodedImage frame(const_cast<uint8_t*>(task_buffer.data()),
299 task_buffer.size(), task_buffer.size());
300 frame._encodedWidth = encoded_width;
301 frame._encodedHeight = encoded_height;
302 frame._timeStamp = frame_extra_info.timestamp_rtp;
303 frame.capture_time_ms_ = capture_time_ns / rtc::kNumNanosecsPerMillisec;
304 frame._frameType = (FrameType)frame_type;
305 frame.rotation_ = (VideoRotation)rotation;
306 frame._completeFrame = complete_frame;
307 if (qp == -1) {
308 frame.qp_ = ParseQp(task_buffer);
309 } else {
310 frame.qp_ = qp;
311 }
312
313 CodecSpecificInfo info(ParseCodecSpecificInfo(frame));
314 callback_->OnEncodedImage(frame, &info, &header);
315 });
316}
317
318int32_t VideoEncoderWrapper::HandleReturnCode(JNIEnv* jni, jobject code) {
319 int32_t value = jni->CallIntMethod(code, get_number_method_);
320 if (value < 0) { // Any errors are represented by negative values.
321 // Try resetting the codec.
322 if (++num_resets_ <= kMaxJavaEncoderResets &&
323 Release() == WEBRTC_VIDEO_CODEC_OK) {
324 LOG(LS_WARNING) << "Reset Java encoder: " << num_resets_;
325 return InitEncodeInternal(jni);
326 }
327
328 LOG(LS_WARNING) << "Falling back to software decoder.";
329 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
330 } else {
331 return value;
332 }
333}
334
335RTPFragmentationHeader VideoEncoderWrapper::ParseFragmentationHeader(
336 const std::vector<uint8_t>& buffer) {
337 RTPFragmentationHeader header;
338 if (codec_settings_.codecType == kVideoCodecH264) {
339 h264_bitstream_parser_.ParseBitstream(buffer.data(), buffer.size());
340
341 // For H.264 search for start codes.
342 const std::vector<H264::NaluIndex> nalu_idxs =
343 H264::FindNaluIndices(buffer.data(), buffer.size());
344 if (nalu_idxs.empty()) {
345 LOG(LS_ERROR) << "Start code is not found!";
346 LOG(LS_ERROR) << "Data:" << buffer[0] << " " << buffer[1] << " "
347 << buffer[2] << " " << buffer[3] << " " << buffer[4] << " "
348 << buffer[5];
349 }
350 header.VerifyAndAllocateFragmentationHeader(nalu_idxs.size());
351 for (size_t i = 0; i < nalu_idxs.size(); i++) {
352 header.fragmentationOffset[i] = nalu_idxs[i].payload_start_offset;
353 header.fragmentationLength[i] = nalu_idxs[i].payload_size;
354 header.fragmentationPlType[i] = 0;
355 header.fragmentationTimeDiff[i] = 0;
356 }
357 } else {
358 // Generate a header describing a single fragment.
359 header.VerifyAndAllocateFragmentationHeader(1);
360 header.fragmentationOffset[0] = 0;
361 header.fragmentationLength[0] = buffer.size();
362 header.fragmentationPlType[0] = 0;
363 header.fragmentationTimeDiff[0] = 0;
364 }
365 return header;
366}
367
368int VideoEncoderWrapper::ParseQp(const std::vector<uint8_t>& buffer) {
369 int qp;
370 bool success;
371 switch (codec_settings_.codecType) {
372 case kVideoCodecVP8:
373 success = vp8::GetQp(buffer.data(), buffer.size(), &qp);
374 break;
375 case kVideoCodecVP9:
376 success = vp9::GetQp(buffer.data(), buffer.size(), &qp);
377 break;
378 case kVideoCodecH264:
379 success = h264_bitstream_parser_.GetLastSliceQp(&qp);
380 break;
381 default: // Default is to not provide QP.
382 success = false;
383 break;
384 }
385 return success ? qp : -1; // -1 means unknown QP.
386}
387
388CodecSpecificInfo VideoEncoderWrapper::ParseCodecSpecificInfo(
389 const EncodedImage& frame) {
390 const bool key_frame = frame._frameType == kVideoFrameKey;
391
392 CodecSpecificInfo info;
393 memset(&info, 0, sizeof(info));
394 info.codecType = codec_settings_.codecType;
395 info.codec_name = implementation_name_.c_str();
396
397 switch (codec_settings_.codecType) {
398 case kVideoCodecVP8:
399 info.codecSpecific.VP8.pictureId = picture_id_;
400 info.codecSpecific.VP8.nonReference = false;
401 info.codecSpecific.VP8.simulcastIdx = 0;
402 info.codecSpecific.VP8.temporalIdx = kNoTemporalIdx;
403 info.codecSpecific.VP8.layerSync = false;
404 info.codecSpecific.VP8.tl0PicIdx = kNoTl0PicIdx;
405 info.codecSpecific.VP8.keyIdx = kNoKeyIdx;
406 break;
407 case kVideoCodecVP9:
408 if (key_frame) {
409 gof_idx_ = 0;
410 }
411 info.codecSpecific.VP9.picture_id = picture_id_;
412 info.codecSpecific.VP9.inter_pic_predicted = key_frame ? false : true;
413 info.codecSpecific.VP9.flexible_mode = false;
414 info.codecSpecific.VP9.ss_data_available = key_frame ? true : false;
415 info.codecSpecific.VP9.tl0_pic_idx = tl0_pic_idx_++;
416 info.codecSpecific.VP9.temporal_idx = kNoTemporalIdx;
417 info.codecSpecific.VP9.spatial_idx = kNoSpatialIdx;
418 info.codecSpecific.VP9.temporal_up_switch = true;
419 info.codecSpecific.VP9.inter_layer_predicted = false;
420 info.codecSpecific.VP9.gof_idx =
421 static_cast<uint8_t>(gof_idx_++ % gof_.num_frames_in_gof);
422 info.codecSpecific.VP9.num_spatial_layers = 1;
423 info.codecSpecific.VP9.spatial_layer_resolution_present = false;
424 if (info.codecSpecific.VP9.ss_data_available) {
425 info.codecSpecific.VP9.spatial_layer_resolution_present = true;
426 info.codecSpecific.VP9.width[0] = frame._encodedWidth;
427 info.codecSpecific.VP9.height[0] = frame._encodedHeight;
428 info.codecSpecific.VP9.gof.CopyGofInfoVP9(gof_);
429 }
430 break;
431 default:
432 break;
433 }
434
435 picture_id_ = (picture_id_ + 1) & 0x7FFF;
436
437 return info;
438}
439
440jobject VideoEncoderWrapper::ToJavaBitrateAllocation(
441 JNIEnv* jni,
442 const BitrateAllocation& allocation) {
443 jobjectArray j_allocation_array = jni->NewObjectArray(
444 kMaxSpatialLayers, *int_array_class_, nullptr /* initial */);
445 for (int spatial_i = 0; spatial_i < kMaxSpatialLayers; ++spatial_i) {
446 jintArray j_array_spatial_layer = jni->NewIntArray(kMaxTemporalStreams);
447 jint* array_spatial_layer =
448 jni->GetIntArrayElements(j_array_spatial_layer, nullptr /* isCopy */);
449 for (int temporal_i = 0; temporal_i < kMaxTemporalStreams; ++temporal_i) {
450 array_spatial_layer[temporal_i] =
451 allocation.GetBitrate(spatial_i, temporal_i);
452 }
453 jni->ReleaseIntArrayElements(j_array_spatial_layer, array_spatial_layer,
454 JNI_COMMIT);
455
456 jni->SetObjectArrayElement(j_allocation_array, spatial_i,
457 j_array_spatial_layer);
458 }
459 return jni->NewObject(*bitrate_allocation_class_,
460 bitrate_allocation_constructor_, j_allocation_array);
461}
462
463std::string VideoEncoderWrapper::GetImplementationName(JNIEnv* jni) const {
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000464 jstring jname = reinterpret_cast<jstring>(
465 jni->CallObjectMethod(*encoder_, get_implementation_name_method_));
sakal07a3bd72017-09-04 03:57:21 -0700466 return JavaToStdString(jni, jname);
467}
468
469JNI_FUNCTION_DECLARATION(void,
470 VideoEncoderWrapperCallback_nativeOnEncodedFrame,
471 JNIEnv* jni,
472 jclass,
473 jlong j_native_encoder,
474 jobject buffer,
475 jint encoded_width,
476 jint encoded_height,
477 jlong capture_time_ns,
478 jint frame_type,
479 jint rotation,
480 jboolean complete_frame,
481 jobject qp) {
482 VideoEncoderWrapper* native_encoder =
483 reinterpret_cast<VideoEncoderWrapper*>(j_native_encoder);
484 native_encoder->OnEncodedFrame(jni, buffer, encoded_width, encoded_height,
485 capture_time_ns, frame_type, rotation,
486 complete_frame, qp);
487}
488
489} // namespace jni
490} // namespace webrtc