Skip to content

Commit

Permalink
Add ffmpeg log callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Mar 16, 2024
1 parent a624f6e commit 6695d5c
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 115 deletions.
28 changes: 1 addition & 27 deletions src/app/FileProber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <Demuxer.h>
#include <common/Formatting.h>
#include <libHandling/FFmpegLibrariesBuilder.h>

#include <cstddef>
Expand Down Expand Up @@ -38,19 +39,6 @@ std::string to_string(const avcodec::CodecDescriptorProperties &properties)
return str.substr(0, str.length() - 2);
}

std::string to_string(const std::vector<std::string> &stringVec)
{
if (stringVec.empty())
return {};

std::ostringstream stream;
for (const auto &item : stringVec)
stream << item << ", ";

const auto outString = stream.str();
return outString.substr(0, outString.length() - 2);
}

std::string to_string(const Rational rational)
{
return std::to_string(rational.numerator) + "/" + std::to_string(rational.denominator);
Expand All @@ -69,20 +57,6 @@ std::string to_string(const ByteVector &bytes)
return stream.str();
}

std::string to_string(const avcodec::AVPacketWrapper::Flags &flags)
{
std::string flagsString;
if (flags.keyframe)
flagsString += "Keyframe,";
if (flags.corrupt)
flagsString += "Corrupt,";
if (flags.discard)
flagsString += "Discard";
if (!flagsString.empty())
flagsString.pop_back();
return flagsString;
}

