Search in sources :

Example 6 with UploadCallBack

use of com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploaderClientTest method uploadStream_validInputs_runCallbacks.

@Test
public void uploadStream_validInputs_runCallbacks() throws InterruptedException {
    Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsFrontendClient", mockKvsFrontendClient));
    Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsDataClient", mockKvsDataClient));
    final boolean[] isStatusChanged = { false };
    final Runnable statusChangedCallBack = () -> isStatusChanged[0] = true;
    final UploadCallBack uploadCallBack = new UploadCallBack(Date.from(Instant.now()), edgeConnectorForKVSConfiguration);
    when(mockKvsFrontendClient.getDataEndpoint(any(GetDataEndpointRequest.class))).thenReturn(new GetDataEndpointResult().withDataEndpoint(DATA_ENDPOINT));
    doNothing().when(mockKvsDataClient).putMedia(any(PutMediaRequest.class), any(PutMediaAckResponseHandler.class));
    // Since we make putMedia do nothing, so it won't end until we close it.
    new Thread(() -> {
        videoUploaderClient.uploadStream(inputStream, Date.from(Instant.now()), statusChangedCallBack, uploadCallBack);
    }).start();
    // wait until task start
    if (!videoUploaderClient.isOpen()) {
        System.out.println("task is not running");
        Thread.sleep(STATUS_CHANGED_TIME);
    }
    videoUploaderClient.close();
    // wait until task end
    if (videoUploaderClient.isOpen()) {
        System.out.println("task is running");
        Thread.sleep(STATUS_CHANGED_TIME);
    }
    Assertions.assertFalse(isStatusChanged[0]);
    verify(streamManager, times(0)).pushData(any(), any(), any(), any());
}
Also used : GetDataEndpointRequest(com.amazonaws.services.kinesisvideo.model.GetDataEndpointRequest) GetDataEndpointResult(com.amazonaws.services.kinesisvideo.model.GetDataEndpointResult) PutMediaAckResponseHandler(com.amazonaws.services.kinesisvideo.PutMediaAckResponseHandler) PutMediaRequest(com.amazonaws.services.kinesisvideo.model.PutMediaRequest) UploadCallBack(com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack) Test(org.junit.jupiter.api.Test)

Example 7 with UploadCallBack

use of com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploaderClientTest method uploadStream_mockAckResponseFailure_runCallbacks.

@Test
public void uploadStream_mockAckResponseFailure_runCallbacks() throws InterruptedException {
    Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsFrontendClient", mockKvsFrontendClient));
    Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsDataClient", mockKvsDataClient));
    final boolean[] isStatusChanged = { false };
    final Runnable statusChangedCallBack = () -> isStatusChanged[0] = true;
    final UploadCallBack uploadCallBack = new UploadCallBack(Date.from(Instant.now()), edgeConnectorForKVSConfiguration);
    ArgumentCaptor<PutMediaAckResponseHandler> putMediaAckResponseArgumentCaptor = ArgumentCaptor.forClass(PutMediaAckResponseHandler.class);
    ArgumentCaptor<PutMediaRequest> putMediaRequestArgumentCaptor = ArgumentCaptor.forClass(PutMediaRequest.class);
    when(mockKvsFrontendClient.getDataEndpoint(any(GetDataEndpointRequest.class))).thenReturn(new GetDataEndpointResult().withDataEndpoint(DATA_ENDPOINT));
    doNothing().when(mockKvsDataClient).putMedia(any(PutMediaRequest.class), any(PutMediaAckResponseHandler.class));
    // Since we make putMedia do nothing, so it won't end until we close it.
    new Thread(() -> {
        Assertions.assertThrows(KvsStreamingException.class, () -> videoUploaderClient.uploadStream(inputStream, Date.from(Instant.now()), statusChangedCallBack, uploadCallBack));
    }).start();
    // wait until task start
    while (!videoUploaderClient.isOpen()) {
        System.out.println("task is not running");
        Thread.sleep(STATUS_CHANGED_TIME);
    }
    verify(mockKvsDataClient).putMedia(putMediaRequestArgumentCaptor.capture(), putMediaAckResponseArgumentCaptor.capture());
    AckEvent event = new AckEvent().withAckEventType(AckEventType.Values.ERROR);
    putMediaAckResponseArgumentCaptor.getValue().onAckEvent(event);
    putMediaAckResponseArgumentCaptor.getValue().onFailure(new RuntimeException("Mock failure"));
    // wait until task end
    while (videoUploaderClient.isOpen()) {
        System.out.println("task is running");
        Thread.sleep(STATUS_CHANGED_TIME);
    }
    Assertions.assertFalse(isStatusChanged[0]);
    verify(streamManager, times(0)).pushData(any(), any(), any(), any());
}
Also used : PutMediaRequest(com.amazonaws.services.kinesisvideo.model.PutMediaRequest) UploadCallBack(com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack) KvsStreamingException(com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException) GetDataEndpointRequest(com.amazonaws.services.kinesisvideo.model.GetDataEndpointRequest) GetDataEndpointResult(com.amazonaws.services.kinesisvideo.model.GetDataEndpointResult) PutMediaAckResponseHandler(com.amazonaws.services.kinesisvideo.PutMediaAckResponseHandler) AckEvent(com.amazonaws.services.kinesisvideo.model.AckEvent) Test(org.junit.jupiter.api.Test)

