org.red5.codec.HEVCVideo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ant-media-server Show documentation
Show all versions of ant-media-server Show documentation
Ant Media Server supports RTMP, RTSP, MP4, HLS, WebRTC, Adaptive Streaming, etc.
/*
* RED5 Open Source Media Server - https://github.com/Red5/ Copyright 2006-2016 by respective authors (see below). All rights reserved. Licensed under the Apache License, Version
* 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless
* required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
package org.red5.codec;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.mina.core.buffer.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Red5 video codec for the AVC (h264) video format. Stores DecoderConfigurationRecord and last keyframe.
*
* @author Tiago Jacobs ([email protected])
* @author Paul Gregoire ([email protected])
*/
public class HEVCVideo extends AVCVideo {
private static Logger log = LoggerFactory.getLogger(HEVCVideo.class);
/**
* AVC video codec constant
*/
static final String CODEC_NAME = "HEVC";
/** {@inheritDoc} */
@Override
public String getName() {
return CODEC_NAME;
}
/** {@inheritDoc} */
@Override
public boolean canDropFrames() {
return true;
}
/** {@inheritDoc} */
@Override
public boolean canHandleData(IoBuffer data) {
boolean result = false;
if (data.limit() > 0) {
// read the first byte and ensure its HEVC / h.265 type
result = ((data.get() & 0x0f) == VideoCodec.HEVC.getId());
data.rewind();
}
return result;
}
/** {@inheritDoc} */
@Override
public boolean addData(IoBuffer data, int timestamp) {
//log.trace("addData timestamp: {} remaining: {}", timestamp, data.remaining());
if (data.hasRemaining()) {
// mark
int start = data.position();
// get frame type
byte frameType = data.get();
byte hevcType = data.get();
if ((frameType & 0x0f) == VideoCodec.HEVC.getId())
{
// check for keyframe
if ((frameType & 0xf0) == FLV_FRAME_KEY) {
if (log.isDebugEnabled()) {
log.debug("Keyframe - HEVC type: {}", hevcType);
}
// rewind
data.rewind();
setFrames(data, timestamp, hevcType);
} else if (bufferInterframes) {
if (log.isDebugEnabled()) {
log.debug("Interframe - HEVC type: {}", hevcType);
}
// rewind
data.rewind();
setInterFrame(data);
}
}
else {
// not HEVC data
log.debug("Non-hevc data, rejecting");
// go back to where we started
data.position(start);
return false;
}
// go back to where we started
data.position(start);
}
return true;
}
}