ndkcamera.cpp 39.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#include "ndkcamera.h"

#include <string>
#include <thread>
#include <queue>
#include <mutex>

#include <android/log.h>
#include <media/NdkMediaCodec.h>
#include <media/NdkMediaMuxer.h>
#include <media/NdkMediaFormat.h>
#include <chrono>
#include <unistd.h>
#include <fcntl.h>

#include <opencv2/core/core.hpp>
#include <sys/time.h>

#include "mat.h"

static void onDisconnected(void* context, ACameraDevice* device)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onDisconnected %p", device);
}

static void onError(void* context, ACameraDevice* device, int error)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onError %p %d", device, error);
}

static void onImageAvailable(void* context, AImageReader* reader)
{
//     __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onImageAvailable %p", reader);

    AImage* image = 0;
    media_status_t status = AImageReader_acquireLatestImage(reader, &image);

    if (status != AMEDIA_OK)
    {
        // error
        return;
    }

    int32_t format;
    AImage_getFormat(image, &format);

    // assert format == AIMAGE_FORMAT_YUV_420_888

    int32_t width = 0;
    int32_t height = 0;
    AImage_getWidth(image, &width);
    AImage_getHeight(image, &height);

    int32_t y_pixelStride = 0;
    int32_t u_pixelStride = 0;
    int32_t v_pixelStride = 0;
    AImage_getPlanePixelStride(image, 0, &y_pixelStride);
    AImage_getPlanePixelStride(image, 1, &u_pixelStride);
    AImage_getPlanePixelStride(image, 2, &v_pixelStride);

    int32_t y_rowStride = 0;
    int32_t u_rowStride = 0;
    int32_t v_rowStride = 0;
    AImage_getPlaneRowStride(image, 0, &y_rowStride);
    AImage_getPlaneRowStride(image, 1, &u_rowStride);
    AImage_getPlaneRowStride(image, 2, &v_rowStride);

    uint8_t* y_data = 0;
    uint8_t* u_data = 0;
    uint8_t* v_data = 0;
    int y_len = 0;
    int u_len = 0;
    int v_len = 0;
    AImage_getPlaneData(image, 0, &y_data, &y_len);
    AImage_getPlaneData(image, 1, &u_data, &u_len);
    AImage_getPlaneData(image, 2, &v_data, &v_len);

    if (u_data == v_data + 1 && v_data == y_data + width * height && y_pixelStride == 1 && u_pixelStride == 2 && v_pixelStride == 2 && y_rowStride == width && u_rowStride == width && v_rowStride == width)
    {
        // already nv21  :)
        ((NdkCamera*)context)->on_image((unsigned char*)y_data, (int)width, (int)height);
    }
    else
    {
        // construct nv21
        unsigned char* nv21 = new unsigned char[width * height + width * height / 2];
        {
            // Y
            unsigned char* yptr = nv21;
            for (int y=0; y<height; y++)
            {
                const unsigned char* y_data_ptr = y_data + y_rowStride * y;
                for (int x=0; x<width; x++)
                {
                    yptr[0] = y_data_ptr[0];
                    yptr++;
                    y_data_ptr += y_pixelStride;
                }
            }

            // UV
            unsigned char* uvptr = nv21 + width * height;
            for (int y=0; y<height/2; y++)
            {
                const unsigned char* v_data_ptr = v_data + v_rowStride * y;
                const unsigned char* u_data_ptr = u_data + u_rowStride * y;
                for (int x=0; x<width/2; x++)
                {
                    uvptr[0] = v_data_ptr[0];
                    uvptr[1] = u_data_ptr[0];
                    uvptr += 2;
                    v_data_ptr += v_pixelStride;
                    u_data_ptr += u_pixelStride;
                }
            }
        }

        ((NdkCamera*)context)->on_image((unsigned char*)nv21, (int)width, (int)height);

        delete[] nv21;
    }

    AImage_delete(image);
}

static void onSessionActive(void* context, ACameraCaptureSession *session)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onSessionActive %p", session);
}

static void onSessionReady(void* context, ACameraCaptureSession *session)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onSessionReady %p", session);
}

static void onSessionClosed(void* context, ACameraCaptureSession *session)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onSessionClosed %p", session);
}

void onCaptureFailed(void* context, ACameraCaptureSession* session, ACaptureRequest* request, ACameraCaptureFailure* failure)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onCaptureFailed %p %p %p", session, request, failure);
}

void onCaptureSequenceCompleted(void* context, ACameraCaptureSession* session, int sequenceId, int64_t frameNumber)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onCaptureSequenceCompleted %p %d %ld", session, sequenceId, frameNumber);
}

