use of com.amazonaws.services.kinesisvideo.model.PutMediaRequest in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploaderClient method doUploadStream.
private void doUploadStream(InputStream inputStream, Date videoUploadingStartTime, Runnable statusChangedCallBack, UploadCallBack uploadCallBack) throws KvsStreamingException {
if (dataEndpoint == null) {
dataEndpoint = getDataEndpoint();
}
putMediaLatch = new CountDownLatch(1);
PutMediaAckResponseHandler rspHandler = createResponseHandler(putMediaLatch, statusChangedCallBack, uploadCallBack);
if (kvsDataClient == null) {
kvsDataClient = AmazonKinesisVideoPutMediaClient.builder().withRegion(region.getName()).withEndpoint(URI.create(dataEndpoint)).withCredentials(awsCredentialsProvider).withConnectionTimeoutInMillis(CONNECTION_TIMEOUT_IN_MILLIS).withNumberOfThreads(1).build();
}
log.info("Uploading from input stream, timestamp: " + videoUploadingStartTime.getTime());
kvsDataClient.putMedia(new PutMediaRequest().withStreamName(kvsStreamName).withFragmentTimecodeType(FragmentTimecodeType.RELATIVE).withPayload(inputStream).withProducerStartTimestamp(videoUploadingStartTime), rspHandler);
try {
putMediaLatch.await();
log.info("putMedia end from latch");
} catch (InterruptedException e) {
log.debug("Put media is interrupted");
}
if (lastKvsStreamingException == null && isTaskTerminating) {
/* It's ending from close request, let's wait a little to receive ACKs. */
try {
inputStream.close();
Thread.sleep(Constants.UPLOADER_WAIT_FOR_ACKS_DELAY_MILLI_SECONDS);
} catch (IOException exception) {
log.error(exception.getMessage());
} catch (InterruptedException exception) {
log.error(exception.getMessage());
} catch (Exception exception) {
// It happens when input stream is not able to be closed
log.error("Unhandled exception caught: " + exception);
}
}
kvsDataClient.close();
kvsDataClient = null;
}
use of com.amazonaws.services.kinesisvideo.model.PutMediaRequest in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploaderClientTest method uploadStream_mockAckResponseCompleteWithNullCallback_taskClosed.
@Test
public void uploadStream_mockAckResponseCompleteWithNullCallback_taskClosed() throws InterruptedException {
Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsFrontendClient", mockKvsFrontendClient));
Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsDataClient", mockKvsDataClient));
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));
// Since we make putMedia do nothing, so it won't end until we close it.
Date dateNow = Date.from(Instant.now());
new Thread(() -> {
videoUploaderClient.uploadStream(inputStream, dateNow, null, null);
}).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.PERSISTED).withFragmentTimecode(dateNow.getTime());
putMediaAckResponseArgumentCaptor.getValue().onAckEvent(event);
putMediaAckResponseArgumentCaptor.getValue().onComplete();
// wait until task end
while (videoUploaderClient.isOpen()) {
System.out.println("task is running");
Thread.sleep(STATUS_CHANGED_TIME);
}
Assertions.assertFalse(videoUploaderClient.isOpen());
}
use of com.amazonaws.services.kinesisvideo.model.PutMediaRequest in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploaderClientTest method uploadStream_mockAckResponseCompleteWithUploadCallback_taskClosed.
@Test
public void uploadStream_mockAckResponseCompleteWithUploadCallback_taskClosed() throws InterruptedException {
Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsFrontendClient", mockKvsFrontendClient));
Assumptions.assumeTrue(setPrivateMember(videoUploaderClient, "kvsDataClient", mockKvsDataClient));
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.
Date dateNow = Date.from(Instant.now());
final UploadCallBack uploadCallBack = new UploadCallBack(dateNow, edgeConnectorForKVSConfiguration);
new Thread(() -> {
videoUploaderClient.uploadStream(inputStream, dateNow, null, 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.PERSISTED).withFragmentTimecode(dateNow.getTime());
putMediaAckResponseArgumentCaptor.getValue().onAckEvent(event);
putMediaAckResponseArgumentCaptor.getValue().onComplete();
// wait until task end
while (videoUploaderClient.isOpen()) {
System.out.println("task is running");
Thread.sleep(STATUS_CHANGED_TIME);
}
Assertions.assertFalse(videoUploaderClient.isOpen());
}
use of com.amazonaws.services.kinesisvideo.model.PutMediaRequest in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploaderClientTest method uploadStream_mockAckResponseComplete_runCallbacks.
@Test
public void uploadStream_mockAckResponseComplete_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));
when(this.edgeConnectorForKVSConfiguration.getStreamManager()).thenReturn(streamManager);
when(streamManager.pushData(any(), any(), any(), any())).thenReturn(0L);
// 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();
Thread.sleep(STATUS_CHANGED_TIME);
// 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.PERSISTED);
event.setFragmentTimecode(1L);
putMediaAckResponseArgumentCaptor.getValue().onAckEvent(event);
// Wait stream manager update values
Thread.sleep(STATUS_CHANGED_TIME);
putMediaAckResponseArgumentCaptor.getValue().onComplete();
// 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(1)).pushData(any(), any(), any(), any());
}
use of com.amazonaws.services.kinesisvideo.model.PutMediaRequest in project amazon-kinesis-video-streams-parser-library by aws.
the class PutMediaWorker method run.
@Override
public void run() {
CountDownLatch latch = new CountDownLatch(1);
putMedia.putMedia(new PutMediaRequest().withStreamName(streamName).withFragmentTimecodeType(FragmentTimecodeType.RELATIVE).withProducerStartTimestamp(new Date()).withPayload(inputStream), new PutMediaAckResponseHandler() {
@Override
public void onAckEvent(AckEvent event) {
log.info("PutMedia Ack for stream {}: {} ", streamName, event.toString());
if (AckEventType.Values.PERSISTED.equals(event.getAckEventType().getEnumValue())) {
numFragmentsPersisted++;
}
}
@Override
public void onFailure(Throwable t) {
log.error("PutMedia for {} has suffered error {}", streamName, t);
latch.countDown();
}
@Override
public void onComplete() {
log.info("PutMedia for {} is complete ", streamName);
latch.countDown();
}
});
log.info("Made PutMedia call for stream {}", streamName);
try {
latch.await();
log.info("PutMedia worker exiting for stream {} number of fragments persisted {} ", streamName, numFragmentsPersisted);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Failure while waiting for PutMedia to finish", e);
} finally {
putMedia.close();
}
}
Aggregations