]> git.sesse.net Git - nageru/blobdiff - nageru/input_mapping.cpp
Make number of cards flexible at runtime.
[nageru] / nageru / input_mapping.cpp
index 45b60095e5af99cadecb01cb88389478974d5cc7..cad723a7a92b9c8dee18809e4525b9011d2ade30 100644 (file)
 
 #include "audio_mixer.h" 
 #include "state.pb.h"
+#include "shared/text_proto.h"
 
 using namespace std;
 using namespace google::protobuf;
 
-string spec_to_string(DeviceSpec device_spec)
-{
-       char buf[256];
-
-       switch (device_spec.type) {
-       case InputSourceType::SILENCE:
-               return "<silence>";
-       case InputSourceType::CAPTURE_CARD:
-               snprintf(buf, sizeof(buf), "Capture card %u", device_spec.index);
-               return buf;
-       case InputSourceType::ALSA_INPUT:
-               snprintf(buf, sizeof(buf), "ALSA input %u", device_spec.index);
-               return buf;
-       case InputSourceType::FFMPEG_VIDEO_INPUT:
-               snprintf(buf, sizeof(buf), "FFmpeg input %u", device_spec.index);
-               return buf;
-       default:
-               assert(false);
-       }
-}
-
 bool save_input_mapping_to_file(const map<DeviceSpec, DeviceInfo> &devices, const InputMapping &input_mapping, const string &filename)
 {
        InputMappingProto mapping_proto;
@@ -54,39 +34,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:
 
@@ -106,12 +62,10 @@ bool load_input_mapping_from_file(const map<DeviceSpec, DeviceInfo> &devices, co
                case DeviceSpecProto::SILENCE:
                        device_mapping.push_back(DeviceSpec{InputSourceType::SILENCE, 0});
                        break;
-               case DeviceSpecProto::FFMPEG_VIDEO_INPUT:
                case DeviceSpecProto::CAPTURE_CARD: {
                        // First see if there's a card that matches on both index and name.
                        DeviceSpec spec;
-                       spec.type = (device_proto.type() == DeviceSpecProto::CAPTURE_CARD) ?
-                               InputSourceType::CAPTURE_CARD : InputSourceType::FFMPEG_VIDEO_INPUT;
+                       spec.type = InputSourceType::CAPTURE_CARD;
                        spec.index = unsigned(device_proto.index());
                        assert(devices.count(spec));