Example 8 with UploadCallBack

use of com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploaderClientTest method uploadHistoricalVideo_uploadAndTaskIsTerminating_runCallbacks.

@Test
public void uploadHistoricalVideo_uploadAndTaskIsTerminating_runCallbacks() throws InterruptedException {
    Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsFrontendClient", mockKvsFrontendClient));
    Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsDataClient", mockKvsDataClient));
    Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "isTaskTerminating", true));
    Assumptions.assumeTrue(tempVideoFilesPresent());
    final boolean[] isStatusChanged = { false };
    final Runnable statusChangedCallBack = () -> isStatusChanged[0] = true;
    final UploadCallBack uploadCallBack = new UploadCallBack(Date.from(Instant.now()), edgeConnectorForKVSConfiguration);
    when(mockKvsFrontendClient.getDataEndpoint(any(GetDataEndpointRequest.class))).thenReturn(new GetDataEndpointResult().withDataEndpoint(DATA_ENDPOINT));
    doNothing().when(mockKvsDataClient).putMedia(any(PutMediaRequest.class), any(PutMediaAckResponseHandler.class));
    // Since we make putMedia do nothing, so it won't end until we close it.
    new Thread(() -> {
        videoUploaderClient.uploadHistoricalVideo(Date.from(instantNow.minusSeconds(600)), Date.from(instantNow.minusSeconds(200)), statusChangedCallBack, uploadCallBack);
    }).start();
    // wait until task start
    if (!videoUploaderClient.isOpen()) {
        System.out.println("task is not running");
        Thread.sleep(STATUS_CHANGED_TIME);
    }
    videoUploaderClient.close();
    // wait until task end
    while (videoUploaderClient.isOpen()) {
        System.out.println("task is running");
        Thread.sleep(STATUS_CHANGED_TIME);
    }
    Assertions.assertFalse(isStatusChanged[0]);
    verify(streamManager, times(0)).pushData(any(), any(), any(), any());
}
Also used : GetDataEndpointRequest(com.amazonaws.services.kinesisvideo.model.GetDataEndpointRequest) GetDataEndpointResult(com.amazonaws.services.kinesisvideo.model.GetDataEndpointResult) PutMediaAckResponseHandler(com.amazonaws.services.kinesisvideo.PutMediaAckResponseHandler) PutMediaRequest(com.amazonaws.services.kinesisvideo.model.PutMediaRequest) UploadCallBack(com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack) Test(org.junit.jupiter.api.Test)

Example 9 with UploadCallBack

use of com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSService method startHistoricalVideoUploading.

private void startHistoricalVideoUploading(EdgeConnectorForKVSConfiguration configuration, long startTime, long endTime) throws InterruptedException {
    log.info("Start uploading video between " + startTime + " and " + endTime + " for stream " + configuration.getKinesisVideoStreamName());
    VideoUploader videoUploader = generateVideoUploader(configuration);
    Date dStartTime = new Date(startTime);
    Date dEndTime = new Date(endTime);
    boolean isUploadingFinished = false;
    do {
        try {
            videoUploader.uploadHistoricalVideo(dStartTime, dEndTime, new StatusChangedCallBack(), new UploadCallBack(dStartTime, configuration));
            isUploadingFinished = true;
        } catch (Exception ex) {
            // Log error and retry historical uploading process
            log.error("Failed to upload historical videos: {}", ex.getMessage());
        }
    } while (retryOnFail && !isUploadingFinished);
}
Also used : VideoUploader(com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader) StatusChangedCallBack(com.aws.iot.edgeconnectorforkvs.videouploader.callback.StatusChangedCallBack) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) EdgeConnectorForKVSUnrecoverableException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UploadCallBack(com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack)

Aggregations

UploadCallBack (com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack)9 Test (org.junit.jupiter.api.Test)7 PutMediaAckResponseHandler (com.amazonaws.services.kinesisvideo.PutMediaAckResponseHandler)6 GetDataEndpointRequest (com.amazonaws.services.kinesisvideo.model.GetDataEndpointRequest)6 GetDataEndpointResult (com.amazonaws.services.kinesisvideo.model.GetDataEndpointResult)6 PutMediaRequest (com.amazonaws.services.kinesisvideo.model.PutMediaRequest)6 AckEvent (com.amazonaws.services.kinesisvideo.model.AckEvent)3 Date (java.util.Date)3 EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)2 EdgeConnectorForKVSUnrecoverableException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException)2 VideoUploader (com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader)2 StatusChangedCallBack (com.aws.iot.edgeconnectorforkvs.videouploader.callback.StatusChangedCallBack)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 VideoRecorder (com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder)1 KvsStreamingException (com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1