Skip to content

Commit 57c104a

Browse files
committed
Merge pull request #134 from cchampet/dev_mediaPropertiesExceptionsPropertiesAsMap
Dev media properties exceptions properties as map
2 parents 5f06065 + 6425da5 commit 57c104a

13 files changed

+323
-287
lines changed

src/AvTranscoder/mediaProperty/AttachementProperties.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ PropertiesMap AttachementProperties::getPropertiesAsMap() const
2424
{
2525
PropertiesMap dataMap;
2626

27-
detail::add( dataMap, "streamId", getStreamId() );
27+
try
28+
{
29+
detail::add( dataMap, "streamId", getStreamId() );
30+
}
31+
catch( const std::exception& e )
32+
{
33+
detail::add( dataMap, "streamId", e.what() );
34+
}
2835

2936
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
3037
{

src/AvTranscoder/mediaProperty/AudioProperties.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,34 @@ size_t AudioProperties::getStreamId() const
3838

3939
std::string AudioProperties::getCodecName() const
4040
{
41-
if( _codec && _codec->name )
42-
return std::string( _codec->name );
43-
return "unknown codec";
41+
if( ! _codec || ! _codec->name )
42+
throw std::runtime_error( "unknown codec name" );
43+
return std::string( _codec->name );
4444
}
4545

4646
std::string AudioProperties::getCodecLongName() const
4747
{
48-
if( _codec && _codec->long_name )
49-
return std::string( _codec->long_name );
50-
return "unknown codec";
48+
if( ! _codec || ! _codec->long_name )
49+
throw std::runtime_error( "unknown codec long name" );
50+
return std::string( _codec->long_name );
5151
}
5252

5353
std::string AudioProperties::getSampleFormatName() const
5454
{
5555
if( ! _codecContext )
56-
return "unknown codec context";
56+
throw std::runtime_error( "unknown codec context" );
5757

5858
const char* fmtName = av_get_sample_fmt_name( _codecContext->sample_fmt );
59-
if( fmtName )
60-
return std::string( fmtName );
61-
return "unknown sample format";
59+
if( ! fmtName )
60+
throw std::runtime_error( "unknown sample format" );
61+
62+
return std::string( fmtName );
6263
}
6364

6465
std::string AudioProperties::getSampleFormatLongName() const
6566
{
6667
if( ! _codecContext )
67-
return "unknown codec context";
68+
throw std::runtime_error( "unknown codec context" );
6869

6970
switch( _codecContext->sample_fmt )
7071
{
@@ -99,7 +100,7 @@ std::string AudioProperties::getSampleFormatLongName() const
99100
std::string AudioProperties::getChannelLayout() const
100101
{
101102
if( ! _codecContext )
102-
return "unknown codec context";
103+
throw std::runtime_error( "unknown codec context" );
103104

104105
char buf1[1024];
105106
av_get_channel_layout_string( buf1, sizeof( buf1 ), -1, _codecContext->channel_layout );
@@ -109,26 +110,27 @@ std::string AudioProperties::getChannelLayout() const
109110
std::string AudioProperties::getChannelName() const
110111
{
111112
if( ! _codecContext )
112-
return "unknown codec context";
113+
throw std::runtime_error( "unknown codec context" );
113114

114115
const char* channelName = av_get_channel_name( _codecContext->channel_layout );
115-
if( channelName )
116-
return std::string( channelName );
117-
return "unknown channel name";
116+
if( ! channelName )
117+
throw std::runtime_error( "unknown channel name" );
118+
119+
return std::string( channelName );
118120
}
119121

120122
std::string AudioProperties::getChannelDescription() const
121123
{
122124
if( ! _codecContext )
123-
return "unknown codec context";
125+
throw std::runtime_error( "unknown codec context" );
124126

125-
#ifdef FF_RESAMPLE_LIBRARY
127+
#ifdef AVTRANSCODER_FFMPEG_DEPENDENCY
126128
const char* channelDescription = av_get_channel_description( _codecContext->channel_layout );
127-
if( channelDescription )
128-
return std::string( channelDescription );
129-
return "unknown channel description";
129+
if( ! channelDescription )
130+
throw std::runtime_error( "unknown channel description" );
131+
return std::string( channelDescription );
130132
#else
131-
return "can't access channel description";
133+
throw std::runtime_error( "can't access channel description" );
132134
#endif
133135
}
134136

@@ -204,22 +206,22 @@ PropertiesMap AudioProperties::getPropertiesAsMap() const
204206
{
205207
PropertiesMap dataMap;
206208

207-
detail::add( dataMap, "streamId", getStreamId() );
208-
detail::add( dataMap, "codecId", getCodecId() );
209-
detail::add( dataMap, "codecName", getCodecName() );
210-
detail::add( dataMap, "codecLongName", getCodecLongName() );
211-
detail::add( dataMap, "sampleFormatName", getSampleFormatName() );
212-
detail::add( dataMap, "sampleFormatLongName", getSampleFormatLongName() );
213-
detail::add( dataMap, "sampleRate", getSampleRate() );
214-
detail::add( dataMap, "bitRate", getBitRate() );
215-
detail::add( dataMap, "nbSamples", getNbSamples() );
216-
detail::add( dataMap, "channels", getChannels() );
217-
detail::add( dataMap, "channelLayout", getChannelLayout() );
218-
detail::add( dataMap, "channelName", getChannelName() );
219-
detail::add( dataMap, "channelDescription", getChannelDescription() );
220-
detail::add( dataMap, "ticksPerFrame", getTicksPerFrame() );
221-
detail::add( dataMap, "timeBase", getTimeBase() );
222-
detail::add( dataMap, "duration", getDuration() );
209+
addProperty( dataMap, "streamId", &AudioProperties::getStreamId );
210+
addProperty( dataMap, "codecId", &AudioProperties::getCodecId );
211+
addProperty( dataMap, "codecName", &AudioProperties::getCodecName );
212+
addProperty( dataMap, "codecLongName", &AudioProperties::getCodecLongName );
213+
addProperty( dataMap, "sampleFormatName", &AudioProperties::getSampleFormatName );
214+
addProperty( dataMap, "sampleFormatLongName", &AudioProperties::getSampleFormatLongName );
215+
addProperty( dataMap, "sampleRate", &AudioProperties::getSampleRate );
216+
addProperty( dataMap, "bitRate", &AudioProperties::getBitRate );
217+
addProperty( dataMap, "nbSamples", &AudioProperties::getNbSamples );
218+
addProperty( dataMap, "channels", &AudioProperties::getChannels );
219+
addProperty( dataMap, "channelLayout", &AudioProperties::getChannelLayout );
220+
addProperty( dataMap, "channelName", &AudioProperties::getChannelName );
221+
addProperty( dataMap, "channelDescription", &AudioProperties::getChannelDescription );
222+
addProperty( dataMap, "ticksPerFrame", &AudioProperties::getTicksPerFrame );
223+
addProperty( dataMap, "timeBase", &AudioProperties::getTimeBase );
224+
addProperty( dataMap, "duration", &AudioProperties::getDuration );
223225

224226
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
225227
{

src/AvTranscoder/mediaProperty/AudioProperties.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ class AvExport AudioProperties
4444

4545
PropertiesMap getPropertiesAsMap() const; ///< Return all audio properties as a map (name of property: value)
4646

47+
private:
48+
#ifndef SWIG
49+
template<typename T>
50+
void addProperty( PropertiesMap& dataMap, const std::string& key, T (AudioProperties::*getter)(void) const ) const
51+
{
52+
try
53+
{
54+
detail::add( dataMap, key, (this->*getter)() );
55+
}
56+
catch( const std::exception& e )
57+
{
58+
detail::add( dataMap, key, e.what() );
59+
}
60+
}
61+
#endif
62+
4763
private:
4864
const AVFormatContext* _formatContext; ///< Has link (no ownership)
4965
AVCodecContext* _codecContext; ///< Has link (no ownership)

src/AvTranscoder/mediaProperty/DataProperties.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ PropertiesMap DataProperties::getPropertiesAsMap() const
3333
{
3434
PropertiesMap dataMap;
3535

36-
detail::add( dataMap, "streamId", getStreamId() );
36+
try
37+
{
38+
detail::add( dataMap, "streamId", getStreamId() );
39+
}
40+
catch( const std::exception& e )
41+
{
42+
detail::add( dataMap, "streamId", e.what() );
43+
}
3744

3845
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
3946
{

src/AvTranscoder/mediaProperty/FileProperties.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ FileProperties::FileProperties( const FormatContext& formatContext )
2121
std::string FileProperties::getFilename() const
2222
{
2323
if( ! _formatContext || ! _formatContext->filename )
24-
return "unknown file name";
24+
throw std::runtime_error( "unknown file name" );
2525
return _formatContext->filename;
2626
}
2727

2828
std::string FileProperties::getFormatName() const
2929
{
3030
if( ! _formatContext || ! _formatContext->iformat || ! _formatContext->iformat->name )
31-
return "unknown format name";
31+
throw std::runtime_error( "unknown format name");
3232
return _formatContext->iformat->name;
3333
}
3434

3535
std::string FileProperties::getFormatLongName() const
3636
{
3737
if( ! _formatContext || ! _formatContext->iformat || ! _formatContext->iformat->long_name )
38-
return "unknown format long name";
38+
throw std::runtime_error( "unknown format long name");
3939
return _formatContext->iformat->long_name;
4040
}
4141

@@ -133,15 +133,16 @@ PropertiesMap FileProperties::getPropertiesAsMap() const
133133
{
134134
PropertiesMap dataMap;
135135

136-
detail::add( dataMap, "filename", getFilename() );
137-
detail::add( dataMap, "formatName", getFormatName() );
138-
detail::add( dataMap, "formatLongName", getFormatLongName() );
136+
addProperty( dataMap, "filename", &FileProperties::getFilename );
137+
addProperty( dataMap, "formatName", &FileProperties::getFormatName );
138+
addProperty( dataMap, "formatLongName", &FileProperties::getFormatLongName );
139+
140+
addProperty( dataMap, "startTime", &FileProperties::getStartTime );
141+
addProperty( dataMap, "duration", &FileProperties::getDuration );
142+
addProperty( dataMap, "bitrate", &FileProperties::getBitRate );
143+
addProperty( dataMap, "numberOfStreams", &FileProperties::getNbStreams );
144+
addProperty( dataMap, "numberOfPrograms", &FileProperties::getProgramsCount );
139145

140-
detail::add( dataMap, "startTime", getStartTime() );
141-
detail::add( dataMap, "duration", getDuration() );
142-
detail::add( dataMap, "bitrate", getBitRate() );
143-
detail::add( dataMap, "numberOfStreams", getNbStreams() );
144-
detail::add( dataMap, "numberOfPrograms", getProgramsCount() );
145146
detail::add( dataMap, "numberOfVideoStreams", getNbVideoStreams() );
146147
detail::add( dataMap, "numberOfAudioStreams", getNbAudioStreams() );
147148
detail::add( dataMap, "numberOfDataStreams", getNbDataStreams() );

src/AvTranscoder/mediaProperty/FileProperties.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ class AvExport FileProperties
7676

7777
void clearStreamProperties(); ///< Clear all array of stream properties
7878

79+
private:
80+
#ifndef SWIG
81+
template<typename T>
82+
void addProperty( PropertiesMap& dataMap, const std::string& key, T (FileProperties::*getter)(void) const ) const
83+
{
84+
try
85+
{
86+
detail::add( dataMap, key, (this->*getter)() );
87+
}
88+
catch( const std::exception& e )
89+
{
90+
detail::add( dataMap, key, e.what() );
91+
}
92+
}
93+
#endif
94+
7995
private:
8096
const AVFormatContext* _formatContext; ///< Has link (no ownership)
8197

0 commit comments

Comments
 (0)