Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 7b6182b0 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

AUDIO: Create the audio context and its streams

parent a677649c
Branches
Aucune étiquette associée trouvée
1 requête de fusion!5Add AudioContext to AudioSubDocument
...@@ -2,6 +2,41 @@ ...@@ -2,6 +2,41 @@
using namespace Vivy; using namespace Vivy;
// AudioContext class implementation
// Create an audio contecxt from a file
AudioContext::AudioContext(const QString &path)
: filePath(path)
{
if (!format)
throw std::runtime_error("out of memory, can't create allocate the AVFormatContext");
const std::string filePathStdHolder = filePath.toStdString();
const char *filename = filePathStdHolder.c_str();
AVFormatContext *formatPtr = format.get();
// Get the format from the audio file
if (avformat_open_input(&formatPtr, filename, NULL, NULL) != 0) {
[[maybe_unused]] void *relatedOnFailure = format.release(); // freed by avformat_open_input
throw std::runtime_error("failed to open file");
}
if (avformat_find_stream_info(formatPtr, NULL) < 0) {
throw std::runtime_error("failed to get audio stream info");
}
// Populate all the stream indexes
for (uint i = 0; i < format->nb_streams; ++i) {
AVStream *itFormat = format->streams[i];
AVCodecParameters *params = itFormat->codecpar;
AVCodec *streamCodec = avcodec_find_decoder(params->codec_id);
if (streamCodec && streamCodec->type == AVMEDIA_TYPE_AUDIO) {
audioStreamIndexes.push_back(i);
audioCodecs.insert(i, std::make_shared<Stream>(formatPtr, itFormat));
}
}
}
// AudioContext::Stream class implementation // AudioContext::Stream class implementation
// Constructor, need an AVFormat and an AVStream // Constructor, need an AVFormat and an AVStream
......
...@@ -113,7 +113,7 @@ public: ...@@ -113,7 +113,7 @@ public:
public: public:
AudioContext(const QString &path); AudioContext(const QString &path);
~AudioContext() noexcept; ~AudioContext() noexcept = default;
int getStreamCount() const noexcept; int getStreamCount() const noexcept;
StreamWeakPtr getStream(int) const noexcept; StreamWeakPtr getStream(int) const noexcept;
...@@ -126,7 +126,7 @@ private: ...@@ -126,7 +126,7 @@ private:
avformat_free_context(ptr); avformat_free_context(ptr);
}; };
using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>; using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>;
AVFormatContextPtr format{ nullptr }; AVFormatContextPtr format{ avformat_alloc_context() };
/* Usefull information */ /* Usefull information */
QString filePath; QString filePath;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter