Wowza Community

FME 3.0 -> Wowza -> Run HTTP request (POST/GET)

(solution on page 2)

I’ve searched the forums for this answer, but no luck.

I’m using FME to broadcast a video stream to Wowza. It works fine. But I need a way to tell my MySQL DB about the current stream, so others can access it.

Wowza has to send a request to “http://…webservice.php” with POST/GET variables

When I publish a stream, I want Wowza to tell my “webservice.php” by POST/GET that “public-ip-of-server”, “stream”, “streamname” has been started, so my webservice can update my database.

When I stop publishing the stream, I want Wowza to run the webservice again, with a different POST/GET variable, so it can tell the DB that stream has ended.

I know very litle Java, so it’s hard for me to edit the code. I’m using the LiveStreamRecordzip module to record, and my “webservice.php” does the job of renaming file and move it to my S3 bucket, when recording is done :slight_smile: )

That’s my plan.

Could anyone cooperate with me and suggest me the best way to accomplish this POST/GET functinality?

First off welcome to Wowza!

Second, this is probably not the best way to do it. Making HTTP calls to PHP is pretty inefficient. So under heavy load this is most likely not going to perform great. I would suggest that you do the DB updates directly from Java using JDBC. That being said…

If you still want to do it with HTTP then take a look at the com.wowza.HTTPUtils class. It contains pre-built methods for making HTTP requests.

Charlie

Download the free Pro10 edition and get a free Pro10 license from here:

http://www.wowza.com/store.html

It includes all the Javadocs, examples…

Charlie

I made some adjustments. Are you using the wizard to create new Wowza Class Module? It will help. You should not alter the signature of methods like onStreamCreate, I don’t think they will work properly.

package;

import com.wowza.wms.module.*;
import com.wowza.wms.stream.*;
import com.wowza.util.HTTPUtils;

public class Testmodule extends ModuleBase {

	public void onStreamCreate(IMediaStream stream) {
		getLogger().info("publishReport: Connecting to server");
		
		String server_url = "http://myserver.com/webservice.php";
		String application = stream.getClient().getAppInstance().getApplication().getName();
		String streamName = stream.getName();
		String post_data = "?status=Publish&application=" + application + "&stream=" + streamName;
		HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);
	}

	public void onStreamDestroy(IMediaStream stream) {
		getLogger().info("unpublishReport: Connecting to server");
		
		String server_url = "http://myserver.com/webservice.php";
		String application = stream.getClient().getAppInstance().getApplication().getName();
		String streamName = stream.getName();
		String post_data = "?status=unPublish&application=" + application + "&stream=" + streamName;
		HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);
	}
		
}

Richard

Just to post to a web page, this works for me:

// put this with the other imports
import java.net.*;
String url = "http://yourdomain/somepage.php?var=val";
try
{
  URL url = new URL(url);
  URLConnection connect = url.openConnection();
  connect.getContent(); 
}
catch(Exception e)
{		
}

Richard

That last was just a snip, to use instead HTTPUtils, just that part. You should put that together with the complete Wowza Module before, which I made some modifications to already. It’s what I have used.

The Wowza IDE has two very useful wizards from the menu. Use these for new Project and Wowza Class files to be sure you are setting up correctly.

File --> New --> Wowza Media Server Pro Project

File --> New --> Wowza Media Server Pro Module Class

Richard

Great!

Richard

Thank you for response Charlie.

I’ve skipped the Module, and I’m now using a plain live-record on my EC2 instance.

But, I can’t find the Javadocs or anything on the installation on my EC2 instance (/home/wowza/…) that helps me build this .java file

Do I just create the .java file and add it to in Servers.xml ? Where can I read more on this?

Thank you

Ok, I know absolutly no Java. But I’ve accomplished to create a project with the IDE tools, and tried to collect some data I’ve found on the forums.

HTTPutils gives me an error, I’m not sure this is correct.

We’re soon going bigscale on EC2 for normal streaming, and this part with Live streaming will also be running in the next week, so it would be cool to have this running asap.

package com.wowza.wms.module;
import com.wowza.wms.application.*;
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.*;
public class Testmodule extends ModuleBase {
	private void onStreamCreate(String message, IApplicationInstance appInstance) {
		getLogger().info("publishReport: Connecting to server");
		
		String server_url = "http://myserver.com/webservice.php";
		String application = appInstance.getApplication();
		String stream = appInstance.getName();
		String post_data = "status=Publish&application=" + application + "&stream=" + stream;
		HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);
	}
	private void onStreamDestroy(String message, IApplicationInstance appInstance) {
		getLogger().info("unpublishReport: Connecting to server");
		
		String server_url = "http://myserver.com/webservice.php";
		String application = appInstance.getApplication();
		String stream = appInstance.getName();
		String post_data = "status=unPublish&application=" + application + "&stream=" + stream;
		HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);
	}
		
}

Thank you for response Richard, this is what I’ve done so far:

I create a new “Wowza Media Server Project”.

Project name: webservice

Project settings: default

Package: com.wowza.wms.module

Name: Webservice

Custom method: empty

Method events: empty

Inserting the code you posted above into Webservice.java, but with

package com.wowza.wms.module;

at the top of the code.

