Skip to content
Snippets Groups Projects
Commit 58de0e05 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

media encoder: fix with Android' hardware encoding

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

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