OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 #include "webrtc/sdk/android/src/jni/videoencoderfactorywrapper.h" | |
12 | |
13 #include "webrtc/api/video_codecs/video_encoder.h" | |
14 #include "webrtc/common_types.h" | |
15 #include "webrtc/rtc_base/logging.h" | |
16 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | |
17 #include "webrtc/sdk/android/src/jni/videoencoderwrapper.h" | |
18 | |
19 namespace webrtc_jni { | |
20 | |
21 VideoEncoderFactoryWrapper::VideoEncoderFactoryWrapper(JNIEnv* jni, | |
22 jobject encoder_factory) | |
23 : video_codec_info_class_(jni, FindClass(jni, "org/webrtc/VideoCodecInfo")), | |
24 hash_map_class_(jni, jni->FindClass("java/util/HashMap")), | |
25 encoder_factory_(jni, encoder_factory) { | |
26 jclass encoder_factory_class = jni->GetObjectClass(*encoder_factory_); | |
27 create_encoder_method_ = jni->GetMethodID( | |
28 encoder_factory_class, "createEncoder", | |
29 "(Lorg/webrtc/VideoCodecInfo;)Lorg/webrtc/VideoEncoder;"); | |
30 get_supported_codecs_method_ = | |
31 jni->GetMethodID(encoder_factory_class, "getSupportedCodecs", | |
32 "()[Lorg/webrtc/VideoCodecInfo;"); | |
33 | |
34 video_codec_info_constructor_ = | |
35 jni->GetMethodID(*video_codec_info_class_, "<init>", | |
36 "(ILjava/lang/String;Ljava/util/Map;)V"); | |
37 payload_field_ = jni->GetFieldID(*video_codec_info_class_, "payload", "I"); | |
38 name_field_ = | |
39 jni->GetFieldID(*video_codec_info_class_, "name", "Ljava/lang/String;"); | |
40 | |
41 hash_map_constructor_ = jni->GetMethodID(*hash_map_class_, "<init>", "()V"); | |
42 put_method_ = jni->GetMethodID( | |
43 *hash_map_class_, "put", | |
44 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); | |
45 | |
46 supported_codecs_ = GetSupportedCodecs(jni); | |
47 } | |
48 | |
49 webrtc::VideoEncoder* VideoEncoderFactoryWrapper::CreateVideoEncoder( | |
50 const cricket::VideoCodec& codec) { | |
51 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | |
52 ScopedLocalRefFrame local_ref_frame(jni); | |
53 jobject j_codec_info = ToJavaCodecInfo(jni, codec); | |
54 jobject encoder = jni->CallObjectMethod(*encoder_factory_, | |
55 create_encoder_method_, j_codec_info); | |
56 return encoder != nullptr ? new VideoEncoderWrapper(jni, encoder) : nullptr; | |
57 } | |
58 | |
59 jobject VideoEncoderFactoryWrapper::ToJavaCodecInfo( | |
60 JNIEnv* jni, | |
61 const cricket::VideoCodec& codec) { | |
62 jobject j_params = jni->NewObject(*hash_map_class_, hash_map_constructor_); | |
63 for (auto const& param : codec.params) { | |
64 jni->CallObjectMethod(j_params, put_method_, | |
65 JavaStringFromStdString(jni, param.first), | |
66 JavaStringFromStdString(jni, param.second)); | |
67 } | |
68 return jni->NewObject(*video_codec_info_class_, video_codec_info_constructor_, | |
69 codec.id, JavaStringFromStdString(jni, codec.name), | |
70 j_params); | |
71 } | |
72 | |
73 std::vector<cricket::VideoCodec> VideoEncoderFactoryWrapper::GetSupportedCodecs( | |
74 JNIEnv* jni) const { | |
75 const jobjectArray j_supported_codecs = static_cast<jobjectArray>( | |
76 jni->CallObjectMethod(*encoder_factory_, get_supported_codecs_method_)); | |
77 const jsize supported_codecs_count = jni->GetArrayLength(j_supported_codecs); | |
78 | |
79 std::vector<cricket::VideoCodec> supported_codecs; | |
80 supported_codecs.resize(supported_codecs_count); | |
81 for (jsize i = 0; i < supported_codecs_count; i++) { | |
82 jobject j_supported_codec = | |
83 jni->GetObjectArrayElement(j_supported_codecs, i); | |
84 int payload = jni->GetIntField(j_supported_codec, payload_field_); | |
85 jstring j_name = static_cast<jstring>( | |
86 jni->GetObjectField(j_supported_codec, name_field_)); | |
87 supported_codecs[i] = | |
88 cricket::VideoCodec(payload, JavaToStdString(jni, j_name)); | |
89 // TODO(sakal): Parse params | |
magjed_webrtc
2017/08/28 16:54:21
Is this not needed?
sakal
2017/08/29 12:59:38
Missed this, implemented it now.
| |
90 } | |
91 return supported_codecs; | |
92 } | |
93 | |
94 void VideoEncoderFactoryWrapper::DestroyVideoEncoder( | |
95 webrtc::VideoEncoder* encoder) { | |
96 delete encoder; | |
97 } | |
98 | |
99 } // namespace webrtc_jni | |
OLD | NEW |