
Originally Posted by
rrlanham
You will have to work through it. One correction tho, in IMediaStreamActionNotify2 you should start the timer in the onPlay and stop the timer in onStop
Richard
Hello People,
I´m trying to do something similiar but is not working.
Actually what I want to do is to start the stream at the 0 second of the music and the Wowza by him self stop the streaming after 30 seconds.
I´m new at WMS and still am trying to understand the idea of extending Wowza.
One thing that I notice is that the onPlay e onStop methods from MyMediaStreamListener only are called when somebody press the button on the the player.
What can I do?
I am send the codes that I Have until now.
MyMediaStreamListener with the WatchDog code to
Code:
package br.com.imusica.stream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import br.com.imusica.util.Reader;
import com.wowza.wms.amf.AMFPacket;
import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.stream.IMediaStream;
import com.wowza.wms.stream.IMediaStreamActionNotify2;
import com.wowza.wms.stream.MediaStreamMap;
public class MyMediaStreamListener implements IMediaStreamActionNotify2 {
private Tests novoTests;
private Reader server_;
public MyMediaStreamListener(Tests tests) {
novoTests = tests;
server_ = new Reader();
getServer_().findServers("/usr/local/WowzaMediaServer/content/Imusica.conf");
}
public void onMetaData(IMediaStream stream, AMFPacket metaDataPacket) {
novoTests.log("------------- onMetaData --------------");
}
public void onPauseRaw(IMediaStream stream, boolean isPause, double location) {
novoTests.log("------------- onPauseRaw --------------");
novoTests.log("valor = " + isPause);
novoTests.log("location = " + location);
novoTests.log("-------------------------------------");
}
public void onPause(IMediaStream stream, boolean isPause, double location) {
novoTests.log("-------------ON PAUSE--------------");
novoTests.log("valor = " + isPause);
novoTests.log("location = " + location);
novoTests.log("-------------------------------------");
}
public void onPlay(IMediaStream stream, String streamName, double playStart, double playLen, int playReset) {
novoTests.log("onPublish - stream name: " + stream.getName());
novoTests.log("query string: " + stream.getQueryStr());
novoTests.log("video codec: " + stream.getPublishVideoCodecId());
StreamWatchDog watchdog = new StreamWatchDog();
IApplicationInstance appInstance;
try {
try {
appInstance = stream.getClient().getAppInstance();
}
catch (Exception ex) {
appInstance = stream.getRTPStream().getAppInstance();
}
watchdog.appInstance = appInstance;
watchdog.streamName = streamName;
watchdog.start();
appInstance.getProperties().setProperty(streamName, watchdog);
}
catch (Exception ex) {
}
novoTests.log("-------------ON PLAY-------------- " + " IMediaStream: " + stream.getName() + " StreamName: " + streamName + " PlayStart: " + playStart + " PlayLen: " + playLen + " PlayReset: " + playReset);
int id = 0;
String queryStr = "";
String sessionid = "";
String portalHost = "";
String userAgentId = "0";
String ip = "";
String extension = "";
String ehWap = "0";
if (stream.getRTPStream() == null) {
queryStr = stream.getQueryStr();
// buscando o id do fonograma na query string
if (stream.getQueryStr().split("=").length >= 2) {
id = Integer.parseInt(stream.getQueryStr().split("=")[1]);
}
// buscando o session id do usuário na query string
if (stream.getQueryStr().split("=").length >= 3) {
sessionid = stream.getQueryStr().split("=")[2];
}
// buscando a url do portal (para fazer as requisições necessárias) na query string
if (stream.getQueryStr().split("=").length >= 4) {
portalHost = stream.getQueryStr().split("=")[3];
}
// buscando o userAgentId na query string
if (stream.getQueryStr().split("=").length >= 5) {
userAgentId = stream.getQueryStr().split("=")[4];
}
ip = stream.getClient().getIp();
extension = stream.getExt();
}
else {
ehWap = "1";
queryStr = stream.getRTPStream().getStreamQueryStr();
// buscando o id do fonograma na query string
if (stream.getRTPStream().getStreamQueryStr().split("=").length >= 2) {
id = Integer.parseInt(stream.getRTPStream().getStreamQueryStr().split("=")[1]);
}
// buscando o session id do usuário na query string
if (stream.getRTPStream().getStreamQueryStr().split("=").length >= 3) {
sessionid = stream.getRTPStream().getStreamQueryStr().split("=")[2];
}
// buscando a url do portal (para fazer as requisições necessárias) na query string
if (stream.getQueryStr().split("=").length >= 4) {
portalHost = stream.getQueryStr().split("=")[3];
}
// buscando o userAgentId na query string
if (stream.getQueryStr().split("=").length >= 5) {
userAgentId = stream.getQueryStr().split("=")[4];
}
ip = stream.getRTPStream().getRTSPOriginIpAddress();
extension = stream.getRTPStream().getStreamExt();
}
if (!extension.equalsIgnoreCase("flv")) {
HttpClient httpClient = new HttpClient();
PostMethod post = new PostMethod("http://" + portalHost + "/playHistoryController/saveBegin/");
post.addParameter("phonogramId", new Integer(id).toString());
post.addParameter("sessionId", sessionid);
post.addParameter("ipAddress", ip);
post.addParameter("userAgentId", userAgentId);
post.addParameter("wap", ehWap);
try {
int returnCode = httpClient.executeMethod(post);
BufferedReader br = null;
String readLine = "";
String idHistory = "";
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
novoTests.log("The Post method is not implemented by this URI");
// still consume the response body
post.getResponseBodyAsString();
}
else {
br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
novoTests.log("-------------RESPONSE BODY SAVE BEGIN-------------------");
novoTests.log("-------------RETURN CODE = " + returnCode + "-------------------");
while (((readLine = br.readLine()) != null)) {
// {"idHistory":id}
idHistory = readLine.substring(14, (readLine.length() - 2));
novoTests.log("ID HISTORY = " + idHistory);
novoTests.log(readLine);
}
novoTests.log("---------------------------------------------------------");
}
if (stream.getRTPStream() == null) {
queryStr = queryStr + "=" + idHistory;
stream.setQueryStr(queryStr);
}
else {
queryStr = queryStr + "=" + idHistory;
stream.setQueryStr(queryStr);
}
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
public void onUnPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend) {
}
public void onPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend) {
}
public void onSeek(IMediaStream stream, double location) {
// TODO Auto-generated method stub
novoTests.log("--------------------- onSeek ---------------------");
}
public void onStop(IMediaStream stream) {
novoTests.log("onUNPublish");
StreamWatchDog watchdog = (StreamWatchDog) stream.getClient().getAppInstance().getProperties().getProperty(stream.getName());
if (watchdog != null) {
watchdog.stop();
}
// TODO Auto-generatedmethod stub
String sessionid = "";
String portalHost = "";
String idHistory = "";
String userAgentId = "0";
int time = 0;
String extension = "";
if (stream.getRTPStream() == null) {
// buscando o session id do usuário na query string
if (stream.getQueryStr().split("=").length >= 3) {
sessionid = stream.getQueryStr().split("=")[2];
}
// buscando a url do portal (para fazer as requisições necessárias) na query string
if (stream.getQueryStr().split("=").length >= 4) {
portalHost = stream.getQueryStr().split("=")[3];
}
// buscando o userAgentId na query string
if (stream.getQueryStr().split("=").length >= 5) {
userAgentId = stream.getQueryStr().split("=")[4];
}
// buscando o idHistory da execução na query string
if (stream.getQueryStr().split("=").length >= 6) {
idHistory = stream.getQueryStr().split("=")[5];
}
time = (int) stream.getElapsedTime().getTimeSeconds();
extension = stream.getExt();
}
else {
// buscando o session id do usuário na query string
if (stream.getQueryStr().split("=").length >= 3) {
sessionid = stream.getQueryStr().split("=")[2];
}
// buscando a url do portal (para fazer as requisições necessárias) na query string
if (stream.getQueryStr().split("=").length >= 4) {
portalHost = stream.getQueryStr().split("=")[3];
}
// buscando o userAgentId na query string
if (stream.getQueryStr().split("=").length >= 5) {
userAgentId = stream.getQueryStr().split("=")[4];
}
// buscando o idHistory da execução na query string
if (stream.getQueryStr().split("=").length >= 6) {
idHistory = stream.getQueryStr().split("=")[5];
}
time = (int) stream.getElapsedTime().getTimeSeconds();
extension = stream.getRTPStream().getStreamExt();
}
novoTests.log("onStop");
Integer tempo = new Integer(time);
novoTests.log("-------------ON STOP--------------");
novoTests.log("QUERY STRING = " + stream.getQueryStr() + "-------------------------");
novoTests.log("-------SESSION ID = " + sessionid + "---------");
novoTests.log("-------URL PORTAL = " + portalHost + "---------");
novoTests.log("-------USER AGENT ID = " + userAgentId + "---------");
novoTests.log("-------HISTORY ID = " + idHistory + "---------");
novoTests.log("-------TEMPO = " + tempo.toString() + "---------");
// post para php
if (!extension.equalsIgnoreCase("flv")) {
HttpClient httpClient = new HttpClient();
PostMethod post = new PostMethod("http://" + portalHost + "/playHistoryController/saveEnd/");
// post.addParameter("sessionId", sessionid);
post.addParameter("time", tempo.toString());
post.addParameter("idHistory", idHistory);
if (stream.getRTPStream() == null) {
try {
int returnCode = httpClient.executeMethod(post);
BufferedReader br = null;
String readLine;
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
// still consume the response body
post.getResponseBodyAsString();
}
else {
br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
novoTests.log("------------RESPONSE BODY SAVE END-------------");
// novoTests.log("url = " + this.getServer_().getCoreServer_() +
// "/playHistoryController/saveEnd/");
novoTests.log("--------RETURN CODE = " + returnCode + "---------------");
while (((readLine = br.readLine()) != null)) {
novoTests.log(readLine);
}
novoTests.log("----------------------------------------");
}
}
catch (Exception e1) {
e1.printStackTrace();
}
}
else {
novoTests.log("-----------------ENTROU----------------------");
post.addParameter("sessionId", sessionid);
post.addParameter("wap", "1");
try {
int returnCode = httpClient.executeMethod(post);
BufferedReader br = null;
String readLine;
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
// still consume the response body
post.getResponseBodyAsString();
}
else {
br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
novoTests.log("------------RESPONSE BODY SAVE END-------------");
// novoTests.log("url = " + this.getServer_().getCoreServer_() +
// "/playHistoryController/saveEnd/");
novoTests.log("--------RETURN CODE = " + returnCode + "---------------");
while (((readLine = br.readLine()) != null)) {
novoTests.log(readLine);
}
novoTests.log("----------------------------------------");
}
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
public void setServer_(Reader server) {
server_ = server;
}
public Reader getServer_() {
return server_;
}
private class StreamWatchDog {
public Timer mTimer;
public TimerTask mTask;
public String streamName;
public IApplicationInstance appInstance;
long streamLastSeq;
boolean isMissing = false;
long currSeq = 0;
String msg;
public StreamWatchDog() {
mTask = new TimerTask() {
public void run() {
novoTests.log("Run StreamWatchDog");
MediaStreamMap mediamap = appInstance.getStreams();
IMediaStream stream = mediamap.getStream(streamName);
List packets = stream.getPlayPackets();
if (packets.size() == 0) {
msg = "Stream not started";
}
else {
AMFPacket packet = (AMFPacket) packets.get(packets.size() - 1);
currSeq = packet.getSeq();
if (currSeq != streamLastSeq) {
streamLastSeq = currSeq;
msg = "Stream OK";
}
else {
msg = "Stream Appears Stalled";
}
}
appInstance.broadcastMsg("streamStats", streamName, currSeq, msg, new Date());
}
};
}
public void start() {
if (mTimer == null) {
mTimer = new Timer();
}
mTimer.schedule(mTask, 10000, 10000);
novoTests.log("Start StreamWatchDog");
}
public void stop() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
novoTests.log("Stop StreamWatchDog");
}
}
}
}
And the WAP classe that implements IModuleOnRTPSession
Code:
package br.com.imusica.stream;
import com.wowza.wms.module.IModuleOnRTPSession;
import com.wowza.wms.module.ModuleBase;
import com.wowza.wms.rtp.model.RTPSession;
public class Wap extends ModuleBase implements IModuleOnRTPSession {
public void onRTPSessionCreate(RTPSession rtpSession) {
getLogger().info("onRTPSessionCreate: " + rtpSession.getSessionId());
String someCookie = rtpSession.getQueryStr();
getLogger().info("----------" + someCookie + "---------------");
}
public void onRTPSessionDestroy(RTPSession rtpSession) {
getLogger().info("onRTPSessionDestroy: " + rtpSession.getSessionId());
String someCookie = rtpSession.getQueryStr();
getLogger().info("----------" + someCookie + "---------------");
}
}