1 /* 2 * Copyright (c) 2012 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/examples/android/media_demo/jni/media_codec_video_decoder.h" 12 13 #include <android/log.h> 14 15 #include "webrtc/examples/android/media_demo/jni/jni_helpers.h" 16 #include "webrtc/modules/utility/interface/helpers_android.h" 17 18 namespace webrtc { 19 20 MediaCodecVideoDecoder::MediaCodecVideoDecoder(JavaVM* vm, jobject decoder) 21 : vm_(vm), decoder_(NULL), j_start_(NULL), j_push_buffer_(NULL) { 22 AttachThreadScoped ats(vm_); 23 JNIEnv* jni = ats.env(); 24 // Make sure that the decoder is not recycled. 25 decoder_ = jni->NewGlobalRef(decoder); 26 27 // Get all function IDs. 28 jclass decoderClass = jni->GetObjectClass(decoder); 29 j_push_buffer_ = 30 jni->GetMethodID(decoderClass, "pushBuffer", "(Ljava/nio/ByteBuffer;J)V"); 31 j_start_ = jni->GetMethodID(decoderClass, "start", "(II)Z"); 32 } 33 34 MediaCodecVideoDecoder::~MediaCodecVideoDecoder() { 35 AttachThreadScoped ats(vm_); 36 JNIEnv* jni = ats.env(); 37 jni->DeleteGlobalRef(decoder_); 38 } 39 40 int32_t MediaCodecVideoDecoder::InitDecode(const VideoCodec* codecSettings, 41 int32_t numberOfCores) { 42 AttachThreadScoped ats(vm_); 43 JNIEnv* jni = ats.env(); 44 if (!jni->CallBooleanMethod(decoder_, j_start_, codecSettings->width, 45 codecSettings->height)) { 46 return WEBRTC_VIDEO_CODEC_ERROR; 47 } 48 return WEBRTC_VIDEO_CODEC_OK; 49 } 50 51 int32_t MediaCodecVideoDecoder::Decode( 52 const EncodedImage& inputImage, bool missingFrames, 53 const RTPFragmentationHeader* fragmentation, 54 const CodecSpecificInfo* codecSpecificInfo, int64_t renderTimeMs) { 55 56 AttachThreadScoped ats(vm_); 57 JNIEnv* jni = ats.env(); 58 jobject byteBuffer = 59 jni->NewDirectByteBuffer(inputImage._buffer, inputImage._length); 60 jni->CallVoidMethod(decoder_, j_push_buffer_, byteBuffer, renderTimeMs); 61 jni->DeleteLocalRef(byteBuffer); 62 return WEBRTC_VIDEO_CODEC_NO_OUTPUT; 63 } 64 65 int32_t MediaCodecVideoDecoder::RegisterDecodeCompleteCallback( 66 DecodedImageCallback* callback) { 67 return WEBRTC_VIDEO_CODEC_OK; 68 } 69 70 int32_t MediaCodecVideoDecoder::Release() { 71 // TODO(hellner): this maps nicely to MediaCodecVideoDecoder::dispose(). 72 return WEBRTC_VIDEO_CODEC_OK; 73 } 74 75 int32_t MediaCodecVideoDecoder::Reset() { 76 // TODO(hellner): implement. MediaCodec::stop() followed by 77 // MediaCodec::start()? 78 return WEBRTC_VIDEO_CODEC_OK; 79 } 80 81 } // namespace webrtc 82