1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix: Stop using deprecated AVStream::codec

We now use the AVCodecParameters instead, as recommended
This commit is contained in:
tux3 2017-02-16 00:38:14 +01:00 committed by Diadlo
parent 300ac12e3f
commit 9674bff0cf
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 45 additions and 8 deletions

View File

@ -69,6 +69,7 @@ extern "C" {
*
* @var AVCodecContext* CameraSource::cctxOrig
* @brief Codec context of the camera's selected video stream
* @deprecated
*
* @var int CameraSource::videoStreamIndex
* @brief A camera can have multiple streams, this is the one we're decoding
@ -93,7 +94,9 @@ CameraSource::CameraSource()
, device{nullptr}
, mode(VideoMode())
, cctx{nullptr}
#if LIBAVCODEC_VERSION_INT < 3747941
, cctxOrig{nullptr}
#endif
, videoStreamIndex{-1}
, _isOpen{false}
, streamBlocker{false}
@ -190,10 +193,14 @@ CameraSource::~CameraSource()
// Free all remaining VideoFrame
VideoFrame::untrackFrames(id, true);
if (cctx)
if (cctx) {
avcodec_free_context(&cctx);
if (cctxOrig)
}
#if LIBAVCODEC_VERSION_INT < 3747941
if (cctxOrig) {
avcodec_close(cctxOrig);
}
#endif
if (device) {
for (int i = 0; i < subscriptions; ++i)
@ -225,7 +232,10 @@ bool CameraSource::subscribe()
while (device && !device->close()) {
}
device = nullptr;
cctx = cctxOrig = nullptr;
cctx = nullptr;
#if LIBAVCODEC_VERSION_INT < 3747941
cctxOrig = nullptr;
#endif
videoStreamIndex = -1;
return false;
}
@ -281,9 +291,15 @@ bool CameraSource::openDevice()
for (int i = 0; i < subscriptions; ++i)
device->open();
// Find the first video stream
// Find the first video stream, if any
for (unsigned i = 0; i < device->context->nb_streams; ++i) {
if (device->context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
AVMediaType type;
#if LIBAVCODEC_VERSION_INT < 3747941
type = device->context->streams[i]->codec->codec_type;
#else
type = device->context->streams[i]->codecpar->codec_type;
#endif
if (type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
@ -294,20 +310,37 @@ bool CameraSource::openDevice()
return false;
}
// Get a pointer to the codec context for the video stream
AVCodecID codecId;
#if LIBAVCODEC_VERSION_INT < 3747941
cctxOrig = device->context->streams[videoStreamIndex]->codec;
codec = avcodec_find_decoder(cctxOrig->codec_id);
codecId = cctxOrig->codec_id;
#else
// Get the stream's codec's parameters and find a matching decoder
AVCodecParameters* cparams = device->context->streams[videoStreamIndex]->codecpar;
codecId = cparams->codec_id;
#endif
codec = avcodec_find_decoder(codecId);
if (!codec) {
qWarning() << "Codec not found";
return false;
}
#if LIBAVCODEC_VERSION_INT < 3747941
// Copy context, since we apparently aren't allowed to use the original
cctx = avcodec_alloc_context3(codec);
if (avcodec_copy_context(cctx, cctxOrig) != 0) {
qWarning() << "Can't copy context";
return false;
}
#else
// Create a context for our codec, using the existing parameters
cctx = avcodec_alloc_context3(codec);
if (avcodec_parameters_to_context(cctx, cparams) < 0) {
qWarning() << "Can't create AV context from parameters";
return false;
}
#endif
cctx->refcounted_frames = 1;
@ -346,8 +379,10 @@ void CameraSource::closeDevice()
// Free our resources and close the device
videoStreamIndex = -1;
avcodec_free_context(&cctx);
#if LIBAVCODEC_VERSION_INT < 3747941
avcodec_close(cctxOrig);
cctxOrig = nullptr;
#endif
while (device && !device->close()) {
}
device = nullptr;

View File

@ -64,7 +64,9 @@ private:
QString deviceName;
CameraDevice* device;
VideoMode mode;
AVCodecContext *cctx, *cctxOrig;
AVCodecContext* cctx;
// TODO: Remove when ffmpeg version will be bumped to the 3.1.0
AVCodecContext* cctxOrig;
int videoStreamIndex;
QReadWriteLock streamMutex;
std::atomic_bool _isOpen;