Skip to content

Commit 821065a

Browse files
committed
Add FormatContext::streams()
1 parent 3e0d0e6 commit 821065a

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

example/api2-samples/api2-decode.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ int main(int argc, char **argv)
5858
return 1;
5959
}
6060

61-
for (size_t i = 0; i < ictx.streamsCount(); ++i) {
62-
auto st = ictx.stream(i);
61+
for (auto&& st : ictx.streams()) {
6362
if (st.mediaType() == AVMEDIA_TYPE_VIDEO) {
64-
videoStream = i;
6563
vst = st;
64+
videoStream = st.index();
6665
break;
6766
}
6867
}

src/avcpp/formatcontext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ Stream FormatContext::addStream(const AudioEncoderContext &encCtx, OptionalError
300300
return addStream(static_cast<const CodecContext2&>(encCtx), ec);
301301
}
302302

303+
FormatContext::StreamsView FormatContext::streams(OptionalErrorCode ec) const {
304+
if (!m_raw || !m_raw->streams || !m_raw->nb_streams) {
305+
throws_if(ec, Errors::Unallocated);
306+
return {};
307+
}
308+
return std::span(m_raw->streams, m_raw->nb_streams) | std::views::transform(_StreamTransform{this});
309+
}
310+
303311
bool FormatContext::seekable() const noexcept
304312
{
305313
if (m_raw && m_raw->pb) {

src/avcpp/formatcontext.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ struct CustomIO
5353

5454
class FormatContext : public FFWrapperPtr<AVFormatContext>, public noncopyable
5555
{
56+
#if AVCPP_CXX_STANDARD >= 20
57+
struct _StreamTransform {
58+
const FormatContext *_parent{};
59+
Stream operator()(AVStream *st) const {
60+
assert(_parent);
61+
return Stream{_parent->m_monitor, st, _parent->isOutput() ? Direction::Encoding : Direction::Decoding};
62+
}
63+
};
64+
using StreamsView = std::ranges::transform_view<std::span<AVStream*>, _StreamTransform>;
65+
#endif
66+
5667
public:
5768
FormatContext();
5869
~FormatContext();
@@ -85,6 +96,10 @@ class FormatContext : public FFWrapperPtr<AVFormatContext>, public noncopyable
8596
Stream addStream(const class VideoEncoderContext& encCtx, OptionalErrorCode ec = throws());
8697
Stream addStream(const class AudioEncoderContext& encCtx, OptionalErrorCode ec = throws());
8798

99+
#if AVCPP_CXX_STANDARD >= 20
100+
StreamsView streams(OptionalErrorCode ec = throws()) const;
101+
#endif
102+
88103
//
89104
// Seeking
90105
//

0 commit comments

Comments
 (0)