Skip to content

Commit 05a6237

Browse files
author
Clement Champetier
committed
mediaProperty: get timeBase and duration from base class
Add properties of base class if the derived class override getPropertiesAsVector (for Video and Audio).
1 parent 9e0c942 commit 05a6237

File tree

6 files changed

+49
-69
lines changed

6 files changed

+49
-69
lines changed

src/AvTranscoder/mediaProperty/AudioProperties.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,31 +180,14 @@ size_t AudioProperties::getTicksPerFrame() const
180180
return _codecContext->ticks_per_frame;
181181
}
182182

183-
Rational AudioProperties::getTimeBase() const
184-
{
185-
if( ! _formatContext )
186-
throw std::runtime_error( "unknown format context" );
187-
188-
Rational timeBase = {
189-
_formatContext->streams[_streamIndex]->time_base.num,
190-
_formatContext->streams[_streamIndex]->time_base.den,
191-
};
192-
return timeBase;
193-
}
194-
195-
double AudioProperties::getDuration() const
196-
{
197-
Rational timeBase = getTimeBase();
198-
double duration = ( timeBase.num / (double) timeBase.den ) * _formatContext->streams[_streamIndex]->duration;
199-
return duration;
200-
}
201-
202183
PropertyVector AudioProperties::getPropertiesAsVector() const
203184
{
204185
PropertyVector data;
205186

206-
addProperty( data, "streamId", &AudioProperties::getStreamId );
207-
detail::add( data, "streamIndex", getStreamIndex() );
187+
// Add properties of base class
188+
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
189+
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );
190+
208191
addProperty( data, "codecId", &AudioProperties::getCodecId );
209192
addProperty( data, "codecName", &AudioProperties::getCodecName );
210193
addProperty( data, "codecLongName", &AudioProperties::getCodecLongName );
@@ -218,13 +201,6 @@ PropertyVector AudioProperties::getPropertiesAsVector() const
218201
addProperty( data, "channelName", &AudioProperties::getChannelName );
219202
addProperty( data, "channelDescription", &AudioProperties::getChannelDescription );
220203
addProperty( data, "ticksPerFrame", &AudioProperties::getTicksPerFrame );
221-
addProperty( data, "timeBase", &AudioProperties::getTimeBase );
222-
addProperty( data, "duration", &AudioProperties::getDuration );
223-
224-
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
225-
{
226-
detail::add( data, _metadatas.at( metadataIndex ).first, _metadatas.at( metadataIndex ).second );
227-
}
228204

229205
return data;
230206
}

src/AvTranscoder/mediaProperty/AudioProperties.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class AvExport AudioProperties : public StreamProperties
2929
size_t getNbSamples() const; ///< 0 if unknown
3030

3131
size_t getTicksPerFrame() const;
32-
Rational getTimeBase() const;
33-
double getDuration() const;
3432

3533
#ifndef SWIG
3634
AVCodecContext& getAVCodecContext() { return *_codecContext; }

src/AvTranscoder/mediaProperty/StreamProperties.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,33 @@ size_t StreamProperties::getStreamId() const
2525
return _formatContext->streams[_streamIndex]->id;
2626
}
2727

