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