I tried running the code from Wowza IDE, and connect with FME. Logs tell me that the stream is published, and unpublished, but it does not reach my webservice. (I’ve made sure the http://myurl/webservice.php exists, and the PHP code just simply inserts a empty row on my DB" (not based on any POST data wowza sends). So, all in all, Wowza does not call the webservice.

INFO session connect 127.0.0.1 -
INFO stream create - -
INFO stream publish livestream -
INFO stream unpublish livestream -
INFO stream destroy livestream -

Am I missing out on something important here? Do I have to BUILD the jar file and include it manually, when testing/debugging? So what is the next step?

Thank you in advance

I think I missunderstood your last post, because this gives just alot of “red” lines and errors.

This is a simple problem, but I’m sorry to be not compentent to do this. (Noone even answers me on Scriptlance or GAF).

package com.wowza.wms.module;
import com.wowza.wms.stream.*;
import com.wowza.util.HTTPUtils;
import java.net.*;
String url = "http://yourdomain/somepage.php?var=val";
try
{
  URL url = new URL(url);
  URLConnection connect = url.openConnection();
  connect.getContent(); 
}
catch(Exception e)
{		
}

It was what I thought! :slight_smile: Thank you it works perfectly now.

For anyone trying to the same thing, this is what I did:

Create new project ->

Project name: webservice

Project settings: default

Create new class ->

Package: com.wowza.wms.module

Name: Webservice

Custom method: empty

Method events: empty

Then I use this code in the Webservice.java file

package com.wowza.wms.module;

import java.net.*;
import com.wowza.wms.stream.*;

public class Webservice extends ModuleBase {

	public void onStreamCreate(IMediaStream stream) {
		String url = "http://xxx/webservice.php";
		try
		{
			URL webserviceURL= new URL(url);
			URLConnection exe = webserviceURL.openConnection();
			exe.getContent(); 
		}
		catch(Exception e)
		{		
		}
	}

	public void onStreamDestroy(IMediaStream stream) {
		String url = "http://xxx/webservice.php";
		try
		{
			URL webserviceURL = new URL(url);
			URLConnection exe = webserviceURL.openConnection();
			exe.getContent(); 
		}
		catch(Exception e)
		{		
		}
	}
		
}

I added this to Application.xml, at the end of the part (the application I want to use this module)


			<Module>
				<Name>webservice</Name>
				<Description>webservice</Description>
				<Class>com.wowza.wms.module.Webservice</Class>
			</Module> 

I also check if webservice.JAR is located in the /lib folder.

I restart Wowza, and it works perfectly.

I will update with how I managed to send Application and Stream name later today.

Because stream-name doesn’t excist before onPublish, I wonder how I can fetch it out, and use it with onStreamCreate? I tried the different approaches on the forum, but it doesn’t quite seem to work.

This is my code.

package com.wowza.wms.module;
import com.wowza.util.*;
import com.wowza.wms.stream.*;
public class Webservice extends ModuleBase {
	public void onStreamCreate(IMediaStream stream) {
		getLogger().info("publishReport: Connecting to server");
		
		String server_url = "http://xxx.net/webservice.php";
		String application = stream.getClient().getAppInstance().getApplication().getName();
		String streamName = stream.getName();
		String post_data = "?status=Publish&application=" + application + "&stream=" + streamName;
		HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);
	}
	public void onStreamDestroy(IMediaStream stream) {
		getLogger().info("unpublishReport: Connecting to server");
		
		String server_url = "http://xxx.net/webservice.php";
		String application = stream.getClient().getAppInstance().getApplication().getName();
		String streamName = stream.getName();
		String post_data = "?status=unPublish&application=" + application + "&stream=" + streamName;
		HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);
	}
		
}

This does what I want.

package com.wowza.wms.module;
import com.wowza.util.*;
import com.wowza.wms.stream.*;
import com.wowza.wms.logging.*;
import com.wowza.wms.amf.*;
public class Webservice extends ModuleBase implements IModuleOnStream {
	class ActionNotify implements IMediaStreamActionNotify
	{
		public void onPause(IMediaStream stream, boolean isPause, double location)
		{
			WMSLoggerFactory.getLogger(null).info("onPause: "+stream.getSrc());
		}
		public void onPlay(IMediaStream stream, String streamName, double playStart, double playLen, int playReset)
		{
			WMSLoggerFactory.getLogger(null).info("onPlay: "+stream.getSrc());
		}
		public void onPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend)
		{
			getLogger().info("publishReport: Connecting to server");
			String server_url = "http://xxx.xxx.net/webservice.php";
			String application = stream.getClient().getAppInstance().getApplication().getName();
			String post_data = "?status=Publish&application=" + application + "&stream=" + streamName;
			HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);		
		}
		public void onSeek(IMediaStream stream, double location)
		{
			WMSLoggerFactory.getLogger(null).info("onSeek: "+stream.getSrc());
		}
		public void onStop(IMediaStream stream)
		{
			WMSLoggerFactory.getLogger(null).info("onStop: "+stream.getSrc());
		}
		public void onUnPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend)
		{
			getLogger().info("unpublishReport: Connecting to server");
			String server_url = "http://xxx.xxx.net/webservice.php";
			String application = stream.getClient().getAppInstance().getApplication().getName();
			String post_data = "?status=unPublish&application=" + application + "&stream=" + streamName;
			HTTPUtils.HTTPRequestToByteArray(server_url, "POST", post_data, null);
		}
		public void onMetaData(IMediaStream stream, AMFPacket metaDataPacket)
		{
			WMSLoggerFactory.getLogger(null).info("onMetaData: "+stream.getSrc());
		}
		public void onPauseRaw(IMediaStream stream, boolean isPause, double location)
		{
			WMSLoggerFactory.getLogger(null).info("onPauseRaw: "+stream.getSrc());
		}
	}
	public void onStreamCreate(IMediaStream stream) {
		stream.addClientListener(new ActionNotify());
	}
	public void onStreamDestroy(IMediaStream stream) {
		
	}
}