package org.oneliveweb.live.streams; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.entermediadb.asset.Asset; import org.entermediadb.asset.MediaArchive; import org.entermediadb.video.CloudTranscodeManager; import org.ijsolutions.testbench.TestBench; import org.ijsolutions.testbench.modules.BaseTestingModule; import org.openedit.Data; import org.openedit.WebPageRequest; import org.openedit.data.Searcher; import org.openedit.page.Page; public class LiveStreamModule extends BaseTestingModule { // https://github.com/arut/nginx-rtmp-module/wiki/Directives#hls_playlist_length // https://www.getpagespeed.com/server-setup/nginx/install-nginx-rtmp-module-in-centos-rhel-or-amazon-linux // https://www.nginx.com/blog/video-streaming-for-remote-learning-with-nginx/ - // did this for building it. // ffmpeg -r 30 -f lavfi -i testsrc -vf scale=1280:960 -vcodec libx264 -profile:v baseline -pix_fmt yuv420p -f flv rtmp://localhost/live/goforit private static final Log log = LogFactory.getLog(LiveStreamManager.class); public void startNginx(WebPageRequest inReq) { TestBench tb = getTestBench(inReq); LiveStreamManager manager = tb.getLiveStreamManager();// (LiveStreamManager) tb.getBean("liveStreamManager"); if(Boolean.parseBoolean(inReq.findValue("local"))) { manager.setLocal(true); } manager.startNginx(); inReq.putPageValue("streammanager", manager); } public void streamStart(WebPageRequest inReq) { log.info("Stream started"); log.info("Param one" + inReq.findValue("param1")); // https://github.com/arut/nginx-rtmp-module/issues/1032 tack // on endlist ? String app = inReq.findValue("app"); String streamid = inReq.getRequestParameter("name"); String catalogid = inReq.findValue("benchid"); TestBench bench = getTestBench(inReq); bench.getLiveStreamManager().startStreams(streamid); } public void streamEnd(WebPageRequest inReq) { log.info("Stream ended"); log.info("Param one" + inReq.findValue("param1")); // https://github.com/arut/nginx-rtmp-module/issues/1032 tack TestBench bench = getTestBench(inReq); String streamid = inReq.getRequestParameter("name"); bench.getLiveStreamManager().cleanupStreams(streamid); } // ffmpeg -r 30 -f lavfi -i testsrc -vf scale=1280:960 -vcodec libx264 // -profile:v baseline -pix_fmt yuv420p -f flv // rtmp://172.25.0.60:1960/live/goforit // ffmpeg -r 30 -f lavfi -i testsrc -vf scale=1280:960 -vcodec libx264 // -profile:v baseline -pix_fmt yuv420p -f flv rtmp://localhost/src/goforit public void finalizeStream(WebPageRequest inReq) { TestBench bench = getTestBench(inReq); String planid = inReq.getRequestParameter("id"); Data plan = bench.getData("livestream", planid); MediaArchive archive = getMediaArchive(inReq); LiveStreamManager manager = bench.getLiveStreamManager();// (LiveStreamManager) tb.getBean("liveStreamManager"); manager.closeVOD(plan); plan.setValue("streamclosed", true); archive.saveData("livestream", plan); } public void convertToAsset(WebPageRequest inReq) { TestBench bench = getTestBench(inReq); String planid = inReq.getRequestParameter("id"); Data plan = bench.getData("livestream", planid); MediaArchive archive = getMediaArchive(inReq); LiveStreamManager manager = bench.getLiveStreamManager();// (LiveStreamManager) tb.getBean("liveStreamManager"); manager.closeVOD(plan); Page output = manager.createMP4(plan); Date now = new Date(); if (output != null) { String sourcepath = "Activities/" + plan.getId() + "/Streams/" + now.getTime(); Asset newasset = bench.createAsset(output, new HashMap(), inReq.getUser(), sourcepath); plan.setValue("assetid", newasset.getId()); archive.saveData("livestream", plan); } // TODO: Copy stream files and video.mp4 over } public void autoCaption(WebPageRequest inReq) { TestBench bench = getTestBench(inReq); String planid = inReq.getRequestParameter("id"); Data plan = bench.getData("livestream", planid); MediaArchive archive = getMediaArchive(inReq); String assetid = plan.get("assetid"); CloudTranscodeManager tmanager = (CloudTranscodeManager) getModuleManager().getBean(archive.getCatalogId(), "cloudTranscodeManager"); String selectedlang = "en"; Asset asset = archive.getAsset(assetid); Data lasttrack = tmanager.addAutoTranscode(archive, selectedlang, asset, inReq.getUserName()); inReq.putPageValue("track", lasttrack); archive.fireSharedMediaEvent("asset/autotranscribe"); } public void deleteStream(WebPageRequest inReq) { TestBench bench = getTestBench(inReq); String planid = inReq.getRequestParameter("id"); Data plan = bench.getData("livestream", planid); LiveStreamManager manager = bench.getLiveStreamManager();// (LiveStreamManager) tb.getBean("liveStreamManager"); manager.deleteStreamFiles(plan); } public void createCaptionIndex(WebPageRequest inReq) { TestBench bench = getTestBench(inReq); String planid = inReq.getRequestParameter("id"); Data plan = bench.getData("livestream", planid); MediaArchive archive = getMediaArchive(inReq); String assetid = plan.get("assetid"); Collection captions = bench.getLiveStreamManager().loadClosedCaptions(assetid); Searcher videopoints = archive.getSearcher("videodata"); for (Iterator iterator = captions.iterator(); iterator.hasNext();) { Data captionset = (Data) iterator.next(); String language = captionset.get("sourcelang"); List extracteddata = (List) captionset.getValues("captions"); for (Iterator iterator2 = extracteddata.iterator(); iterator2.hasNext();) { Map somemap = (Map) iterator2.next(); int timecodelength = (int) somemap.get("timecodelength"); String text = (String) somemap.get("cliplabel"); int timecodestart = (int) somemap.get("timecodestart"); Data currentcaption = videopoints.query().exact("timecodestart", String.valueOf(timecodestart)).exact("assetid", assetid).searchOne(); if(currentcaption == null) { currentcaption = videopoints.createNewData(); } currentcaption.setValue("timecodelength", timecodelength); currentcaption.setValue("cliplabel", text); currentcaption.setValue("timecodestart", timecodestart); currentcaption.setValue("assetid", assetid); videopoints.saveData(currentcaption); } } // #foreach( $track in $tracks) // // #end // #end // } }