1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
| package com.wowza.wms.plugin.closedcaption.test;
import java.io.*;
import java.util.*;
import com.wowza.util.*;
import com.wowza.wms.amf.*;
import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.media.model.*;
import com.wowza.wms.module.ModuleBase;
import com.wowza.wms.stream.*;
public class ModulePublishOnTextData extends ModuleBase
{
class OnTextData
{
String text = "";
public OnTextData(String text)
{
this.text = text;
}
}
class PublishThread extends Thread
{
private boolean running = true;
private Object lock = new Object();
private int interval = 500;
private int publishInterval = 1000;
private long lastSend = -1;
private IApplicationInstance appInstance = null;
private IMediaStream stream = null;
private int count = 0;
public void doStop()
{
synchronized(lock)
{
this.running = false;
}
}
public PublishThread(IApplicationInstance appInstance, IMediaStream stream)
{
this.appInstance = appInstance;
this.stream = stream;
}
public void run()
{
getLogger().info("ModulePublishOnTextData#PublishThread.run["+stream.getContextStr()+"]: START");
int index = 0;
while(true)
{
try
{
long currTime = System.currentTimeMillis();
if (lastSend < 0 || (currTime - lastSend) > publishInterval)
{
lastSend = currTime;
while(true)
{
if (onTextDataList.size() <= 0)
break;
OnTextData onTextData = onTextDataList.get(index%onTextDataList.size());
index++;
if (onTextData == null)
break;
sendTextDataMessage(stream, onTextData);
break;
}
}
Thread.sleep(interval);
}
catch(Exception e)
{
getLogger().error("ModulePublishOnTextData#PublishThread.run["+stream.getContextStr()+"]: "+e.toString());
}
synchronized(lock)
{
if (!running)
break;
}
}
getLogger().info("ModulePublishOnTextData#PublishThread.run["+stream.getContextStr()+"]: STOP");
}
public void sendTextDataMessage(IMediaStream stream, OnTextData onTextData)
{
try
{
AMFDataObj amfData = new AMFDataObj();
//System.out.println("Text: "+count);
//debug amfData.put("text", new AMFDataItem("T: "+(count++)));
amfData.put("text", new AMFDataItem(onTextData.text));
amfData.put("language", new AMFDataItem(languageID));
amfData.put("trackid", new AMFDataItem(trackNumber));
stream.sendDirect("onTextData", amfData);
((MediaStream)stream).processSendDirectMessages();
}
catch(Exception e)
{
getLogger().error("ModulePublishOnTextData#PublishThread.sendTextDataMessage["+stream.getContextStr()+"]: "+e.toString());
e.printStackTrace();
}
}
public int getPublishInterval()
{
return publishInterval;
}
public void setPublishInterval(int publishInterval)
{
this.publishInterval = publishInterval;
}
}
public class MyMediaStreamListener implements IMediaStreamActionNotify3
{
private PublishThread publishThread = null;
public void onPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend)
{
IApplicationInstance appInstance = stream.getStreams().getAppInstance();
if (!stream.isTranscodeResult())
{
publishThread = new PublishThread(appInstance, stream);
publishThread.setName("OnTextDataPublisher-"+appInstance.getContextStr()+"-"+streamName);
publishThread.setDaemon(true);
publishThread.setPublishInterval(publishInterval);
publishThread.start();
}
}
public void onUnPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend)
{
if (publishThread != null)
publishThread.doStop();
publishThread = null;
}
public void onMetaData(IMediaStream stream, AMFPacket metaDataPacket)
{
}
public void onPauseRaw(IMediaStream stream, boolean isPause, double location)
{
}
public void onPause(IMediaStream stream, boolean isPause, double location)
{
}
public void onPlay(IMediaStream stream, String streamName, double playStart, double playLen, int playReset)
{
}
public void onSeek(IMediaStream stream, double location)
{
}
public void onStop(IMediaStream stream)
{
}
public void onCodecInfoVideo(IMediaStream stream, MediaCodecInfoVideo codecInfoVideo)
{
}
public void onCodecInfoAudio(IMediaStream stream, MediaCodecInfoAudio codecInfoAudio)
{
}
}
private List<OnTextData> onTextDataList = new ArrayList<OnTextData>();
private boolean charsetTest = false;
private String languageID = "eng";
private int trackNumber = 99;
//private final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private int publishInterval = 1000;
public void onAppStart(IApplicationInstance appInstance)
{
getLogger().info("ModulePublishOnTextData.onAppStart["+appInstance.getContextStr()+"]");
String onTextDataFile = "${com.wowza.wms.context.VHostConfigHome}/content/ontextdata.txt";
publishInterval = appInstance.getProperties().getPropertyInt("publishOnTextDataPublishInterval", publishInterval);
onTextDataFile = appInstance.getProperties().getPropertyStr("publishOnTextDataFile", onTextDataFile);
languageID = appInstance.getProperties().getPropertyStr("publishOnTextDataLanguageID", languageID);
trackNumber = appInstance.getProperties().getPropertyInt("publishOnTextDataTrackNumber", trackNumber);
charsetTest = appInstance.getProperties().getPropertyBoolean("publishOnTextCharsetTest", charsetTest);
Map<String, String> pathMap = new HashMap<String, String>();
pathMap.put("com.wowza.wms.context.VHost", appInstance.getVHost().getName());
pathMap.put("com.wowza.wms.context.VHostConfigHome", appInstance.getVHost().getHomePath());
pathMap.put("com.wowza.wms.context.Application", appInstance.getApplication().getName());
pathMap.put("com.wowza.wms.context.ApplicationInstance", appInstance.getName());
onTextDataFile = SystemUtils.expandEnvironmentVariables(onTextDataFile, pathMap);
File file = new File(onTextDataFile);
getLogger().info("ModulePublishOnTextData.onAppStart["+appInstance.getContextStr()+"]: sendInterval: "+publishInterval);
if (charsetTest)
{
int charCode = 0x20;
int lastChar = 0x100;
int charsPerLine = 20;
while(true)
{
int charsToPublish = lastChar-charCode;
if (charsToPublish > charsPerLine)
charsToPublish = charsPerLine;
String bytesStr = "";
for(int i=0;i<charsToPublish;i++)
{
int thisChar = charCode+i;
// map unicode codepoint to utf-8
int myChar = 0;
if (thisChar >= 0x020 && thisChar < 0x080)
myChar = (int)thisChar;
else if (thisChar >= 0x080 && thisChar < 0x0C0)
myChar = (int)(0x0c280 + (thisChar - 0x080));
else if (thisChar >= 0x0C0 && thisChar < 0x100)
myChar = (int)(0x0c380 + (thisChar - 0x0C0));
try
{
bytesStr += new String(BufferUtils.intToByteArray(myChar, (myChar<0x080?1:2)), "UTF-8");
}
catch(Exception e)
{
}
}
bytesStr = "0x"+Integer.toHexString(charCode)+":"+bytesStr+":";
onTextDataList.add(new OnTextData(bytesStr));
charCode += charsToPublish;
if (charCode >= lastChar)
break;
}
}
else
{
getLogger().info("ModulePublishOnTextData.onAppStart["+appInstance.getContextStr()+"]: onTextDataFile[exists:"+file.exists()+"]: "+onTextDataFile);
BufferedReader inf = null;
if (file.exists())
{
try
{
inf = new BufferedReader(new FileReader(file));
String line;
while ((line = inf.readLine()) != null)
{
line = line.trim();
if (line.startsWith("#"))
continue;
if (line.length() == 0)
continue;
onTextDataList.add(new OnTextData(line));
}
}
catch(Exception e)
{
getLogger().error("ModulePublishOnTextData.onAppStart[read]: "+ e.toString());
}
}
getLogger().info("ModulePublishOnTextData.onAppStart["+appInstance.getContextStr()+"]: onTextDataFileCount: "+onTextDataList.size());
try
{
if (inf != null)
inf.close();
inf = null;
}
catch(Exception e)
{
getLogger().error("ModulePublishOnTextData.onAppStart[close]: "+ e.toString());
}
}
}
public void onStreamCreate(IMediaStream stream)
{
stream.addClientListener(new MyMediaStreamListener());
}
} |
Partager