Custom metadata added to Live stream not sent | Flash Media Server

Issue

To add custom metadata to the beginning of a live webcam stream published from Flash Media Server, use the NetStream.send() to publish the metadata  in client-side ActionScript. However, if  Flash Media Server records the live stream, the custom metadata is not stored in the file. This issue has been filed as bug 2548763.

Solution

When you record a live video stream (F4V/FLV file or DVR applications), Flash Media Server requires that the metadata is available before it flushes the first segment. There are two ways you can ensure that the metadata is available before the first segment flush:

  1. Send the metadata immediately after publishing the stream.
  2. Control  the recording of the stream in server-side ActionScript.

Bad network conditions can still delay delivery of custom metadata. In cases of network inefficiencies, the server administrator can increase either the <MaxFlushTime> tag or the <MaxFlushSize> tag in server.xml.

The following code blocks provide examples of the recommended workarounds:

Send metadata from a Flash client

The typical flow for a publishing a stream from a client application:

  1. Create a NetConnection (nc) and attach an event handler;
  2. Listen for the "NetaConnection.Connect.Success" status, then create a NetStream (ns) and attach an event handler;
  3. Create camera (cam) and microphone (mic) objects;
  4. Attach the cam and mic to the NetStream;
  5. Publish the NetStream and invoke recording on the server: ns.publish("myLiveStream", "record");
  6. Listen for the "NetStream.Publish.Start" then send metadata: ns.send("@setDataFrame", "onMetaData", myMetadataObj);

To ensure that the metadata is sent before the server flushes the first segments to disk, send the metadata immediately after calling publish. Do not wait for the "NetStream.Publish.Start" to send the metadata. Modify the typical publishing flow with the following example code that runs inside an event handler for the NetConnection. 

private function onNetStatus(event:NetStatusEvent):void {
    switch(event.info.code) {
        case "NetConnection.Connect.Success":
            //nc is the existing NetConnection object
            ns = new NetStream(nc);
            nc.client = this;
 
           //Attach event handlers to the NetStream object
            ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
            ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

            //Create camera and microphone objects
            cam = Camera.getCamera();
            mic = Microphone.getMicrophone();

            //Attach camera and microphone objects to the NetStream
            ns.attachCamera(cam);
            ns.attachAudio(mic);

            //Publish and record the stream on Flash Media Server
            ns.publish("myLiveStream", "record");

            //Immediately send metadata objects to the server
            ns.send("@setDataFrame", "onMetaData", myMetaDataObj);
            break;
        case "NetStream.Publish.Start":
            trace("Camera is publishing....");
            break;
    }
}

Use server-side actionscript to control recording

Configure the application on the server to start recording the stream after a configured delay. The delay allows the server to wait for the custom metadata before starting the recording. Modify the following example code to run on your server:

application.onPublish = function(clientObj, streamObj) {
    //Get the published stream name
    streamName = streamObj.name;

    if(streamName) {
        //Create a local stream object on the server    
        myStream = Stream.get(streamName);

        //Play the published stream locally for any subscribers
        myStream.play(streamName);

        //Create a delay to allow client to send metadata
        setInterval(function(){trace("Pausing for 1 second");}, 1000);

        //Initiate the recording of published stream with metadata
        myStream.record();
    }
}

Note: Flash Media Streaming Server is not capable of running custom application code.

Adjusting the server configuration to counter network latency

Modify the rate at which Flash Media Server flushes its recording buffer:

  1. Edit fms_root/conf/Server.xml
  2. Find the <MaxFlushTime> and <MaxFlushSize> tags in Root > Server > ResourceLimits > RecBuffer,
  3. Either increase the time value (in seconds)  for MaxFlushTime OR increase value (in kilobytes) of the MaxFlushSize.
  4. Save the Server.xml.
  5. Restart Flash Media Server.