Search in sources :

Example 1 with RawImage

use of com.android.ddmlib.RawImage in project VideoOptimzer by attdevsupport.

the class VideoCaptureImpl method run.

/**
 * Process to capture the image of emulator and pass the raw image to create
 * a video.
 *
 * NOTE: If USB got disconnected, the Video capture thread will continue running.
 *	 	 When the "isUSBConnected" was set to false ,we won't get any screen shot.
 * 		 The failure count in the catch blocks will be temporarily stopped until
 * 		 "isUSBConnected" become true.
 */
public void run() {
    RawImage priorImage = null;
    boolean captureFail = false;
    int failCounter = 0;
    if (device != null) {
        RawImage rawImage = null;
        BufferedImage image = null;
        iExceptionCount = 0;
        allDone = false;
        Date lastFrameTime = this.videoStartTime = new Date();
        hasExited = false;
        isUSBConnected = true;
        while (!allDone) {
            try {
                // Screen shot is captured from the emulator.
                synchronized (device) {
                    if (isUSBConnected) {
                        try {
                            rawImage = device.getScreenshot();
                            priorImage = rawImage;
                            failCounter = 0;
                        } catch (Exception e) {
                            if (priorImage != null) {
                                // set this flag to allow image reuse in the video
                                captureFail = true;
                            }
                            rawImage = priorImage;
                        }
                    } else {
                        rawImage = null;
                    }
                }
                if (rawImage != null) {
                    Date timestamp = new Date();
                    int duration = Math.round((float) (timestamp.getTime() - lastFrameTime.getTime()) * videowriter.getTimeUnits() / 1000f);
                    if (duration > 0) {
                        if (image == null) {
                            image = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_INT_RGB);
                        }
                        ImageHelper.convertImage(rawImage, image);
                        if (captureFail) {
                            overlayMessage(image, rawImage.width, rawImage.height, String.format("Screen Capture Blocked :%d", failCounter++));
                            captureFail = false;
                        }
                        videowriter.writeFrame(image, duration);
                        lastFrameTime = timestamp;
                        callSubscriber(image);
                    }
                }
                try {
                    if (deviceManufacturer != null) {
                        if (deviceManufacturer.contains("htc")) {
                            // Is it necessary now?
                            // We would sleep for 1 sec for lower frame rate video on HTC devices via USB bridge
                            Thread.sleep(1000);
                        } else {
                            // Delay for 100 for > 2fps video
                            Thread.sleep(100);
                        }
                    }
                } catch (InterruptedException interruptedExp) {
                    LOGGER.error(interruptedExp.getMessage());
                }
            } catch (IOException ioExp) {
                if (isUSBConnected) {
                    iExceptionCount++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        LOGGER.error("Thread Interrupted Exception at LiveScreen Capture: ", e);
                    }
                    LOGGER.error("IOException ", ioExp);
                }
            }
            if (iExceptionCount > MAX_FETCH_EXCEPTIONS) {
                allDone = true;
                LOGGER.error("Too many exceptions caught, now exiting video capture");
            }
        }
        try {
            videowriter.close();
        } catch (IOException ioExp) {
            LOGGER.warn("Exception closing video output stream", ioExp);
        }
        hasExited = true;
    }
}
Also used : RawImage(com.android.ddmlib.RawImage) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Date(java.util.Date) IOException(java.io.IOException)

Example 2 with RawImage

use of com.android.ddmlib.RawImage in project InspectorClient by zhkl0228.

the class InspectorClient method screenShot.

private static BufferedImage screenShot(IDevice device) throws TimeoutException, AdbCommandRejectedException, IOException {
    RawImage rawImage = device.getScreenshot();
    // device/adb not available?
    if (rawImage == null)
        return null;
    // convert raw data to an Image
    BufferedImage image = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_INT_ARGB);
    int index = 0;
    int indexinc = rawImage.bpp >> 3;
    for (int y = 0; y < rawImage.height; y++) {
        for (int x = 0; x < rawImage.width; x++) {
            int value = rawImage.getARGB(index);
            index += indexinc;
            image.setRGB(x, y, value);
        }
    }
    return image;
}
Also used : RawImage(com.android.ddmlib.RawImage) BufferedImage(java.awt.image.BufferedImage)

Example 3 with RawImage

use of com.android.ddmlib.RawImage in project android by JetBrains.

the class ScreenshotTask method run.

@Override
public void run(@NotNull ProgressIndicator indicator) {
    indicator.setIndeterminate(true);
    indicator.setText(AndroidBundle.message("android.ddms.screenshot.task.step.obtain"));
    RawImage rawImage;
    ScreenshotRetrieverTask retrieverTask = new ScreenshotRetrieverTask(myDevice);
    ApplicationManager.getApplication().executeOnPooledThread(retrieverTask);
    Future<RawImage> image = retrieverTask.getRawImage();
    while (true) {
        try {
            rawImage = image.get(100, TimeUnit.MILLISECONDS);
            break;
        } catch (InterruptedException e) {
            myError = AndroidBundle.message("android.ddms.screenshot.task.error1", ExceptionUtil.getMessage(e));
            return;
        } catch (ExecutionException e) {
            myError = AndroidBundle.message("android.ddms.screenshot.task.error1", ExceptionUtil.getMessage(e));
            return;
        } catch (TimeoutException e) {
            if (indicator.isCanceled()) {
                return;
            }
        }
    }
    if (rawImage.bpp != 16 && rawImage.bpp != 32) {
        myError = AndroidBundle.message("android.ddms.screenshot.task.error.invalid.bpp", rawImage.bpp);
        return;
    }
    indicator.setText(AndroidBundle.message("android.ddms.screenshot.task.step.load"));
    //noinspection UndesirableClassUsage
    myImage = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_INT_ARGB);
    for (int y = 0; y < rawImage.height; y++) {
        for (int x = 0; x < rawImage.width; x++) {
            int argb = rawImage.getARGB((x + y * rawImage.width) * (rawImage.bpp / 8));
            myImage.setRGB(x, y, argb);
        }
    }
}
Also used : RawImage(com.android.ddmlib.RawImage) ExecutionException(java.util.concurrent.ExecutionException) BufferedImage(java.awt.image.BufferedImage) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with RawImage

use of com.android.ddmlib.RawImage in project VideoOptimzer by attdevsupport.

the class VideoCaptureImplTest method run_test2.

@SuppressWarnings("rawtypes")
@Test
public void run_test2() throws TimeoutException, AdbCommandRejectedException, IOException {
    videoCapture.setDevice(device);
    RawImage testRaw = new RawImage();
    testRaw.data = imagedata;
    testRaw.width = 10;
    testRaw.height = 10;
    testRaw.bpp = 16;
    when(device.getScreenshot()).thenReturn(testRaw);
    when(videoWriter.getTimeUnits()).thenReturn((int) 1000f);
    IVideoImageSubscriber videoImageSub = mock(IVideoImageSubscriber.class);
    videoCapture.addSubscriber(videoImageSub);
    videoCapture.setDeviceManufacturer("somethingHTC");
    doAnswer(new Answer() {

        public Object answer(InvocationOnMock invocation) {
            ((Runnable) invocation.getArguments()[0]).run();
            videoCapture.stopRecording();
            return null;
        }
    }).when(threadPool).execute(any(Runnable.class));
    videoCapture.run();
    assertEquals(0, videoCapture.getiExceptionCount());
    assertTrue(videoCapture.isAllDone());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) IVideoImageSubscriber(com.att.aro.core.datacollector.IVideoImageSubscriber) RawImage(com.android.ddmlib.RawImage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BaseTest(com.att.aro.core.BaseTest) Test(org.junit.Test)

Aggregations

RawImage (com.android.ddmlib.RawImage)4 BufferedImage (java.awt.image.BufferedImage)3 BaseTest (com.att.aro.core.BaseTest)1 IVideoImageSubscriber (com.att.aro.core.datacollector.IVideoImageSubscriber)1 IOException (java.io.IOException)1 Date (java.util.Date)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1