blob: 629f28a6ec19706d94c2ecaf8878d15932b4d19f [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"
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +010024#include "sdk/android/src/jni/videoframe.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,
Magnus Jedvert18ce0f02017-11-07 17:34:38 +010058 jobject j_caller,
sakal07a3bd72017-09-04 03:57:21 -070059 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_;
sakal07a3bd72017-09-04 03:57:21 -070092 const ScopedGlobalRef<jclass> frame_type_class_;
sakal07a3bd72017-09-04 03:57:21 -070093 const ScopedGlobalRef<jclass> int_array_class_;
94
sakal07a3bd72017-09-04 03:57:21 -070095 std::string implementation_name_;
96
97 rtc::TaskQueue* encoder_queue_;
sakal07a3bd72017-09-04 03:57:21 -070098 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_