Skip to content

Allows developers to install and customize their connected camera and other devices to securely stream video, audio, and time-encoded data to Kinesis Video Streams

License

Notifications You must be signed in to change notification settings

awslabs/amazon-kinesis-video-streams-producer-sdk-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Amazon Kinesis Video Streams Producer SDK Java

Build • Run • Troubleshooting • Resources • Development • Release Notes • License

Introduction

Amazon Kinesis Video Streams makes it easy to securely stream video from connected devices to AWS for analytics, machine learning (ML), and other processing.

The Amazon Kinesis Video Streams Producer SDK Java makes it easy to build an on-device application that securely connects to a video stream, and reliably publishes video and other media data to Kinesis Video Streams. It takes care of all the underlying tasks required to package the frames and fragments generated by the device's media pipeline. The SDK also handles stream creation, token rotation for secure and uninterrupted streaming, processing acknowledgements returned by Kinesis Video Streams, and other tasks.

Resources

Prerequisites

  • Java 11, 17, or 21
  • maven
  • JNI libraries

Using pre-built JNI libraries

  • You can find available pre-built KinesisVideoProducerJNI library in src/main/resources/lib/ for Mac (x64), Ubuntu (x64) and Raspbian (x86) and Windows 10.

Building JNI libraries

  • If pre-built libraries did not work for you, KinesisVideoProducerJNI native library needs to be built first before running the Java demo application. Please follow the steps here.

Building the Java Producer SDK and Demo Applications from Source

In an IDE

Import the Maven project (pom.xml) to your IDE, it will find dependency packages from Maven and build.

Using command-line

Make sure you have Java and Maven installed on your system.

git clone https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-java.git
cd amazon-kinesis-video-streams-producer-sdk-java
mvn clean compile assembly:single

Run Examples

Preparing a stream

AWS Management Console

You can create a stream using the AWS management console using the instructions here.

You can also edit the data retention in the AWS management console in the section under the media player.

AWS CLI

If you have AWS CLI and jq installed and configured, you can also run the prepareStream.sh.

This script will create a stream with the given name if the stream doesn't exist.

Note

The script will increase the data retention of your stream to 2 hours if it's 0.

./scripts/prepareStream.sh <YourKinesisVideoStreamName>

Launching Demoapp sample application

Demo app will start running and putting sample video frames in a loop into Kinesis Video Streams. You can change your stream settings in DemoAppMain.java before you run the app.

To run DemoAppMain.java in ./src/main/demo with JVM arguments:

  1. Credentials: aws.accessKeyId, aws.secretKey, aws.sessionToken (optional)
  2. Stream name: kvs-stream
  3. Path to JNI library directory: java.library.path
  4. Log4j configuration file: log4j.configurationFile
