]> git.sesse.net Git - nageru/blob - input_mapping.cpp
Write 1.4.0 changelog.
[nageru] / input_mapping.cpp
1 #include "input_mapping.h"
2
3 #include <assert.h>
4 #include <fcntl.h>
5 #include <google/protobuf/io/zero_copy_stream_impl.h>
6 #include <google/protobuf/text_format.h>
7 #include <stdio.h>
8 #include <set>
9 #include <utility>
10
11 #include "audio_mixer.h" 
12 #include "state.pb.h"
13
14 using namespace std;
15 using namespace google::protobuf;
16
17 bool save_input_mapping_to_file(const map<DeviceSpec, DeviceInfo> &devices, const InputMapping &input_mapping, const string &filename)
18 {
19         InputMappingProto mapping_proto;
20         {
21                 map<DeviceSpec, unsigned> used_devices;
22                 for (const InputMapping::Bus &bus : input_mapping.buses) {
23                         if (!used_devices.count(bus.device)) {
24                                 used_devices.emplace(bus.device, used_devices.size());
25                                 global_audio_mixer->serialize_device(bus.device, mapping_proto.add_device());
26                         }
27
28                         BusProto *bus_proto = mapping_proto.add_bus();
29                         bus_proto->set_name(bus.name);
30                         bus_proto->set_device_index(used_devices[bus.device]);
31                         bus_proto->set_source_channel_left(bus.source_channel[0]);
32                         bus_proto->set_source_channel_right(bus.source_channel[1]);
33                 }
34         }
35
36         // Save to disk. We use the text format because it's friendlier
37         // for a user to look at and edit.
38         int fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
39         if (fd == -1) {
40                 perror(filename.c_str());
41                 return false;
42         }
43         io::FileOutputStream output(fd);  // Takes ownership of fd.
44         if (!TextFormat::Print(mapping_proto, &output)) {
45                 // TODO: Don't overwrite the old file (if any) on error.
46                 output.Close();
47                 return false;
48         }
49
50         output.Close();
51         return true;
52 }
53
54 bool load_input_mapping_from_file(const map<DeviceSpec, DeviceInfo> &devices, const string &filename, InputMapping *new_mapping)
55 {
56         // Read and parse the protobuf from disk.
57         int fd = open(filename.c_str(), O_RDONLY);
58         if (fd == -1) {
59                 perror(filename.c_str());
60                 return false;
61         }
62         io::FileInputStream input(fd);  // Takes ownership of fd.
63         InputMappingProto mapping_proto;
64         if (!TextFormat::Parse(&input, &mapping_proto)) {
65                 input.Close();
66                 return false;
67         }
68         input.Close();
69
70         // Map devices in the proto to our current ones:
71
72         // Get a list of all active devices.
73         set<DeviceSpec> remaining_devices;
74         for (const auto &device_spec_and_info : devices) {
75                 remaining_devices.insert(device_spec_and_info.first);
76         }
77
78         // Now look at every device in the serialized protobuf and try to map
79         // it to one device we haven't taken yet. This isn't a full maximal matching,
80         // but it's good enough for our uses.
81         vector<DeviceSpec> device_mapping;
82         for (unsigned device_index = 0; device_index < unsigned(mapping_proto.device_size()); ++device_index) {
83                 const DeviceSpecProto &device_proto = mapping_proto.device(device_index);
84                 switch (device_proto.type()) {
85                 case DeviceSpecProto::SILENCE:
86                         device_mapping.push_back(DeviceSpec{InputSourceType::SILENCE, 0});
87                         break;
88                 case DeviceSpecProto::CAPTURE_CARD: {
89                         // First see if there's a card that matches on both index and name.
90                         DeviceSpec spec{InputSourceType::CAPTURE_CARD, unsigned(device_proto.index())};
91                         assert(devices.count(spec));
92                         const DeviceInfo &dev = devices.find(spec)->second;
93                         if (remaining_devices.count(spec) &&
94                             dev.display_name == device_proto.display_name()) {
95                                 device_mapping.push_back(spec);
96                                 remaining_devices.erase(spec);
97                                 goto found_capture_card;
98                         }
99
100                         // Scan and see if there's a match on name alone.
101                         for (const DeviceSpec &spec : remaining_devices) {
102                                 if (spec.type == InputSourceType::CAPTURE_CARD &&
103                                     dev.display_name == device_proto.display_name()) {
104                                         device_mapping.push_back(spec);
105                                         remaining_devices.erase(spec);
106                                         goto found_capture_card;
107                                 }
108                         }
109
110                         // OK, see if at least the index is free.
111                         if (remaining_devices.count(spec)) {
112                                 device_mapping.push_back(spec);
113                                 remaining_devices.erase(spec);
114                                 goto found_capture_card;
115                         }
116
117                         // Give up.
118                         device_mapping.push_back(DeviceSpec{InputSourceType::SILENCE, 0});
119 found_capture_card:
120                         break;
121                 }
122                 case DeviceSpecProto::ALSA_INPUT: {
123                         // For ALSA, we don't really care about index, but we can use address
124                         // in its place.
125
126                         // First see if there's a card that matches on name, num_channels and address.
127                         for (const DeviceSpec &spec : remaining_devices) {
128                                 assert(devices.count(spec));
129                                 const DeviceInfo &dev = devices.find(spec)->second;
130                                 if (spec.type == InputSourceType::ALSA_INPUT &&
131                                     dev.alsa_name == device_proto.alsa_name() &&
132                                     dev.alsa_info == device_proto.alsa_info() &&
133                                     int(dev.num_channels) == device_proto.num_channels() &&
134                                     dev.alsa_address == device_proto.address()) {
135                                         device_mapping.push_back(spec);
136                                         remaining_devices.erase(spec);
137                                         goto found_alsa_input;
138                                 }
139                         }
140
141                         // Looser check: Ignore the address.
142                         for (const DeviceSpec &spec : remaining_devices) {
143                                 assert(devices.count(spec));
144                                 const DeviceInfo &dev = devices.find(spec)->second;
145                                 if (spec.type == InputSourceType::ALSA_INPUT &&
146                                     dev.alsa_name == device_proto.alsa_name() &&
147                                     dev.alsa_info == device_proto.alsa_info() &&
148                                     int(dev.num_channels) == device_proto.num_channels()) {
149                                         device_mapping.push_back(spec);
150                                         remaining_devices.erase(spec);
151                                         goto found_alsa_input;
152                                 }
153                         }
154
155                         // OK, so we couldn't map this to a device, but perhaps one is added
156                         // at some point in the future through hotplug. Create a dead card
157                         // matching this one; right now, it will give only silence,
158                         // but it could be replaced with something later.
159                         //
160                         // NOTE: There's a potential race condition here, if the card
161                         // gets inserted while we're doing the device remapping
162                         // (or perhaps more realistically, while we're reading the
163                         // input mapping from disk).
164                         DeviceSpec dead_card_spec;
165                         dead_card_spec = global_audio_mixer->create_dead_card(
166                                 device_proto.alsa_name(), device_proto.alsa_info(), device_proto.num_channels());
167                         device_mapping.push_back(dead_card_spec);
168
169 found_alsa_input:
170                         break;
171                 }
172                 default:
173                         assert(false);
174                 }
175         }
176
177         for (const BusProto &bus_proto : mapping_proto.bus()) {
178                 if (bus_proto.device_index() < 0 || unsigned(bus_proto.device_index()) >= device_mapping.size()) {
179                         return false;
180                 }
181                 InputMapping::Bus bus;
182                 bus.name = bus_proto.name();
183                 bus.device = device_mapping[bus_proto.device_index()];
184                 bus.source_channel[0] = bus_proto.source_channel_left();
185                 bus.source_channel[1] = bus_proto.source_channel_right();
186                 new_mapping->buses.push_back(bus);
187         }
188
189         return true;
190 }