Search in sources :

Example 1 with KinesisVideoCredentialsProvider

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class DefaultServiceCallbacksImpl method createStream.

@Override
public void createStream(@NonNull final String deviceName, @NonNull final String streamName, @NonNull final String contentType, @Nullable final String kmsKeyId, final long retentionPeriod, final long callAfter, final long timeout, @Nullable final byte[] authData, final int authType, final long customData) throws ProducerException {
    Preconditions.checkState(isInitialized(), "Service callbacks object should be initialized first");
    final long delay = calculateRelativeServiceCallAfter(callAfter);
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            int statusCode;
            String streamArn = null;
            final KinesisVideoCredentialsProvider credentialsProvider = getCredentialsProvider(authData, log);
            final long retentionInHours = retentionPeriod / Time.HUNDREDS_OF_NANOS_IN_AN_HOUR;
            final long timeoutInMillis = timeout / Time.HUNDREDS_OF_NANOS_IN_A_MILLISECOND;
            try {
                streamArn = kinesisVideoServiceClient.createStream(streamName, deviceName, contentType, kmsKeyId, retentionInHours, timeoutInMillis, credentialsProvider);
                statusCode = HTTP_OK;
            } catch (final KinesisVideoException e) {
                statusCode = getStatusCodeFromException(e);
                log.error("Kinesis Video service client returned an error " + e.getMessage() + ". Reporting to Kinesis Video PIC.");
            }
            try {
                kinesisVideoProducer.createStreamResult(customData, streamArn, statusCode);
            } catch (final ProducerException e) {
                // TODO: Deal with the runtime exception properly in this and following cases
                throw new RuntimeException(e);
            }
        }
    };
    executor.schedule(task, delay, TimeUnit.NANOSECONDS);
}
Also used : ProducerException(com.amazonaws.kinesisvideo.producer.ProducerException) KinesisVideoCredentialsProvider(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)

Example 2 with KinesisVideoCredentialsProvider

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class DefaultServiceCallbacksImpl method getStreamingToken.

@Override
public void getStreamingToken(@NonNull final String streamName, final long callAfter, final long timeout, @Nullable final byte[] authData, final int authType, final long streamHandle, final KinesisVideoProducerStream stream) throws ProducerException {
    Preconditions.checkState(isInitialized(), "Service callbacks object should be initialized first");
    final long delay = calculateRelativeServiceCallAfter(callAfter);
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            // Currently, we have no support for getting a streaming token. We will refresh the credentials
            // and return a credential from the credentials provider we got initially.
            final KinesisVideoCredentialsProvider credentialsProvider = configuration.getCredentialsProvider();
            // Stores the serialized credentials as a streaming token
            byte[] serializedCredentials = null;
            long expiration = 0;
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                final KinesisVideoCredentials credentials = credentialsProvider.getUpdatedCredentials();
                // Serialize the credentials
                expiration = credentials.getExpiration().getTime() * Time.HUNDREDS_OF_NANOS_IN_A_MILLISECOND;
                // Serialize the credentials as streaming token
                final ObjectOutput outputStream = new ObjectOutputStream(byteArrayOutputStream);
                outputStream.writeObject(credentials);
                outputStream.flush();
                serializedCredentials = byteArrayOutputStream.toByteArray();
                outputStream.close();
            } catch (final IOException e) {
                log.exception(e);
            } catch (final KinesisVideoException e) {
                log.exception(e);
            } finally {
                try {
                    byteArrayOutputStream.close();
                } catch (final IOException ex) {
                // Do nothing
                }
            }
            final int statusCode = HTTP_OK;
            try {
                kinesisVideoProducer.getStreamingTokenResult(stream, streamHandle, serializedCredentials, expiration, statusCode);
            } catch (final ProducerException e) {
                throw new RuntimeException(e);
            }
        }
    };
    executor.schedule(task, delay, TimeUnit.NANOSECONDS);
}
Also used : ProducerException(com.amazonaws.kinesisvideo.producer.ProducerException) KinesisVideoCredentialsProvider(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider) KinesisVideoCredentials(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)