java -classpath target/*jar-with-dependencies.jar \
  -Daws.accessKeyId=<YourAwsAccessKey> \
  -Daws.secretKey=<YourAwsSecretKey> \
  -Dkvs-stream=<YourKinesisVideoStreamName> \
  -Djava.library.path=<NativeLibraryPath ex. src/main/lib/mac> \
  -Dlog4j.configurationFile=log4j2.xml \
  com.amazonaws.kinesisvideo.demoapp.DemoAppMain

Note

On Windows command prompt, the newline separator is ^ instead of \.

Note

NativeLibraryPath must contain path to the folder containing your "KinesisVideoProducerJNI" library (not to the file itself). File name depends on your operating system:

  • libKinesisVideoProducerJNI.so for Linux
  • libKinesisVideoProducerJNI.dylib for Mac OS
  • KinesisVideoProducerJNI.dll for Windows

Tip

Pre-built JNI libraries for some systems can be found in src/resources/lib.

Note

If your system isn't part of the pre-built JNI libraries, you will need to build the JNI yourself. You can find instructions here.

Changing the log level

Locate the log4j configuration file log4j2.xml, and change the level (currently WARN) to something else like INFO or DEBUG. For a full list of levels, see the log4j documentation.

<Loggers>
    <Root level="WARN">
        <AppenderRef ref="ConsoleAppender"/>
        <AppenderRef ref="RollingFile"/>
    </Root>
</Loggers>
To change log level for a single class

To change the log level for a single class, add a <Logger> element inside the <Loggers> section.

<Logger name="com.amazonaws.kinesisvideo.java.client.KinesisVideoJavaClientFactory" level="TRACE" additivity="false">
    <AppenderRef ref="ConsoleAppender"/>
    <AppenderRef ref="RollingFile"/>
</Logger>

In the example above, we set the JNI logs to TRACE.

Log levels to PIC equivalents:

  • TRACE = 1 -- LOG_LEVEL_VERBOSE
  • DEBUG = 2 -- LOG_LEVEL_DEBUG
  • INFO = 3 -- LOG_LEVEL_INFO
  • WARN = 4 -- LOG_LEVEL_WARN
  • ERROR = 5+ -- LOG_LEVEL_ERROR
Run API and functionality tests
mvn clean test -DargLine="\
  -Daws.accessKeyId=<YourAwsAccessKey> \
  -Daws.secretKey=<YourAwsSecretKey> \
  -Daws.sessionToken=<YourAwsSessionToken> \
  -Djava.library.path=<NativeLibraryPath> \
  -Dlog4j.configurationFile=log4j2.xml"

Note

The tests require pre-provisioned streams and require data retention enabled. If either or both conditions are not met, the tests will attempt to fix them before starting. Make sure this is OK in your account before running. Set the TEST_STREAMS_PREFIX environment variable to add a prefix to the streams used in the integration tests.

Generate code coverage report
mvn clean verify -DargLine="-Daws.accessKeyId=<YourAwsAccessKey> -Daws.secretKey=<YourAwsSecretKey> -Daws.sessionToken=<YourAwsSessionToken> -Djava.library.path=<NativeLibraryPath> -Dlog4j.configurationFile=log4j2.xml"

This will run the tests and generate a code coverage report. It will get output in target/site/jacoco. You can open target/site/jacoco/index.html in a web browser of your choice.

Run the demo application from Docker

Refer the README.md file in the dockerscripts folder for running the build and demo app within Docker container.

Launching PutMediaDemo sample application

Run PutMediaDemo.java to send sample mkv stream to Kinesis Video Streams. Note: ACCESS_KEY, SECRET_KEY, and a KINESIS_VIDEO_STREAM are required for running this sample application as well. However, this demo application does not require JNI.

-Daws.accessKeyId=<YourAwsAccessKey> -Daws.secretKey=<YourAwsSecretKey> -Dkvs-stream=<YourKinesisVideoStreamName>

for non-temporary AWS credential.

-Daws.accessKeyId=<YourAwsAccessKey> -Daws.secretKey=<YourAwsSecretKey> -Daws.sessionToken=<YourAwsSessionToken> -Dkvs-stream=<YourKinesisVideoStreamName>

Pre-built KinesisVideoProducerJNI library supported platforms

  • Mac OS X (El capitan 10.11 or above)
  • Ubuntu (14.04 or above)
  • Raspbian (9 stretch or above)

Additional Examples

For additional examples on using Kinesis Video Streams Java SDK and Kinesis Video Streams Parsing Library refer:

JavaDoc

Use the following command to build the JavaDoc locally:

mvn clean compile javadoc:javadoc

You can then open target/site/apidocs/index.html in a browser of your choice.

Troubleshooting

Class not found

If you receive an error like this:

Error: Could not find or load main class com.amazonaws.kinesisvideo.demoapp.DemoAppMain
Caused by: java.lang.ClassNotFoundException: com.amazonaws.kinesisvideo.demoapp.DemoAppMain

Check the file path to the .jar is correct, and that the class name is correct.

Unsatisfied link error - no KinesisVideoProducerJNI in java.library.path

Provide the path to the folder containing the KinesisVideoProducerJNI library (not the file itself) as part of java.library.path JVM argument.

Other Unsatisfied link error

If you notice error in loading the native library (JNI), it may be due to CPU architecture mismatch. Check the error in the logging output.

Sample output
20:49:47.747 [main] ERROR com.amazonaws.kinesisvideo.java.client.KinesisVideoJavaClientFactory - Unsatisfied link error.
java.lang.UnsatisfiedLinkError: /Users/me/Desktop/libKinesisVideoProducerJNI.dylib: dlopen(/Users/me/Desktop/libKinesisVideoProducerJNI.dylib, 0x0001): tried: '/Users/me/Desktop/libKinesisVideoProducerJNI.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/me/Desktop/libKinesisVideoProducerJNI.dylib' (no such file), '/Users/me/Desktop/libKinesisVideoProducerJNI.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64'))
	at jdk.internal.loader.NativeLibraries.load(Native Method) ~[?:?]
	at jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:331) ~[?:?]
	at jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:197) ~[?:?]
	at jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:139) ~[?:?]
	at jdk.internal.loader.NativeLibraries.findFromPaths(NativeLibraries.java:259) ~[?:?]
	at jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:251) ~[?:?]
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:2451) ~[?:?]
	at java.lang.Runtime.loadLibrary0(Runtime.java:916) ~[?:?]
	at java.lang.System.loadLibrary(System.java:2063) ~[?:?]
	at com.amazonaws.kinesisvideo.internal.producer.jni.NativeLibraryLoader.loadNativeLibraryIndirect(NativeLibraryLoader.java:76) [classes/:?]
	at com.amazonaws.kinesisvideo.internal.producer.jni.NativeLibraryLoader.loadNativeLibrary(NativeLibraryLoader.java:44) [classes/:?]
	at com.amazonaws.kinesisvideo.internal.producer.jni.NativeKinesisVideoProducerJni.initializeLibrary(NativeKinesisVideoProducerJni.java:1175) [classes/:?]
	at com.amazonaws.kinesisvideo.internal.producer.jni.NativeKinesisVideoProducerJni.create(NativeKinesisVideoProducerJni.java:228) [classes/:?]
	at com.amazonaws.kinesisvideo.internal.producer.jni.NativeKinesisVideoProducerJni.createSync(NativeKinesisVideoProducerJni.java:246) [classes/:?]
	at com.amazonaws.kinesisvideo.internal.producer.jni.NativeKinesisVideoProducerJni.createSync(NativeKinesisVideoProducerJni.java:211) [classes/:?]
	at com.amazonaws.kinesisvideo.internal.client.NativeKinesisVideoClient.initializeNewKinesisVideoProducer(NativeKinesisVideoClient.java:239) [classes/:?]
	at com.amazonaws.kinesisvideo.internal.client.NativeKinesisVideoClient.initialize(NativeKinesisVideoClient.java:119) [classes/:?]
	at com.amazonaws.kinesisvideo.java.client.KinesisVideoJavaClientFactory.createKinesisVideoClient(KinesisVideoJavaClientFactory.java:154) [classes/:?]
	at com.amazonaws.kinesisvideo.java.client.KinesisVideoJavaClientFactory.createKinesisVideoClient(KinesisVideoJavaClientFactory.java:127) [classes/:?]
	at com.amazonaws.kinesisvideo.demoapp.DemoAppMain.main(DemoAppMain.java:65) [classes/:?]
Exception in thread "main" java.lang.RuntimeException: com.amazonaws.kinesisvideo.producer.ProducerException: Failed loading native library StatusCode: 0xd
	at com.amazonaws.kinesisvideo.demoapp.DemoAppMain.main(DemoAppMain.java:93)
Caused by: com.amazonaws.kinesisvideo.producer.ProducerException: Failed loading native library StatusCode: 0xd

If using the pre-built JNI libraries located in src/main/resources/lib, make sure you are using the one that matches your CPU architecture.

If your architecture isn't provided as one of the pre-built libraries, you will need to build the JNI yourself. Building the JNI on your local system will make sure the correct architecture and bit-ness are used. See instructions here.

Note

If you run into issues building the JNI on your system, please raise an issue in repository where the JNI source code is located.

macOS security warning

If you receive an error like this when trying to use the JNI:

Apple could not verify "libKinesisVideoProducerJNI.dylib" is free of malware that may harm your Mac or compromise your privacy.

The above error should not appear if you cloned the repository using git clone. Download the artifacts using git clone.

Development

The repository is using develop branch as the aggregation and all of the feature development is done in appropriate feature branches. The PRs (Pull Requests) are cut on a feature branch and once approved with all the checks passed they can be merged by a click of a button on the PR tool. The master branch should always be build-able and all the tests should be passing. We are welcoming any contribution to the code base. The master branch contains our most recent release cycle from develop.

Release Notes

Release 1.12.1 (May 2022)

  • Allow updating automaticStreamingFlags (default: AUTOMATIC_STREAMING_INTERMITTENT_PRODUCER) in ClientInfo
  • Allow updating storePressurePolicy (default: CONTENT_STORE_PRESSURE_POLICY_DROP_TAIL_ITEM) in StreamInfo

Release 1.12.0 (February 2022)

  • Update guice from 4.2.3 to 5.1.0
  • Update junit from 4.13.1 to 4.13.2
  • Update mockito-core from 2.18.3 to 4.3.1
  • Update annotations from 2.0.3 to 3.0.1
  • Update commons-lang3 from 3.4 to 3.12.0
  • Update gson from 2.8.2 to 2.9.0
  • Update jsr-275 from 1.0.0 to 0.9.1 (artifact moved)
  • Update commons-io from 2.7 to 2.11.0
  • Update httpasyncclient from 4.1.4 to 4.1.5
  • Replace custom logger with log4j2
  • Obtain PIC logs in Java with JNI
  • Add ProducerFunctionalityTests and ProducerApiTests

Release 1.11.0 (11 September 2020)

  • Improve TLS validation by implementing hostname verification

Release 1.10.0 (29 May 2020)

  • Updated docker scripts to incorporate the changes in producer SDK
  • Provision of CachedInfoMultiAuthServiceCallbacks which implements caching layer and per-stream auth. Useful in the following scenarios:
    • A single client object is used for multiple streams that are intended for different accounts
    • For service proxy type of scenarios

Release 1.9.5 (16 Jan 2020)

  • Remove finalized() function and require explicit call on free() to avoid dangling finalized() causing memory leak.
  • Performance improvement on sending data in multiple ongoing stream in one client.
  • Fix stream information mis-alignment due to missing retention in describeStreamResult

Release 1.9.4 (9 July 2019)

  • License update: KVS SDK is under Apache 2.0 license now.
  • Stablization updates in C layer.
  • Added internal retry logic to handle error case that SDK can recover, i.e. network unstability.
  • Skip over error fragments - SDK will continue skip any invalid fragments are ingested through SDK earlier and continue streaming.
  • Automatic CPD (codec private data) extraction from the stream when CPD is part of the first H264 AnnexB frame.

Release 1.9.3 (12 March 2019)

  • Bug fix to avoid crash due to access to freed native stream object.

Release 1.9.2 (21 Feburary 2019)

  • Bug fix for broken MKV generated due to difference between trackInfoType in Java and C layer.

Release 1.9.1 (19 Feburary 2019)

  • Bug fix for credentials not rotating issue when given credentials expire in less than 40 minutes.
  • Add audio video sample to support injesting multiple track data into Kinesis Video.

Release 1.9.0 (8 Feburary 2019)

  • Bug fix for KinesisVideoClient.unregisterMediaSource() accessing to freed native object issue.
  • Add KinesisVideoClient.freeMediaSource() clean-up function to handle async behavior.

Release 1.8.0 (25 January 2019)

  • Fix duplicate stream error after unregistering media source when service call failed
  • Fix inputstream not closing after stopSync issue
  • Updating the name and description of Java SDK to publish in maven

Release 1.7.0 (3 December 2018)

  • Added support for uploading files(offline mode) to Kinesis Video Stream
  • Additional fixes

Release 1.6.0 (3 December 2018)

  • Remove streamName parameter from KinesisVideoClient.registerMediaSource() as MediaSource already has the stream name in StreamInfo.
  • Add KinesisVideoClient.unregisterMediaSource() to remove MediaSource to KinesisVideoProducerStream binding from KinesisVideoClient. Customers can use unregisterMediaSource() after they stop streaming, so MediaSource data will not to be sent to Kinesis Video Streams.
  • Add getStreamInfo() to MediaSource instead of MediaSourceConfiguration. If customers have implemented their own MediaSource and MediaSourceConfiguration, they would need to provide stream information via MediaSource.getStreamInfo(). The MediaSourceConfiguration.getStreamInfo() will not work.
  • The following classes are no longer publicly available.
  • MediaSource
  • MediaSourceConfiguration
  • MediaSourceSink
  • AbstractKinesisVideoClient
  • NativeKinesisVideoClient
  • BytesGenerator
  • BytesMediaSource
  • BytesMediaSourceConfiguration
  • ProducerStreamSink
  • KinesisVideoServiceClient
  • NativeKinesisVideoProducerJni
  • NativeKinesisVideoProducerStream
  • NativeLibraryLoader
  • KinesisVideoMetrics
  • KinesisVideoProducer
  • KinesisVideoProducerStream
  • KinesisVideoStreamMetrics
  • ReadResult
  • ServiceCallbacks
  • com.amazonaws.kinesisvideo.service.exception.AccessDeniedException
  • com.amazonaws.kinesisvideo.service.exception.AmazonServiceException
  • com.amazonaws.kinesisvideo.service.exception.ResourceInUseException
  • com.amazonaws.kinesisvideo.service.exception.ResourceNotFoundException
  • AckConsumer
  • BlockingAckConsumer
  • DefaultServiceCallbacksImpl

Release 1.5.0 (24 August 2018)

  • Windows native library available for Producer SDK
  • Intermittent producer support
  • Per-stream customized callback support

Release 1.3.1 (23 July 2018)

  • Add reset connection function.
  • Fix key frame data-flag matching issue which could cause parsing issue in decoding process.

Release 1.3.0 (15 March 2018)

  • Provide pre-built KinesisVideoProducerJNI library for Mac (x64), Ubuntu (x64) and Raspbian (x86).
  • Remove Lombok dependency on Java Producer SDK.
  • Update instruction in README about KinsisVideoProducerJNI build.
  • Compatible changes in Java Adapter to work with latest changes in Kinesis Video Streams Producer SDK CPP.

Release 1.2.1 (February 2018)

  • Remove some unit tests relying on native library to avoid mvn package build (without -skipTests=true) failure.

Release 1.2.0 (February 2018)

  • Bug fixes and performance enhancement.
  • There are some interface changes to be compatible with native library changes.

Release 1.1.0 (December 2017)

  • Updated JNI code to expose ACKs as callbacks so developer can get more information about how the streaming is going.
  • The version of JNI is bumped to 1.2, this will require corresponding "KinesisVideoProducerJNI" with same version.

Release 1.0.0 (November 2017)

  • First release of the Amazon Kinesis Video Producer SDK Java.

License

This library is licensed under the Apache License, 2.0.

About

Allows developers to install and customize their connected camera and other devices to securely stream video, audio, and time-encoded data to Kinesis Video Streams

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 34