Interface MediaSource
-
- All Known Implementing Classes:
AudioVideoFileMediaSource,BytesMediaSource,ImageFileMediaSource,MultiTrackMediaSource
public interface MediaSourceInterface representing a media source that produces video/audio frames and delivers them to Kinesis Video Streams.A MediaSource is responsible for:
- Generating or capturing media frames (video/audio data)
- Converting frames into
KinesisVideoFrameobjects - Pushing frames to a
MediaSourceSinkfor streaming to Kinesis Video Streams - Managing its own lifecycle (start, stop, cleanup)
The
MediaSourcefollows a producer-consumer pattern where:- The
MediaSourceacts as the producer of media frames - The
MediaSourceSinkacts as the consumer that receives frames - The sink handles the actual frame submission to the Kinesis Video Stream via
KinesisVideoProducerStream.putFrame(KinesisVideoFrame)
Data flow:
MediaSource → MediaSourceSink → KinesisVideoProducerStream → Kinesis Video Streams
Frame delivery:
- MediaSource calls
sink.onFrame(frame)for each video/audio frame - Sink validates and forwards frame to the producer stream
- Producer handles packaging, and network transmission
Typical usage flow:
- Create and
configure(MediaSourceConfiguration)theMediaSource - Submit the
MediaSourceto theKinesisVideoClientviaKinesisVideoClient.registerMediaSource(MediaSource) - Start the
MediaSource - Unregister the
MediaSourcevia
Interactions with
NativeKinesisVideoClient:NativeKinesisVideoClient.registerMediaSource(MediaSource)willinitialize(MediaSourceSink)it with an instance ofProducerStreamSinkcreated using the providedStreamInfoviagetStreamInfo()NativeKinesisVideoClient.unregisterMediaSource(MediaSource)will stop the media source if not already, then callfree().
- See Also:
MediaSourceSink,KinesisVideoFrame,MediaSourceConfiguration
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidconfigure(MediaSourceConfiguration configuration)Applies configuration settings to this media source.voidfree()Releases all resources held by this media source and performs final cleanup.MediaSourceConfigurationgetConfiguration()Returns the configuration object used to create and configure this media source.MediaSourceSinkgetMediaSourceSink()Returns the sink that receives frames from this media source.MediaSourceStategetMediaSourceState()Returns the current state of this media source.StreamCallbacksgetStreamCallbacks()Returns stream-specific callback implementations for handling producer events.StreamInfogetStreamInfo()Returns stream information describing the Kinesis Video Stream this is producing frames for.voidinitialize(MediaSourceSink mediaSourceSink)Initializes the media source with a sink that will receive produced frames.booleanisStopped()Returns true if the media source has been stopped and is no longer producing frames.voidstart()Starts frame production and streaming.voidstop()Stops frame production and streaming synchronously.
-
-
-
Method Detail
-
getMediaSourceState
MediaSourceState getMediaSourceState()
Returns the current state of this media source.States include: STARTED, STOPPED, etc.
Use this to check if the media source is ready to start streaming.
- Returns:
- the current
MediaSourceState
-
getConfiguration
MediaSourceConfiguration getConfiguration()
Returns the configuration object used to create and configure this media source.The configuration contains media source specific settings such as:
- Media source description
- Source-specific parameters (mime type, camera id, file path, etc.)
- Returns:
- the
MediaSourceConfigurationfor this media source
-
getStreamInfo
StreamInfo getStreamInfo() throws KinesisVideoException
Returns stream information describing the Kinesis Video Stream this is producing frames for.NativeKinesisVideoClient.registerMediaSource(MediaSource)willinitialize(MediaSourceSink)it with an instance ofProducerStreamSinkcreated using the providedStreamInforeturned by this method.- Returns:
- the
StreamInfodescribing this stream - Throws:
KinesisVideoException- if stream info cannot be determined
-
initialize
void initialize(@Nonnull MediaSourceSink mediaSourceSink) throws KinesisVideoException
Initializes the media source with a sink that will receive produced frames.NativeKinesisVideoClient.registerMediaSource(MediaSource)will call this method with an instance ofProducerStreamSinkcreated using the providedStreamInfoviagetStreamInfo()This method:
- Establishes the connection between this MediaSource and the provided sink
- Prepares the media source for frame production
- Must be called before
start()
After initialization, the MediaSource should be ready to start producing frames when
start()is called.- Parameters:
mediaSourceSink- the sink that will receive frames from this media source- Throws:
KinesisVideoException- if initialization fails
-
configure
void configure(MediaSourceConfiguration configuration)
Applies configuration settings to this media source.This method allows runtime configuration of the media source with source-specific parameters. Should typically be called before
initialize(MediaSourceSink).- Parameters:
configuration- the configuration to apply to this media source
-
start
void start() throws KinesisVideoExceptionStarts frame production and streaming.After calling this method:
- MediaSource begins capturing/generating frames
- Frames are continuously sent to the MediaSourceSink via
MediaSourceSink.onFrame(com.amazonaws.kinesisvideo.producer.KinesisVideoFrame) - Codec private data is sent via
MediaSourceSink.onCodecPrivateData(byte[]) - Any metadata is sent via
MediaSourceSink.onFragmentMetadata(String, String, boolean)
The MediaSource must be initialized before starting.
- Throws:
KinesisVideoException- if the media source cannot be started or is not properly initialized
-
stop
void stop() throws KinesisVideoException
Stops frame production and streaming synchronously. This method should be idempotent.After calling this method and it returns:
- No more frames will be produced or sent to the sink
- Resources may be released, but the MediaSource can potentially be restarted
- Use
isStopped()to verify the stop operation completed
To completely clean up resources, call
free()after stopping.- Throws:
KinesisVideoException- if the media source cannot be stopped cleanly
-
isStopped
boolean isStopped()
Returns true if the media source has been stopped and is no longer producing frames.This method can be used to:
- Verify that
stop()completed successfully - Check if the media source needs to be restarted
- Determine if cleanup is needed
- Returns:
- true if the media source is stopped, false if it's still active
- Verify that
-
free
void free() throws KinesisVideoException
Releases all resources held by this media source and performs final cleanup.NativeKinesisVideoClientwill call this whenNativeKinesisVideoClient.free()is called with this Media Source still registered. If the Media Source wasNativeKinesisVideoClient.unregisterMediaSource(MediaSource), then you will need to call this method yourself.MediaSourcecannot be used again after freeing.This method:
- Releases native resources (the native Kinesis Video Stream).
- Should be called after
stop()to ensure clean shutdown - Makes the MediaSource unusable - it cannot be restarted after freeing
- Throws:
KinesisVideoException- if cleanup fails
-
getMediaSourceSink
MediaSourceSink getMediaSourceSink()
Returns the sink that receives frames from this media source.This is the same sink that was provided to
initialize(MediaSourceSink). Useful for accessing the underlying producer stream or for debugging.- Returns:
- the
MediaSourceSinkreceiving frames from this source, or null if not initialized
-
getStreamCallbacks
@Nullable StreamCallbacks getStreamCallbacks()
Returns stream-specific callback implementations for handling producer events.StreamCallbacks allow the MediaSource to:
- Receive acknowledgments from Kinesis Video Streams
- Handle stream errors and implement custom retry logic
- Monitor stream health and performance metrics
- Implement custom stream lifecycle management
Return null if no custom callbacks are needed (default behavior will be used).
- Returns:
- custom
StreamCallbacksimplementation, or null for default behavior
-
-