Example 3 with KinesisVideoCredentialsProvider

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class DefaultServiceCallbacksImpl method getStreamingEndpoint.

@Override
public void getStreamingEndpoint(@NonNull final String streamName, @NonNull final String apiName, final long callAfter, final long timeout, @Nullable final byte[] authData, final int authType, final long streamHandle, final KinesisVideoProducerStream stream) throws ProducerException {
    Preconditions.checkState(isInitialized(), "Service callbacks object should be initialized first");
    final long delay = calculateRelativeServiceCallAfter(callAfter);
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            final KinesisVideoCredentialsProvider credentialsProvider = getCredentialsProvider(authData, log);
            final long timeoutInMillis = timeout / Time.HUNDREDS_OF_NANOS_IN_A_MILLISECOND;
            int statusCode = HTTP_OK;
            String endpoint = "";
            try {
                endpoint = kinesisVideoServiceClient.getDataEndpoint(streamName, apiName, timeoutInMillis, credentialsProvider);
            } catch (final KinesisVideoException e) {
                log.error("Kinesis Video service client returned an error " + e.getMessage() + ". Reporting to Kinesis Video PIC.");
                statusCode = getStatusCodeFromException(e);
            }
            if (statusCode != HTTP_OK && isBlank(endpoint)) {
                // TODO: more URI validation
                statusCode = HTTP_NOT_FOUND;
            }
            try {
                kinesisVideoProducer.getStreamingEndpointResult(stream, streamHandle, endpoint, statusCode);
            } catch (final ProducerException e) {
                throw new RuntimeException(e);
            }
        }
    };
    executor.schedule(task, delay, TimeUnit.NANOSECONDS);
}
Also used : ProducerException(com.amazonaws.kinesisvideo.producer.ProducerException) KinesisVideoCredentialsProvider(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)

Example 4 with KinesisVideoCredentialsProvider

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class KinesisVideoAndroidClientFactory method createKinesisVideoClient.

/**
 * Create KinesisVideo client.
 *
 * @param context Android context to use
 * @param regions Regions object
 * @param awsCredentialsProvider Credentials provider
 * @return
 * @throws KinesisVideoException
 */
public static KinesisVideoClient createKinesisVideoClient(@NonNull final Context context, @NonNull final Regions regions, @NonNull final AWSCredentialsProvider awsCredentialsProvider) throws KinesisVideoException {
    final OutputChannel outputChannel = new AndroidLogOutputChannel();
    final Log log = new Log(outputChannel, LogLevel.VERBOSE, LOG_TAG);
    final KinesisVideoCredentialsProvider kinesisVideoCredentialsProvider = new KinesisVideoCredentialsProviderImpl(awsCredentialsProvider, log);
    final KinesisVideoClientConfiguration configuration = KinesisVideoClientConfiguration.builder().withRegion(regions.getName()).withCredentialsProvider(kinesisVideoCredentialsProvider).withLogChannel(outputChannel).withStorageCallbacks(new DefaultStorageCallbacks()).build();
    final ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREADS_IN_POOL);
    return createKinesisVideoClient(context, configuration, defaultDeviceInfo(context), log, executor);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DefaultStorageCallbacks(com.amazonaws.kinesisvideo.storage.DefaultStorageCallbacks) Log(com.amazonaws.kinesisvideo.common.logging.Log) KinesisVideoCredentialsProviderImpl(com.amazonaws.mobileconnectors.kinesisvideo.auth.KinesisVideoCredentialsProviderImpl) OutputChannel(com.amazonaws.kinesisvideo.common.logging.OutputChannel) AndroidLogOutputChannel(com.amazonaws.mobileconnectors.kinesisvideo.util.AndroidLogOutputChannel) KinesisVideoCredentialsProvider(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider) KinesisVideoClientConfiguration(com.amazonaws.kinesisvideo.client.KinesisVideoClientConfiguration) AndroidLogOutputChannel(com.amazonaws.mobileconnectors.kinesisvideo.util.AndroidLogOutputChannel)

