package ctbrec.sites.showup; import static ctbrec.Model.State.*; import java.io.IOException; import java.text.MessageFormat; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.concurrent.ExecutionException; import javax.xml.bind.JAXBException; import com.iheartradio.m3u8.ParseException; import com.iheartradio.m3u8.PlaylistException; import com.squareup.moshi.JsonReader; import com.squareup.moshi.JsonWriter; import ctbrec.AbstractModel; import ctbrec.Config; import ctbrec.Model; import ctbrec.recorder.download.Download; import ctbrec.recorder.download.StreamSource; public class ShowupModel extends AbstractModel { private long uid; private String streamId; private String streamTranscoderAddr; private int[] resolution = new int[2]; @Override public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { if (ignoreCache) { boolean foundInList = false; List modelList = getShowupSite().getModelList(); for (Model model : modelList) { ShowupModel m = (ShowupModel) model; if (m.getUid() == uid) { setOnlineState(m.getOnlineState(false)); setStreamId(m.getStreamId()); setStreamTranscoderAddr(m.getStreamTranscoderAddr()); setDescription(m.getDescription()); setPreview(m.getPreview()); foundInList = true; } } if (!foundInList) { setOnlineState(State.OFFLINE); } } State state = getOnlineState(true); return state == ONLINE || state == GROUP || state == PRIVATE; } @Override public List getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException, JAXBException { StreamSource src = new StreamSource(); src.width = 480; src.height = 360; if(streamId == null || streamTranscoderAddr == null) { List modelList = getShowupSite().getModelList(); for (Model model : modelList) { ShowupModel m = (ShowupModel) model; if (m.getUid() == uid) { streamId = m.getStreamId(); streamTranscoderAddr = m.getStreamTranscoderAddr(); } } } int cdnHost = 1 + new Random().nextInt(5); src.mediaPlaylistUrl = MessageFormat.format("https://cdn-e0{0}.showup.tv/h5live/http/playlist.m3u8?url=rtmp%3A%2F%2F{1}%3A1935%2Fwebrtc&stream={2}_aac", cdnHost, streamTranscoderAddr, streamId); return Collections.singletonList(src); } @Override public void invalidateCacheEntries() { // noop } @Override public void receiveTip(Double tokens) throws IOException { // noop } @Override public int[] getStreamResolution(boolean failFast) throws ExecutionException { return resolution; } @Override public boolean follow() throws IOException { return false; } @Override public boolean unfollow() throws IOException { return false; } public long getUid() { return uid; } public void setUid(long uid) { this.uid = uid; } @Override public void readSiteSpecificData(JsonReader reader) throws IOException { reader.nextName(); uid = reader.nextLong(); } @Override public void writeSiteSpecificData(JsonWriter writer) throws IOException { writer.name("uid").value(uid); } private Showup getShowupSite() { return (Showup) getSite(); } public String getStreamId() { return streamId; } public void setStreamId(String streamId) { this.streamId = streamId; } public String getStreamTranscoderAddr() { return streamTranscoderAddr; } public void setStreamTranscoderAddr(String streamTranscoderAddr) { this.streamTranscoderAddr = streamTranscoderAddr; } @Override public Download createDownload() { if (Config.isServerMode() && !Config.getInstance().getSettings().recordSingleFile) { return new ShowupDownload(getSite().getHttpClient()); } else { return new ShowupMergedDownload(getSite().getHttpClient()); } } }