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"
13 #include "shared/text_proto.h"
16 using namespace google::protobuf;
18 string spec_to_string(DeviceSpec device_spec)
22 switch (device_spec.type) {
23 case InputSourceType::SILENCE:
25 case InputSourceType::CAPTURE_CARD:
26 snprintf(buf, sizeof(buf), "Capture card %u", device_spec.index);
28 case InputSourceType::ALSA_INPUT:
29 snprintf(buf, sizeof(buf), "ALSA input %u", device_spec.index);
31 case InputSourceType::FFMPEG_VIDEO_INPUT:
32 snprintf(buf, sizeof(buf), "FFmpeg input %u", device_spec.index);
39 bool save_input_mapping_to_file(const map<DeviceSpec, DeviceInfo> &devices, const InputMapping &input_mapping, const string &filename)
41 InputMappingProto mapping_proto;
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());
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]);
58 return save_proto_to_file(mapping_proto, filename);
61 bool load_input_mapping_from_file(const map<DeviceSpec, DeviceInfo> &devices, const string &filename, InputMapping *new_mapping)
63 InputMappingProto mapping_proto;
64 if (!load_proto_from_file(filename, &mapping_proto)) {
68 // Map devices in the proto to our current ones:
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);
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});
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.
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));
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;
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;
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;
121 device_mapping.push_back(DeviceSpec{InputSourceType::SILENCE, 0});
125 case DeviceSpecProto::ALSA_INPUT: {
126 // For ALSA, we don't really care about index, but we can use address
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;
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;
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.
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);
180 for (const BusProto &bus_proto : mapping_proto.bus()) {
181 if (bus_proto.device_index() < 0 || unsigned(bus_proto.device_index()) >= device_mapping.size()) {
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);