diff --git a/src/Audio.cc b/src/Audio.cc index aad8d5eef400af096f85ff01f48747d458a7c09e..36beea4d38b4ec53d2b29152f77f4f64fd3f9f76 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 d1693ae39a324992e2938e87d1eb013fcb3efdb4..3c7ce506e2a34c5e8b3c8867ca60fdb15cdfbfc0 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 }; }; }