void onCaptureSequenceAborted(void* context, ACameraCaptureSession* session, int sequenceId)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onCaptureSequenceAborted %p %d", session, sequenceId);
}

void onCaptureCompleted(void* context, ACameraCaptureSession* session, ACaptureRequest* request, const ACameraMetadata* result)
{
//     __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "onCaptureCompleted %p %p %p", session, request, result);
}

NdkCamera::NdkCamera()
{
    camera_facing = 0;
    camera_orientation = 0;

    camera_manager = 0;
    camera_device = 0;
    image_reader = 0;
    image_reader_surface = 0;
    image_reader_target = 0;
    capture_request = 0;
    capture_session_output_container = 0;
    capture_session_output = 0;
    capture_session = 0;


    // setup imagereader and its surface
    {
        AImageReader_new(640, 480, AIMAGE_FORMAT_YUV_420_888, /*maxImages*/2, &image_reader);

        AImageReader_ImageListener listener;
        listener.context = this;
        listener.onImageAvailable = onImageAvailable;

        AImageReader_setImageListener(image_reader, &listener);

        AImageReader_getWindow(image_reader, &image_reader_surface);

        ANativeWindow_acquire(image_reader_surface);
    }
}

NdkCamera::~NdkCamera()
{
    close();

    if (image_reader)
    {
        AImageReader_delete(image_reader);
        image_reader = 0;
    }

    if (image_reader_surface)
    {
        ANativeWindow_release(image_reader_surface);
        image_reader_surface = 0;
    }
}

int NdkCamera::open(int _camera_facing)
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "open");

    camera_facing = _camera_facing;

    camera_manager = ACameraManager_create();

    // find front camera
    std::string camera_id;
    {
        ACameraIdList* camera_id_list = 0;
        ACameraManager_getCameraIdList(camera_manager, &camera_id_list);

        for (int i = 0; i < camera_id_list->numCameras; ++i)
        {
            const char* id = camera_id_list->cameraIds[i];
            ACameraMetadata* camera_metadata = 0;
            ACameraManager_getCameraCharacteristics(camera_manager, id, &camera_metadata);

            // query faceing
            acamera_metadata_enum_android_lens_facing_t facing = ACAMERA_LENS_FACING_FRONT;
            {
                ACameraMetadata_const_entry e = { 0 };
                ACameraMetadata_getConstEntry(camera_metadata, ACAMERA_LENS_FACING, &e);
                facing = (acamera_metadata_enum_android_lens_facing_t)e.data.u8[0];
            }

            if (camera_facing == 0 && facing != ACAMERA_LENS_FACING_FRONT)
            {
                ACameraMetadata_free(camera_metadata);
                continue;
            }

            if (camera_facing == 1 && facing != ACAMERA_LENS_FACING_BACK)
            {
                ACameraMetadata_free(camera_metadata);
                continue;
            }

            camera_id = id;

            // query orientation
            int orientation = 0;
            {
                ACameraMetadata_const_entry e = { 0 };
                ACameraMetadata_getConstEntry(camera_metadata, ACAMERA_SENSOR_ORIENTATION, &e);

                orientation = (int)e.data.i32[0];
            }

            camera_orientation = orientation;

            ACameraMetadata_free(camera_metadata);

            break;
        }

        ACameraManager_deleteCameraIdList(camera_id_list);
    }

    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "open %s %d", camera_id.c_str(), camera_orientation);

    // open camera
    {
        ACameraDevice_StateCallbacks camera_device_state_callbacks;
        camera_device_state_callbacks.context = this;
        camera_device_state_callbacks.onDisconnected = onDisconnected;
        camera_device_state_callbacks.onError = onError;

        ACameraManager_openCamera(camera_manager, camera_id.c_str(), &camera_device_state_callbacks, &camera_device);
    }

    // capture request
    {
        ACameraDevice_createCaptureRequest(camera_device, TEMPLATE_PREVIEW, &capture_request);

        ACameraOutputTarget_create(image_reader_surface, &image_reader_target);
        ACaptureRequest_addTarget(capture_request, image_reader_target);
    }

    // capture session
    {
        ACameraCaptureSession_stateCallbacks camera_capture_session_state_callbacks;
        camera_capture_session_state_callbacks.context = this;
        camera_capture_session_state_callbacks.onActive = onSessionActive;
        camera_capture_session_state_callbacks.onReady = onSessionReady;
        camera_capture_session_state_callbacks.onClosed = onSessionClosed;

        ACaptureSessionOutputContainer_create(&capture_session_output_container);

        ACaptureSessionOutput_create(image_reader_surface, &capture_session_output);

        ACaptureSessionOutputContainer_add(capture_session_output_container, capture_session_output);

        ACameraDevice_createCaptureSession(camera_device, capture_session_output_container, &camera_capture_session_state_callbacks, &capture_session);

        ACameraCaptureSession_captureCallbacks camera_capture_session_capture_callbacks;
        camera_capture_session_capture_callbacks.context = this;
        camera_capture_session_capture_callbacks.onCaptureStarted = 0;
        camera_capture_session_capture_callbacks.onCaptureProgressed = 0;
        camera_capture_session_capture_callbacks.onCaptureCompleted = onCaptureCompleted;
        camera_capture_session_capture_callbacks.onCaptureFailed = onCaptureFailed;
        camera_capture_session_capture_callbacks.onCaptureSequenceCompleted = onCaptureSequenceCompleted;
        camera_capture_session_capture_callbacks.onCaptureSequenceAborted = onCaptureSequenceAborted;
        camera_capture_session_capture_callbacks.onCaptureBufferLost = 0;

        ACameraCaptureSession_setRepeatingRequest(capture_session, &camera_capture_session_capture_callbacks, 1, &capture_request, nullptr);
    }

    return 0;
}

