this repo has no description
1/*
2This file is part of Darling.
3
4Copyright (C) 2020 Lubos Dolezel
5
6Darling is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11Darling is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Darling. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20
21#ifndef _AUDIO_FILE_FORMAT_MANAGER_H
22#define _AUDIO_FILE_FORMAT_MANAGER_H
23#include <AudioToolbox/AudioComponent.h>
24#include <AudioToolbox/AudioFileComponent.h>
25#include <vector>
26#include <string>
27#include <set>
28#include <list>
29#include <unordered_map>
30
31class __attribute__((visibility("hidden"))) AudioFileFormatManager
32{
33public:
34 static AudioFileFormatManager* instance();
35
36 std::set<UInt32> availableFormatIDs(UInt32 fileType) const;
37
38 struct ComponentInfo
39 {
40 AudioComponentDescription acd;
41 UInt32 fileType; // container type
42 std::string name;
43 std::vector<std::string> utis, extensions, mimeTypes;
44 // codec type
45 std::unordered_map<UInt32, std::vector<AudioStreamBasicDescription>> formatDescriptions;
46 bool canRead, canWrite;
47 };
48
49 const ComponentInfo* fileType(UInt32 type) const;
50 std::set<UInt32> types(bool writableTypes) const;
51
52 std::set<UInt32> typesForMIME(const char* mime) const;
53 std::set<UInt32> typesForUTI(const char* uti) const;
54 std::set<UInt32> typesForExtension(const char* ext) const;
55
56 std::set<std::string> allMIMEs() const;
57 std::set<std::string> allUTIs() const;
58 std::set<std::string> allExtensions() const;
59private:
60 AudioFileFormatManager();
61 void buildDatabase();
62 void analyzeAudioFileComponent(AudioFileComponent component);
63 void registerComponent(const ComponentInfo& ci);
64 static void addToMap(std::unordered_map<std::string, std::vector<ComponentInfo*>>& map, const std::string& key, ComponentInfo* ci);
65 static std::set<UInt32> findTypes(const std::unordered_map<std::string, std::vector<ComponentInfo*>>& map, const char* key);
66
67 template <typename K, typename V>
68 std::set<K> allKeys(const std::unordered_map<K,V>& map) const
69 {
70 std::set<K> rv;
71 for (auto const& [k, v] : map)
72 rv.insert(k);
73 return rv;
74 }
75private:
76 std::list<ComponentInfo> m_components;
77 std::unordered_map<UInt32, ComponentInfo*> m_fileTypeMap;
78 std::unordered_map<std::string, std::vector<ComponentInfo*>> m_mimeMap, m_utiMap, m_extensionMap;
79};
80
81#endif
82