/* * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of NVIDIA CORPORATION nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include "ArgusHelpers.h" #include "CommonOptions.h" using namespace Argus; using namespace EGLStream; #define TWO_CAM (1) #define EXIT_IF_NULL(val,msg) \ {if (!val) {printf("%s\n",msg); return EXIT_FAILURE;}} #define EXIT_IF_NOT_OK(val,msg) \ {if (val!=Argus::STATUS_OK) {printf("%s\n",msg); return EXIT_FAILURE;}} /* Debug print macros */ #define PRODUCER_PRINT(...) printf("PRODUCER: " __VA_ARGS__) #define CONSUMER_PRINT(...) printf("CONSUMER: " __VA_ARGS__) #ifdef ANDROID #define FILE_PREFIX "/sdcard/DCIM/" #else #define FILE_PREFIX "" #endif /* * Program: oneShot * Function: Capture a single image from a camera device and write to a JPG file * Purpose: To demonstrate the most simplistic approach to getting the Argus Framework * running, submitting a capture request, retrieving the resulting image and * then writing the image as a JPEG formatted file. */ int main(int argc, char** argv) { ArgusSamples::CommonOptions options(basename(argv[0]), ArgusSamples::CommonOptions::Option_D_CameraDevice | ArgusSamples::CommonOptions::Option_M_SensorMode); if (!options.parse(argc, argv)) return EXIT_FAILURE; if (options.requestedExit()) return EXIT_SUCCESS; const uint64_t FIVE_SECONDS_IN_NANOSECONDS = 5000000000; /* * Set up Argus API Framework, identify available camera devices, and create * a capture session for the first available device */ Argus::UniqueObj cameraProvider(Argus::CameraProvider::create()); Argus::ICameraProvider *iCameraProvider = Argus::interface_cast(cameraProvider); EXIT_IF_NULL(iCameraProvider, "Cannot get core camera provider interface"); printf("Argus Version: %s\n", iCameraProvider->getVersion().c_str()); Argus::CameraDevice *device = ArgusSamples::ArgusHelpers::getCameraDevice( cameraProvider.get(), options.cameraDeviceIndex()); /* Get the camera devices */ std::vector cameraDevices; iCameraProvider->getCameraDevices(&cameraDevices); if (cameraDevices.size() == 0) ORIGINATE_ERROR("No cameras available"); Argus::SensorMode* sensorMode = ArgusSamples::ArgusHelpers::getSensorMode( cameraDevices[0], options.sensorModeIndex()); Argus::ISensorMode *iSensorMode = Argus::interface_cast(sensorMode); if (!iSensorMode) { REPORT_ERROR("Failed to get sensor mode interface"); return EXIT_FAILURE; } printf("Capturing from device %d using sensor mode %d (%dx%d)\n", options.cameraDeviceIndex(), options.sensorModeIndex(), iSensorMode->getResolution().width(), iSensorMode->getResolution().height()); Argus::Status status; Argus::UniqueObj captureSession0( iCameraProvider->createCaptureSession(cameraDevices[0], &status)); EXIT_IF_NOT_OK(status, "Failed to create capture session"); #if TWO_CAM Argus::UniqueObj captureSession1( iCameraProvider->createCaptureSession(cameraDevices[1], &status)); EXIT_IF_NOT_OK(status, "Failed to create capture session"); #endif Argus::ICaptureSession *iSession0 = Argus::interface_cast(captureSession0); EXIT_IF_NULL(iSession0, "Cannot get Capture Session Interface"); #if TWO_CAM Argus::ICaptureSession *iSession1 = Argus::interface_cast(captureSession1); EXIT_IF_NULL(iSession1, "Cannot get Capture Session Interface"); #endif /* * Creates the stream between the Argus camera image capturing * sub-system (producer) and the image acquisition code (consumer). A consumer object is * created from the stream to be used to request the image frame. A successfully submitted * capture request activates the stream's functionality to eventually make a frame available * for acquisition. */ Argus::UniqueObj streamSettings0( iSession0->createOutputStreamSettings(Argus::STREAM_TYPE_EGL)); #if TWO_CAM Argus::UniqueObj streamSettings1( iSession1->createOutputStreamSettings(Argus::STREAM_TYPE_EGL)); #endif Argus::IEGLOutputStreamSettings *iEGLStreamSettings0 = Argus::interface_cast(streamSettings0); EXIT_IF_NULL(iEGLStreamSettings0, "Cannot get IEGLOutputStreamSettings Interface"); iEGLStreamSettings0->setPixelFormat(Argus::PIXEL_FMT_RAW16); iEGLStreamSettings0->setResolution(iSensorMode->getResolution()); iEGLStreamSettings0->setMetadataEnable(true); #if TWO_CAM Argus::IEGLOutputStreamSettings *iEGLStreamSettings1 = Argus::interface_cast(streamSettings1); EXIT_IF_NULL(iEGLStreamSettings1, "Cannot get IEGLOutputStreamSettings Interface"); iEGLStreamSettings1->setPixelFormat(Argus::PIXEL_FMT_RAW16); iEGLStreamSettings1->setResolution(iSensorMode->getResolution()); iEGLStreamSettings1->setMetadataEnable(true); #endif Argus::UniqueObj stream0( iSession0->createOutputStream(streamSettings0.get())); EXIT_IF_NULL(stream0, "Failed to create EGLOutputStream"); #if TWO_CAM Argus::UniqueObj stream1( iSession1->createOutputStream(streamSettings1.get())); EXIT_IF_NULL(stream1, "Failed to create EGLOutputStream"); #endif Argus::UniqueObj consumer0( EGLStream::FrameConsumer::create(stream0.get())); #if TWO_CAM Argus::UniqueObj consumer1( EGLStream::FrameConsumer::create(stream1.get())); #endif EGLStream::IFrameConsumer *iFrameConsumer0 = Argus::interface_cast(consumer0); EXIT_IF_NULL(iFrameConsumer0, "Failed to initialize Consumer"); #if TWO_CAM EGLStream::IFrameConsumer *iFrameConsumer1 = Argus::interface_cast(consumer1); EXIT_IF_NULL(iFrameConsumer1, "Failed to initialize Consumer"); #endif Argus::UniqueObj request0( iSession0->createRequest(Argus::CAPTURE_INTENT_PREVIEW)); #if TWO_CAM Argus::UniqueObj request1( iSession1->createRequest(Argus::CAPTURE_INTENT_PREVIEW)); #endif Argus::IRequest *iRequest0 = Argus::interface_cast(request0); EXIT_IF_NULL(iRequest0, "Failed to get capture request interface"); #if TWO_CAM Argus::IRequest *iRequest1 = Argus::interface_cast(request1); EXIT_IF_NULL(iRequest1, "Failed to get capture request interface"); #endif status = iRequest0->enableOutputStream(stream0.get()); EXIT_IF_NOT_OK(status, "Failed to enable stream in capture request"); #if TWO_CAM status = iRequest1->enableOutputStream(stream1.get()); EXIT_IF_NOT_OK(status, "Failed to enable stream in capture request"); #endif Argus::ISourceSettings *iSourceSettings0 = Argus::interface_cast(request0); EXIT_IF_NULL(iSourceSettings0, "Failed to get source settings request interface"); iSourceSettings0->setSensorMode(sensorMode); #if TWO_CAM Argus::ISourceSettings *iSourceSettings1 = Argus::interface_cast(request1); EXIT_IF_NULL(iSourceSettings1, "Failed to get source settings request interface"); iSourceSettings1->setSensorMode(sensorMode); #endif for (uint32_t m = 0; m < 20; m++) { uint32_t requestId0 = iSession0->capture(request0.get()); EXIT_IF_NULL(requestId0, "Failed to submit capture request"); #if TWO_CAM uint32_t requestId1 = iSession1->capture(request1.get()); EXIT_IF_NULL(requestId1, "Failed to submit capture request"); #endif /* * Acquire a frame generated by the capture request, get the image from the frame * and create a .JPG file of the captured image */ Argus::UniqueObj frame0( iFrameConsumer0->acquireFrame(FIVE_SECONDS_IN_NANOSECONDS, &status)); #if TWO_CAM Argus::UniqueObj frame1( iFrameConsumer1->acquireFrame(FIVE_SECONDS_IN_NANOSECONDS, &status)); #endif EGLStream::IFrame *iFrame0 = Argus::interface_cast(frame0); EXIT_IF_NULL(iFrame0, "Failed to get IFrame0 interface"); #if TWO_CAM EGLStream::IFrame *iFrame1 = Argus::interface_cast(frame1); EXIT_IF_NULL(iFrame1, "Failed to get IFrame1 interface"); #endif EGLStream::Image *image0 = iFrame0->getImage(); EXIT_IF_NULL(image0, "Failed to get Image from iFrame->getImage()"); #if TWO_CAM EGLStream::Image *image1 = iFrame1->getImage(); EXIT_IF_NULL(image1, "Failed to get Image from iFrame->getImage()"); #endif EGLStream::IImage2D *iImage2D0 = Argus::interface_cast(image0); Argus::Size2D size0 = iImage2D0->getSize(0); uint32_t totalsize0 = iImage2D0->getStride(0) * size0.height(); CONSUMER_PRINT("\tIImage(2D0): " "buffer (%ux%u, %u stride),\n ", size0.width(), size0.height(), iImage2D0->getStride(0) ); #if TWO_CAM EGLStream::IImage2D *iImage2D1 = Argus::interface_cast(image1); Argus::Size2D size1 = iImage2D1->getSize(0); uint32_t totalsize1 = iImage2D1->getStride(0) * size1.height(); CONSUMER_PRINT("\tIImage(2D1): " "buffer (%ux%u, %u stride), \n", size1.width(), size1.height(), iImage2D1->getStride(0) ); #endif } // Shut down Argus. cameraProvider.reset(); return EXIT_SUCCESS; }