Skip to content

Commit 46bb30c

Browse files
author
Clement Champetier
committed
ProfileLoader: check profile before adding it
Avoid to check a profile at any step during process (see setProfile functions).
1 parent 34b89c0 commit 46bb30c

File tree

5 files changed

+80
-26
lines changed

5 files changed

+80
-26
lines changed

src/AvTranscoder/ProfileLoader.cpp

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,35 @@ void ProfileLoader::loadProfile( const std::string& avProfileFile )
3131
if( keyValue.size() == 2 )
3232
customProfile[ keyValue.at( 0 ) ] = keyValue.at( 1 );
3333
}
34-
// check if profile contains required values
35-
if(
36-
customProfile.count( constants::avProfileIdentificator ) &&
37-
customProfile.count( constants::avProfileIdentificatorHuman ) &&
38-
customProfile.count( constants::avProfileType ) &&
39-
( customProfile.find( constants::avProfileType )->second == constants::avProfileTypeFormat ||
40-
customProfile.find( constants::avProfileType )->second == constants::avProfileTypeVideo ||
41-
customProfile.find( constants::avProfileType )->second == constants::avProfileTypeAudio )
42-
)
34+
35+
// check profile long name
36+
if( ! customProfile.count( constants::avProfileIdentificator ) )
4337
{
44-
_profiles.push_back( customProfile );
38+
std::cout << "Warning: A profile has no name. It will not be loaded." << std::endl;
39+
return;
40+
}
41+
42+
// check profile type
43+
if( customProfile.count( constants::avProfileType ) == 0 )
44+
{
45+
std::cout << "Warning: The profile " << customProfile.find( constants::avProfileIdentificator )->second << " has not type. It will not be loaded." << std::endl;
46+
return;
4547
}
48+
49+
// check complete profile
50+
bool isValid = false;
51+
std::string type( customProfile.find( constants::avProfileType )->second );
52+
if( type == constants::avProfileTypeFormat )
53+
isValid = checkFormatProfile( customProfile );
54+
else if( type == constants::avProfileTypeVideo )
55+
isValid = checkVideoProfile( customProfile );
56+
else if( type == constants::avProfileTypeAudio )
57+
isValid = checkAudioProfile( customProfile );
58+
59+
if( isValid )
60+
_profiles.push_back( customProfile );
61+
else
62+
std::cout << "Warning: The profile " << customProfile.find( constants::avProfileIdentificator )->second << " is invalid. It will not be loaded." << std::endl;
4663
}
4764

4865
void ProfileLoader::loadProfiles( const std::string& avProfilesPath )
@@ -147,4 +164,52 @@ ProfileLoader::Profile& ProfileLoader::getProfile( const std::string& avProfileI
147164
throw std::runtime_error( "unable to find profile: " + avProfileIdentificator );
148165
}
149166

167+
168+
bool ProfileLoader::checkFormatProfile( const Profile& profileToCheck )
169+
{
170+
bool isValid = true;
171+
172+
if( ! profileToCheck.count( constants::avProfileIdentificator ) ||
173+
! profileToCheck.count( constants::avProfileIdentificatorHuman ) ||
174+
! profileToCheck.count( constants::avProfileType ) ||
175+
! profileToCheck.count( constants::avProfileFormat ) )
176+
{
177+
isValid = false;
178+
}
179+
180+
return isValid;
181+
}
182+
183+
bool ProfileLoader::checkVideoProfile( const Profile& profileToCheck )
184+
{
185+
bool isValid = true;
186+
187+
if( ! profileToCheck.count( constants::avProfileIdentificator ) ||
188+
! profileToCheck.count( constants::avProfileIdentificatorHuman ) ||
189+
! profileToCheck.count( constants::avProfileType ) ||
190+
! profileToCheck.count( constants::avProfileCodec ) ||
191+
! profileToCheck.count( constants::avProfileFrameRate ) )
192+
{
193+
isValid = false;
194+
}
195+
196+
return isValid;
197+
}
198+
199+
bool ProfileLoader::checkAudioProfile( const Profile& profileToCheck )
200+
{
201+
bool isValid = true;
202+
203+
if( ! profileToCheck.count( constants::avProfileIdentificator ) ||
204+
! profileToCheck.count( constants::avProfileIdentificatorHuman ) ||
205+
! profileToCheck.count( constants::avProfileType ) ||
206+
! profileToCheck.count( constants::avProfileCodec ) )
207+
{
208+
isValid = false;
209+
}
210+
211+
return isValid;
212+
}
213+
214+
150215
}

src/AvTranscoder/ProfileLoader.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class AvExport ProfileLoader
5050

5151
Profile& getProfile( const std::string& avProfileIdentificator );
5252

53+
private:
54+
bool checkFormatProfile( const Profile& profileToCheck );
55+
bool checkVideoProfile( const Profile& profileToCheck );
56+
bool checkAudioProfile( const Profile& profileToCheck );
57+
5358
private:
5459
Profiles _profiles;
5560
};

src/AvTranscoder/encoder/AudioEncoder.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ bool AudioEncoder::encodeFrame( Frame& codedFrame )
161161

162162
void AudioEncoder::setProfile( const ProfileLoader::Profile& profile, const AudioFrameDesc& frameDesc )
163163
{
164-
if( ! profile.count( constants::avProfileCodec ) )
165-
{
166-
throw std::runtime_error( "The profile " + profile.find( constants::avProfileIdentificatorHuman )->second + " is invalid." );
167-
}
168-
169164
_codec.setAudioParameters( frameDesc );
170165

171166
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )

src/AvTranscoder/encoder/VideoEncoder.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,6 @@ bool VideoEncoder::encodeFrame( Frame& codedFrame )
148148

149149
void VideoEncoder::setProfile( const ProfileLoader::Profile& profile, const avtranscoder::VideoFrameDesc& frameDesc )
150150
{
151-
if( ! profile.count( constants::avProfileCodec ) ||
152-
! profile.count( constants::avProfileFrameRate ) )
153-
{
154-
throw std::runtime_error( "The profile " + profile.find( constants::avProfileIdentificatorHuman )->second + " is invalid." );
155-
}
156-
157151
const size_t frameRate = std::strtoul( profile.find( constants::avProfileFrameRate )->second.c_str(), NULL, 0 );
158152
_codec.setTimeBase( 1, frameRate );
159153

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ void OutputFile::addMetadata( const std::string& key, const std::string& value )
150150

151151
void OutputFile::setProfile( const ProfileLoader::Profile& profile )
152152
{
153-
if( ! profile.count( constants::avProfileFormat ) )
154-
{
155-
throw std::runtime_error( "The profile " + profile.find( constants::avProfileIdentificatorHuman )->second + " is invalid." );
156-
}
157-
158153
if( ! matchFormat( profile.find( constants::avProfileFormat )->second, _filename ) )
159154
{
160155
throw std::runtime_error( "Invalid format according to the file extension." );

0 commit comments

Comments
 (0)