void NdkCamera::close()
{
    __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "close");

    if (capture_session)
    {
        ACameraCaptureSession_stopRepeating(capture_session);
        ACameraCaptureSession_close(capture_session);
        capture_session = 0;
    }

    if (camera_device)
    {
        ACameraDevice_close(camera_device);
        camera_device = 0;
    }

    if (capture_session_output_container)
    {
        ACaptureSessionOutputContainer_free(capture_session_output_container);
        capture_session_output_container = 0;
    }

    if (capture_session_output)
    {
        ACaptureSessionOutput_free(capture_session_output);
        capture_session_output = 0;
    }

    if (capture_request)
    {
        ACaptureRequest_free(capture_request);
        capture_request = 0;
    }

    if (image_reader_target)
    {
        ACameraOutputTarget_free(image_reader_target);
        image_reader_target = 0;
    }

    if (camera_manager)
    {
        ACameraManager_delete(camera_manager);
        camera_manager = 0;
    }
}

void NdkCamera::on_image(const cv::Mat& rgb) const
{
}

void NdkCamera::on_image(const unsigned char* nv21, int nv21_width, int nv21_height) const
{
    // rotate nv21
    int w = 0;
    int h = 0;
    int rotate_type = 0;
    {
        if (camera_orientation == 0)
        {
            w = nv21_width;
            h = nv21_height;
            rotate_type = camera_facing == 0 ? 2 : 1;
        }
        if (camera_orientation == 90)
        {
            w = nv21_height;
            h = nv21_width;
            rotate_type = camera_facing == 0 ? 5 : 6;
        }
        if (camera_orientation == 180)
        {
            w = nv21_width;
            h = nv21_height;
            rotate_type = camera_facing == 0 ? 4 : 3;
        }
        if (camera_orientation == 270)
        {
            w = nv21_height;
            h = nv21_width;
            rotate_type = camera_facing == 0 ? 7 : 8;
        }
    }

    cv::Mat nv21_rotated(h + h / 2, w, CV_8UC1);
    ncnn::kanna_rotate_yuv420sp(nv21, nv21_width, nv21_height, nv21_rotated.data, w, h, rotate_type);

    // nv21_rotated to rgb
    cv::Mat rgb(h, w, CV_8UC3);
    ncnn::yuv420sp2rgb(nv21_rotated.data, w, h, rgb.data);

    on_image(rgb);
}

static const int NDKCAMERAWINDOW_ID = 233;

NdkCameraWindow::NdkCameraWindow() : NdkCamera()
{
    sensor_manager = 0;
    sensor_event_queue = 0;
    accelerometer_sensor = 0;
    win = 0;

    accelerometer_orientation = 0;

    // sensor
    sensor_manager = ASensorManager_getInstance();

    accelerometer_sensor = ASensorManager_getDefaultSensor(sensor_manager, ASENSOR_TYPE_ACCELEROMETER);

    // recording
    recording_active = false;
    recording_thread = nullptr;
    video_encoder = nullptr;
    audio_encoder = nullptr;
    media_muxer = nullptr;
    video_track_index = -1;
    audio_track_index = -1;
    muxer_started = false;
}

NdkCameraWindow::~NdkCameraWindow()
{
    // stop recording if active
    if (recording_active) {
        stopRecording();
    }

    if (accelerometer_sensor)
    {
        ASensorEventQueue_disableSensor(sensor_event_queue, accelerometer_sensor);
        accelerometer_sensor = 0;
    }

    if (sensor_event_queue)
    {
        ASensorManager_destroyEventQueue(sensor_manager, sensor_event_queue);
        sensor_event_queue = 0;
    }

    if (win)
    {
        ANativeWindow_release(win);
    }
}

