use of com.feeyo.hls.HlsLiveStream in project feeyo-hlsserver by variflight.
the class HlsLiveHandler method execute.
@Override
public void execute(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
HttpRequest request = (DefaultHttpRequest) e.getMessage();
String uri = request.getUri();
String path = uri.split("[?]")[0].trim();
String[] pathArray = path.split("/");
String alias = pathArray[2];
String requestFile = pathArray[3];
// 校验 alias & requestFile
if (alias == null || requestFile == null) {
HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
return;
}
// 根据 alias 获取 live
HlsLiveStream liveStream = HlsLiveStreamMagr.INSTANCE().getHlsLiveStreamByAlias(alias);
if (liveStream == null) {
HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
return;
}
// live.m3u8
if (requestFile.equals(LIVE_M3U8)) {
HlsClientSession clientSession = null;
// 提取 sid
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
List<String> sessionId = decoder.getParameters().get("sid");
if (sessionId != null && !sessionId.isEmpty()) {
clientSession = liveStream.getClientSessionsById(sessionId.get(0));
}
LOGGER.info("request m3u8 file, uri={}, clientSession={}", uri, clientSession);
// 重定向, 解决标识问题
if (clientSession == null) {
clientSession = liveStream.newClientSession();
StringBuffer url = new StringBuffer(50);
url.append(path).append("?sid=").append(clientSession.getId());
LOGGER.info("response redirect, url={}", url.toString());
HttpResponse response = HttpUtil.redirectFound(url.toString());
e.getChannel().write(response);
return;
}
M3U8 m3u8 = clientSession.getM3u8File(requestFile);
DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
byte[] content = m3u8.getBuf();
long fileMTime = m3u8.getTime();
response.headers().add(HttpHeaders.Names.SERVER, Versions.SERVER_VERSION);
response.headers().add(HttpHeaders.Names.DATE, HttpUtil.getDateString(fileMTime));
response.headers().add(HttpHeaders.Names.CONTENT_TYPE, HttpUtil.getMimeType(requestFile));
response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, content.length);
//
response.headers().add(HttpHeaders.Names.CACHE_CONTROL, "private, max-age=5");
response.setContent(ChannelBuffers.copiedBuffer(content));
e.getChannel().write(response);
// 1...N.ts
} else {
LOGGER.info("request ts file, uri={} ", uri);
int tsIndex = Integer.valueOf(requestFile.substring(0, requestFile.indexOf(".ts"))).intValue();
//
String ifModifiedSince = request.headers().get(HttpHeaders.Names.IF_MODIFIED_SINCE);
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HttpUtil.HTTP_DATE_FORMAT, Locale.US);
Date mdate = dateFormatter.parse(ifModifiedSince);
int mdateSec = (int) (mdate.getTime() / 1000L);
TsSegment tsSegment = liveStream.fetchTsSegment(tsIndex);
int fileMTimeSec = tsSegment != null ? (int) (tsSegment.getCtime() / 1000L) : 0;
if (mdateSec == fileMTimeSec) {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_MODIFIED);
response.headers().add(HttpHeaders.Names.CACHE_CONTROL, "max-age=1");
HttpUtil.sendNotModified(ctx, response);
return;
}
}
TsSegment tsSegment = liveStream.fetchTsSegment(tsIndex);
if (tsSegment == null) {
HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
return;
}
DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
byte[] content = tsSegment.getData();
long fileMTime = tsSegment.getCtime();
response.headers().add(HttpHeaders.Names.SERVER, Versions.SERVER_VERSION);
response.headers().add(HttpHeaders.Names.DATE, HttpUtil.getDateString(fileMTime));
response.headers().add(HttpHeaders.Names.CONTENT_TYPE, HttpUtil.getMimeType(requestFile));
response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, content.length);
response.headers().add(HttpHeaders.Names.LAST_MODIFIED, HttpUtil.getDateString(fileMTime));
// 相对当前的过期时间,以分钟为单位
response.headers().add(HttpHeaders.Names.EXPIRES, HttpUtil.getDateString(fileMTime + LIVE_CACHE_TIME));
response.headers().add(HttpHeaders.Names.CACHE_CONTROL, "max-age=" + (LIVE_CACHE_TIME / 1000));
response.setContent(ChannelBuffers.copiedBuffer(content));
e.getChannel().write(response);
}
}
Aggregations