• How to add onMetaData event on the fly to video on demand streams

    The following code snippet enables you to modify the onMetaData data that's associated with an on demand stream. The class extends the MediaReaderFLV class and overrides the getMetaData method. It then calls the MediaReaderFLV getMetadata method, deserializes the data into an AMFDataList object, allows you to inject or modify the metadata, serializes the packet and returns it to the streamer.

    package com.wowza.wms.plugin.test.mediareader;
    
    import java.util.*; 
    import java.nio.*;
    
    import com.wowza.wms.mediareader.flv.*;
    import com.wowza.wms.amf.*;
    
    public class MediaReaderFLVInjectMetadata extends MediaReaderFLV
    {
    	public List getMetadata()
    	{
    		List ret = super.getMetadata();
    		
    		while(true)
    		{
    			if (ret == null)
    				break;
    			if (ret.size() <= 0)
    				break;
    			
    			ByteBuffer packet = (ByteBuffer)ret.get(0);
    			
    			AMFDataList myMetadata = new AMFDataList(packet);
    
    			AMFDataMixedArray dataObj = (AMFDataMixedArray)myMetadata.get(1);
    			dataObj.put("wowzafield", new AMFDataItem("wowzadata"));
    			
    			byte[] data = myMetadata.serialize();
    			ByteBuffer newPacket = ByteBuffer.wrap(data);			
    			ret.set(0, newPacket);
    			break;
    		}
    
    		return ret;
    	}
    
    }
    To hook up this class edit [install-dir]/conf/MediaReaders.xml and change the BassClass for the flv file type like this:

    from:
    <ClassBase>com.wowza.wms.mediareader.flv.MediaReaderFLV</ClassBase>
    
    to:
    <ClassBase>com.wowza.wms.plugin.test.mediareader.MediaReaderFLVInjectMetadata</ClassBase>
    Note: The above change is only possible with Wowza Media Server™ 3 software. For Wowza Streaming Engine™ software, use the following VHost Listener.
    package com.wowza.wms.plugin.test.mediareader;
    
    import com.wowza.wms.amf.AMFDataList;
    import com.wowza.wms.client.IClient;
    import com.wowza.wms.request.RequestFunction;
    import com.wowza.wms.stream.MediaReaderItem;
    import com.wowza.wms.vhost.IVHost;
    import com.wowza.wms.vhost.IVHostNotify;
    
    public class VHostListenerMediaReaderOverride implements IVHostNotify
    {
    
    	@Override
    	public void onVHostCreate(IVHost vhost)
    	{
    	}
    
    	@Override
    	public void onVHostInit(IVHost vhost)
    	{
    		MediaReaderItem item = vhost.getMediaReaders().getMediaReaderDef("flv");
    		if (item != null)
    			item.setBaseClass("com.wowza.wms.plugin.test.mediareader.MediaReaderFLVInjectMetadata");
    	}
    
    	@Override
    	public void onVHostShutdownStart(IVHost vhost)
    	{
    	}
    
    	@Override
    	public void onVHostShutdownComplete(IVHost vhost)
    	{
    	}
    
    	@Override
    	public void onVHostClientConnect(IVHost vhost, IClient inClient, RequestFunction function, AMFDataList params)
    	{
    	}
    }
    To enable the VHost Listener, edit [install-dir]/conf/Server.xml and add the following to the VHostListeners section.
    <VHostListener>
    	<BaseClass>com.wowza.wms.plugin.test.mediareader.VHostListenerMediaReaderOverride</BaseClass>
    </VHostListener>
    This same methodology can be used to inject metadata into MP3 and MP4 streams. You just need to extends the MediaReaderMP3 and MediaReaderH264 classes.


    If you're having problems or want to discuss this article, post in our forum.