void NdkCameraWindow::set_window(ANativeWindow* _win)
{
    if (win)
    {
        ANativeWindow_release(win);
    }

    win = _win;
    ANativeWindow_acquire(win);
}

void NdkCameraWindow::on_image_render(cv::Mat& rgb) const
{
}

void NdkCameraWindow::on_image(const unsigned char* nv21, int nv21_width, int nv21_height) const
{
    // resolve orientation from camera_orientation and accelerometer_sensor
    {
        if (!sensor_event_queue)
        {
            sensor_event_queue = ASensorManager_createEventQueue(sensor_manager, ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS), NDKCAMERAWINDOW_ID, 0, 0);

            ASensorEventQueue_enableSensor(sensor_event_queue, accelerometer_sensor);
        }

        int id = ALooper_pollOnce(0, 0, 0, 0);
        if (id == NDKCAMERAWINDOW_ID)
        {
            ASensorEvent e[8];
            ssize_t num_event = 0;
            while (ASensorEventQueue_hasEvents(sensor_event_queue) == 1)
            {
                num_event = ASensorEventQueue_getEvents(sensor_event_queue, e, 8);
                if (num_event < 0)
                    break;
            }

            if (num_event > 0)
            {
                float acceleration_x = e[num_event - 1].acceleration.x;
                float acceleration_y = e[num_event - 1].acceleration.y;
                float acceleration_z = e[num_event - 1].acceleration.z;
//                 __android_log_print(ANDROID_LOG_WARN, "NdkCameraWindow", "x = %f, y = %f, z = %f", x, y, z);

                if (acceleration_y > 7)
                {
                    accelerometer_orientation = 0;
                }
                if (acceleration_x < -7)
                {
                    accelerometer_orientation = 90;
                }
                if (acceleration_y < -7)
                {
                    accelerometer_orientation = 180;
                }
                if (acceleration_x > 7)
                {
                    accelerometer_orientation = 270;
                }
            }
        }
    }

    // roi crop and rotate nv21
    int nv21_roi_x = 0;
    int nv21_roi_y = 0;
    int nv21_roi_w = 0;
    int nv21_roi_h = 0;
    int roi_x = 0;
    int roi_y = 0;
    int roi_w = 0;
    int roi_h = 0;
    int rotate_type = 0;
    int render_w = 0;
    int render_h = 0;
    int render_rotate_type = 0;
    {
        int win_w = ANativeWindow_getWidth(win);
        int win_h = ANativeWindow_getHeight(win);

        if (accelerometer_orientation == 90 || accelerometer_orientation == 270)
        {
            std::swap(win_w, win_h);
        }

        const int final_orientation = (camera_orientation + accelerometer_orientation) % 360;

        if (final_orientation == 0 || final_orientation == 180)
        {
            if (win_w * nv21_height > win_h * nv21_width)
            {
                roi_w = nv21_width;
                roi_h = (nv21_width * win_h / win_w) / 2 * 2;
                roi_x = 0;
                roi_y = ((nv21_height - roi_h) / 2) / 2 * 2;
            }
            else
            {
                roi_h = nv21_height;
                roi_w = (nv21_height * win_w / win_h) / 2 * 2;
                roi_x = ((nv21_width - roi_w) / 2) / 2 * 2;
                roi_y = 0;
            }

            nv21_roi_x = roi_x;
            nv21_roi_y = roi_y;
            nv21_roi_w = roi_w;
            nv21_roi_h = roi_h;
        }
        if (final_orientation == 90 || final_orientation == 270)
        {
            if (win_w * nv21_width > win_h * nv21_height)
            {
                roi_w = nv21_height;
                roi_h = (nv21_height * win_h / win_w) / 2 * 2;
                roi_x = 0;
                roi_y = ((nv21_width - roi_h) / 2) / 2 * 2;
            }
            else
            {
                roi_h = nv21_width;
                roi_w = (nv21_width * win_w / win_h) / 2 * 2;
                roi_x = ((nv21_height - roi_w) / 2) / 2 * 2;
                roi_y = 0;
            }

            nv21_roi_x = roi_y;
            nv21_roi_y = roi_x;
            nv21_roi_w = roi_h;
            nv21_roi_h = roi_w;
        }

        if (camera_facing == 0)
        {
            if (camera_orientation == 0 && accelerometer_orientation == 0)
            {
                rotate_type = 2;
            }
            if (camera_orientation == 0 && accelerometer_orientation == 90)
            {
                rotate_type = 7;
            }
            if (camera_orientation == 0 && accelerometer_orientation == 180)
            {
                rotate_type = 4;
            }
            if (camera_orientation == 0 && accelerometer_orientation == 270)
            {
                rotate_type = 5;
            }
            if (camera_orientation == 90 && accelerometer_orientation == 0)
            {
                rotate_type = 5;
            }
            if (camera_orientation == 90 && accelerometer_orientation == 90)
            {
                rotate_type = 2;
            }
            if (camera_orientation == 90 && accelerometer_orientation == 180)
            {
                rotate_type = 7;
            }
            if (camera_orientation == 90 && accelerometer_orientation == 270)
            {
                rotate_type = 4;
            }
            if (camera_orientation == 180 && accelerometer_orientation == 0)
            {
                rotate_type = 4;
            }
            if (camera_orientation == 180 && accelerometer_orientation == 90)
            {
                rotate_type = 5;
            }
            if (camera_orientation == 180 && accelerometer_orientation == 180)
            {
                rotate_type = 2;
            }
            if (camera_orientation == 180 && accelerometer_orientation == 270)
            {
                rotate_type = 7;
            }
            if (camera_orientation == 270 && accelerometer_orientation == 0)
            {
                rotate_type = 7;
            }
            if (camera_orientation == 270 && accelerometer_orientation == 90)
            {
                rotate_type = 4;
            }
            if (camera_orientation == 270 && accelerometer_orientation == 180)
            {
                rotate_type = 5;
            }
            if (camera_orientation == 270 && accelerometer_orientation == 270)
            {
                rotate_type = 2;
            }
        }
        else
        {
            if (final_orientation == 0)
            {
                rotate_type = 1;
            }
            if (final_orientation == 90)
            {
                rotate_type = 6;
            }
            if (final_orientation == 180)
            {
                rotate_type = 3;
            }
            if (final_orientation == 270)
            {
                rotate_type = 8;
            }
        }

        if (accelerometer_orientation == 0)
        {
            render_w = roi_w;
            render_h = roi_h;
            render_rotate_type = 1;
        }
        if (accelerometer_orientation == 90)
        {
            render_w = roi_h;
            render_h = roi_w;
            render_rotate_type = 8;
        }
        if (accelerometer_orientation == 180)
        {
            render_w = roi_w;
            render_h = roi_h;
            render_rotate_type = 3;
        }
        if (accelerometer_orientation == 270)
        {
            render_w = roi_h;
            render_h = roi_w;
            render_rotate_type = 6;
        }
    }

    // crop and rotate nv21
    cv::Mat nv21_croprotated(roi_h + roi_h / 2, roi_w, CV_8UC1);
    {
        const unsigned char* srcY = nv21 + nv21_roi_y * nv21_width + nv21_roi_x;
        unsigned char* dstY = nv21_croprotated.data;
        ncnn::kanna_rotate_c1(srcY, nv21_roi_w, nv21_roi_h, nv21_width, dstY, roi_w, roi_h, roi_w, rotate_type);

        const unsigned char* srcUV = nv21 + nv21_width * nv21_height + nv21_roi_y * nv21_width / 2 + nv21_roi_x;
        unsigned char* dstUV = nv21_croprotated.data + roi_w * roi_h;
        ncnn::kanna_rotate_c2(srcUV, nv21_roi_w / 2, nv21_roi_h / 2, nv21_width, dstUV, roi_w / 2, roi_h / 2, roi_w, rotate_type);
    }

    // nv21_croprotated to rgb
    cv::Mat rgb(roi_h, roi_w, CV_8UC3);
    ncnn::yuv420sp2rgb(nv21_croprotated.data, roi_w, roi_h, rgb.data);

    on_image_render(rgb);

    // rotate to native window orientation
    cv::Mat rgb_render(render_h, render_w, CV_8UC3);
    ncnn::kanna_rotate_c3(rgb.data, roi_w, roi_h, rgb_render.data, render_w, render_h, render_rotate_type);

    ANativeWindow_setBuffersGeometry(win, render_w, render_h, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM);

    ANativeWindow_Buffer buf;
    ANativeWindow_lock(win, &buf, NULL);

    // scale to target size
    if (buf.format == AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM || buf.format == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM)
    {
        for (int y = 0; y < render_h; y++)
        {
            const unsigned char* ptr = rgb_render.ptr<const unsigned char>(y);
            unsigned char* outptr = (unsigned char*)buf.bits + buf.stride * 4 * y;

            int x = 0;
#if __ARM_NEON
            for (; x + 7 < render_w; x += 8)
            {
                uint8x8x3_t _rgb = vld3_u8(ptr);
                uint8x8x4_t _rgba;
                _rgba.val[0] = _rgb.val[0];
                _rgba.val[1] = _rgb.val[1];
                _rgba.val[2] = _rgb.val[2];
                _rgba.val[3] = vdup_n_u8(255);
                vst4_u8(outptr, _rgba);

                ptr += 24;
                outptr += 32;
            }
#endif // __ARM_NEON
            for (; x < render_w; x++)
            {
                outptr[0] = ptr[0];
                outptr[1] = ptr[1];
                outptr[2] = ptr[2];
                outptr[3] = 255;

                ptr += 3;
                outptr += 4;
            }
        }
    }

    ANativeWindow_unlockAndPost(win);
}

