OLD | NEW |
---|---|
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 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 758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
769 jni, *j_frame_class_, "<init>", | 769 jni, *j_frame_class_, "<init>", |
770 "(IIILjava/lang/Object;IJ)V")), | 770 "(IIILjava/lang/Object;IJ)V")), |
771 j_byte_buffer_class_(jni, FindClass(jni, "java/nio/ByteBuffer")) { | 771 j_byte_buffer_class_(jni, FindClass(jni, "java/nio/ByteBuffer")) { |
772 CHECK_EXCEPTION(jni); | 772 CHECK_EXCEPTION(jni); |
773 } | 773 } |
774 | 774 |
775 virtual ~JavaVideoRendererWrapper() {} | 775 virtual ~JavaVideoRendererWrapper() {} |
776 | 776 |
777 void RenderFrame(const cricket::VideoFrame* video_frame) override { | 777 void RenderFrame(const cricket::VideoFrame* video_frame) override { |
778 ScopedLocalRefFrame local_ref_frame(jni()); | 778 ScopedLocalRefFrame local_ref_frame(jni()); |
779 // Make a shallow copy. |j_callbacks_| is responsible for releasing the | |
780 // copy by calling VideoRenderer.renderFrameDone(). | |
781 video_frame = video_frame->Copy(); | |
782 jobject j_frame = (video_frame->GetNativeHandle() != nullptr) | 779 jobject j_frame = (video_frame->GetNativeHandle() != nullptr) |
783 ? CricketToJavaTextureFrame(video_frame) | 780 ? CricketToJavaTextureFrame(video_frame) |
784 : CricketToJavaI420Frame(video_frame); | 781 : CricketToJavaI420Frame(video_frame); |
782 // |j_callbacks_| is responsible for releasing |j_frame| with | |
783 // VideoRenderer.renderFrameDone(). | |
785 jni()->CallVoidMethod(*j_callbacks_, j_render_frame_id_, j_frame); | 784 jni()->CallVoidMethod(*j_callbacks_, j_render_frame_id_, j_frame); |
786 CHECK_EXCEPTION(jni()); | 785 CHECK_EXCEPTION(jni()); |
787 } | 786 } |
788 | 787 |
789 private: | 788 private: |
789 // Make a shallow copy of |frame| to be used with Java. The callee has | |
790 // ownership of the frame, and the frame should be released with | |
791 // VideoRenderer.releaseNativeFrame(). | |
792 static jlong javaShallowCopy(const cricket::VideoFrame* frame) { | |
793 return jlongFromPointer(frame->Copy()); | |
794 } | |
795 | |
790 // Return a VideoRenderer.I420Frame referring to the data in |frame|. | 796 // Return a VideoRenderer.I420Frame referring to the data in |frame|. |
791 jobject CricketToJavaI420Frame(const cricket::VideoFrame* frame) { | 797 jobject CricketToJavaI420Frame(const cricket::VideoFrame* frame) { |
792 jintArray strides = jni()->NewIntArray(3); | 798 jintArray strides = jni()->NewIntArray(3); |
793 jint* strides_array = jni()->GetIntArrayElements(strides, NULL); | 799 jint* strides_array = jni()->GetIntArrayElements(strides, NULL); |
794 strides_array[0] = frame->GetYPitch(); | 800 strides_array[0] = frame->GetYPitch(); |
795 strides_array[1] = frame->GetUPitch(); | 801 strides_array[1] = frame->GetUPitch(); |
796 strides_array[2] = frame->GetVPitch(); | 802 strides_array[2] = frame->GetVPitch(); |
797 jni()->ReleaseIntArrayElements(strides, strides_array, 0); | 803 jni()->ReleaseIntArrayElements(strides, strides_array, 0); |
798 jobjectArray planes = jni()->NewObjectArray(3, *j_byte_buffer_class_, NULL); | 804 jobjectArray planes = jni()->NewObjectArray(3, *j_byte_buffer_class_, NULL); |
799 jobject y_buffer = jni()->NewDirectByteBuffer( | 805 jobject y_buffer = jni()->NewDirectByteBuffer( |
800 const_cast<uint8*>(frame->GetYPlane()), | 806 const_cast<uint8*>(frame->GetYPlane()), |
801 frame->GetYPitch() * frame->GetHeight()); | 807 frame->GetYPitch() * frame->GetHeight()); |
802 jobject u_buffer = jni()->NewDirectByteBuffer( | 808 jobject u_buffer = jni()->NewDirectByteBuffer( |
803 const_cast<uint8*>(frame->GetUPlane()), frame->GetChromaSize()); | 809 const_cast<uint8*>(frame->GetUPlane()), frame->GetChromaSize()); |
804 jobject v_buffer = jni()->NewDirectByteBuffer( | 810 jobject v_buffer = jni()->NewDirectByteBuffer( |
805 const_cast<uint8*>(frame->GetVPlane()), frame->GetChromaSize()); | 811 const_cast<uint8*>(frame->GetVPlane()), frame->GetChromaSize()); |
806 jni()->SetObjectArrayElement(planes, 0, y_buffer); | 812 jni()->SetObjectArrayElement(planes, 0, y_buffer); |
807 jni()->SetObjectArrayElement(planes, 1, u_buffer); | 813 jni()->SetObjectArrayElement(planes, 1, u_buffer); |
808 jni()->SetObjectArrayElement(planes, 2, v_buffer); | 814 jni()->SetObjectArrayElement(planes, 2, v_buffer); |
809 return jni()->NewObject( | 815 return jni()->NewObject( |
810 *j_frame_class_, j_i420_frame_ctor_id_, | 816 *j_frame_class_, j_i420_frame_ctor_id_, |
811 frame->GetWidth(), frame->GetHeight(), | 817 frame->GetWidth(), frame->GetHeight(), |
812 static_cast<int>(frame->GetVideoRotation()), | 818 static_cast<int>(frame->GetVideoRotation()), |
813 strides, planes, frame); | 819 strides, planes, javaShallowCopy(frame)); |
perkj_webrtc
2015/09/29 12:56:04
Even if this is shallow, it allocates memory. Who
magjed_webrtc
2015/09/29 13:16:16
It's documented in the comment of javaShallowCopy(
| |
814 } | 820 } |
815 | 821 |
816 // Return a VideoRenderer.I420Frame referring texture object in |frame|. | 822 // Return a VideoRenderer.I420Frame referring texture object in |frame|. |
817 jobject CricketToJavaTextureFrame(const cricket::VideoFrame* frame) { | 823 jobject CricketToJavaTextureFrame(const cricket::VideoFrame* frame) { |
818 NativeHandleImpl* handle = | 824 NativeHandleImpl* handle = |
819 reinterpret_cast<NativeHandleImpl*>(frame->GetNativeHandle()); | 825 reinterpret_cast<NativeHandleImpl*>(frame->GetNativeHandle()); |
820 jobject texture_object = reinterpret_cast<jobject>(handle->GetHandle()); | 826 jobject texture_object = reinterpret_cast<jobject>(handle->GetHandle()); |
821 int texture_id = handle->GetTextureId(); | 827 int texture_id = handle->GetTextureId(); |
822 return jni()->NewObject( | 828 return jni()->NewObject( |
823 *j_frame_class_, j_texture_frame_ctor_id_, | 829 *j_frame_class_, j_texture_frame_ctor_id_, |
824 frame->GetWidth(), frame->GetHeight(), | 830 frame->GetWidth(), frame->GetHeight(), |
825 static_cast<int>(frame->GetVideoRotation()), | 831 static_cast<int>(frame->GetVideoRotation()), |
826 texture_object, texture_id, frame); | 832 texture_object, texture_id, javaShallowCopy(frame)); |
827 } | 833 } |
828 | 834 |
829 JNIEnv* jni() { | 835 JNIEnv* jni() { |
830 return AttachCurrentThreadIfNeeded(); | 836 return AttachCurrentThreadIfNeeded(); |
831 } | 837 } |
832 | 838 |
833 ScopedGlobalRef<jobject> j_callbacks_; | 839 ScopedGlobalRef<jobject> j_callbacks_; |
834 jmethodID j_render_frame_id_; | 840 jmethodID j_render_frame_id_; |
835 ScopedGlobalRef<jclass> j_frame_class_; | 841 ScopedGlobalRef<jclass> j_frame_class_; |
836 jmethodID j_i420_frame_ctor_id_; | 842 jmethodID j_i420_frame_ctor_id_; |
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1848 rtc::scoped_ptr<jbyte> buffer(static_cast<jbyte*>(malloc(log_size))); | 1854 rtc::scoped_ptr<jbyte> buffer(static_cast<jbyte*>(malloc(log_size))); |
1849 stream->ReadAll(buffer.get(), log_size, &read, nullptr); | 1855 stream->ReadAll(buffer.get(), log_size, &read, nullptr); |
1850 | 1856 |
1851 jbyteArray result = jni->NewByteArray(read); | 1857 jbyteArray result = jni->NewByteArray(read); |
1852 jni->SetByteArrayRegion(result, 0, read, buffer.get()); | 1858 jni->SetByteArrayRegion(result, 0, read, buffer.get()); |
1853 | 1859 |
1854 return result; | 1860 return result; |
1855 } | 1861 } |
1856 | 1862 |
1857 } // namespace webrtc_jni | 1863 } // namespace webrtc_jni |
OLD | NEW |