blob: 14ced839e70c65b8ebda9bcfe6e18ba90264e4c3 [file] [log] [blame]
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +02001/*
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_VIDEODECODERWRAPPER_H_
12#define SDK_ANDROID_SRC_JNI_VIDEODECODERWRAPPER_H_
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020013
14#include <jni.h>
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020015#include <deque>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video_codecs/video_decoder.h"
18#include "common_video/h264/h264_bitstream_parser.h"
19#include "sdk/android/src/jni/jni_helpers.h"
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020020
magjeda3d4f682017-08-28 16:24:06 -070021namespace webrtc {
22namespace jni {
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020023
Magnus Jedvert4eb01882017-11-20 22:33:40 +010024// Wraps a Java decoder and delegates all calls to it.
magjeda3d4f682017-08-28 16:24:06 -070025class VideoDecoderWrapper : public VideoDecoder {
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020026 public:
27 VideoDecoderWrapper(JNIEnv* jni, jobject decoder);
28
magjeda3d4f682017-08-28 16:24:06 -070029 int32_t InitDecode(const VideoCodec* codec_settings,
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020030 int32_t number_of_cores) override;
31
magjeda3d4f682017-08-28 16:24:06 -070032 int32_t Decode(const EncodedImage& input_image,
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020033 bool missing_frames,
magjeda3d4f682017-08-28 16:24:06 -070034 const RTPFragmentationHeader* fragmentation,
35 const CodecSpecificInfo* codec_specific_info,
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020036 int64_t render_time_ms) override;
37
38 int32_t RegisterDecodeCompleteCallback(
magjeda3d4f682017-08-28 16:24:06 -070039 DecodedImageCallback* callback) override;
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020040
41 int32_t Release() override;
42
43 // Returns true if the decoder prefer to decode frames late.
44 // That is, it can not decode infinite number of frames before the decoded
45 // frame is consumed.
46 bool PrefersLateDecoding() const override;
47
48 const char* ImplementationName() const override;
49
50 // Wraps the frame to a AndroidVideoBuffer and passes it to the callback.
Magnus Jedvert4eb01882017-11-20 22:33:40 +010051 void OnDecodedFrame(JNIEnv* env,
52 jobject j_caller,
53 jobject j_frame,
54 jobject j_decode_time_ms,
55 jobject j_qp);
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020056
57 private:
58 struct FrameExtraInfo {
Sami Kalliomäkice86e292017-11-22 13:20:11 +010059 int64_t timestamp_ns; // Used as an identifier of the frame.
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020060
61 uint32_t timestamp_rtp;
Sami Kalliomäkice86e292017-11-22 13:20:11 +010062 int64_t timestamp_ntp;
sakal4dee3442017-08-17 02:18:04 -070063 rtc::Optional<uint8_t> qp;
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020064 };
65
66 int32_t InitDecodeInternal(JNIEnv* jni);
67
68 // Takes Java VideoCodecStatus, handles it and returns WEBRTC_VIDEO_CODEC_*
69 // status code.
70 int32_t HandleReturnCode(JNIEnv* jni, jobject code);
71
magjeda3d4f682017-08-28 16:24:06 -070072 rtc::Optional<uint8_t> ParseQP(const EncodedImage& input_image);
sakal4dee3442017-08-17 02:18:04 -070073
magjeda3d4f682017-08-28 16:24:06 -070074 VideoCodec codec_settings_;
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020075 int32_t number_of_cores_;
76
77 bool initialized_;
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020078 std::deque<FrameExtraInfo> frame_extra_infos_;
sakal4dee3442017-08-17 02:18:04 -070079 bool qp_parsing_enabled_;
magjeda3d4f682017-08-28 16:24:06 -070080 H264BitstreamParser h264_bitstream_parser_;
Sami Kalliomäkicee3e532017-10-13 15:20:32 +020081 std::string implementation_name_;
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020082
magjeda3d4f682017-08-28 16:24:06 -070083 DecodedImageCallback* callback_;
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020084
85 const ScopedGlobalRef<jobject> decoder_;
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020086};
87
magjeda3d4f682017-08-28 16:24:06 -070088} // namespace jni
89} // namespace webrtc
Sami Kalliomäki93ad1f72017-06-27 10:03:49 +020090
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020091#endif // SDK_ANDROID_SRC_JNI_VIDEODECODERWRAPPER_H_