// Recording functions implementation
bool NdkCameraWindow::startRecording(const char* filepath) {
    std::lock_guard<std::mutex> lock(recording_mutex);
    
    if (recording_active) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Recording already active");
        return false;
    }
    
    // 保存文件路径
    recording_filepath = filepath;
    
    // 创建MediaMuxer - 使用文件描述符
    int fd = ::open(recording_filepath.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
    if (fd < 0) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to open file: %s", recording_filepath.c_str());
        return false;
    }
    
    media_muxer = AMediaMuxer_new(fd, AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4);
    if (!media_muxer) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to create media muxer");
        ::close(fd);
        return false;
    }
    
    // 初始化编码器
    if (!setup_video_encoder(640, 480, 30)) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to setup video encoder");
        AMediaMuxer_delete(media_muxer);
        media_muxer = nullptr;
        ::close(fd);
        return false;
    }
    
    if (!setup_audio_encoder()) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to setup audio encoder");
        AMediaCodec_delete(video_encoder);
        video_encoder = nullptr;
        AMediaMuxer_delete(media_muxer);
        media_muxer = nullptr;
        ::close(fd);
        return false;
    }
    
    recording_active = true;
    video_track_index = -1;
    audio_track_index = -1;
    muxer_started = false;
    
    // 清空帧队列
    while (!video_frame_queue.empty()) {
        video_frame_queue.pop();
    }
    
    // 创建录制线程
    recording_thread = new std::thread(&NdkCameraWindow::recording_worker, this);
    
    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Recording started: %s", filepath);
    return true;
}

