Created
March 29, 2022 15:42
-
-
Save mondain/d485f8b655291f252eb7c6488580f9f4 to your computer and use it in GitHub Desktop.
Example LiveStreamListService for Red5 Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.Map; | |
import org.red5.server.api.scope.IScope; | |
import org.springframework.core.io.Resource; | |
public class LiveStreamListService { | |
// thread-safe in newer jdks | |
private static SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy H:mm:ss", Locale.US); | |
private Red5ProLive app; | |
public LiveStreamListService(Red5ProLive owner) { | |
app = owner; | |
} | |
public List<String> getLiveStreams() { | |
List<String> ret = app.getLiveStreams(); | |
return ret; | |
} | |
public Map<String, Map<String, Object>> getListOfAvailableFLVs() { | |
IScope scope = app.getScope(); | |
Map<String, Map<String, Object>> filesMap = new HashMap<>(); | |
try { | |
addToMap(filesMap, scope.getResources("streams/*.flv")); | |
addToMap(filesMap, scope.getResources("streams/*.mp4")); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return filesMap; | |
} | |
private String formatDate(Date date) { | |
return formatter.format(date); | |
} | |
private void addToMap(Map<String, Map<String, Object>> filesMap, Resource[] files) throws IOException { | |
if (files != null) { | |
for (Resource flv : files) { | |
File file = flv.getFile(); | |
Date lastModifiedDate = new Date(file.lastModified()); | |
String lastModified = formatDate(lastModifiedDate); | |
String flvName = flv.getFile().getName(); | |
String flvBytes = Long.toString(file.length()); | |
Map<String, Object> fileInfo = new HashMap<>(); | |
fileInfo.put("name", flvName); | |
fileInfo.put("lastModified", lastModified); | |
fileInfo.put("size", flvBytes); | |
filesMap.put(flvName, fileInfo); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspiration for this is from https://github.com/Red5/red5-examples/blob/master/oflaDemo/src/main/java/org/red5/demos/oflaDemo/DemoServiceImpl.java