OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 int64_t /* rtt */) override; | 120 int64_t /* rtt */) override; |
121 int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override; | 121 int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override; |
122 | 122 |
123 // rtc::MessageHandler implementation. | 123 // rtc::MessageHandler implementation. |
124 void OnMessage(rtc::Message* msg) override; | 124 void OnMessage(rtc::Message* msg) override; |
125 | 125 |
126 void OnDroppedFrame() override; | 126 void OnDroppedFrame() override; |
127 | 127 |
128 int GetTargetFramerate() override; | 128 int GetTargetFramerate() override; |
129 | 129 |
130 bool SupportsNativeHandle() const override { return true; } | 130 bool SupportsNativeHandle() const override { return egl_context_ != nullptr; } |
131 const char* ImplementationName() const override; | 131 const char* ImplementationName() const override; |
132 | 132 |
133 private: | 133 private: |
134 // CHECK-fail if not running on |codec_thread_|. | 134 // CHECK-fail if not running on |codec_thread_|. |
135 void CheckOnCodecThread(); | 135 void CheckOnCodecThread(); |
136 | 136 |
137 private: | 137 private: |
138 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and | 138 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and |
139 // InitEncodeOnCodecThread() in an attempt to restore the codec to an | 139 // InitEncodeOnCodecThread() in an attempt to restore the codec to an |
140 // operable state. Necessary after all manner of OMX-layer errors. | 140 // operable state. Necessary after all manner of OMX-layer errors. |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 render_times_ms_.clear(); | 513 render_times_ms_.clear(); |
514 frame_rtc_times_ms_.clear(); | 514 frame_rtc_times_ms_.clear(); |
515 drop_next_input_frame_ = false; | 515 drop_next_input_frame_ = false; |
516 use_surface_ = use_surface; | 516 use_surface_ = use_surface; |
517 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; | 517 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; |
518 gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1); | 518 gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1); |
519 tl0_pic_idx_ = static_cast<uint8_t>(rand()); | 519 tl0_pic_idx_ = static_cast<uint8_t>(rand()); |
520 gof_idx_ = 0; | 520 gof_idx_ = 0; |
521 | 521 |
522 // We enforce no extra stride/padding in the format creation step. | 522 // We enforce no extra stride/padding in the format creation step. |
523 jobject j_video_codec_enum = JavaEnumFromIndex( | 523 jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( |
524 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); | 524 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); |
525 const bool encode_status = jni->CallBooleanMethod( | 525 const bool encode_status = jni->CallBooleanMethod( |
526 *j_media_codec_video_encoder_, j_init_encode_method_, | 526 *j_media_codec_video_encoder_, j_init_encode_method_, |
527 j_video_codec_enum, width, height, kbps, fps, | 527 j_video_codec_enum, width, height, kbps, fps, |
528 (use_surface ? egl_context_ : nullptr)); | 528 (use_surface ? egl_context_ : nullptr)); |
529 if (!encode_status) { | 529 if (!encode_status) { |
530 ALOGE << "Failed to configure encoder."; | 530 ALOGE << "Failed to configure encoder."; |
531 return WEBRTC_VIDEO_CODEC_ERROR; | 531 return WEBRTC_VIDEO_CODEC_ERROR; |
532 } | 532 } |
533 CHECK_EXCEPTION(jni); | 533 CHECK_EXCEPTION(jni); |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1107 } | 1107 } |
1108 | 1108 |
1109 int MediaCodecVideoEncoder::GetTargetFramerate() { | 1109 int MediaCodecVideoEncoder::GetTargetFramerate() { |
1110 return scale_ ? quality_scaler_.GetTargetFramerate() : -1; | 1110 return scale_ ? quality_scaler_.GetTargetFramerate() : -1; |
1111 } | 1111 } |
1112 | 1112 |
1113 const char* MediaCodecVideoEncoder::ImplementationName() const { | 1113 const char* MediaCodecVideoEncoder::ImplementationName() const { |
1114 return "MediaCodec"; | 1114 return "MediaCodec"; |
1115 } | 1115 } |
1116 | 1116 |
1117 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() | 1117 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() { |
1118 : egl_context_(nullptr) { | |
1119 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 1118 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
1120 ScopedLocalRefFrame local_ref_frame(jni); | 1119 ScopedLocalRefFrame local_ref_frame(jni); |
1121 jclass j_encoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoEncoder"); | 1120 jclass j_encoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoEncoder"); |
1122 supported_codecs_.clear(); | 1121 supported_codecs_.clear(); |
1123 | 1122 |
1124 bool is_vp8_hw_supported = jni->CallStaticBooleanMethod( | 1123 bool is_vp8_hw_supported = jni->CallStaticBooleanMethod( |
1125 j_encoder_class, | 1124 j_encoder_class, |
1126 GetStaticMethodID(jni, j_encoder_class, "isVp8HwSupported", "()Z")); | 1125 GetStaticMethodID(jni, j_encoder_class, "isVp8HwSupported", "()Z")); |
1127 CHECK_EXCEPTION(jni); | 1126 CHECK_EXCEPTION(jni); |
1128 if (is_vp8_hw_supported) { | 1127 if (is_vp8_hw_supported) { |
(...skipping 16 matching lines...) Expand all Loading... |
1145 j_encoder_class, | 1144 j_encoder_class, |
1146 GetStaticMethodID(jni, j_encoder_class, "isH264HwSupported", "()Z")); | 1145 GetStaticMethodID(jni, j_encoder_class, "isH264HwSupported", "()Z")); |
1147 CHECK_EXCEPTION(jni); | 1146 CHECK_EXCEPTION(jni); |
1148 if (is_h264_hw_supported) { | 1147 if (is_h264_hw_supported) { |
1149 ALOGD << "H.264 HW Encoder supported."; | 1148 ALOGD << "H.264 HW Encoder supported."; |
1150 supported_codecs_.push_back(VideoCodec(kVideoCodecH264, "H264", | 1149 supported_codecs_.push_back(VideoCodec(kVideoCodecH264, "H264", |
1151 MAX_VIDEO_WIDTH, MAX_VIDEO_HEIGHT, MAX_VIDEO_FPS)); | 1150 MAX_VIDEO_WIDTH, MAX_VIDEO_HEIGHT, MAX_VIDEO_FPS)); |
1152 } | 1151 } |
1153 } | 1152 } |
1154 | 1153 |
1155 MediaCodecVideoEncoderFactory::~MediaCodecVideoEncoderFactory() {} | 1154 MediaCodecVideoEncoderFactory::~MediaCodecVideoEncoderFactory() { |
| 1155 ALOGD << "MediaCodecVideoEncoderFactory dtor"; |
| 1156 } |
1156 | 1157 |
1157 void MediaCodecVideoEncoderFactory::SetEGLContext( | 1158 void MediaCodecVideoEncoderFactory::SetEGLContext( |
1158 JNIEnv* jni, jobject render_egl_context) { | 1159 JNIEnv* jni, jobject render_egl_context) { |
1159 ALOGD << "MediaCodecVideoEncoderFactory::SetEGLContext"; | 1160 ALOGD << "MediaCodecVideoEncoderFactory::SetEGLContext"; |
1160 if (egl_context_) { | 1161 if (!egl_base_.CreateEglBase(jni, render_egl_context)) { |
1161 jni->DeleteGlobalRef(egl_context_); | 1162 ALOGW << "Invalid EGL context - HW surface encoding is disabled."; |
1162 egl_context_ = NULL; | |
1163 } | |
1164 if (!IsNull(jni, render_egl_context)) { | |
1165 egl_context_ = jni->NewGlobalRef(render_egl_context); | |
1166 if (CheckException(jni)) { | |
1167 ALOGE << "error calling NewGlobalRef for EGL Context."; | |
1168 egl_context_ = NULL; | |
1169 } else { | |
1170 jclass j_egl_context_class = | |
1171 FindClass(jni, "org/webrtc/EglBase14$Context"); | |
1172 if (!jni->IsInstanceOf(egl_context_, j_egl_context_class)) { | |
1173 ALOGE << "Wrong EGL Context."; | |
1174 jni->DeleteGlobalRef(egl_context_); | |
1175 egl_context_ = NULL; | |
1176 } | |
1177 } | |
1178 } | |
1179 if (egl_context_ == NULL) { | |
1180 ALOGW << "NULL VideoDecoder EGL context - HW surface encoding is disabled."; | |
1181 } | 1163 } |
1182 } | 1164 } |
1183 | 1165 |
1184 webrtc::VideoEncoder* MediaCodecVideoEncoderFactory::CreateVideoEncoder( | 1166 webrtc::VideoEncoder* MediaCodecVideoEncoderFactory::CreateVideoEncoder( |
1185 VideoCodecType type) { | 1167 VideoCodecType type) { |
1186 if (supported_codecs_.empty()) { | 1168 if (supported_codecs_.empty()) { |
1187 ALOGW << "No HW video encoder for type " << (int)type; | 1169 ALOGW << "No HW video encoder for type " << (int)type; |
1188 return NULL; | 1170 return nullptr; |
1189 } | 1171 } |
1190 for (std::vector<VideoCodec>::const_iterator it = supported_codecs_.begin(); | 1172 for (std::vector<VideoCodec>::const_iterator it = supported_codecs_.begin(); |
1191 it != supported_codecs_.end(); ++it) { | 1173 it != supported_codecs_.end(); ++it) { |
1192 if (it->type == type) { | 1174 if (it->type == type) { |
1193 ALOGD << "Create HW video encoder for type " << (int)type << | 1175 ALOGD << "Create HW video encoder for type " << (int)type << |
1194 " (" << it->name << ")."; | 1176 " (" << it->name << ")."; |
1195 return new MediaCodecVideoEncoder(AttachCurrentThreadIfNeeded(), type, | 1177 return new MediaCodecVideoEncoder(AttachCurrentThreadIfNeeded(), type, |
1196 egl_context_); | 1178 egl_base_.egl_base_context()); |
1197 } | 1179 } |
1198 } | 1180 } |
1199 ALOGW << "Can not find HW video encoder for type " << (int)type; | 1181 ALOGW << "Can not find HW video encoder for type " << (int)type; |
1200 return NULL; | 1182 return nullptr; |
1201 } | 1183 } |
1202 | 1184 |
1203 const std::vector<MediaCodecVideoEncoderFactory::VideoCodec>& | 1185 const std::vector<MediaCodecVideoEncoderFactory::VideoCodec>& |
1204 MediaCodecVideoEncoderFactory::codecs() const { | 1186 MediaCodecVideoEncoderFactory::codecs() const { |
1205 return supported_codecs_; | 1187 return supported_codecs_; |
1206 } | 1188 } |
1207 | 1189 |
1208 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1190 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
1209 webrtc::VideoEncoder* encoder) { | 1191 webrtc::VideoEncoder* encoder) { |
1210 ALOGD << "Destroy video encoder."; | 1192 ALOGD << "Destroy video encoder."; |
1211 delete encoder; | 1193 delete encoder; |
1212 } | 1194 } |
1213 | 1195 |
1214 } // namespace webrtc_jni | 1196 } // namespace webrtc_jni |
1215 | 1197 |
OLD | NEW |