bool NdkCameraWindow::stopRecording() {
    {
        std::lock_guard<std::mutex> lock(recording_mutex);
        if (!recording_active) {
            __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "Recording not active");
            return false;
        }
        recording_active = false;
        __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Recording flag set to false");
    }
    
    // 等待录制线程结束
    if (recording_thread && recording_thread->joinable()) {
        __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Waiting for recording thread to finish...");
        recording_thread->join();
        delete recording_thread;
        recording_thread = nullptr;
        __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Recording thread finished");
    }
    
    // 清理资源
    if (media_muxer) {
        __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Stopping media muxer");
        if (muxer_started) {
            AMediaMuxer_stop(media_muxer);
        }
        AMediaMuxer_delete(media_muxer);
        media_muxer = nullptr;
    }
    
    if (video_encoder) {
        __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Stopping video encoder");
        AMediaCodec_stop(video_encoder);
        AMediaCodec_delete(video_encoder);
        video_encoder = nullptr;
    }
    
    if (audio_encoder) {
        __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Stopping audio encoder");
        AMediaCodec_stop(audio_encoder);
        AMediaCodec_delete(audio_encoder);
        audio_encoder = nullptr;
    }
    
    // 清空帧队列
    {
        std::lock_guard<std::mutex> lock(recording_mutex);
        while (!video_frame_queue.empty()) {
            video_frame_queue.pop();
        }
    }
    
    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Recording stopped successfully");
    return true;
}

bool NdkCameraWindow::isRecording() const {
    std::lock_guard<std::mutex> lock(const_cast<std::mutex&>(recording_mutex));
    return recording_active;
}

