Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: talk/app/webrtc/java/jni/androidmediaencoder_jni.cc

Issue 1396073003: Prepare MediaCodecVideoEncoder for surface textures. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 15 matching lines...) Expand all
26 * 26 *
27 */ 27 */
28 28
29 #include "talk/app/webrtc/java/jni/androidmediaencoder_jni.h" 29 #include "talk/app/webrtc/java/jni/androidmediaencoder_jni.h"
30 #include "talk/app/webrtc/java/jni/classreferenceholder.h" 30 #include "talk/app/webrtc/java/jni/classreferenceholder.h"
31 #include "talk/app/webrtc/java/jni/androidmediacodeccommon.h" 31 #include "talk/app/webrtc/java/jni/androidmediacodeccommon.h"
32 #include "webrtc/base/bind.h" 32 #include "webrtc/base/bind.h"
33 #include "webrtc/base/checks.h" 33 #include "webrtc/base/checks.h"
34 #include "webrtc/base/logging.h" 34 #include "webrtc/base/logging.h"
35 #include "webrtc/base/thread.h" 35 #include "webrtc/base/thread.h"
36 #include "webrtc/base/thread_checker.h"
36 #include "webrtc/modules/rtp_rtcp/source/h264_bitstream_parser.h" 37 #include "webrtc/modules/rtp_rtcp/source/h264_bitstream_parser.h"
37 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" 38 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
38 #include "webrtc/modules/video_coding/utility/include/quality_scaler.h" 39 #include "webrtc/modules/video_coding/utility/include/quality_scaler.h"
39 #include "webrtc/modules/video_coding/utility/include/vp8_header_parser.h" 40 #include "webrtc/modules/video_coding/utility/include/vp8_header_parser.h"
40 #include "webrtc/system_wrappers/interface/field_trial.h" 41 #include "webrtc/system_wrappers/interface/field_trial.h"
41 #include "webrtc/system_wrappers/interface/logcat_trace_context.h" 42 #include "webrtc/system_wrappers/interface/logcat_trace_context.h"
42 #include "third_party/libyuv/include/libyuv/convert.h" 43 #include "third_party/libyuv/include/libyuv/convert.h"
43 #include "third_party/libyuv/include/libyuv/convert_from.h" 44 #include "third_party/libyuv/include/libyuv/convert_from.h"
44 #include "third_party/libyuv/include/libyuv/video_common.h" 45 #include "third_party/libyuv/include/libyuv/video_common.h"
45 46
(...skipping 26 matching lines...) Expand all
72 // MediaCodecVideoEncoder is a webrtc::VideoEncoder implementation that uses 73 // MediaCodecVideoEncoder is a webrtc::VideoEncoder implementation that uses
73 // Android's MediaCodec SDK API behind the scenes to implement (hopefully) 74 // Android's MediaCodec SDK API behind the scenes to implement (hopefully)
74 // HW-backed video encode. This C++ class is implemented as a very thin shim, 75 // HW-backed video encode. This C++ class is implemented as a very thin shim,
75 // delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder. 76 // delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder.
76 // MediaCodecVideoEncoder is created, operated, and destroyed on a single 77 // MediaCodecVideoEncoder is created, operated, and destroyed on a single
77 // thread, currently the libjingle Worker thread. 78 // thread, currently the libjingle Worker thread.
78 class MediaCodecVideoEncoder : public webrtc::VideoEncoder, 79 class MediaCodecVideoEncoder : public webrtc::VideoEncoder,
79 public rtc::MessageHandler { 80 public rtc::MessageHandler {
80 public: 81 public:
81 virtual ~MediaCodecVideoEncoder(); 82 virtual ~MediaCodecVideoEncoder();
82 explicit MediaCodecVideoEncoder(JNIEnv* jni, VideoCodecType codecType); 83 MediaCodecVideoEncoder(JNIEnv* jni,
84 VideoCodecType codecType);
83 85
84 // webrtc::VideoEncoder implementation. Everything trampolines to 86 // webrtc::VideoEncoder implementation. Everything trampolines to
85 // |codec_thread_| for execution. 87 // |codec_thread_| for execution.
86 int32_t InitEncode(const webrtc::VideoCodec* codec_settings, 88 int32_t InitEncode(const webrtc::VideoCodec* codec_settings,
87 int32_t /* number_of_cores */, 89 int32_t /* number_of_cores */,
88 size_t /* max_payload_size */) override; 90 size_t /* max_payload_size */) override;
89 int32_t Encode( 91 int32_t Encode(
90 const webrtc::VideoFrame& input_image, 92 const webrtc::VideoFrame& input_image,
91 const webrtc::CodecSpecificInfo* /* codec_specific_info */, 93 const webrtc::CodecSpecificInfo* /* codec_specific_info */,
92 const std::vector<webrtc::VideoFrameType>* frame_types) override; 94 const std::vector<webrtc::VideoFrameType>* frame_types) override;
93 int32_t RegisterEncodeCompleteCallback( 95 int32_t RegisterEncodeCompleteCallback(
94 webrtc::EncodedImageCallback* callback) override; 96 webrtc::EncodedImageCallback* callback) override;
95 int32_t Release() override; 97 int32_t Release() override;
96 int32_t SetChannelParameters(uint32_t /* packet_loss */, 98 int32_t SetChannelParameters(uint32_t /* packet_loss */,
97 int64_t /* rtt */) override; 99 int64_t /* rtt */) override;
98 int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override; 100 int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override;
99 101
100 // rtc::MessageHandler implementation. 102 // rtc::MessageHandler implementation.
101 void OnMessage(rtc::Message* msg) override; 103 void OnMessage(rtc::Message* msg) override;
102 104
103 void OnDroppedFrame() override; 105 void OnDroppedFrame() override;
104 106
105 int GetTargetFramerate() override; 107 int GetTargetFramerate() override;
106 108
107 private: 109 private:
108 // CHECK-fail if not running on |codec_thread_|. 110 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and
109 void CheckOnCodecThread(); 111 // InitEncodeOnCodecThread() in an attempt to restore the codec to an
110
111 // Release() and InitEncode() in an attempt to restore the codec to an
112 // operable state. Necessary after all manner of OMX-layer errors. 112 // operable state. Necessary after all manner of OMX-layer errors.
113 void ResetCodec(); 113 void ResetCodecOnCodecThread();
114 114
115 // Implementation of webrtc::VideoEncoder methods above, all running on the 115 // Implementation of webrtc::VideoEncoder methods above, all running on the
116 // codec thread exclusively. 116 // codec thread exclusively.
117 // 117 //
118 // If width==0 then this is assumed to be a re-initialization and the 118 // If width==0 then this is assumed to be a re-initialization and the
119 // previously-current values are reused instead of the passed parameters 119 // previously-current values are reused instead of the passed parameters
120 // (makes it easier to reason about thread-safety). 120 // (makes it easier to reason about thread-safety).
121 int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps); 121 int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps);
122 // Reconfigure to match |frame| in width, height. Returns false if
123 // reconfiguring fails.
124 bool MaybeReconfigureEncoderOnCodecThread(const webrtc::VideoFrame& frame);
122 int32_t EncodeOnCodecThread( 125 int32_t EncodeOnCodecThread(
123 const webrtc::VideoFrame& input_image, 126 const webrtc::VideoFrame& input_image,
124 const std::vector<webrtc::VideoFrameType>* frame_types); 127 const std::vector<webrtc::VideoFrameType>* frame_types);
128 bool EncodeByteBufferOnCodecThread(JNIEnv* jni,
129 bool key_frame, const webrtc::VideoFrame& frame);
130
125 int32_t RegisterEncodeCompleteCallbackOnCodecThread( 131 int32_t RegisterEncodeCompleteCallbackOnCodecThread(
126 webrtc::EncodedImageCallback* callback); 132 webrtc::EncodedImageCallback* callback);
127 int32_t ReleaseOnCodecThread(); 133 int32_t ReleaseOnCodecThread();
128 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); 134 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate);
129 135
130 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. 136 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members.
131 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); 137 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info);
132 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); 138 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info);
133 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); 139 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info);
134 jlong GetOutputBufferInfoPresentationTimestampUs( 140 jlong GetOutputBufferInfoPresentationTimestampUs(
135 JNIEnv* jni, jobject j_output_buffer_info); 141 JNIEnv* jni, jobject j_output_buffer_info);
136 142
137 // Deliver any outputs pending in the MediaCodec to our |callback_| and return 143 // Deliver any outputs pending in the MediaCodec to our |callback_| and return
138 // true on success. 144 // true on success.
139 bool DeliverPendingOutputs(JNIEnv* jni); 145 bool DeliverPendingOutputs(JNIEnv* jni);
140 146
141 // Search for H.264 start codes. 147 // Search for H.264 start codes.
142 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); 148 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size);
143 149
144 // Type of video codec. 150 // Type of video codec.
145 VideoCodecType codecType_; 151 VideoCodecType codecType_;
146 152
147 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to 153 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to
148 // |codec_thread_| synchronously. 154 // |codec_thread_| synchronously.
149 webrtc::EncodedImageCallback* callback_; 155 webrtc::EncodedImageCallback* callback_;
150 156
151 // State that is constant for the lifetime of this object once the ctor 157 // State that is constant for the lifetime of this object once the ctor
152 // returns. 158 // returns.
153 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec. 159 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec.
160 rtc::ThreadChecker codec_thread_checker_;
154 ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_; 161 ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_;
155 ScopedGlobalRef<jobject> j_media_codec_video_encoder_; 162 ScopedGlobalRef<jobject> j_media_codec_video_encoder_;
156 jmethodID j_init_encode_method_; 163 jmethodID j_init_encode_method_;
164 jmethodID j_get_input_buffers_method_;
157 jmethodID j_dequeue_input_buffer_method_; 165 jmethodID j_dequeue_input_buffer_method_;
158 jmethodID j_encode_method_; 166 jmethodID j_encode_buffer_method_;
159 jmethodID j_release_method_; 167 jmethodID j_release_method_;
160 jmethodID j_set_rates_method_; 168 jmethodID j_set_rates_method_;
161 jmethodID j_dequeue_output_buffer_method_; 169 jmethodID j_dequeue_output_buffer_method_;
162 jmethodID j_release_output_buffer_method_; 170 jmethodID j_release_output_buffer_method_;
163 jfieldID j_color_format_field_; 171 jfieldID j_color_format_field_;
164 jfieldID j_info_index_field_; 172 jfieldID j_info_index_field_;
165 jfieldID j_info_buffer_field_; 173 jfieldID j_info_buffer_field_;
166 jfieldID j_info_is_key_frame_field_; 174 jfieldID j_info_is_key_frame_field_;
167 jfieldID j_info_presentation_timestamp_us_field_; 175 jfieldID j_info_presentation_timestamp_us_field_;
168 176
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 ScopedLocalRefFrame local_ref_frame(jni); 241 ScopedLocalRefFrame local_ref_frame(jni);
234 // It would be nice to avoid spinning up a new thread per MediaCodec, and 242 // It would be nice to avoid spinning up a new thread per MediaCodec, and
235 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug 243 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug
236 // 2732 means that deadlocks abound. This class synchronously trampolines 244 // 2732 means that deadlocks abound. This class synchronously trampolines
237 // to |codec_thread_|, so if anything else can be coming to _us_ from 245 // to |codec_thread_|, so if anything else can be coming to _us_ from
238 // |codec_thread_|, or from any thread holding the |_sendCritSect| described 246 // |codec_thread_|, or from any thread holding the |_sendCritSect| described
239 // in the bug, we have a problem. For now work around that with a dedicated 247 // in the bug, we have a problem. For now work around that with a dedicated
240 // thread. 248 // thread.
241 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); 249 codec_thread_->SetName("MediaCodecVideoEncoder", NULL);
242 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; 250 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder";
243 251 codec_thread_checker_.DetachFromThread();
244 jclass j_output_buffer_info_class = 252 jclass j_output_buffer_info_class =
245 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo"); 253 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo");
246 j_init_encode_method_ = GetMethodID( 254 j_init_encode_method_ = GetMethodID(
247 jni, 255 jni,
248 *j_media_codec_video_encoder_class_, 256 *j_media_codec_video_encoder_class_,
249 "initEncode", 257 "initEncode",
250 "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;IIII)" 258 "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;IIII)Z");
251 "[Ljava/nio/ByteBuffer;"); 259 j_get_input_buffers_method_ = GetMethodID(
260 jni,
261 *j_media_codec_video_encoder_class_,
262 "getInputBuffers",
263 "()[Ljava/nio/ByteBuffer;");
252 j_dequeue_input_buffer_method_ = GetMethodID( 264 j_dequeue_input_buffer_method_ = GetMethodID(
253 jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I"); 265 jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I");
254 j_encode_method_ = GetMethodID( 266 j_encode_buffer_method_ = GetMethodID(
255 jni, *j_media_codec_video_encoder_class_, "encode", "(ZIIJ)Z"); 267 jni, *j_media_codec_video_encoder_class_, "encodeBuffer", "(ZIIJ)Z");
256 j_release_method_ = 268 j_release_method_ =
257 GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V"); 269 GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V");
258 j_set_rates_method_ = GetMethodID( 270 j_set_rates_method_ = GetMethodID(
259 jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z"); 271 jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z");
260 j_dequeue_output_buffer_method_ = GetMethodID( 272 j_dequeue_output_buffer_method_ = GetMethodID(
261 jni, 273 jni,
262 *j_media_codec_video_encoder_class_, 274 *j_media_codec_video_encoder_class_,
263 "dequeueOutputBuffer", 275 "dequeueOutputBuffer",
264 "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;"); 276 "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;");
265 j_release_output_buffer_method_ = GetMethodID( 277 j_release_output_buffer_method_ = GetMethodID(
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 quality_scaler_.ReportFramerate(frame_rate); 380 quality_scaler_.ReportFramerate(frame_rate);
369 381
370 return codec_thread_->Invoke<int32_t>( 382 return codec_thread_->Invoke<int32_t>(
371 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, 383 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread,
372 this, 384 this,
373 new_bit_rate, 385 new_bit_rate,
374 frame_rate)); 386 frame_rate));
375 } 387 }
376 388
377 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { 389 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) {
390 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
378 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 391 JNIEnv* jni = AttachCurrentThreadIfNeeded();
379 ScopedLocalRefFrame local_ref_frame(jni); 392 ScopedLocalRefFrame local_ref_frame(jni);
380 393
381 // We only ever send one message to |this| directly (not through a Bind()'d 394 // We only ever send one message to |this| directly (not through a Bind()'d
382 // functor), so expect no ID/data. 395 // functor), so expect no ID/data.
383 RTC_CHECK(!msg->message_id) << "Unexpected message!"; 396 RTC_CHECK(!msg->message_id) << "Unexpected message!";
384 RTC_CHECK(!msg->pdata) << "Unexpected message!"; 397 RTC_CHECK(!msg->pdata) << "Unexpected message!";
385 CheckOnCodecThread();
386 if (!inited_) { 398 if (!inited_) {
387 return; 399 return;
388 } 400 }
389 401
390 // It would be nice to recover from a failure here if one happened, but it's 402 // It would be nice to recover from a failure here if one happened, but it's
391 // unclear how to signal such a failure to the app, so instead we stay silent 403 // unclear how to signal such a failure to the app, so instead we stay silent
392 // about it and let the next app-called API method reveal the borkedness. 404 // about it and let the next app-called API method reveal the borkedness.
393 DeliverPendingOutputs(jni); 405 DeliverPendingOutputs(jni);
394 codec_thread_->PostDelayed(kMediaCodecPollMs, this); 406 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
395 } 407 }
396 408
397 void MediaCodecVideoEncoder::CheckOnCodecThread() { 409 void MediaCodecVideoEncoder::ResetCodecOnCodecThread() {
398 RTC_CHECK(codec_thread_ == ThreadManager::Instance()->CurrentThread()) 410 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
399 << "Running on wrong thread!"; 411 ALOGE << "ResetOnCodecThread";
400 } 412 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK ||
401 413 InitEncodeOnCodecThread(width_, height_, 0, 0)
402 void MediaCodecVideoEncoder::ResetCodec() { 414 != WEBRTC_VIDEO_CODEC_OK) {
403 ALOGE << "ResetCodec";
404 if (Release() != WEBRTC_VIDEO_CODEC_OK ||
405 codec_thread_->Invoke<int32_t>(Bind(
406 &MediaCodecVideoEncoder::InitEncodeOnCodecThread, this,
407 width_, height_, 0, 0)) != WEBRTC_VIDEO_CODEC_OK) {
408 // TODO(fischman): wouldn't it be nice if there was a way to gracefully 415 // TODO(fischman): wouldn't it be nice if there was a way to gracefully
409 // degrade to a SW encoder at this point? There isn't one AFAICT :( 416 // degrade to a SW encoder at this point? There isn't one AFAICT :(
410 // https://code.google.com/p/webrtc/issues/detail?id=2920 417 // https://code.google.com/p/webrtc/issues/detail?id=2920
411 } 418 }
412 } 419 }
413 420
414 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( 421 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread(
415 int width, int height, int kbps, int fps) { 422 int width, int height, int kbps, int fps) {
416 CheckOnCodecThread(); 423 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
417 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 424 JNIEnv* jni = AttachCurrentThreadIfNeeded();
418 ScopedLocalRefFrame local_ref_frame(jni); 425 ScopedLocalRefFrame local_ref_frame(jni);
419 426
420 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << 427 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " <<
421 width << " x " << height << ". Bitrate: " << kbps << 428 width << " x " << height << ". Bitrate: " << kbps <<
422 " kbps. Fps: " << fps; 429 " kbps. Fps: " << fps;
423 if (kbps == 0) { 430 if (kbps == 0) {
424 kbps = last_set_bitrate_kbps_; 431 kbps = last_set_bitrate_kbps_;
425 } 432 }
426 if (fps == 0) { 433 if (fps == 0) {
(...skipping 16 matching lines...) Expand all
443 current_encoding_time_ms_ = 0; 450 current_encoding_time_ms_ = 0;
444 last_input_timestamp_ms_ = -1; 451 last_input_timestamp_ms_ = -1;
445 last_output_timestamp_ms_ = -1; 452 last_output_timestamp_ms_ = -1;
446 output_timestamp_ = 0; 453 output_timestamp_ = 0;
447 output_render_time_ms_ = 0; 454 output_render_time_ms_ = 0;
448 timestamps_.clear(); 455 timestamps_.clear();
449 render_times_ms_.clear(); 456 render_times_ms_.clear();
450 frame_rtc_times_ms_.clear(); 457 frame_rtc_times_ms_.clear();
451 drop_next_input_frame_ = false; 458 drop_next_input_frame_ = false;
452 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; 459 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF;
460
453 // We enforce no extra stride/padding in the format creation step. 461 // We enforce no extra stride/padding in the format creation step.
454 jobject j_video_codec_enum = JavaEnumFromIndex( 462 jobject j_video_codec_enum = JavaEnumFromIndex(
455 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); 463 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_);
464 const bool encode_status = jni->CallBooleanMethod(
465 *j_media_codec_video_encoder_, j_init_encode_method_,
466 j_video_codec_enum, width, height, kbps, fps);
467 if (!encode_status) {
468 ALOGE << "Failed to configure encoder.";
469 return WEBRTC_VIDEO_CODEC_ERROR;
470 }
471 CHECK_EXCEPTION(jni);
472
456 jobjectArray input_buffers = reinterpret_cast<jobjectArray>( 473 jobjectArray input_buffers = reinterpret_cast<jobjectArray>(
457 jni->CallObjectMethod(*j_media_codec_video_encoder_, 474 jni->CallObjectMethod(*j_media_codec_video_encoder_,
458 j_init_encode_method_, 475 j_get_input_buffers_method_));
459 j_video_codec_enum,
460 width_,
461 height_,
462 kbps,
463 fps));
464 CHECK_EXCEPTION(jni); 476 CHECK_EXCEPTION(jni);
465 if (IsNull(jni, input_buffers)) { 477 if (IsNull(jni, input_buffers)) {
466 return WEBRTC_VIDEO_CODEC_ERROR; 478 return WEBRTC_VIDEO_CODEC_ERROR;
467 } 479 }
468 480
469 inited_ = true;
470 switch (GetIntField(jni, *j_media_codec_video_encoder_, 481 switch (GetIntField(jni, *j_media_codec_video_encoder_,
471 j_color_format_field_)) { 482 j_color_format_field_)) {
472 case COLOR_FormatYUV420Planar: 483 case COLOR_FormatYUV420Planar:
473 encoder_fourcc_ = libyuv::FOURCC_YU12; 484 encoder_fourcc_ = libyuv::FOURCC_YU12;
474 break; 485 break;
475 case COLOR_FormatYUV420SemiPlanar: 486 case COLOR_FormatYUV420SemiPlanar:
476 case COLOR_QCOM_FormatYUV420SemiPlanar: 487 case COLOR_QCOM_FormatYUV420SemiPlanar:
477 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: 488 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m:
478 encoder_fourcc_ = libyuv::FOURCC_NV12; 489 encoder_fourcc_ = libyuv::FOURCC_NV12;
479 break; 490 break;
480 default: 491 default:
481 LOG(LS_ERROR) << "Wrong color format."; 492 LOG(LS_ERROR) << "Wrong color format.";
482 return WEBRTC_VIDEO_CODEC_ERROR; 493 return WEBRTC_VIDEO_CODEC_ERROR;
483 } 494 }
484 size_t num_input_buffers = jni->GetArrayLength(input_buffers); 495 size_t num_input_buffers = jni->GetArrayLength(input_buffers);
485 RTC_CHECK(input_buffers_.empty()) 496 RTC_CHECK(input_buffers_.empty())
486 << "Unexpected double InitEncode without Release"; 497 << "Unexpected double InitEncode without Release";
487 input_buffers_.resize(num_input_buffers); 498 input_buffers_.resize(num_input_buffers);
488 for (size_t i = 0; i < num_input_buffers; ++i) { 499 for (size_t i = 0; i < num_input_buffers; ++i) {
489 input_buffers_[i] = 500 input_buffers_[i] =
490 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); 501 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i));
491 int64_t yuv_buffer_capacity = 502 int64_t yuv_buffer_capacity =
492 jni->GetDirectBufferCapacity(input_buffers_[i]); 503 jni->GetDirectBufferCapacity(input_buffers_[i]);
493 CHECK_EXCEPTION(jni); 504 CHECK_EXCEPTION(jni);
494 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; 505 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity";
495 } 506 }
496 CHECK_EXCEPTION(jni); 507 CHECK_EXCEPTION(jni);
497 508
509
510 inited_ = true;
498 codec_thread_->PostDelayed(kMediaCodecPollMs, this); 511 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
499 return WEBRTC_VIDEO_CODEC_OK; 512 return WEBRTC_VIDEO_CODEC_OK;
500 } 513 }
501 514
502 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( 515 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread(
503 const webrtc::VideoFrame& frame, 516 const webrtc::VideoFrame& frame,
504 const std::vector<webrtc::VideoFrameType>* frame_types) { 517 const std::vector<webrtc::VideoFrameType>* frame_types) {
505 CheckOnCodecThread(); 518 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
506 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 519 JNIEnv* jni = AttachCurrentThreadIfNeeded();
507 ScopedLocalRefFrame local_ref_frame(jni); 520 ScopedLocalRefFrame local_ref_frame(jni);
508 521
509 if (!inited_) { 522 if (!inited_) {
510 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; 523 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
511 } 524 }
525
512 frames_received_++; 526 frames_received_++;
513 if (!DeliverPendingOutputs(jni)) { 527 if (!DeliverPendingOutputs(jni)) {
514 ResetCodec(); 528 ResetCodecOnCodecThread();
515 // Continue as if everything's fine. 529 // Continue as if everything's fine.
516 } 530 }
517 531
518 if (drop_next_input_frame_) { 532 if (drop_next_input_frame_) {
519 ALOGV("Encoder drop frame - failed callback."); 533 ALOGD << "Encoder drop frame - failed callback.";
520 drop_next_input_frame_ = false; 534 drop_next_input_frame_ = false;
521 return WEBRTC_VIDEO_CODEC_OK; 535 return WEBRTC_VIDEO_CODEC_OK;
522 } 536 }
523 537
524 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; 538 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count";
525 // Check framerate before spatial resolution change. 539 // Check framerate before spatial resolution change.
526 if (scale_) 540 if (scale_)
527 quality_scaler_.OnEncodeFrame(frame); 541 quality_scaler_.OnEncodeFrame(frame);
528 542
529 const VideoFrame& input_frame = 543 const VideoFrame& input_frame =
530 scale_ ? quality_scaler_.GetScaledFrame(frame) : frame; 544 scale_ ? quality_scaler_.GetScaledFrame(frame) : frame;
531 545
532 if (input_frame.width() != width_ || input_frame.height() != height_) { 546 if (!MaybeReconfigureEncoderOnCodecThread(frame)) {
533 ALOGD << "Frame resolution change from " << width_ << " x " << height_ << 547 ALOGE << "Failed to reconfigure encoder.";
534 " to " << input_frame.width() << " x " << input_frame.height(); 548 return WEBRTC_VIDEO_CODEC_ERROR;
535 width_ = input_frame.width();
536 height_ = input_frame.height();
537 ResetCodec();
538 return WEBRTC_VIDEO_CODEC_OK;
539 } 549 }
540 550
541 // Check if we accumulated too many frames in encoder input buffers 551 // Check if we accumulated too many frames in encoder input buffers
542 // or the encoder latency exceeds 70 ms and drop frame if so. 552 // or the encoder latency exceeds 70 ms and drop frame if so.
543 if (frames_in_queue_ > 0 && last_input_timestamp_ms_ >= 0) { 553 if (frames_in_queue_ > 0 && last_input_timestamp_ms_ >= 0) {
544 int encoder_latency_ms = last_input_timestamp_ms_ - 554 int encoder_latency_ms = last_input_timestamp_ms_ -
545 last_output_timestamp_ms_; 555 last_output_timestamp_ms_;
546 if (frames_in_queue_ > 2 || encoder_latency_ms > 70) { 556 if (frames_in_queue_ > 2 || encoder_latency_ms > 70) {
547 ALOGD << "Drop frame - encoder is behind by " << encoder_latency_ms << 557 ALOGD << "Drop frame - encoder is behind by " << encoder_latency_ms <<
548 " ms. Q size: " << frames_in_queue_; 558 " ms. Q size: " << frames_in_queue_;
549 frames_dropped_++; 559 frames_dropped_++;
550 // Report dropped frame to quality_scaler_. 560 // Report dropped frame to quality_scaler_.
551 OnDroppedFrame(); 561 OnDroppedFrame();
552 return WEBRTC_VIDEO_CODEC_OK; 562 return WEBRTC_VIDEO_CODEC_OK;
553 } 563 }
554 } 564 }
555 565
566 last_input_timestamp_ms_ =
567 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec;
568 frames_in_queue_++;
569
570 // Save input image timestamps for later output
571 timestamps_.push_back(input_frame.timestamp());
572 render_times_ms_.push_back(input_frame.render_time_ms());
573 frame_rtc_times_ms_.push_back(GetCurrentTimeMs());
574
575 const bool key_frame = frame_types->front() != webrtc::kDeltaFrame;
576 const bool encode_status =
577 EncodeByteBufferOnCodecThread(jni, key_frame, input_frame);
578
579 current_timestamp_us_ += 1000000 / last_set_fps_;
580
581 if (!encode_status || !DeliverPendingOutputs(jni)) {
582 ALOGE << "Failed deliver pending outputs.";
583 ResetCodecOnCodecThread();
584 return WEBRTC_VIDEO_CODEC_ERROR;
585 }
586 return WEBRTC_VIDEO_CODEC_OK;
587 }
588
589 bool MediaCodecVideoEncoder::MaybeReconfigureEncoderOnCodecThread(
590 const webrtc::VideoFrame& frame) {
591 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
592
593 const bool reconfigure_due_to_size =
594 frame.width() != width_ || frame.height() != height_;
595
596 if (reconfigure_due_to_size) {
597 ALOGD << "Reconfigure encoder due to frame resolution change from "
598 << width_ << " x " << height_ << " to " << frame.width() << " x "
599 << frame.height();
600 width_ = frame.width();
601 height_ = frame.height();
602 }
603
604 if (!reconfigure_due_to_size)
605 return true;
606
607 ReleaseOnCodecThread();
608
609 return InitEncodeOnCodecThread(width_, height_, 0, 0) ==
610 WEBRTC_VIDEO_CODEC_OK;
611 }
612
613 bool MediaCodecVideoEncoder::EncodeByteBufferOnCodecThread(JNIEnv* jni,
614 bool key_frame, const webrtc::VideoFrame& frame) {
615 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
556 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, 616 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_,
557 j_dequeue_input_buffer_method_); 617 j_dequeue_input_buffer_method_);
558 CHECK_EXCEPTION(jni); 618 CHECK_EXCEPTION(jni);
559 if (j_input_buffer_index == -1) { 619 if (j_input_buffer_index == -1) {
560 // Video codec falls behind - no input buffer available. 620 // Video codec falls behind - no input buffer available.
561 ALOGV("Encoder drop frame - no input buffers available"); 621 ALOGD <<"Encoder drop frame - no input buffers available";
562 frames_dropped_++; 622 frames_dropped_++;
563 // Report dropped frame to quality_scaler_. 623 // Report dropped frame to quality_scaler_.
564 OnDroppedFrame(); 624 OnDroppedFrame();
565 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. 625 return true; // TODO(fischman): see webrtc bug 2887.
566 } 626 }
567 if (j_input_buffer_index == -2) { 627 if (j_input_buffer_index == -2) {
568 ResetCodec(); 628 return false;
569 return WEBRTC_VIDEO_CODEC_ERROR;
570 } 629 }
571 630
572 ALOGV("Encoder frame in # %d. TS: %lld. Q: %d", 631 ALOGV("Encoder frame in # %d. TS: %lld. Q: %d",
573 frames_received_ - 1, current_timestamp_us_ / 1000, frames_in_queue_); 632 frames_received_ - 1, current_timestamp_us_ / 1000, frames_in_queue_);
574 633
575 jobject j_input_buffer = input_buffers_[j_input_buffer_index]; 634 jobject j_input_buffer = input_buffers_[j_input_buffer_index];
576 uint8_t* yuv_buffer = 635 uint8_t* yuv_buffer =
577 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); 636 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer));
578 CHECK_EXCEPTION(jni); 637 CHECK_EXCEPTION(jni);
579 RTC_CHECK(yuv_buffer) << "Indirect buffer??"; 638 RTC_CHECK(yuv_buffer) << "Indirect buffer??";
580 RTC_CHECK(!libyuv::ConvertFromI420( 639 RTC_CHECK(!libyuv::ConvertFromI420(
581 input_frame.buffer(webrtc::kYPlane), input_frame.stride(webrtc::kYPlane), 640 frame.buffer(webrtc::kYPlane), frame.stride(webrtc::kYPlane),
582 input_frame.buffer(webrtc::kUPlane), input_frame.stride(webrtc::kUPlane), 641 frame.buffer(webrtc::kUPlane), frame.stride(webrtc::kUPlane),
583 input_frame.buffer(webrtc::kVPlane), input_frame.stride(webrtc::kVPlane), 642 frame.buffer(webrtc::kVPlane), frame.stride(webrtc::kVPlane),
584 yuv_buffer, width_, width_, height_, encoder_fourcc_)) 643 yuv_buffer, width_, width_, height_, encoder_fourcc_))
585 << "ConvertFromI420 failed"; 644 << "ConvertFromI420 failed";
586 last_input_timestamp_ms_ = current_timestamp_us_ / 1000;
587 frames_in_queue_++;
588 645
589 // Save input image timestamps for later output
590 timestamps_.push_back(input_frame.timestamp());
591 render_times_ms_.push_back(input_frame.render_time_ms());
592 frame_rtc_times_ms_.push_back(GetCurrentTimeMs());
593 646
594 bool key_frame = frame_types->front() != webrtc::kDeltaFrame;
595 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, 647 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
596 j_encode_method_, 648 j_encode_buffer_method_,
597 key_frame, 649 key_frame,
598 j_input_buffer_index, 650 j_input_buffer_index,
599 yuv_size_, 651 yuv_size_,
600 current_timestamp_us_); 652 current_timestamp_us_);
601 CHECK_EXCEPTION(jni); 653 CHECK_EXCEPTION(jni);
602 current_timestamp_us_ += 1000000 / last_set_fps_; 654 return encode_status;
603
604 if (!encode_status || !DeliverPendingOutputs(jni)) {
605 ResetCodec();
606 return WEBRTC_VIDEO_CODEC_ERROR;
607 }
608
609 return WEBRTC_VIDEO_CODEC_OK;
610 } 655 }
611 656
612 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( 657 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread(
613 webrtc::EncodedImageCallback* callback) { 658 webrtc::EncodedImageCallback* callback) {
614 CheckOnCodecThread(); 659 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
615 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 660 JNIEnv* jni = AttachCurrentThreadIfNeeded();
616 ScopedLocalRefFrame local_ref_frame(jni); 661 ScopedLocalRefFrame local_ref_frame(jni);
617 callback_ = callback; 662 callback_ = callback;
618 return WEBRTC_VIDEO_CODEC_OK; 663 return WEBRTC_VIDEO_CODEC_OK;
619 } 664 }
620 665
621 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { 666 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() {
667 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
622 if (!inited_) { 668 if (!inited_) {
623 return WEBRTC_VIDEO_CODEC_OK; 669 return WEBRTC_VIDEO_CODEC_OK;
624 } 670 }
625 CheckOnCodecThread();
626 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 671 JNIEnv* jni = AttachCurrentThreadIfNeeded();
627 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << 672 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " <<
628 frames_received_ << ". Encoded: " << frames_encoded_ << 673 frames_received_ << ". Encoded: " << frames_encoded_ <<
629 ". Dropped: " << frames_dropped_; 674 ". Dropped: " << frames_dropped_;
630 ScopedLocalRefFrame local_ref_frame(jni); 675 ScopedLocalRefFrame local_ref_frame(jni);
631 for (size_t i = 0; i < input_buffers_.size(); ++i) 676 for (size_t i = 0; i < input_buffers_.size(); ++i)
632 jni->DeleteGlobalRef(input_buffers_[i]); 677 jni->DeleteGlobalRef(input_buffers_[i]);
633 input_buffers_.clear(); 678 input_buffers_.clear();
634 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); 679 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_);
635 CHECK_EXCEPTION(jni); 680 CHECK_EXCEPTION(jni);
636 rtc::MessageQueueManager::Clear(this); 681 rtc::MessageQueueManager::Clear(this);
637 inited_ = false; 682 inited_ = false;
638 ALOGD << "EncoderReleaseOnCodecThread done."; 683 ALOGD << "EncoderReleaseOnCodecThread done.";
639 return WEBRTC_VIDEO_CODEC_OK; 684 return WEBRTC_VIDEO_CODEC_OK;
640 } 685 }
641 686
642 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, 687 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate,
643 uint32_t frame_rate) { 688 uint32_t frame_rate) {
644 CheckOnCodecThread(); 689 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
645 if (last_set_bitrate_kbps_ == new_bit_rate && 690 if (last_set_bitrate_kbps_ == new_bit_rate &&
646 last_set_fps_ == frame_rate) { 691 last_set_fps_ == frame_rate) {
647 return WEBRTC_VIDEO_CODEC_OK; 692 return WEBRTC_VIDEO_CODEC_OK;
648 } 693 }
649 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 694 JNIEnv* jni = AttachCurrentThreadIfNeeded();
650 ScopedLocalRefFrame local_ref_frame(jni); 695 ScopedLocalRefFrame local_ref_frame(jni);
651 if (new_bit_rate > 0) { 696 if (new_bit_rate > 0) {
652 last_set_bitrate_kbps_ = new_bit_rate; 697 last_set_bitrate_kbps_ = new_bit_rate;
653 } 698 }
654 if (frame_rate > 0) { 699 if (frame_rate > 0) {
655 last_set_fps_ = frame_rate; 700 last_set_fps_ = frame_rate;
656 } 701 }
657 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, 702 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
658 j_set_rates_method_, 703 j_set_rates_method_,
659 last_set_bitrate_kbps_, 704 last_set_bitrate_kbps_,
660 last_set_fps_); 705 last_set_fps_);
661 CHECK_EXCEPTION(jni); 706 CHECK_EXCEPTION(jni);
662 if (!ret) { 707 if (!ret) {
663 ResetCodec(); 708 ResetCodecOnCodecThread();
664 return WEBRTC_VIDEO_CODEC_ERROR; 709 return WEBRTC_VIDEO_CODEC_ERROR;
665 } 710 }
666 return WEBRTC_VIDEO_CODEC_OK; 711 return WEBRTC_VIDEO_CODEC_OK;
667 } 712 }
668 713
669 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( 714 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex(
670 JNIEnv* jni, 715 JNIEnv* jni,
671 jobject j_output_buffer_info) { 716 jobject j_output_buffer_info) {
672 return GetIntField(jni, j_output_buffer_info, j_info_index_field_); 717 return GetIntField(jni, j_output_buffer_info, j_info_index_field_);
673 } 718 }
(...skipping 11 matching lines...) Expand all
685 } 730 }
686 731
687 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( 732 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs(
688 JNIEnv* jni, 733 JNIEnv* jni,
689 jobject j_output_buffer_info) { 734 jobject j_output_buffer_info) {
690 return GetLongField( 735 return GetLongField(
691 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); 736 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_);
692 } 737 }
693 738
694 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { 739 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) {
740 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
695 while (true) { 741 while (true) {
696 jobject j_output_buffer_info = jni->CallObjectMethod( 742 jobject j_output_buffer_info = jni->CallObjectMethod(
697 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); 743 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_);
698 CHECK_EXCEPTION(jni); 744 CHECK_EXCEPTION(jni);
699 if (IsNull(jni, j_output_buffer_info)) { 745 if (IsNull(jni, j_output_buffer_info)) {
700 break; 746 break;
701 } 747 }
702 748
703 int output_buffer_index = 749 int output_buffer_index =
704 GetOutputBufferInfoIndex(jni, j_output_buffer_info); 750 GetOutputBufferInfoIndex(jni, j_output_buffer_info);
705 if (output_buffer_index == -1) { 751 if (output_buffer_index == -1) {
706 ResetCodec(); 752 ResetCodecOnCodecThread();
707 return false; 753 return false;
708 } 754 }
709 755
710 // Get key and config frame flags. 756 // Get key and config frame flags.
711 jobject j_output_buffer = 757 jobject j_output_buffer =
712 GetOutputBufferInfoBuffer(jni, j_output_buffer_info); 758 GetOutputBufferInfoBuffer(jni, j_output_buffer_info);
713 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); 759 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info);
714 760
715 // Get frame timestamps from a queue - for non config frames only. 761 // Get frame timestamps from a queue - for non config frames only.
716 int64_t frame_encoding_time_ms = 0; 762 int64_t frame_encoding_time_ms = 0;
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 } 863 }
818 scPosition += naluPosition; 864 scPosition += naluPosition;
819 scPositions[scPositionsLength++] = scPosition; 865 scPositions[scPositionsLength++] = scPosition;
820 scPosition += H264_SC_LENGTH; 866 scPosition += H264_SC_LENGTH;
821 } 867 }
822 if (scPositionsLength == 0) { 868 if (scPositionsLength == 0) {
823 ALOGE << "Start code is not found!"; 869 ALOGE << "Start code is not found!";
824 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] 870 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1]
825 << " " << image->_buffer[2] << " " << image->_buffer[3] 871 << " " << image->_buffer[2] << " " << image->_buffer[3]
826 << " " << image->_buffer[4] << " " << image->_buffer[5]; 872 << " " << image->_buffer[4] << " " << image->_buffer[5];
827 ResetCodec(); 873 ResetCodecOnCodecThread();
828 return false; 874 return false;
829 } 875 }
830 scPositions[scPositionsLength] = payload_size; 876 scPositions[scPositionsLength] = payload_size;
831 header.VerifyAndAllocateFragmentationHeader(scPositionsLength); 877 header.VerifyAndAllocateFragmentationHeader(scPositionsLength);
832 for (size_t i = 0; i < scPositionsLength; i++) { 878 for (size_t i = 0; i < scPositionsLength; i++) {
833 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; 879 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH;
834 header.fragmentationLength[i] = 880 header.fragmentationLength[i] =
835 scPositions[i + 1] - header.fragmentationOffset[i]; 881 scPositions[i + 1] - header.fragmentationOffset[i];
836 header.fragmentationPlType[i] = 0; 882 header.fragmentationPlType[i] = 0;
837 header.fragmentationTimeDiff[i] = 0; 883 header.fragmentationTimeDiff[i] = 0;
838 } 884 }
839 } 885 }
840 886
841 callback_status = callback_->Encoded(*image, &info, &header); 887 callback_status = callback_->Encoded(*image, &info, &header);
842 } 888 }
843 889
844 // Return output buffer back to the encoder. 890 // Return output buffer back to the encoder.
845 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, 891 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
846 j_release_output_buffer_method_, 892 j_release_output_buffer_method_,
847 output_buffer_index); 893 output_buffer_index);
848 CHECK_EXCEPTION(jni); 894 CHECK_EXCEPTION(jni);
849 if (!success) { 895 if (!success) {
850 ResetCodec(); 896 ResetCodecOnCodecThread();
851 return false; 897 return false;
852 } 898 }
853 899
854 if (callback_status > 0) { 900 if (callback_status > 0) {
855 drop_next_input_frame_ = true; 901 drop_next_input_frame_ = true;
856 // Theoretically could handle callback_status<0 here, but unclear what 902 // Theoretically could handle callback_status<0 here, but unclear what
857 // that would mean for us. 903 // that would mean for us.
858 } 904 }
859 } 905 }
860 906
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 } 999 }
954 1000
955 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( 1001 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder(
956 webrtc::VideoEncoder* encoder) { 1002 webrtc::VideoEncoder* encoder) {
957 ALOGD << "Destroy video encoder."; 1003 ALOGD << "Destroy video encoder.";
958 delete encoder; 1004 delete encoder;
959 } 1005 }
960 1006
961 } // namespace webrtc_jni 1007 } // namespace webrtc_jni
962 1008
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698