2016-02-26 23:04:30 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
2016-02-26 23:04:30 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-02-26 23:04:30 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-02-26 23:04:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2016-02-27 07:41:11 +00:00
|
|
|
#include "display.h"
|
2016-02-27 00:59:55 +00:00
|
|
|
#include "instructions.h"
|
2016-02-26 23:04:30 +00:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include <guacamole/client.h>
|
2016-02-27 00:05:13 +00:00
|
|
|
#include <guacamole/error.h>
|
|
|
|
#include <guacamole/parser.h>
|
|
|
|
#include <guacamole/socket.h>
|
2016-02-26 23:04:30 +00:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-03-16 00:08:39 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-03-16 00:24:25 +00:00
|
|
|
#include <stdbool.h>
|
2016-02-26 23:04:30 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-02-27 00:05:13 +00:00
|
|
|
/**
|
|
|
|
* Reads and handles all Guacamole instructions from the given guac_socket
|
|
|
|
* until end-of-stream is reached.
|
|
|
|
*
|
2016-02-27 07:41:11 +00:00
|
|
|
* @param display
|
|
|
|
* The current internal display of the Guacamole video encoder.
|
|
|
|
*
|
2016-02-27 00:05:13 +00:00
|
|
|
* @param path
|
|
|
|
* The name of the file being parsed (for logging purposes). This file
|
|
|
|
* must already be open and available through the given socket.
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* The guac_socket through which instructions should be read.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Zero on success, non-zero if parsing of Guacamole protocol data through
|
|
|
|
* the given socket fails.
|
|
|
|
*/
|
2016-02-27 07:41:11 +00:00
|
|
|
static int guacenc_read_instructions(guacenc_display* display,
|
|
|
|
const char* path, guac_socket* socket) {
|
2016-02-27 00:05:13 +00:00
|
|
|
|
|
|
|
/* Obtain Guacamole protocol parser */
|
|
|
|
guac_parser* parser = guac_parser_alloc();
|
|
|
|
if (parser == NULL)
|
|
|
|
return 1;
|
2016-02-26 23:04:30 +00:00
|
|
|
|
2016-02-27 00:59:55 +00:00
|
|
|
/* Continuously read and handle all instructions */
|
2016-02-27 00:05:13 +00:00
|
|
|
while (!guac_parser_read(parser, socket, -1)) {
|
2018-02-07 05:19:24 +00:00
|
|
|
if (guacenc_handle_instruction(display, parser->opcode,
|
|
|
|
parser->argc, parser->argv)) {
|
2018-04-12 22:07:45 +00:00
|
|
|
guacenc_log(GUAC_LOG_DEBUG, "Handling of \"%s\" instruction "
|
|
|
|
"failed.", parser->opcode);
|
2018-02-07 05:19:24 +00:00
|
|
|
}
|
2016-02-27 00:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail on read/parse error */
|
|
|
|
if (guac_error != GUAC_STATUS_CLOSED) {
|
|
|
|
guacenc_log(GUAC_LOG_ERROR, "%s: %s",
|
|
|
|
path, guac_status_string(guac_error));
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse complete */
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-11 22:01:16 +00:00
|
|
|
int guacenc_encode(const char* path, const char* out_path, const char* codec,
|
2016-03-16 00:24:25 +00:00
|
|
|
int width, int height, int bitrate, bool force) {
|
2016-02-26 23:04:30 +00:00
|
|
|
|
|
|
|
/* Open input file */
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
guacenc_log(GUAC_LOG_ERROR, "%s: %s", path, strerror(errno));
|
2016-03-12 06:15:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-16 00:08:39 +00:00
|
|
|
/* Lock entire input file for reading by the current process */
|
|
|
|
struct flock file_lock = {
|
|
|
|
.l_type = F_RDLCK,
|
|
|
|
.l_whence = SEEK_SET,
|
|
|
|
.l_start = 0,
|
|
|
|
.l_len = 0,
|
|
|
|
.l_pid = getpid()
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Abort if file cannot be locked for reading */
|
2016-03-16 00:24:25 +00:00
|
|
|
if (!force && fcntl(fd, F_SETLK, &file_lock) == -1) {
|
2016-03-16 00:08:39 +00:00
|
|
|
|
|
|
|
/* Warn if lock cannot be acquired */
|
|
|
|
if (errno == EACCES || errno == EAGAIN)
|
|
|
|
guacenc_log(GUAC_LOG_WARNING, "Refusing to encode in-progress "
|
2016-03-16 00:24:25 +00:00
|
|
|
"recording \"%s\" (specify the -f option to override "
|
|
|
|
"this behavior).", path);
|
2016-03-16 00:08:39 +00:00
|
|
|
|
|
|
|
/* Log an error if locking fails in an unexpected way */
|
|
|
|
else
|
|
|
|
guacenc_log(GUAC_LOG_ERROR, "Cannot lock \"%s\" for reading: %s",
|
|
|
|
path, strerror(errno));
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-12 06:15:02 +00:00
|
|
|
/* Allocate display for encoding process */
|
|
|
|
guacenc_display* display = guacenc_display_alloc(out_path, codec,
|
|
|
|
width, height, bitrate);
|
|
|
|
if (display == NULL) {
|
|
|
|
close(fd);
|
2016-02-26 23:04:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-27 00:05:13 +00:00
|
|
|
/* Obtain guac_socket wrapping file descriptor */
|
|
|
|
guac_socket* socket = guac_socket_open(fd);
|
|
|
|
if (socket == NULL) {
|
|
|
|
guacenc_log(GUAC_LOG_ERROR, "%s: %s", path,
|
|
|
|
guac_status_string(guac_error));
|
|
|
|
close(fd);
|
2016-02-27 07:41:11 +00:00
|
|
|
guacenc_display_free(display);
|
2016-02-27 00:05:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-11 22:01:16 +00:00
|
|
|
guacenc_log(GUAC_LOG_INFO, "Encoding \"%s\" to \"%s\" ...", path, out_path);
|
2016-02-27 00:05:13 +00:00
|
|
|
|
|
|
|
/* Attempt to read all instructions in the file */
|
2016-02-27 07:41:11 +00:00
|
|
|
if (guacenc_read_instructions(display, path, socket)) {
|
2016-02-27 00:05:13 +00:00
|
|
|
guac_socket_free(socket);
|
2016-02-27 07:41:11 +00:00
|
|
|
guacenc_display_free(display);
|
2016-02-27 00:05:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-02-26 23:04:30 +00:00
|
|
|
|
2016-02-27 07:41:11 +00:00
|
|
|
/* Close input and finish encoding process */
|
2016-02-27 00:05:13 +00:00
|
|
|
guac_socket_free(socket);
|
2016-02-27 07:41:11 +00:00
|
|
|
return guacenc_display_free(display);
|
2016-02-26 23:04:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|