Example 5 with KinesisVideoCredentialsProvider

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class DefaultServiceCallbacksImpl method putStream.

@Override
public void putStream(@NonNull final String streamName, @NonNull final String containerType, final long streamStartTime, final boolean absoluteFragmentTimes, final boolean ackRequired, @NonNull final String dataEndpoint, final long callAfter, final long timeout, @Nullable final byte[] authData, final int authType, final KinesisVideoProducerStream kinesisVideoProducerStream) throws ProducerException {
    Preconditions.checkState(isInitialized(), "Service callbacks object should be initialized first");
    final long delay = calculateRelativeServiceCallAfter(callAfter);
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            if (kinesisVideoProducerStream == null) {
                throw new IllegalStateException("Couldn't find the correct stream");
            }
            final long timeoutInMillis = timeout / Time.HUNDREDS_OF_NANOS_IN_A_MILLISECOND;
            final long streamStartTimeInMillis = streamStartTime / Time.HUNDREDS_OF_NANOS_IN_A_MILLISECOND;
            int statusCode = HTTP_OK;
            final KinesisVideoCredentialsProvider credentialsProvider = getCredentialsProvider(authData, log);
            final long clientUploadHandle = getUploadHandle();
            try {
                final InputStream dataStream = kinesisVideoProducerStream.getDataStream(clientUploadHandle);
                final AckConsumer ackConsumer = new AckConsumer(clientUploadHandle, kinesisVideoProducerStream, log);
                final BlockingAckConsumer blockingAckConsumer = new BlockingAckConsumer(ackConsumer, log, kinesisVideoProducerStream);
                final CompletionCallback completionCallback = new CompletionCallback(kinesisVideoProducerStream, clientUploadHandle);
                // This will kick-off a long running operation
                kinesisVideoServiceClient.putMedia(streamName, containerType, streamStartTimeInMillis, absoluteFragmentTimes, ackRequired, dataEndpoint, timeoutInMillis, credentialsProvider, dataStream, blockingAckConsumer, completionCallback);
                // Block until we parse the headers
                blockingAckConsumer.awaitResponse();
            } catch (final KinesisVideoException e) {
                statusCode = getStatusCodeFromException(e);
                log.error("Kinesis Video service client returned an error " + e.getMessage() + ". Reporting to Kinesis Video PIC.");
            }
            try {
                log.info("putStreamResult uploadHandle " + clientUploadHandle + " status " + statusCode);
                kinesisVideoProducer.putStreamResult(kinesisVideoProducerStream, clientUploadHandle, statusCode);
            } catch (final ProducerException e) {
                throw new RuntimeException(e);
            }
        }
    };
    executor.schedule(task, delay, TimeUnit.NANOSECONDS);
}
Also used : ProducerException(com.amazonaws.kinesisvideo.producer.ProducerException) KinesisVideoCredentialsProvider(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)

Aggregations

KinesisVideoCredentialsProvider (com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider)7 KinesisVideoException (com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)6 ProducerException (com.amazonaws.kinesisvideo.producer.ProducerException)6 KinesisVideoCredentials (com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials)1 KinesisVideoClientConfiguration (com.amazonaws.kinesisvideo.client.KinesisVideoClientConfiguration)1 Log (com.amazonaws.kinesisvideo.common.logging.Log)1 OutputChannel (com.amazonaws.kinesisvideo.common.logging.OutputChannel)1 StreamDescription (com.amazonaws.kinesisvideo.producer.StreamDescription)1 Tag (com.amazonaws.kinesisvideo.producer.Tag)1 DefaultStorageCallbacks (com.amazonaws.kinesisvideo.storage.DefaultStorageCallbacks)1 KinesisVideoCredentialsProviderImpl (com.amazonaws.mobileconnectors.kinesisvideo.auth.KinesisVideoCredentialsProviderImpl)1 AndroidLogOutputChannel (com.amazonaws.mobileconnectors.kinesisvideo.util.AndroidLogOutputChannel)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1