• How to block a duplicate publish stream

    You can override releaseStream to authorize a stream that is being published. This example checks to see if a stream with the same name is already being published and does not publish it if it does. Otherwise the new stream will take precendence, and clients subscribing to that stream will start seeing 2nd stream.

    Code:
    package com.wowza.wms.plugin.collection.module;
    
    import com.wowza.wms.amf.*;
    import com.wowza.wms.client.*;
    import com.wowza.wms.module.*;
    import com.wowza.wms.request.*;
    import com.wowza.wms.stream.*;
    import com.wowza.wms.util.*;
    import com.wowza.wms.application.*;
    
    public class ModuleBlockDuplicateStreamNames extends ModuleBase
    {
    	IApplicationInstance appInstance = null;
    	
    	public void onAppStart(IApplicationInstance appInstance)
    	{
    		this.appInstance = appInstance;
    	}
    	
    	public void publish(IClient client, RequestFunction function, AMFDataList params)
    	{
    		String streamName = extractStreamName(client, function, params);
    		if (isDuplicateStream(client, streamName))
    		{
    			getLogger().info("ModuleBlockDuplicateStreamNames.publish["+appInstance.getContextStr()+"]: Stream name is already in use: "+streamName);
    			sendClientOnStatusError(client, "NetStream.Publish.Denied", "Stream name is already in use: "+streamName);
    		}
    		else
    			invokePrevious(client, function, params);
    	}
    
    	public void releaseStream(IClient client, RequestFunction function, AMFDataList params)
    	{
    		String streamName = extractStreamName(client, function, params);
    		if (isDuplicateStream(client, streamName))
    		{
    			getLogger().info("ModuleBlockDuplicateStreamNames.releaseStream["+appInstance.getContextStr()+"]: Stream name is already in use: "+streamName);
    			sendClientOnStatusError(client, "NetStream.Publish.Denied", "Stream name is already in use: "+streamName);
    		}
    		else
    			invokePrevious(client, function, params);
    	}
    
    	public String extractStreamName(IClient client, RequestFunction function, AMFDataList params)
    	{
    		String streamName = params.getString(PARAM1);
    		if (streamName != null)
    		{
    			String streamExt = MediaStream.BASE_STREAM_EXT;
    			
    			String[] streamDecode = ModuleUtils.decodeStreamExtension(streamName, streamExt);
    			streamName = streamDecode[0];
    			streamExt = streamDecode[1];
    		}
    
    		return streamName;
    	}
    	
    	public boolean isDuplicateStream(IClient client, String streamName)
    	{
    		if (streamName != null)
    			return client.getAppInstance().getStreams().getStream(streamName) != null;
    		
    		return false;
    	}
    
    }
    A compiled version of this module is included in the Wowza Modules Collection. Download and unzip the collection, then copy /lib/wms-plugin-collection.jar from the package to the Wowza /lib folder. Then restart Wowza.

    Add this module last in the list of Modules in the /conf/[app-name]/Application.xml file.

    Code:
    <Module>
    	<Name>ModuleBlockDuplicateStreamNames</Name>
    	<Description>ModuleBlockDuplicateStreamNames</Description>
    	<Class>com.wowza.wms.plugin.collection.module.ModuleBlockDuplicateStreamNames</Class>
    </Module>
    Comments 34 Comments
    1. notreg -
      Interesting, what for exactly do need to override releaseStream, what is protocol for publishing?
    1. rogerlittin -
      As the description says, if a second stream is published with the same name then it will stop the original stream from being received. releaseStream is called when there is already a stream being published with the same name. At this point, you can allow or disallow the new stream.
    1. liveam -
      where should I place this code
    1. rrlanham -
      This example is included in the Wowza Modules Collection. You will just add a jar file to your Wowza lib folder, add Module to an Application.xml for the app you want to use it with, then restart Wowza.

      Richard
    1. kyanit -
      The publish function isn't getting hit, an added onStreamCreate function gets hit though. Any ideas?
    1. rrlanham -
      Are you building the example in this post, or using the Modules collection?

      You should see a comment in the logs:

      Authorized: true
      Richard
    1. kyanit -
      Tried building the example in the post but no "Authorized: true". I have also tried

      public void publish(IClient client, RequestFunction function,
      AMFDataList params) {
      getLogger().info("I am not being hit!");
      }

      public void onStreamCreate(IMediaStream stream) {
      getLogger().info("I am being hit!");
      }
      onStreamCreate gets hit, publish does not.
    1. rrlanham -
      What kind of encoder are you using? Do you see publish event in the logs when you start the encoder?

      Richard
    1. kyanit -
      Hi Richard,

      I'm using "Adobe Flash Media Live Encoder 3.2"

      This is what the log shows:-

      INFO session connect-pending 127.0.0.1 -
      INFO session connect 127.0.0.1 -
      INFO server comment - MediaStreamMap.removeLiveStreamPacketizer[live/_definst_/civico/thursday]: Destroy live stream packetizer: smoothstreamingpacketizer
      INFO server comment - MediaStreamMap.removeLiveStreamPacketizer[live/_definst_/civico/thursday]: Destroy live stream packetizer: cupertinostreamingpacketizer
      INFO server comment - I am being hit!
      INFO stream create - -
      INFO stream publish testing/testing -
      INFO server comment - LiveStreamPacketizerCupertino.init[live/_definst_/testing/testing]: chunkDurationTarget: 10000
      INFO server comment - LiveStreamPacketizerCupertino.init[live/_definst_/testing/testing]: chunkDurationTolerance: 500
      INFO server comment - LiveStreamPacketizerCupertino.init[live/_definst_/testing/testing]: audioGroupCount: 3
      INFO server comment - LiveStreamPacketizerCupertino.init[live/_definst_/testing/testing]: playlistChunkCount:3
      INFO server comment - MediaStreamMap.getLiveStreamPacketizer: Create live stream packetizer: cupertinostreamingpacketizer:testing/testing
      INFO server comment - MediaStreamMap.getLiveStreamPacketizer: Create live stream packetizer: smoothstreamingpacketizer:testing/testing
      INFO server comment - CupertinoPacketHandler.startStream[live/_definst_/testing/testing]
      INFO server comment - LiveStreamPacketizerSmoothStreaming.startStream[live/_definst_/testing/testing]
      INFO server comment - LiveStreamPacketizerCupertino.handlePacket[live/_definst_/testing/testing]: Audio codec:MP3 isCompatible:true
      INFO server comment - LiveStreamPacketizerSmoothStreaming.flushPendingAu dio: Bitrate[live/_definst_/testing/testing]: 127522
      INFO server comment - LiveStreamPacketizerSmoothStreaming.addFragment[live/_definst_/testing/testing]: Add chunk: type:audio id:0 count:77 duration:2021
      INFO server comment - LiveStreamPacketizerSmoothStreaming.addFragment[live/_definst_/testing/testing]: Add chunk: type:audio id:1 count:77 duration:2010
      INFO stream unpublish testing/testing -
      INFO stream destroy testing/testing -
    1. rrlanham -
      It will work. Have you tried copying and pasting this module as is:

      http://www.wowzamedia.com/forums/con...te-stream-name)

      Then add the Module to a live application.

      Richard
    1. kyanit -
      Yup, copied and pasted, made the relivent package and Application.xml changes. And when that didn't work I droped in a 'onStreamCreate' function to make sure that the class was being loaded, which it was.
    1. rrlanham -
      You will have to package it up so we can look.

      Zip up and send conf *and* logs folders and send to support@wowzamedia.com. Please include everything in those folders. If there are tons of old logs, you can delete most but make sure the last re-start of Wowza and your recent testing is included.

      Please make the simplest possible application module to show what you are doing, and include that.

      Include a screen shot of your encoder.

      Include a link to this thread for reference.

      Richard
    1. kyanit -
      That's been sent now
    1. hranum -
      Tricky litle thing this one. I've had checks in onPublish, but just found out FMLE can release and shutdown a stream before onPublish is hit! To keep that logic in OnPublish I override relaseStream.

      public void releaseStream(IClient client, RequestFunction function, AMFDataList params) {
      // Keep empty, do not call invokePrevious
      }
    1. Marian -
      Hi,

      I've realated question. I create stream with
      static Stream createInstance(IApplicationInstance appInstance, String sName);
      In
      public void onStreamCreate(IMediaStream stream)
      I would like to get stream name that was create however stream.getName() gives me empty string. Do you know how to get stream name here.

      Regards,
      Marek
    1. rrlanham -
      Marek,

      You can do this:
      Code:
      IMediaStream imediaStream = stream.getPublisher().getStream();
      String streamName = imediaStream.getName();
      Also, take a look at the onPlaylistItemStart event handler in this example:
      http://www.wowza.com/forums/content....t-in-smil-file

      Richard
    1. capostrike93 -
      Quote Originally Posted by rrlanham View Post
      You will have to package it up so we can look.

      Zip up and send conf *and* logs folders and send to support@wowzamedia.com. Please include everything in those folders. If there are tons of old logs, you can delete most but make sure the last re-start of Wowza and your recent testing is included.

      Please make the simplest possible application module to show what you are doing, and include that.

      Include a screen shot of your encoder.

      Include a link to this thread for reference.

      Richard
      the problem is the module base no call invokeprevious.., if you put this module in top of modules list the module base is called first and this module never is called...
    1. A. Obuhov -
      Hello!
      We are successfully using this module, but now we have met some issue. We are using Adobe FMLE for live streaming and we have now not very stable Internet connection. When Internet connection interrupts, FMLE can not restart encoding because of stream name is already in use:
      Code:
      Wed Nov 30 2011 13:49:36 : Primary - Network Status: NetConnection.Connect.Closed status
      Wed Nov 30 2011 13:49:41 : Primary - Disconnected
      Wed Nov 30 2011 13:49:44 : Primary - Re-establishing connection, attempt 1
      Wed Nov 30 2011 13:49:45 : Primary - Reconnected to FMS/3,5,5,2004
      Wed Nov 30 2011 13:49:45 : Primary - Network Status: NetStream.Publish.Denied error Stream name is already in use: soyuz
      Wed Nov 30 2011 13:49:45 : Primary - Network Command: onFCPublish
      Wed Nov 30 2011 13:49:45 : Primary - Stream[soyuz] Status: Success
      Wed Nov 30 2011 13:49:45 : Primary - Stream[soyuz] Status: NetStream.Publish.Start
      Wed Nov 30 2011 13:49:45 : Primary - Stream[soyuz] Status: NetStream.Unpublish.Success
      Wed Nov 30 2011 13:49:45 : Primary - Network Command: onFCUnpublish
      Wed Nov 30 2011 13:49:45 : Primary - Network Status: NetConnection.Connect.Closed status
      Wed Nov 30 2011 13:49:45 : Primary - Disconnected
      Wed Nov 30 2011 15:08:43 : Audio Encoding Stopped
      Wed Nov 30 2011 15:08:43 : Video Encoding Stopped
      Wed Nov 30 2011 15:08:43 : Session Stopped
      Wed Nov 30 2011 15:08:47 : Primary - Connected to FMS/3,5,5,2004
      Wed Nov 30 2011 15:08:47 : Primary - Network Command: onFCPublish
      Wed Nov 30 2011 15:08:47 : Primary - Stream[soyuz] Status: Success
      Wed Nov 30 2011 15:08:47 : Primary - Stream[soyuz] Status: NetStream.Publish.Start
      Wed Nov 30 2011 15:08:48 : Session Started
      Wed Nov 30 2011 15:08:48 : Video Encoding Started 
      Wed Nov 30 2011 15:08:48 : Audio Encoding Started
      The trouble is that FMLE is continuing encoding as nothing happen, though it got the error message and connection to server is closed. At 15:08:43 I have restarted manually and it worked out for me, but what to do with automated restarts? I have to restart encoding manually every time now.
      Thanks for soon answer!
    1. rrlanham -
      Looks okay. Your custom module is logging:

      INFO server comment - I am being hit!
      Richard
    1. A. Obuhov -
      But why stream name is in use? Encoder is disconnected while link is broken. Shouldn't stream name to be released just at disconnection?