AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProfileLoader.cpp
Go to the documentation of this file.
1 #include "ProfileLoader.hpp"
2 
3 #include "util.hpp"
4 
5 #include <fstream>
6 #include <cstdlib>
7 #include <stdexcept>
8 
9 namespace avtranscoder
10 {
11 
12 ProfileLoader::ProfileLoader(const bool autoload)
13 {
14  if(autoload)
15  loadProfiles();
16 }
17 
18 void ProfileLoader::loadProfile(const std::string& avProfileFileName)
19 {
20  std::ifstream infile;
21  infile.open(avProfileFileName.c_str(), std::ifstream::in);
22 
23  ProfileLoader::Profile customProfile;
24 
25  std::string line;
26  while(std::getline(infile, line))
27  {
28  std::vector<std::string> keyValue;
29  split(keyValue, line, "=");
30  if(keyValue.size() == 2)
31  customProfile[keyValue.at(0)] = keyValue.at(1);
32  }
33  loadProfile(customProfile);
34 }
35 
36 void ProfileLoader::loadProfiles(const std::string& avProfilesPath)
37 {
38  std::string realAvProfilesPath = avProfilesPath;
39  if(realAvProfilesPath.empty())
40  {
41  // get custom profiles location from AVPROFILES environment variable
42  if(std::getenv("AVPROFILES"))
43  realAvProfilesPath = std::getenv("AVPROFILES");
44  // else get default profiles location
45  else
46  realAvProfilesPath = AVTRANSCODER_DEFAULT_AVPROFILES;
47  }
48 
49  std::vector<std::string> paths;
50  split(paths, realAvProfilesPath, ":");
51  for(std::vector<std::string>::iterator dirIt = paths.begin(); dirIt != paths.end(); ++dirIt)
52  {
53  std::vector<std::string> files;
54  if(getFilesInDir(*dirIt, files) != 0)
55  continue;
56 
57  for(std::vector<std::string>::iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt)
58  {
59  const std::string absPath = (*dirIt) + "/" + (*fileIt);
60  try
61  {
62  loadProfile(absPath);
63  }
64  catch(const std::exception& e)
65  {
66  LOG_WARN(e.what())
67  }
68  }
69  }
70 }
71 
72 void ProfileLoader::loadProfile(const Profile& profile)
73 {
74  // check profile identificator
75  if(!profile.count(constants::avProfileIdentificator))
76  {
77  throw std::runtime_error("Warning: A profile has no name. It will not be loaded.");
78  }
79 
80  // check profile type
81  if(profile.count(constants::avProfileType) == 0)
82  {
83  throw std::runtime_error("Warning: The profile " + profile.find(constants::avProfileIdentificator)->second +
84  " has not type. It will not be loaded.");
85  }
86 
87  // check complete profile
88  bool isValid = false;
89  std::string type(profile.find(constants::avProfileType)->second);
91  isValid = ProfileLoader::checkFormatProfile(profile);
92  else if(type == constants::avProfileTypeVideo)
93  isValid = ProfileLoader::checkVideoProfile(profile);
94  else if(type == constants::avProfileTypeAudio)
95  isValid = ProfileLoader::checkAudioProfile(profile);
96 
97  if(isValid)
98  {
99  LOG_INFO("Profile '" << profile.find(constants::avProfileIdentificatorHuman)->second << "' loaded")
100  _profiles.push_back(profile);
101  }
102  else
103  throw std::runtime_error("Warning: The profile " + profile.find(constants::avProfileIdentificator)->second +
104  " is invalid. It will not be loaded.");
105 }
106 
107 bool ProfileLoader::hasProfile(const Profile& profile) const
108 {
109  // check profile identificator
110  if(!profile.count(constants::avProfileIdentificator))
111  {
112  throw std::runtime_error("Warning: A profile has no name. It will not be loaded.");
113  }
114 
115  for(Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it)
116  {
118  return true;
119  }
120  return false;
121 }
122 
124 {
125  return _profiles;
126 }
127 
129 {
130  Profiles profiles;
131 
132  for(Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it)
133  {
135  profiles.push_back(*it);
136  }
137 
138  return profiles;
139 }
140 
142 {
143  Profiles profiles;
144 
145  for(Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it)
146  {
148  profiles.push_back(*it);
149  }
150 
151  return profiles;
152 }
153 
155 {
156  Profiles profiles;
157 
158  for(Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it)
159  {
161  profiles.push_back(*it);
162  }
163 
164  return profiles;
165 }
166 
168 {
169  for(Profiles::const_iterator it = _profiles.begin(); it != _profiles.end(); ++it)
170  {
172  {
173  return (*it);
174  }
175  }
176  throw std::runtime_error("unable to find profile: " + avProfileIdentificator);
177 }
178 
179 bool ProfileLoader::checkFormatProfile(const Profile& profileToCheck)
180 {
181  bool isValid = true;
182 
183  if(!profileToCheck.count(constants::avProfileIdentificator) ||
184  !profileToCheck.count(constants::avProfileIdentificatorHuman) || !profileToCheck.count(constants::avProfileType) ||
185  !profileToCheck.count(constants::avProfileFormat))
186  {
187  isValid = false;
188  }
189 
190  return isValid;
191 }
192 
193 bool ProfileLoader::checkVideoProfile(const Profile& profileToCheck)
194 {
195  bool isValid = true;
196 
197  if(!profileToCheck.count(constants::avProfileIdentificator) ||
198  !profileToCheck.count(constants::avProfileIdentificatorHuman) || !profileToCheck.count(constants::avProfileType) ||
199  !profileToCheck.count(constants::avProfileCodec))
200  {
201  isValid = false;
202  }
203 
204  return isValid;
205 }
206 
207 bool ProfileLoader::checkAudioProfile(const Profile& profileToCheck)
208 {
209  bool isValid = true;
210 
211  if(!profileToCheck.count(constants::avProfileIdentificator) ||
212  !profileToCheck.count(constants::avProfileIdentificatorHuman) || !profileToCheck.count(constants::avProfileType) ||
213  !profileToCheck.count(constants::avProfileCodec))
214  {
215  isValid = false;
216  }
217 
218  return isValid;
219 }
220 
221 #ifndef SWIG
222 std::ostream& operator<<(std::ostream& os, const ProfileLoader::Profile& profile)
223 {
224  for(ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it)
225  os << "(" << it->first << ", " << it->second << ")" << std::endl;
226  return os;
227 }
228 #endif
229 }
void loadProfile(const std::string &avProfileFileName)
Load the profile defines in the given file.
std::vector< Profile > Profiles
ProfileLoader(const bool autoload=true)
static bool checkFormatProfile(const Profile &profileToCheck)
const std::string avProfileFormat
bool hasProfile(const Profile &profile) const
std::map< std::string, std::string > Profile
const std::string avProfileTypeAudio
const std::string avProfileIdentificator
const std::string avProfileIdentificatorHuman
const Profile & getProfile(const std::string &avProfileIdentificator) const
const std::string avProfileTypeVideo
#define LOG_INFO(...)
Definition: log.hpp:23
const std::string avProfileTypeFormat
int getFilesInDir(const std::string &dir, std::vector< std::string > &files)
Definition: util.hpp:47
#define LOG_WARN(...)
Definition: log.hpp:29
static bool checkAudioProfile(const Profile &profileToCheck)
const Profiles & getProfiles() const
const std::string avProfileCodec
Profiles getFormatProfiles() const
void loadProfiles(const std::string &avProfilesPath="")
Load profiles from files in avProfilesPath directory.
std::ostream & operator<<(std::ostream &flux, const InputFile &input)
Definition: InputFile.cpp:171
const std::string avProfileType
Profiles getVideoProfiles() const
void split(std::vector< std::string > &splitString, const std::string &inputString, const std::string &splitChars)
Definition: util.hpp:37
Profiles getAudioProfiles() const
static bool checkVideoProfile(const Profile &profileToCheck)