struct Settings
{
bool showPackets{};
Expand Down
29 changes: 26 additions & 3 deletions src/lib/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Decoder.h"

#include <common/Error.h>
#include <common/Formatting.h>

namespace ffmpeg
{
Expand Down Expand Up @@ -37,11 +38,17 @@ bool Decoder::openForDecoding(const avformat::AVStreamWrapper &stream)
else
{
this->decoderContext = stream.getCodecContext();
if (!this->decoderContext)
return false;
openContextSuccessfull = this->decoderContext->openContextForDecoding();
if (this->decoderContext)
openContextSuccessfull = this->decoderContext->openContextForDecoding();
else
openContextSuccessfull = false;
}

if (openContextSuccessfull)
this->libraries->log(LogLevel::Info, "Opening of decoder successfull");
else
this->libraries->log(LogLevel::Error, "Opening of deoder failed.");

this->decoderState = openContextSuccessfull ? State::NeedsMoreData : State::Error;
return openContextSuccessfull;
}
Expand All @@ -50,13 +57,25 @@ Decoder::SendPacketResult Decoder::sendPacket(const avcodec::AVPacketWrapper &pa
{
if (this->decoderState == State::NotOpened || this->decoderState == State::Error ||
this->decoderState == State::EndOfBitstream || this->flushing)
{
this->libraries->log(LogLevel::Error,
"Can not send packet because decoder state is " +
to_string(this->decoderState));
return SendPacketResult::Error;
}

if (!packet)
{
this->libraries->log(LogLevel::Error,
"Can not send null packet. Use setFlushing to start flushing.");
return SendPacketResult::Error;
}

if (this->decoderState == State::RetrieveFrames)
{
this->libraries->log(LogLevel::Debug, "Sending packet failed. Frames must be pulled first.");
return SendPacketResult::NotSentPullFramesFirst;
}

ReturnCode returnCode;
if (this->libraries->getLibrariesVersion().avcodec.major == 56)
Expand All @@ -77,11 +96,14 @@ Decoder::SendPacketResult Decoder::sendPacket(const avcodec::AVPacketWrapper &pa

if (returnCode == ReturnCode::TryAgain)
{
this->libraries->log(LogLevel::Debug,
"Pushing packet failed (TryAgain). Swithing to state RetrieveFrames.");
this->decoderState = State::RetrieveFrames;
return SendPacketResult::NotSentPullFramesFirst;
}
if (returnCode != ReturnCode::Ok)
{
this->libraries->log(LogLevel::Error, "Error sending packet.");
this->decoderState = State::Error;
return SendPacketResult::Error;
}
Expand All @@ -98,6 +120,7 @@ void Decoder::setFlushing()
if (this->libraries->getLibrariesVersion().avcodec.major > 56)
returnCode = this->decoderContext->sendFlushPacket();

this->libraries->log(LogLevel::Debug, "Setting decoder to flushing.");
this->flushing = true;
this->decoderState = (returnCode == ReturnCode::Ok) ? State::RetrieveFrames : State::Error;
}
Expand Down
25 changes: 25 additions & 0 deletions src/lib/Demuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@

#include "Demuxer.h"

#include <common/Formatting.h>

namespace ffmpeg
{

namespace
{

std::string logPacket(const avcodec::AVPacketWrapper &packet)
{
std::stringstream stream;
stream << " DTS " << packet.getDTS();
stream << " PTS ";
const auto pts = packet.getPTS();
if (pts)
stream << *pts;
else
stream << "-";
stream << " Flags [" << to_string(packet.getFlags()) << "]";
return stream.str();
}

} // namespace

Demuxer::Demuxer(std::shared_ptr<IFFmpegLibraries> ffmpegLibraries)
: formatContext(avformat::AVFormatContextWrapper(ffmpegLibraries))
{
Expand Down Expand Up @@ -37,7 +58,11 @@ std::optional<avcodec::AVPacketWrapper> Demuxer::getNextPacket()
{
avcodec::AVPacketWrapper packet(this->ffmpegLibraries);
if (!this->formatContext.getNextPacket(packet))
{
this->ffmpegLibraries->log(LogLevel::Debug, "Got empty packet");
return {};
}
this->ffmpegLibraries->log(LogLevel::Debug, "Got Packet with " + logPacket(packet));
return packet;
}

Expand Down
53 changes: 52 additions & 1 deletion src/lib/common/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace ffmpeg
{

std::string toString(int64_t timestamp, Rational timebase)
std::string to_string(int64_t timestamp, Rational timebase)
{
auto absolutSeconds = static_cast<double>(timestamp) * static_cast<double>(timebase.numerator) /
static_cast<double>(timebase.denominator);
Expand All @@ -38,4 +38,55 @@ std::string toString(int64_t timestamp, Rational timebase)
return stream.str();
}

std::string to_string(const avcodec::AVPacketWrapper::Flags &flags)
{
std::string flagsString;
if (flags.keyframe)
flagsString += "Keyframe,";
if (flags.corrupt)
flagsString += "Corrupt,";
if (flags.discard)
flagsString += "Discard";
if (!flagsString.empty())
flagsString.pop_back();
return flagsString;
}

std::string to_string(const std::vector<std::string> &strings,
const ConcatenationSymbol concatenationSymbol)
{
std::ostringstream stream;
for (auto it = strings.begin(); it != strings.end(); it++)
{
if (it != strings.begin())
{
if (concatenationSymbol == ConcatenationSymbol::Comma)
stream << ", ";
else if (concatenationSymbol == ConcatenationSymbol::Newline)
stream << "\n";
}
stream << (*it);
}
return stream.str();
}

std::string to_string(const Decoder::State state)
{
switch (state)
{
case Decoder::State::NotOpened:
return "NotOpened";
case Decoder::State::NeedsMoreData:
return "NeedsMoreData";
case Decoder::State::RetrieveFrames:
return "RetrieveFrames";
case Decoder::State::EndOfBitstream:
return "EndOfBitstream";
case Decoder::State::Error:
return "Error";
default:
return "";
}
}

} // namespace ffmpeg
17 changes: 16 additions & 1 deletion src/lib/common/Formatting.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@
#pragma once

#include "Types.h"
#include <AVCodec/wrappers/AVPacketWrapper.h>
#include <Decoder.h>

namespace ffmpeg
{

std::string toString(int64_t timestamp, Rational timebase);

}
std::string to_string(const avcodec::AVPacketWrapper::Flags &flags);

enum class ConcatenationSymbol
{
Comma,
Newline
};

std::string to_string(const std::vector<std::string> &strings,
const ConcatenationSymbol concatenationSymbol = ConcatenationSymbol::Comma);

std::string to_string(const Decoder::State state);

} // namespace ffmpeg
18 changes: 0 additions & 18 deletions src/lib/common/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,4 @@
namespace ffmpeg
{

std::string to_string(const std::vector<std::string> &strings,
const ConcatenationSymbol concatenationSymbol)
{
std::ostringstream stream;
for (auto it = strings.begin(); it != strings.end(); it++)
{
if (it != strings.begin())
{
if (concatenationSymbol == ConcatenationSymbol::Comma)
stream << ", ";
else if (concatenationSymbol == ConcatenationSymbol::Newline)
stream << "\n";
}
stream << (*it);
}
return stream.str();
}

} // namespace ffmpeg
9 changes: 0 additions & 9 deletions src/lib/common/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
namespace ffmpeg
{

enum class ConcatenationSymbol
{
Comma,
Newline
};

std::string to_string(const std::vector<std::string> &strings,
const ConcatenationSymbol concatenationSymbol = ConcatenationSymbol::Comma);

inline ByteVector copyDataFromRawArray(const uint8_t *inputData, const int inputDataSize)
{
if (inputDataSize <= 0 || inputData == nullptr)
Expand Down
Loading

0 comments on commit 6695d5c

Please sign in to comment.