Inserting STANAG Klv metadata into TS stream sent over UDP.

In this article, we’ll show how to get up and running quickly with a simple app that inserts ASYNC Klv or SYNC Klv data into a transport stream. We’ll use KlvInjector SDK.

First things first, lets create a VS project, add the references and make sure all the required dependencies are copied to the target directory (we’ll do it using the ‘post-build event’)
Next, we create an instance of the Klv Injector, initialize it and add events:

There are some additional parameters we can set up.
Ok, we’re ready to go. Let’s first check the playback.

If everything is Ok and you have a source stream (as defined in the source url), a preview window will open and we’ll see the video.
We should get a Player_State.Starting and after that Player_State.Running event.
As during the initialization we set the NotifyPidDetection event, it should trigger shortly after the playback is started. That’s a good place to configure what should be included into our output stream.
OnPidDetectionEvent will receive a list of pids found in the stream, so we can find out if there is a klv pid out there. If it is, we can decide whether to leave it in the stream or remove and replace with a brand new one.
Last thing we have to do is to setup the actual insertion.
If all we needed is to insert an ASYNC klv into the stream, we could simply use timer and call WritePacketToOutputPid method.
But, if we want it to work with both files and stream, we need more control over the timing – in case of file processing (without video preview rendering) the processing is done faster than real time, so we must know where in the stream we are. Fortunately, OnSyncFrameEvent method provides this and other useful information.
OnSyncFrameEvent provides the information on the video frames and existing klv or private data pids accompanied with the timing information. So, based on this information we can easily insert a synchronized data (external of remuxed with the current one).
As you can see in the code below, we’re using m_KlvInjector.WritePacketToOutputPid method to insert the KLV data, specifying PTS timestamp.
 Please remember, we MUST take care of the Klv Packet timestamp. There is a helper function (AdjustTime) that sets a correct time and recalculates packet’s checksum.
Note on CaptureMode.

KlvStreamInjector has to receive (especially when working with files) timing info in order to be able sync Klv data (insert it at the correct place in the file). This can be achieved with or without actual decoding. If no decoding is done, the offline process can be a lot faster (and real time / stream mode use less CPU). So, when no preview is needed, set CaptureMode to Compressed (fCompressedVideo = true) and disable Uncompressed capture (VideoCaptureMode.uncompressedVideo = UncompressedVideoModeWr.UncompressedVideoMode_None;) to avoid unnecessary video decoding.

That’s it! You can now open your stream with any STANAG player and see the results.

No Comments

Post a Comment