void NdkCameraWindow::recording_worker() {
    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Recording worker started");
    
    int64_t start_time = get_current_timestamp();
    int frame_count = 0;
    
    // 录制工作线程
    while (true) {
        {
            std::lock_guard<std::mutex> lock(recording_mutex);
            if (!recording_active) {
                break;
            }
        }
        
        // 处理视频帧队列
        std::vector<uint8_t> frame;
        bool has_frame = false;
        {
            std::lock_guard<std::mutex> lock(recording_mutex);
            if (!video_frame_queue.empty()) {
                frame = video_frame_queue.front();
                video_frame_queue.pop();
                has_frame = true;
                frame_count++;
            }
        }
        
        if (has_frame && video_encoder) {
            // 编码视频帧
            encode_video_frame_data(frame.data(), frame.size());
        }
        
        // 处理编码器输出
        process_encoder_output();
        
        // 记录录制时长
        int64_t current_time = get_current_timestamp();
        int64_t elapsed_seconds = (current_time - start_time) / 1000000;
        
        // 每5秒记录一次状态
        if (elapsed_seconds > 0 && elapsed_seconds % 5 == 0) {
            __android_log_print(ANDROID_LOG_INFO, "NdkCamera", 
                              "Recording: %ld seconds, %d frames processed", 
                              elapsed_seconds, frame_count);
        }
        
        // 短暂休眠避免过度占用CPU
        usleep(10000); // 10ms
    }
    
    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Recording worker stopping");
    
    // 录制结束时,处理剩余的编码器输出
    if (video_encoder) {
        process_encoder_output_final();
    }
    
    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Recording worker finished, total frames: %d", frame_count);
}

bool NdkCameraWindow::setup_video_encoder(int width, int height, int fps) {
    // 创建视频编码器
    const char* mime_type = "video/avc";
    video_encoder = AMediaCodec_createEncoderByType(mime_type);
    if (!video_encoder) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to create video encoder");
        return false;
    }
    
    // 配置视频格式
    AMediaFormat* format = AMediaFormat_new();
    AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, mime_type);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, width);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, height);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_FRAME_RATE, fps);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, 1);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, 2000000);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT, 
                         21); // COLOR_FormatYUV420SemiPlanar
    
    // 配置编码器
    media_status_t status = AMediaCodec_configure(video_encoder, format, 
                                                nullptr, nullptr, 
                                                AMEDIACODEC_CONFIGURE_FLAG_ENCODE);
    AMediaFormat_delete(format);
    
    if (status != AMEDIA_OK) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to configure video encoder: %d", status);
        AMediaCodec_delete(video_encoder);
        video_encoder = nullptr;
        return false;
    }
    
    // 启动编码器
    status = AMediaCodec_start(video_encoder);
    if (status != AMEDIA_OK) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to start video encoder: %d", status);
        AMediaCodec_delete(video_encoder);
        video_encoder = nullptr;
        return false;
    }
    
    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Video encoder setup: %dx%d %dfps", width, height, fps);
    return true;
}

bool NdkCameraWindow::setup_audio_encoder() {
    // 创建音频编码器
    const char* mime_type = "audio/mp4a-latm";
    audio_encoder = AMediaCodec_createEncoderByType(mime_type);
    if (!audio_encoder) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to create audio encoder");
        return false;
    }
    
    // 配置音频格式
    AMediaFormat* format = AMediaFormat_new();
    AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, mime_type);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, 44100);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, 1);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, 128000);
    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_AAC_PROFILE, 2); // AAC LC
    
    // 配置编码器
    media_status_t status = AMediaCodec_configure(audio_encoder, format, 
                                                nullptr, nullptr, 
                                                AMEDIACODEC_CONFIGURE_FLAG_ENCODE);
    AMediaFormat_delete(format);
    
    if (status != AMEDIA_OK) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to configure audio encoder: %d", status);
        AMediaCodec_delete(audio_encoder);
        audio_encoder = nullptr;
        return false;
    }
    
    // 启动编码器
    status = AMediaCodec_start(audio_encoder);
    if (status != AMEDIA_OK) {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to start audio encoder: %d", status);
        AMediaCodec_delete(audio_encoder);
        audio_encoder = nullptr;
        return false;
    }
    
    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Audio encoder setup");
    return true;
}

void NdkCameraWindow::encode_video_frame(const unsigned char* nv21_data, int width, int height, int64_t timestamp_us) {
    if (!video_encoder) {
        return;
    }
    
    // 检查录制状态
    {
        std::lock_guard<std::mutex> lock(recording_mutex);
        if (!recording_active) {
            return;
        }
    }
    
    // 限制队列大小,避免内存溢出
    const size_t max_queue_size = 30; // 最多缓存30帧
    
    std::lock_guard<std::mutex> lock(recording_mutex);
    if (video_frame_queue.size() >= max_queue_size) {
        // 队列已满,丢弃最旧的一帧
        video_frame_queue.pop();
        __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "Video frame queue full, dropping frame");
    }
    
    // 将视频帧添加到队列供录制线程处理
    std::vector<uint8_t> frame_data(nv21_data, nv21_data + width * height * 3 / 2);
    video_frame_queue.push(std::move(frame_data));
}

