diff --git a/src/Audio.cc b/src/Audio.cc index 5abc1f5a85257876a7145153d21eabaa37877a8d..aad8d5eef400af096f85ff01f48747d458a7c09e 100644 --- a/src/Audio.cc +++ b/src/Audio.cc @@ -2,6 +2,41 @@ 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 // Constructor, need an AVFormat and an AVStream diff --git a/src/Audio.hh b/src/Audio.hh index d71d80c013201735ffce2f5b36d732dddf338cbf..d1693ae39a324992e2938e87d1eb013fcb3efdb4 100644 --- a/src/Audio.hh +++ b/src/Audio.hh @@ -113,7 +113,7 @@ public: public: AudioContext(const QString &path); - ~AudioContext() noexcept; + ~AudioContext() noexcept = default; int getStreamCount() const noexcept; StreamWeakPtr getStream(int) const noexcept; @@ -126,7 +126,7 @@ private: avformat_free_context(ptr); }; using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>; - AVFormatContextPtr format{ nullptr }; + AVFormatContextPtr format{ avformat_alloc_context() }; /* Usefull information */ QString filePath;