28+
Rational StreamProperties::getTimeBase() const
29+
{
30+
if( ! _formatContext )
31+
throw std::runtime_error( "unknown format context" );
32+
33+
Rational timeBase = {
34+
_formatContext->streams[_streamIndex]->time_base.num,
35+
_formatContext->streams[_streamIndex]->time_base.den,
36+
};
37+
return timeBase;
38+
}
39+
40+
double StreamProperties::getDuration() const
41+
{
42+
Rational timeBase = getTimeBase();
43+
double duration = ( timeBase.num / (double) timeBase.den ) * _formatContext->streams[_streamIndex]->duration;
44+
return duration;
45+
}
46+
2847
PropertyVector StreamProperties::getPropertiesAsVector() const
2948
{
3049
PropertyVector data;
3150

32-
try
33-
{
34-
detail::add( data, "streamId", getStreamId() );
35-
}
36-
catch( const std::exception& e )
37-
{
38-
detail::add( data, "streamId", e.what() );
39-
}
40-
detail::add( data, "streamIndex", getStreamIndex() );
51+
addProperty( data, "streamId", &StreamProperties::getStreamId );
52+
addProperty( data, "streamIndex", &StreamProperties::getStreamIndex );
53+
addProperty( data, "timeBase", &StreamProperties::getTimeBase );
54+
addProperty( data, "duration", &StreamProperties::getDuration );
4155

4256
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
4357
{

src/AvTranscoder/mediaProperty/StreamProperties.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class AvExport StreamProperties
1717

1818
size_t getStreamIndex() const { return _streamIndex; }
1919
size_t getStreamId() const;
20+
Rational getTimeBase() const;
21+
double getDuration() const; ///< in seconds
2022
PropertyVector& getMetadatas() { return _metadatas; }
2123

2224
#ifndef SWIG
@@ -26,6 +28,22 @@ class AvExport StreamProperties
2628
PropertyMap getPropertiesAsMap() const; ///< Return all properties as a map (name of property, value)
2729
PropertyVector getPropertiesAsVector() const; ///< Same data with a specific order
2830

31+
private:
32+
#ifndef SWIG
33+
template<typename T>
34+
void addProperty( PropertyVector& dataVector, const std::string& key, T (StreamProperties::*getter)(void) const ) const
35+
{
36+
try
37+
{
38+
detail::add( dataVector, key, (this->*getter)() );
39+
}
40+
catch( const std::exception& e )
41+
{
42+
detail::add( dataVector, key, e.what() );
43+
}
44+
}
45+
#endif
46+
2947
protected:
3048
const AVFormatContext* _formatContext; ///< Has link (no ownership)
3149

src/AvTranscoder/mediaProperty/VideoProperties.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,6 @@ std::string VideoProperties::getStartTimecodeString() const
334334
return os.str();
335335
}
336336

337-
Rational VideoProperties::getTimeBase() const
338-
{
339-
if( ! _formatContext )
340-
throw std::runtime_error( "unknown format context" );
341-
342-
Rational timeBase = {
343-
_formatContext->streams[_streamIndex]->time_base.num,
344-
_formatContext->streams[_streamIndex]->time_base.den,
345-
};
346-
return timeBase;
347-
}
348-
349337
Rational VideoProperties::getSar() const
350338
{
351339
if( ! _codecContext )
@@ -554,13 +542,6 @@ double VideoProperties::getFps() const
554542
return fps;
555543
}
556544

557-
double VideoProperties::getDuration() const
558-
{
559-
Rational timeBase = getTimeBase();
560-
double duration = ( timeBase.num / (double) timeBase.den ) * _formatContext->streams[_streamIndex]->duration;
561-
return duration;
562-
}
563-
564545
bool VideoProperties::hasBFrames() const
565546
{
566547
if( ! _codecContext )
@@ -644,8 +625,10 @@ PropertyVector VideoProperties::getPropertiesAsVector() const
644625
{
645626
PropertyVector data;
646627

647-
addProperty( data, "streamId", &VideoProperties::getStreamId );
648-
detail::add( data, "streamIndex", getStreamIndex() );
628+
// Add properties of base class
629+
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
630+
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );
631+
649632
addProperty( data, "codecId", &VideoProperties::getCodecId );
650633
addProperty( data, "codecName", &VideoProperties::getCodecName );
651634
addProperty( data, "codecLongName", &VideoProperties::getCodecLongName );
@@ -666,8 +649,6 @@ PropertyVector VideoProperties::getPropertiesAsVector() const
666649
addProperty( data, "interlaced ", &VideoProperties::isInterlaced );
667650
addProperty( data, "topFieldFirst", &VideoProperties::isTopFieldFirst );
668651
addProperty( data, "fieldOrder", &VideoProperties::getFieldOrder );
669-
addProperty( data, "timeBase", &VideoProperties::getTimeBase );
670-
addProperty( data, "duration", &VideoProperties::getDuration );
671652
addProperty( data, "fps", &VideoProperties::getFps );
672653
addProperty( data, "nbFrame", &VideoProperties::getNbFrames );
673654
addProperty( data, "ticksPerFrame", &VideoProperties::getTicksPerFrame );
@@ -688,11 +669,6 @@ PropertyVector VideoProperties::getPropertiesAsVector() const
688669
addProperty( data, "hasBFrames", &VideoProperties::hasBFrames );
689670
addProperty( data, "referencesFrames", &VideoProperties::getReferencesFrames );
690671

691-
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
692-
{
693-
detail::add( data, _metadatas.at( metadataIndex ).first, _metadatas.at( metadataIndex ).second );
694-
}
695-
696672
// Add properties of the pixel
697673
PropertyVector pixelProperties = _pixelProperties.getPropertiesAsVector();
698674
data.insert( data.end(), pixelProperties.begin(), pixelProperties.end() );

src/AvTranscoder/mediaProperty/VideoProperties.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class AvExport VideoProperties : public StreamProperties
3838
int64_t getStartTimecode() const;
3939
std::string getStartTimecodeString() const;
4040

41-
Rational getTimeBase() const;
4241
Rational getSar() const; // sample/pixel aspect ratio
4342
Rational getDar() const; // display aspect ratio
4443

@@ -58,7 +57,6 @@ class AvExport VideoProperties : public StreamProperties
5857
int getLevel() const;
5958

6059
double getFps() const;
61-
double getDuration() const; ///< in seconds
6260

6361
bool hasBFrames() const;
6462
//bool isClosedGop() const;

0 commit comments

Comments
 (0)