Commit 58de0e05 authored by Adrien Béraud's avatar Adrien Béraud

media encoder: fix with Android' hardware encoding

* initialize when sending first packet
* avoid out of bound access

Change-Id: I8dc02a851cf89a5ad704e5bd0ea65245085c6ef7
parent b85d3342
......@@ -463,7 +463,7 @@ MediaEncoder::encode(AVFrame* frame, int streamIdx)
// Initialize on first video frame, or first audio frame if no video stream
bool isVideo = (frame->width > 0 && frame->height > 0);
if (isVideo or not videoOpts_.isValid()) {
initStream(videoCodec_, frame->hw_frames_ctx);
streamIdx = initStream(videoCodec_, frame->hw_frames_ctx);
startIO();
} else {
return 0;
......@@ -502,16 +502,22 @@ MediaEncoder::encode(AVFrame* frame, int streamIdx)
bool
MediaEncoder::send(AVPacket& pkt, int streamIdx)
{
if (!initialized_) {
streamIdx = initStream(videoCodec_, nullptr);
startIO();
}
if (streamIdx < 0)
streamIdx = currentStreamIdx_;
auto encoderCtx = encoders_[streamIdx];
pkt.stream_index = streamIdx;
if (pkt.pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(pkt.pts, encoderCtx->time_base,
outputCtx_->streams[streamIdx]->time_base);
if (pkt.dts != AV_NOPTS_VALUE)
pkt.dts = av_rescale_q(pkt.dts, encoderCtx->time_base,
outputCtx_->streams[streamIdx]->time_base);
if (streamIdx >= 0 and streamIdx < encoders_.size()) {
auto encoderCtx = encoders_[streamIdx];
pkt.stream_index = streamIdx;
if (pkt.pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(pkt.pts, encoderCtx->time_base,
outputCtx_->streams[streamIdx]->time_base);
if (pkt.dts != AV_NOPTS_VALUE)
pkt.dts = av_rescale_q(pkt.dts, encoderCtx->time_base,
outputCtx_->streams[streamIdx]->time_base);
}
// write the compressed frame
auto ret = av_write_frame(outputCtx_, &pkt);
if (ret < 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment