Skip to content

Commit cdbc5e5

Browse files
committed
Merge pull request #136 from cchampet/hotfix_loadProfiles
Hotfix: load profiles, avoiding duplication
2 parents b1aef13 + d830180 commit cdbc5e5

File tree

4 files changed

+71
-39
lines changed

4 files changed

+71
-39
lines changed

src/AvTranscoder/profile/ProfileLoader.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void ProfileLoader::loadProfiles( const std::string& avProfilesPath )
7171

7272
void ProfileLoader::loadProfile( const Profile& profile )
7373
{
74-
// check profile long name
74+
// check profile identificator
7575
if( ! profile.count( constants::avProfileIdentificator ) )
7676
{
7777
throw std::runtime_error( "Warning: A profile has no name. It will not be loaded." );
@@ -102,16 +102,32 @@ void ProfileLoader::loadProfile( const Profile& profile )
102102
throw std::runtime_error( "Warning: The profile " + profile.find( constants::avProfileIdentificator )->second + " is invalid. It will not be loaded." );
103103
}
104104

105-
const ProfileLoader::Profiles& ProfileLoader::getProfiles()
105+
bool ProfileLoader::hasProfile( const Profile& profile ) const
106+
{
107+
// check profile identificator
108+
if( ! profile.count( constants::avProfileIdentificator ) )
109+
{
110+
throw std::runtime_error( "Warning: A profile has no name. It will not be loaded." );
111+
}
112+
113+
for( Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it )
114+
{
115+
if( (*it).at( constants::avProfileIdentificator ) == profile.at( constants::avProfileIdentificator ) )
116+
return true;
117+
}
118+
return false;
119+
}
120+
121+
const ProfileLoader::Profiles& ProfileLoader::getProfiles() const
106122
{
107123
return _profiles;
108124
}
109125

110-
ProfileLoader::Profiles ProfileLoader::getFormatProfiles()
126+
ProfileLoader::Profiles ProfileLoader::getFormatProfiles() const
111127
{
112128
Profiles profiles;
113129

114-
for( Profiles::iterator it = _profiles.begin(); it != _profiles.end(); ++it )
130+
for( Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it )
115131
{
116132
if( (*it).find( constants::avProfileType )->second == constants::avProfileTypeFormat )
117133
profiles.push_back( *it );
@@ -120,11 +136,11 @@ ProfileLoader::Profiles ProfileLoader::getFormatProfiles()
120136
return profiles;
121137
}
122138

123-
ProfileLoader::Profiles ProfileLoader::getVideoProfiles()
139+
ProfileLoader::Profiles ProfileLoader::getVideoProfiles() const
124140
{
125141
Profiles profiles;
126142

127-
for( Profiles::iterator it = _profiles.begin(); it != _profiles.end(); ++it )
143+
for( Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it )
128144
{
129145
if( (*it).find( constants::avProfileType )->second == constants::avProfileTypeVideo )
130146
profiles.push_back( *it );
@@ -133,11 +149,11 @@ ProfileLoader::Profiles ProfileLoader::getVideoProfiles()
133149
return profiles;
134150
}
135151

136-
ProfileLoader::Profiles ProfileLoader::getAudioProfiles()
152+
ProfileLoader::Profiles ProfileLoader::getAudioProfiles() const
137153
{
138154
Profiles profiles;
139155

140-
for( Profiles::iterator it = _profiles.begin(); it != _profiles.end(); ++it )
156+
for( Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it )
141157
{
142158
if( (*it).find( constants::avProfileType )->second == constants::avProfileTypeAudio )
143159
profiles.push_back( *it );
@@ -146,9 +162,9 @@ ProfileLoader::Profiles ProfileLoader::getAudioProfiles()
146162
return profiles;
147163
}
148164

149-
ProfileLoader::Profile& ProfileLoader::getProfile( const std::string& avProfileIdentificator )
165+
const ProfileLoader::Profile& ProfileLoader::getProfile( const std::string& avProfileIdentificator ) const
150166
{
151-
for( Profiles::iterator it = _profiles.begin(); it != _profiles.end(); ++it )
167+
for( Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it )
152168
{
153169
if( (*it).find( constants::avProfileIdentificator )->second == avProfileIdentificator )
154170
{
@@ -159,7 +175,7 @@ ProfileLoader::Profile& ProfileLoader::getProfile( const std::string& avProfileI
159175
}
160176

161177

162-
bool ProfileLoader::checkFormatProfile( const Profile& profileToCheck )
178+
bool ProfileLoader::checkFormatProfile( const Profile& profileToCheck ) const
163179
{
164180
bool isValid = true;
165181

@@ -174,7 +190,7 @@ bool ProfileLoader::checkFormatProfile( const Profile& profileToCheck )
174190
return isValid;
175191
}
176192

177-
bool ProfileLoader::checkVideoProfile( const Profile& profileToCheck )
193+
bool ProfileLoader::checkVideoProfile( const Profile& profileToCheck ) const
178194
{
179195
bool isValid = true;
180196

@@ -189,7 +205,7 @@ bool ProfileLoader::checkVideoProfile( const Profile& profileToCheck )
189205
return isValid;
190206
}
191207

192-
bool ProfileLoader::checkAudioProfile( const Profile& profileToCheck )
208+
bool ProfileLoader::checkAudioProfile( const Profile& profileToCheck ) const
193209
{
194210
bool isValid = true;
195211

@@ -204,4 +220,12 @@ bool ProfileLoader::checkAudioProfile( const Profile& profileToCheck )
204220
return isValid;
205221
}
206222

223+
// To print a profile
224+
std::ostream &operator<<( std::ostream &os, const ProfileLoader::Profile &profile )
225+
{
226+
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
227+
os << "(" << it->first << ", " << it->second << ")" << std::endl;
228+
return os;
229+
}
230+
207231
}

src/AvTranscoder/profile/ProfileLoader.hpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77
#include <vector>
88
#include <map>
9+
#include <iostream>
910

1011
namespace avtranscoder
1112
{
@@ -51,27 +52,32 @@ class AvExport ProfileLoader
5152
void loadProfile( const std::string& avProfileFileName );
5253

5354
/**
54-
* @brief Load the given profile
55+
* @brief Load the given profile
5556
* @exception throw std::runtime_error if the profile is invalid
5657
*/
5758
void loadProfile( const Profile& profile );
5859

59-
const Profiles& getProfiles();
60+
bool hasProfile( const Profile& profile ) const;
6061

61-
Profiles getFormatProfiles();
62-
Profiles getVideoProfiles();
63-
Profiles getAudioProfiles();
62+
const Profiles& getProfiles() const;
6463

65-
Profile& getProfile( const std::string& avProfileIdentificator );
64+
Profiles getFormatProfiles() const;
65+
Profiles getVideoProfiles() const;
66+
Profiles getAudioProfiles() const;
67+
68+
const Profile& getProfile( const std::string& avProfileIdentificator ) const;
6669

6770
private:
68-
bool checkFormatProfile( const Profile& profileToCheck );
69-
bool checkVideoProfile( const Profile& profileToCheck );
70-
bool checkAudioProfile( const Profile& profileToCheck );
71+
bool checkFormatProfile( const Profile& profileToCheck ) const;
72+
bool checkVideoProfile( const Profile& profileToCheck ) const;
73+
bool checkAudioProfile( const Profile& profileToCheck ) const;
7174

7275
private:
7376
Profiles _profiles;
7477
};
7578

79+
// To print a profile
80+
std::ostream &operator<<( std::ostream &os, const ProfileLoader::Profile &profile );
81+
7682
}
7783
#endif

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
4747
// Transcode
4848
else
4949
{
50-
ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
50+
const ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
5151
add( filename, streamIndex, transcodeProfile, offset );
5252
}
5353
}
@@ -66,12 +66,12 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
6666
// Transcode
6767
else
6868
{
69-
ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
69+
const ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
7070
add( filename, streamIndex, transcodeProfile, codec, offset );
7171
}
7272
}
7373

74-
void Transcoder::add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, const double offset )
74+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const ProfileLoader::Profile& profile, const double offset )
7575
{
7676
// Check filename
7777
if( ! filename.length() )
@@ -80,7 +80,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro
8080
addTranscodeStream( filename, streamIndex, -1, profile, offset );
8181
}
8282

83-
void Transcoder::add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset )
83+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const ProfileLoader::Profile& profile, ICodec& codec, const double offset )
8484
{
8585
// Generator
8686
if( ! filename.length() )
@@ -119,7 +119,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
119119
// Transcode
120120
else
121121
{
122-
ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
122+
const ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
123123
add( filename, streamIndex, subStreamIndex, transcodeProfile, offset );
124124
}
125125
}
@@ -150,12 +150,12 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
150150
// Transcode
151151
else
152152
{
153-
ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
153+
const ProfileLoader::Profile& transcodeProfile = _profileLoader.getProfile( profileName );
154154
add( filename, streamIndex, subStreamIndex, transcodeProfile, codec, offset );
155155
}
156156
}
157157

158-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const double offset )
158+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const ProfileLoader::Profile& profile, const double offset )
159159
{
160160
// No subStream selected
161161
if( subStreamIndex < 0 )
@@ -171,7 +171,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
171171
addTranscodeStream( filename, streamIndex, subStreamIndex, profile, offset );
172172
}
173173

174-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset )
174+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const ProfileLoader::Profile& profile, ICodec& codec, const double offset )
175175
{
176176
// No subStream selected
177177
if( subStreamIndex < 0 )
@@ -290,17 +290,18 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
290290
ProfileLoader::Profile profile = getProfileFromFile( *referenceFile, streamIndex );
291291

292292
// override channels parameter to manage demultiplexing
293-
ProfileLoader::Profile::iterator it = profile.find( constants::avProfileChannel );
293+
ProfileLoader::Profile::iterator it = profile.find( constants::avProfileChannel );
294294
if( it != profile.end() )
295295
it->second = "1";
296296

297297
addTranscodeStream( filename, streamIndex, subStreamIndex, profile, offset );
298298
}
299299

300-
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const double offset )
300+
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const ProfileLoader::Profile& profile, const double offset )
301301
{
302302
// Add profile
303-
_profileLoader.loadProfile( profile );
303+
if( ! _profileLoader.hasProfile( profile ) )
304+
_profileLoader.loadProfile( profile );
304305

305306
LOG_INFO( "Add transcode stream from file '" << filename << "' / index=" << streamIndex << " / channel=" << subStreamIndex << " / encodingProfile=" << profile.at( constants::avProfileIdentificatorHuman ) << " / offset=" << offset << "s" )
306307

@@ -329,7 +330,8 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
329330
void Transcoder::addDummyStream( const ProfileLoader::Profile& profile, const ICodec& codec )
330331
{
331332
// Add profile
332-
_profileLoader.loadProfile( profile );
333+
if( ! _profileLoader.hasProfile( profile ) )
334+
_profileLoader.loadProfile( profile );
333335

334336
LOG_INFO( "Add generated stream with codec '" << codec.getCodecName() << "' / encodingProfile=" << profile.at( constants::avProfileIdentificatorHuman ) )
335337

src/AvTranscoder/transcoder/Transcoder.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ class AvExport Transcoder
6666
* @brief Add a stream and set a custom profile
6767
* @note Profile will be updated, be sure to pass unique profile name.
6868
*/
69-
void add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, const double offset = 0 );
69+
void add( const std::string& filename, const size_t streamIndex, const ProfileLoader::Profile& profile, const double offset = 0 );
7070
/*
7171
* @note If filename is empty, add a generated stream.
7272
*/
73-
void add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset = 0 );
73+
void add( const std::string& filename, const size_t streamIndex, const ProfileLoader::Profile& profile, ICodec& codec, const double offset = 0 );
7474

7575
/**
7676
* @brief Add a stream and set a profile
@@ -89,11 +89,11 @@ class AvExport Transcoder
8989
* @note Profile will be updated, be sure to pass unique profile name.
9090
* @note If subStreamIndex is negative, no substream is selected it's the stream.
9191
*/
92-
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const double offset = 0 );
92+
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const ProfileLoader::Profile& profile, const double offset = 0 );
9393
/**
9494
* @note If filename is empty, add a generated stream.
9595
*/
96-
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset = 0 );
96+
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const ProfileLoader::Profile& profile, ICodec& codec, const double offset = 0 );
9797

9898
/**
9999
* @brief Add the stream
@@ -139,7 +139,7 @@ class AvExport Transcoder
139139
void addRewrapStream( const std::string& filename, const size_t streamIndex );
140140

141141
void addTranscodeStream( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const double offset );
142-
void addTranscodeStream( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const double offset = 0 );
142+
void addTranscodeStream( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const ProfileLoader::Profile& profile, const double offset = 0 );
143143

144144
void addDummyStream( const ProfileLoader::Profile& profile, const ICodec& codec );
145145

0 commit comments

Comments
 (0)