]> git.sesse.net Git - nageru/blobdiff - nageru/input_mapping.cpp
Make the delay analyzer understand that two sources can have different starting times.
[nageru] / nageru / input_mapping.cpp
index 45b60095e5af99cadecb01cb88389478974d5cc7..e41f9950332244d83a9e4d99c8e200541b9e89eb 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "audio_mixer.h" 
 #include "state.pb.h"
+#include "shared/text_proto.h"
 
 using namespace std;
 using namespace google::protobuf;
@@ -43,7 +44,13 @@ bool save_input_mapping_to_file(const map<DeviceSpec, DeviceInfo> &devices, cons
                for (const InputMapping::Bus &bus : input_mapping.buses) {
                        if (!used_devices.count(bus.device)) {
                                used_devices.emplace(bus.device, used_devices.size());
-                               global_audio_mixer->serialize_device(bus.device, mapping_proto.add_device());
+                               DeviceSpecProto *device_proto = mapping_proto.add_device();
+                               global_audio_mixer->serialize_device(bus.device, device_proto);
+
+                               const auto delay_it = input_mapping.extra_delay_ms.find(bus.device);
+                               if (delay_it != input_mapping.extra_delay_ms.end()) {
+                                       device_proto->set_extra_delay_ms(delay_it->second);
+                               }
                        }
 
                        BusProto *bus_proto = mapping_proto.add_bus();
@@ -54,39 +61,15 @@ bool save_input_mapping_to_file(const map<DeviceSpec, DeviceInfo> &devices, cons
                }
        }
 
-       // Save to disk. We use the text format because it's friendlier
-       // for a user to look at and edit.
-       int fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
-       if (fd == -1) {
-               perror(filename.c_str());
-               return false;
-       }
-       io::FileOutputStream output(fd);  // Takes ownership of fd.
-       if (!TextFormat::Print(mapping_proto, &output)) {
-               // TODO: Don't overwrite the old file (if any) on error.
-               output.Close();
-               return false;
-       }
-
-       output.Close();
-       return true;
+       return save_proto_to_file(mapping_proto, filename);
 }
 
 bool load_input_mapping_from_file(const map<DeviceSpec, DeviceInfo> &devices, const string &filename, InputMapping *new_mapping)
 {
-       // Read and parse the protobuf from disk.
-       int fd = open(filename.c_str(), O_RDONLY);
-       if (fd == -1) {
-               perror(filename.c_str());
-               return false;
-       }
-       io::FileInputStream input(fd);  // Takes ownership of fd.
        InputMappingProto mapping_proto;
-       if (!TextFormat::Parse(&input, &mapping_proto)) {
-               input.Close();
+       if (!load_proto_from_file(filename, &mapping_proto)) {
                return false;
        }
-       input.Close();
 
        // Map devices in the proto to our current ones:
 
@@ -198,6 +181,7 @@ found_alsa_input:
                default:
                        assert(false);
                }
+               new_mapping->extra_delay_ms.emplace(device_mapping.back(), device_proto.extra_delay_ms());
        }
 
        for (const BusProto &bus_proto : mapping_proto.bus()) {