//
//  Utilities.swift
//  masterkey
//
//  Created by Ian Kohl on 4/6/19.
//  Copyright © 2019 One Sky IT. All rights reserved.
//

import Foundation

class Utilities {
    func startRecording(filename: String, num_screens: Int) -> String
    {
        //Create output filename
        let timestamp = String(NSDate().timeIntervalSince1970)
        let home = NSHomeDirectory()
        let recording_filename = timestamp + filename + ".mkv"
        let recording_path = "\(home)/recordings/\(recording_filename)"
        
        let device_string = "\"\(num_screens):0\""
        
        //Get Devices
        //ffmpeg -f avfoundation -list_devices true -i ""
        
        //Start recording
        //ffmpeg -f avfoundation -i "2:<audio device index>" ~/recordings/filename.mkv
        
        let task:Process = Process()
        let pipe:Pipe = Pipe()
        
        task.launchPath = "/usr/local/bin/ffmpeg"
        task.arguments = ["-f","avfoundation","-i",device_string,recording_path]
        task.standardOutput = pipe
        task.launch()
        
        let handle = pipe.fileHandleForReading
        let data = handle.readDataToEndOfFile()
        let result_s = String(data: data, encoding: String.Encoding.utf8)!
        return result_s
    }
}