void NdkCameraWindow::encode_video_frame_data(const uint8_t* data, size_t size) {
    if (!video_encoder || !recording_active) {
        return;
    }
    
    // 获取输入缓冲区
    ssize_t input_index = AMediaCodec_dequeueInputBuffer(video_encoder, 10000);
    if (input_index >= 0) {
        size_t buffer_size;
        uint8_t* buffer = AMediaCodec_getInputBuffer(video_encoder, input_index, &buffer_size);
        
        if (buffer && buffer_size >= size) {
            // 复制数据到缓冲区
            memcpy(buffer, data, size);
            
            // 提交缓冲区给编码器
            media_status_t status = AMediaCodec_queueInputBuffer(video_encoder, input_index, 
                                                               0, size, 
                                                               get_current_timestamp(), 0);
            if (status != AMEDIA_OK) {
                __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to queue input buffer: %d", status);
            } else {
                __android_log_print(ANDROID_LOG_DEBUG, "NdkCamera", "Queued input buffer: index=%zd, size=%zu", 
                                  input_index, size);
            }
        } else {
            __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "Input buffer too small: need=%zu, have=%zu", 
                              size, buffer_size);
        }
    } else if (input_index == AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
        __android_log_print(ANDROID_LOG_DEBUG, "NdkCamera", "No input buffer available, try again later");
    } else {
        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to get input buffer: %zd", input_index);
    }
}

void NdkCameraWindow::process_encoder_output() {
    if (!video_encoder || !recording_active) {
        return;
    }
    
    // 处理输出缓冲区
    AMediaCodecBufferInfo info;
    ssize_t output_index = AMediaCodec_dequeueOutputBuffer(video_encoder, &info, 10000); // 10ms超时
    while (output_index >= 0) {
        if (info.flags & AMEDIACODEC_BUFFER_FLAG_CODEC_CONFIG) {
            // 配置数据
            if (!muxer_started && media_muxer) {
                // 添加视频轨道
                AMediaFormat* format = AMediaCodec_getOutputFormat(video_encoder);
                if (format) {
                    video_track_index = AMediaMuxer_addTrack(media_muxer, format);
                    AMediaFormat_delete(format);
                    
                    __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "Video track added: %d", video_track_index);
                    
                    // 如果音频轨道也已准备好,启动混合器
                    if (audio_track_index >= 0 || !audio_encoder) {
                        media_status_t status = AMediaMuxer_start(media_muxer);
                        if (status == AMEDIA_OK) {
                            muxer_started = true;
                            __android_log_print(ANDROID_LOG_INFO, "NdkCamera", "MediaMuxer started");
                        } else {
                            __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to start MediaMuxer: %d", status);
                        }
                    }
                }
            }
        } else if (info.size > 0) {
            // 编码数据
            if (muxer_started && media_muxer) {
                size_t buffer_size;
                uint8_t* buffer = AMediaCodec_getOutputBuffer(video_encoder, output_index, &buffer_size);
                if (buffer) {
                    media_status_t status = AMediaMuxer_writeSampleData(media_muxer, video_track_index, buffer, &info);
                    if (status != AMEDIA_OK) {
                        __android_log_print(ANDROID_LOG_ERROR, "NdkCamera", "Failed to write sample data: %d", status);
                    } else {
                        __android_log_print(ANDROID_LOG_DEBUG, "NdkCamera", "Wrote sample data: size=%d, pts=%ld", 
                                          info.size, info.presentationTimeUs);
                    }
                }
            }
        }
        
        AMediaCodec_releaseOutputBuffer(video_encoder, output_index, false);
        output_index = AMediaCodec_dequeueOutputBuffer(video_encoder, &info, 10000);
    }
}

void NdkCameraWindow::process_encoder_output_final() {
    if (!video_encoder) {
        return;
    }
    
    // 处理所有剩余的编码器输出
    AMediaCodecBufferInfo info;
    ssize_t output_index = AMediaCodec_dequeueOutputBuffer(video_encoder, &info, 1000000); // 1秒超时
    while (output_index >= 0) {
        if (info.size > 0 && muxer_started && media_muxer) {
            size_t buffer_size;
            uint8_t* buffer = AMediaCodec_getOutputBuffer(video_encoder, output_index, &buffer_size);
            if (buffer) {
                AMediaMuxer_writeSampleData(media_muxer, video_track_index, buffer, &info);
            }
        }
        
        AMediaCodec_releaseOutputBuffer(video_encoder, output_index, false);
        output_index = AMediaCodec_dequeueOutputBuffer(video_encoder, &info, 1000000);
    }
}

int64_t NdkCameraWindow::get_current_timestamp() {
    struct timeval tv;
    gettimeofday(&tv, nullptr);
    return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}