Wowza Reload Schedule via HTTP

Bài đăng lúc: 14:00:26 03/07/2016
Live Streaming
0 0
Rate this post

WowzaStreamingEngine là một trong ba phần mềm làm Live/VoD stream tốt nhất hiện nay. Chi phí mua bản quyền phần mềm này không quá đắt, chỉ khoảng 2.000$ cho bản lifetime [Perpetual Bundle Unlimited License].

Bài viết này sẽ giúp cho việc reload schedule trở nên dễ dàng hơn, thay vì phải mở phần mềm Java Jconsole, đăng nhập và tìm đến app cần reload mất khá nhiều thời gian, làm cho dịch vụ bị gián đoạn khá lâu. Thì bây giờ chỉ cần nhập link trên browser hoặc gọi link với câu lênh CURL. Hơn nữa cũng có thể lập lịch [crontab] gọi link reload schedule một cách tự động.

Đầu tiên bạn cần download file thư viện tại đây và copy vào thư mục lib của Wowza:

ví dụ: /usr/local/WowzaStreamingEngine/lib/

Sau đó edit VHost.xml trong /usr/local/WOwzaStreamingEngine/conf/ và thêm nội dung bên dưới vào phần HTTPProviders

<HTTPProvider>
    <BaseClass>test.HTTPReloadSchedule</BaseClass>
    <RequestFilters>scheduleloader*</RequestFilters>
    <AuthenticationMethod>none</AuthenticationMethod>
</HTTPProvider>

Reload schedule:

http://[wowza-address]:PORT/scheduleloader?action=load&app=APP-NAME

Unload schedule:

http://[wowza-address]:PORT/scheduleloader?action=unload&app=APP-NAME

Xem thêm: Nôi dung file thư viện ở trên:

package test;

import java.util.List;
import java.io.OutputStream;
import java.util.Map;
import java.util.stream.Stream;

import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.http.HTTProvider2Base;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.wowza.wms.plugin.collection.serverlistener.ServerListenerStreamPublisher;
import com.wowza.wms.server.Server;
import com.wowza.wms.vhost.IVHost;

public class HTTPReloadSchedule extends HTTProvider2Base {
	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
		if (!doHTTPAuthentication(vhost, req, resp))
			return;
		WMSLoggerFactory.getLogger(null).error("ThuanNguyen.NET :|: schedule load ===============================");
		final String PROP_NAME_PREFIX = "streamPublisher";

		req.parseBodyForParams(true);
		
		Map<String, List> params = req.getParameterMap();
		
		String action = "";
		String app = "";
		String report = "ThuanNguyen.NET :|: Nothing to do";
		
		ServerListenerStreamPublisher streamPublisher;
		IApplicationInstance appInstance = null;
		WMSLogger logger;
		
		streamPublisher = (ServerListenerStreamPublisher)Server.getInstance().getProperties().get(ServerListenerStreamPublisher.PROP_STREAMPUBLISHER);
		
		
		if (params.containsKey("action"))
			action = params.get("action").get(0);
		
		if (params.containsKey("app"))
		{
			app = params.get("app").get(0);
		}
		
		if (vhost.applicationExists(app)) {
			appInstance = vhost.getApplication(app).getAppInstance("_definst_");
		}
		
		if (action.equalsIgnoreCase("load"))
		{
			try {
				streamPublisher.loadSchedule(appInstance);
				report = "ThuanNguyen.NET :|: Wowza Schedule Streaming Reloaded";
			} 
			catch (Exception e) {
				e.printStackTrace();
				report = "ThuanNguyen.NET :|: Load Schedule Error: " + e.getMessage();
			}
		} 
		else if (action.equalsIgnoreCase("unload"))
		{
			Map<String, Stream> streams = (Map<String, Stream>)appInstance.getProperties().remove(PROP_NAME_PREFIX + "Streams");
			if(streams != null)
			{
				for(Stream stream : streams.values())
				{
					streamPublisher.shutdownStream(appInstance, (com.wowza.wms.stream.publish.Stream) stream);
				}
				streams.clear();
			}
			report = "ThuanNguyen.NET :|: Wowza Schedule Streaming Unloaded";
		}
                String retStr = "<html><head><title>ThuanNguyen.NET :|: Wowza Schedule Streaming Loader</title></head><body><h1>ThuanNguyen.NET :|: Wowza Schedule Streaming Loader</h1>" + report + "</body></html>";
		try {
			OutputStream out = resp.getOutputStream();
			byte[] outBytes = retStr.getBytes();
			out.write(outBytes);
		} catch (Exception e) {
			WMSLoggerFactory.getLogger(null).error(
					"ThuanNguyen.NET :|: MediaCasterHTTP: " + e.toString());
		}

	}
}

ThuanNguyen.NET