1 #include "input_mapping.h"
5 #include <google/protobuf/io/zero_copy_stream_impl.h>
6 #include <google/protobuf/text_format.h>
11 #include "audio_mixer.h"
15 using namespace google::protobuf;
17 bool save_input_mapping_to_file(const map<DeviceSpec, DeviceInfo> &devices, const InputMapping &input_mapping, const string &filename)
19 InputMappingProto mapping_proto;
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());
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]);
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);
40 perror(filename.c_str());
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.
54 bool load_input_mapping_from_file(const map<DeviceSpec, DeviceInfo> &devices, const string &filename, InputMapping *new_mapping)
56 // Read and parse the protobuf from disk.
57 int fd = open(filename.c_str(), O_RDONLY);
59 perror(filename.c_str());
62 io::FileInputStream input(fd); // Takes ownership of fd.
63 InputMappingProto mapping_proto;
64 if (!TextFormat::Parse(&input, &mapping_proto)) {
70 // Map devices in the proto to our current ones:
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);
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});
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;
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;
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;
118 device_mapping.push_back(DeviceSpec{InputSourceType::SILENCE, 0});
122 case DeviceSpecProto::ALSA_INPUT: {
123 // For ALSA, we don't really care about index, but we can use address
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;
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;
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.
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);
177 for (const BusProto &bus_proto : mapping_proto.bus()) {
178 if (bus_proto.device_index() < 0 || unsigned(bus_proto.device_index()) >= device_mapping.size()) {
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);