From 7fb36b063ff612b678b34ca6228bf7bc232da04e Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Fri, 2 Jul 2021 22:58:16 +0200 Subject: [PATCH] AUDIO: Finish implementing the AudioContext --- src/Audio.cc | 21 ++++++++++++++++++++- src/Audio.hh | 12 ++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Audio.cc b/src/Audio.cc index aad8d5ee..36beea4d 100644 --- a/src/Audio.cc +++ b/src/Audio.cc @@ -32,11 +32,30 @@ AudioContext::AudioContext(const QString &path) 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)); + audioStreams.insert(i, std::make_shared<Stream>(formatPtr, itFormat)); } } } +// Get the number of audio streams in the audio context +int +AudioContext::getStreamCount() const noexcept +{ + return audioStreamIndexes.size(); +} + +// Get a specific audio stream, try to lock the weak pointer. if the index was +// not present the used_count will be 0 and you won't be able to lock it. +AudioContext::StreamWeakPtr +AudioContext::getStream(uint index) const noexcept +{ + if (QMap<uint, StreamPtr>::const_iterator found = audioStreams.find(index); + found != audioStreams.end()) { + return StreamWeakPtr{ *found }; + } + return StreamWeakPtr{ spareNullSreamPtr }; +} + // AudioContext::Stream class implementation // Constructor, need an AVFormat and an AVStream diff --git a/src/Audio.hh b/src/Audio.hh index d1693ae3..3c7ce506 100644 --- a/src/Audio.hh +++ b/src/Audio.hh @@ -116,10 +116,10 @@ public: ~AudioContext() noexcept = default; int getStreamCount() const noexcept; - StreamWeakPtr getStream(int) const noexcept; + StreamWeakPtr getStream(uint) const noexcept; private: - /* Regarding the format */ + // Regarding the format static inline constexpr auto avFormatContextDeleter = [](AVFormatContext *ptr) noexcept -> void { if (ptr) @@ -128,10 +128,14 @@ private: using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>; AVFormatContextPtr format{ avformat_alloc_context() }; - /* Usefull information */ + // Usefull information QString filePath; QVector<uint> audioStreamIndexes{}; - QMap<uint, StreamPtr> audioCodecs{}; + QMap<uint, StreamPtr> audioStreams{}; + + // Spare always null shared pointer, to be used when the audioStream[i] was + // not found. + StreamPtr spareNullSreamPtr{ nullptr }; }; } -- GitLab