Insta360 Desktop CameraSDK (C++)

repository·master·Indexed 41 days ago

https://github.com/insta360develop/desktop-camerasdk-cpp

A C++ library for enterprise developers to control Insta360 panoramic cameras (including X3, X4, and X5) via USB. The SDK enables remote operation, parameter configuration, file management, firmware updates, and access to preview streams for video, audio, and gyro data. Supported platforms include Windows x86_64 and Linux (x86_64 and AArch64).

Tokens
18.2K
Snippets
51
Records
65
Agent score
51%

What's inside Insta360 CameraSDK

  1. Overview of CameraSDK capabilities

    master

    CameraSDK is designed for enterprise users to interact with Insta360 panoramic cameras via USB.

    Core Capabilities:

    • Connecting to cameras and managing connections.
    • Configuring and retrieving camera parameters.
    • Controlling camera operations (photo capture, video recording).
    • Downloading files.
    • Upgrading firmware (supported for X4 and later models only).

    Supported Camera Models:

    • ONE X (Discontinued)
    • ONE R Twin Edition (Discontinued)
    • ONE X2
    • ONE RS 1-Inch 360 Edition (Discontinued)
    • X3
    • X4
    • X4 Air
    • X5

    Supported Platforms:

    • Windows x86_64: Windows 7 or later.
    • Linux x86_64: Ubuntu 22.04.
    • Linux AArch64 (ARM64): Built with ARM GNU, Linaro, and NVIDIA Jetson official toolchains.
  2. Manage camera connection lifecycle

    master

    The connection lifecycle involves creating a camera instance from a DeviceDescriptor, opening the connection, and closing it when finished.

    1. Create Instance: Use ins_camera::Camera with the DeviceConnectionInfo from a discovered device. 2. Open Camera: Call Open() to establish the connection. 3. Check Status: Use IsConnected() to verify the connection (avoid calling this while the camera is switching modes). 4. Close Camera: Call Close() to disconnect.

    Important: If you perform a firmware upgrade, you must call Close(), then recreate the camera instance after the upgrade completes.

    // 1. Create Instance
    auto camera_info = list[0].info;
    auto camera = std::make_shared<ins_camera::Camera>(camera_info);
    
    // 2. Open Camera
    bool success = camera->Open();
    if(!success) {
        std::cout << "failed to open camera" << std::endl;
        return -1;
    }
    
    // 3. Check Status
    // camera->IsConnected();
    
    // 4. Disconnect
    camera->Close();
  3. Implement the StreamDelegate for preview streams

    master

    To access raw video, audio, gyro, or exposure data, you must inherit from ins_camera::StreamDelegate and override its virtual methods.

    Video Stream Details:

    • For resolutions below 5.7K, there is a single video stream.
    • For resolutions 5.7K or higher, there are two video streams (representing two fisheye images). The data is not stitched.
    • The encoding format (H.264 or H.265) can be identified via GetVideoEncodeType.
    • The recommended preview resolution is 1920×960.
    class TestStreamDelegate : public ins_camera::StreamDelegate {
    public:
        TestStreamDelegate() {}
    
        // Callback for audio data
        void OnAudioData(const uint8_t* data, size_t size, int64_t timestamp) override {}
    
        // Callback for video data
        void OnVideoData(const uint8_t* data, size_t size, int64_t timestamp, uint8_t streamType, int stream_index) override {
            if (stream_index == 0) {
                // First video stream
            }
            if (stream_index == 1) {
                // Second video stream
            }
        }
    
        // Callback for gyro data
        void OnGyroData(const std::vector<ins_camera::GyroData>& data) override {}
    
        // Callback for exposure data
        void OnExposureData(const ins_camera::ExposureData& data) override {}
    };
  4. Configure White Balance Parameters

    master

    White balance is managed through the CaptureSettings interface. To change the white balance, retrieve the current CaptureSettings for a mode, update the value, and apply it back.

    Workflow:

    1. Call GetCaptureSettings(mode) to retrieve the settings.
    2. Use SetWhiteBalance() with a value from PhotographyOptions_WhiteBalance.
    3. Call SetCaptureSettings(mode, settings) to apply.
    // 1. Get White Balance parameters
    const auto funtion_mode = ins_camera::CameraFunctionMode::FUNCTION_MODE_NORMAL_IMAGE;
    auto capture_setting = camera->GetCaptureSettings(funtion_mode);
    
    // 2. Set White Balance parameters
    capture_setting->SetWhiteBalance(ins_camera::PhotographyOptions_WhiteBalance::WB_6500K);
    
    // 3. Apply the parameters to the camera
    camera->SetCaptureSettings(funtion_mode, capture_setting);
  5. Upgrade Firmware (X4 and later only)

    master

    Firmware upgrades are supported only for X4 and later cameras. Use the UploadFile interface to upload the firmware binary.

    Process:

    1. Identify the correct firmware filename (e.g., Insta360X4FW.bin or Insta360X5FW.bin).
    2. Call UploadFile with the local path and the firmware name, providing a progress callback.
    3. Crucial: After a successful upload, you must call camera->close() and power off the camera. The camera must restart to complete the upgrade.
    const std::string local_file = "/path/to/firmware/Insta360X4FW.bin";
    const std::string fireware_name = "Insta360X4FW.bin";
    
    // Start uploading
    bool ret = cam->UploadFile(local_file, fireware_name, [](int64_t current, int64_t total_size) {
        std::cout << "current :" << current << ";total_size: " << total_size << std::endl;
    });
    
    if (ret) {
        std::cout << "succeeded to upload file" << std::endl;
        // Must close and power off after success
        camera->close();
    }
  6. Install drivers for Linux and Windows

    master

    Linux

    Ensure libusb and libudev are installed. You can use apt-get or build from source.

    Using apt-get:

    sudo apt-get install libusb-dev
    sudo apt-get install libudev-dev

    Building from source:

    wget http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.9/libusb-1.0.9.tar.bz2
    tar xjf libusb-1.0.9.tar.bz2
    cd libusb-1.0.9
    ./configure 
    make
    sudo make install

    Verification: Run lsusb. If you see any device with Vendor ID 0x2e1a, the driver is installed correctly.

    Note: On Linux, demonstration programs must be run with sudo:

    sudo ./CameraSDKDemo

    Windows

    Ensure the libusbK driver is installed. You can install it directly or use Zadig to assist with the installation.

  7. Upgrade camera firmware

    master

    Firmware upgrades are supported for X4 and later models.

    1. Identify the correct firmware filename (e.g., Insta360X4FW.bin or Insta360X5FW.bin).
    2. Use UploadFile(local_path, firmware_name, callback) to upload the firmware. The callback tracks upload progress.
    3. Crucial: After a successful upload, you must call camera->close() and wait for the camera to restart to complete the upgrade process.
    // Example for X4
    const std::string fireware_name = "Insta360X4FW.bin";
    const std::string local_file = "/path/to/firmware/Insta360X4FW.bin";
    
    bool ret = cam->UploadFile(local_file, fireware_name, [](int64_t current, int64_t total_size) {
        std::cout << "current :" << current << "; total_size: " << total_size << std::endl;
    });
    
    if (ret) {
        std::cout << "succeed to Upload file !!!" << std::endl;
        // Must close camera to allow upgrade to complete
        camera->close();
    }
  8. Manage camera files (List, Download, Delete)

    master

    List Files

    • GetCameraFilesCount(): Returns the number of files on the SD card.
    • GetCameraFilesList(): Returns a list of all file paths on the SD card.
    • GetRecordingFiles(): Returns the names of files currently being recorded.

    Download Files

    Use DownloadCameraFile(camera_path, local_path, callback) to download files.

    • The callback provides current and total_size for progress tracking.
    • Important: Ensure the local directory exists before downloading.
    • Port Conflict: The SDK starts an HTTP server (default port 9099). If the port is occupied, use SetServicePort before calling Open to change it.
    • Use CancelDownload() to abort an ongoing download.

    Delete Files

    Use DeleteCameraFile(camera_path) to remove a file from the SD card.

    // List files
    auto file_list = camera->GetCameraFilesList();
    for(const auto& file : file_list) {
        std::cout << file << std::endl;
    }
    
    // Download file with progress
    std::string camera_file = "/DCIM/Camera01/VID_20250122_071405_00_001.insv";
    std::string local_save_file = "/path/to/local/VID_20250122_071405_00_001.insv";
    
    bool ret = camera->DownloadCameraFile(camera_file, local_save_file, [](int64_t current, int64_t total_size) {
        std::cout << "current :" << current << "; total_size: " << total_size << std::endl;
    });
    
    // Delete file
    camera->DeleteCameraFile("/DCIM/Camera01/VID_20250122_071405_00_001.insv");
  9. Perform Timelapse recording

    master

    To perform a timelapse recording:

    1. Switch the camera to timelapse mode using SetVideoSubMode(SubVideoMode::VIDEO_TIMELAPSE).
    2. Set the resolution and frame rate using SetVideoCaptureParams with the appropriate CameraFunctionMode (e.g., FUNCTION_MODE_MOBILE_TIMELAPSE).
    3. Configure timelapse-specific parameters (mode, duration, interval) using SetTimeLapseOption and a TimelapseParam struct.
    4. Start the timelapse with StartTimeLapse(mode).
    5. Stop the timelapse with StopTimeLapse(mode).
    // 1. Switch to timelapse mode
    bool ret = cam->SetVideoSubMode(ins_camera::SubVideoMode::VIDEO_TIMELAPSE);
    if (!ret) {
        std::cout << "change sub mode failed!" << std::endl;
        continue;
    }
    
    // 2. Set resolution and frame rate
    auto function_mode = ins_camera::CameraFunctionMode::FUNCTION_MODE_MOBILE_TIMELAPSE;
    ins_camera::RecordParams record_params;
    record_params.resolution = ins_camera::VideoResolution::RES_3840_3840P30;
    if (!cam->SetVideoCaptureParams(record_params, function_mode )) {
        std::cerr << "failed to set capture settings." << std::endl;
        break;
    }
    
    // 3. Set timelapse parameters
    auto timelapse_mode = ins_camera::CameraTimelapseMode::MOBILE_TIMELAPSE_VIDEO;
    const uint32_t record_duration_s = 10; // 10 seconds duration
    const uint32_t lapseTime_ms = 1000;    // 1 second interval
    ins_camera::TimelapseParam param = { timelapse_mode, 10, 1000, 0 };
    if (!cam->SetTimeLapseOption(param)) {
        std::cerr << "failed to set capture settings." << std::endl;
    }
    else {
    // 4. Start timelapse
        const auto ret = cam->StartTimeLapse(param.mode);
        if (ret) {
            std::cerr << "success!" << std::endl;
        }
        else {
            std::cerr << "failed to start timelapse" << std::endl;
        }
    }
    
    // ... later to stop ...
    auto url = cam->StopTimeLapse(timelapse_mode );
    if (!url.Empty()) {
        auto& origins = url.OriginUrls();
        for (auto& origin_url : origins) {
            std::cout << "url:" << origin_url << std::endl;
        }
    }
  10. Switch camera to Android mode

    master

    By default, Insta360 cameras connect to computers in U-disk mode (acting as a USB storage device). To use the CameraSDK, you must switch the camera to Android mode.

    For ONE X

    1. Upgrade to a special firmware version (available via the developer link in the documentation).
    2. In the camera settings, find the USB option and set it to Android mode.

    For ONE R/RS, ONE X2, X3

    1. Swipe down on the camera screen to enter the main menu.
    2. Go to Settings -> General.
    3. Set USB Mode to Android and U-disk Mode to Android.

    For X4, X5, X4 Air

    1. Connect the USB cable.
    2. When the mode selection interface pops up, select Android mode and wait for the switch to complete.