# FFmpegKit Linux API: AbstractSession.cpp Source File

AbstractSession.cpp
Go to the documentation of this file.
/*
* Copyright (c) 2022 Taner Sener
*
* This file is part of FFmpegKit.
*
* FFmpegKit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FFmpegKit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General License for more details.
*
* You should have received a copy of the GNU Lesser General License
* along with FFmpegKit. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AbstractSession.h"
#include "FFmpegKit.h"
#include "FFmpegKitConfig.h"
#include "LogCallback.h"
#include "ReturnCode.h"
#include <mutex>
#include <thread>
#include <iostream>
#include <atomic>
#include <algorithm>
#include <condition_variable>
static std::atomic<long> sessionIdGenerator(1);
extern void addSessionToSessionHistory(const std::shared_ptr<ffmpegkit::Session> session);
ffmpegkit::AbstractSession::AbstractSession(const std::list<std::string>& arguments, const ffmpegkit::LogCallback logCallback, const LogRedirectionStrategy logRedirectionStrategy) :
_arguments{std::make_shared<std::list<std::string>>(arguments)},
_sessionId{sessionIdGenerator++},
_logCallback{logCallback},
_createTime{std::chrono::system_clock::now()},
_logs{std::make_shared<std::list<std::shared_ptr<ffmpegkit::Log>>>()},
_state{SessionStateCreated},
_returnCode{nullptr},
_logRedirectionStrategy{logRedirectionStrategy} {
}
void ffmpegkit::AbstractSession::waitForAsynchronousMessagesInTransmit(const int timeout) const {
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
std::condition_variable condition_variable;
const std::chrono::time_point<std::chrono::system_clock> expireTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout);
while (this->thereAreAsynchronousMessagesInTransmit() && (std::chrono::system_clock::now() < expireTime)) {
condition_variable.wait_for(lock, std::chrono::milliseconds(100));
}
}
ffmpegkit::LogCallback ffmpegkit::AbstractSession::getLogCallback() const {
return _logCallback;
}
long ffmpegkit::AbstractSession::getSessionId() const {
return _sessionId;
}
std::chrono::time_point<std::chrono::system_clock> ffmpegkit::AbstractSession::getCreateTime() const {
return _createTime;
}
std::chrono::time_point<std::chrono::system_clock> ffmpegkit::AbstractSession::getStartTime() const {
return _startTime;
}
std::chrono::time_point<std::chrono::system_clock> ffmpegkit::AbstractSession::getEndTime() const {
return _endTime;
}
long ffmpegkit::AbstractSession::getDuration() const {
const std::chrono::time_point<std::chrono::system_clock> startTime = _startTime;
const std::chrono::time_point<std::chrono::system_clock> endTime = _endTime;
if (startTime.time_since_epoch() != std::chrono::microseconds(0) && endTime.time_since_epoch() != std::chrono::microseconds(0)) {
return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
}
return 0;
}
std::shared_ptr<std::list<std::string>> ffmpegkit::AbstractSession::getArguments() const {
return _arguments;
}
std::string ffmpegkit::AbstractSession::getCommand() const {
return ffmpegkit::FFmpegKitConfig::argumentsToString(_arguments);
}
std::shared_ptr<std::list<std::shared_ptr<ffmpegkit::Log>>> ffmpegkit::AbstractSession::getAllLogsWithTimeout(const int waitTimeout) const {
this->waitForAsynchronousMessagesInTransmit(waitTimeout);
if (this->thereAreAsynchronousMessagesInTransmit()) {
std::cout << "getAllLogsWithTimeout was called to return all logs but there are still logs being transmitted for session id " << _sessionId << "." << std::endl;
}
return this->getLogs();
}
std::shared_ptr<std::list<std::shared_ptr<ffmpegkit::Log>>> ffmpegkit::AbstractSession::getAllLogs() const {
return this->getAllLogsWithTimeout(ffmpegkit::AbstractSession::DefaultTimeoutForAsynchronousMessagesInTransmit);
}
std::shared_ptr<std::list<std::shared_ptr<ffmpegkit::Log>>> ffmpegkit::AbstractSession::getLogs() const {
return _logs;
}
std::string ffmpegkit::AbstractSession::getAllLogsAsStringWithTimeout(const int waitTimeout) const {
this->waitForAsynchronousMessagesInTransmit(waitTimeout);
if (this->thereAreAsynchronousMessagesInTransmit()) {
std::cout << "getAllLogsAsStringWithTimeout was called to return all logs but there are still logs being transmitted for session id " << _sessionId << "." << std::endl;
}
return this->getLogsAsString();
}
std::string ffmpegkit::AbstractSession::getAllLogsAsString() const {
return this->getAllLogsAsStringWithTimeout(ffmpegkit::AbstractSession::DefaultTimeoutForAsynchronousMessagesInTransmit);
}
std::string ffmpegkit::AbstractSession::getLogsAsString() const {
std::string concatenatedString;
std::for_each(_logs->cbegin(), _logs->cend(), [&](std::shared_ptr<ffmpegkit::Log> log) {
concatenatedString.append(log->getMessage());
});
return concatenatedString;
}
std::string ffmpegkit::AbstractSession::getOutput() const {
return this->getAllLogsAsString();
}
ffmpegkit::SessionState ffmpegkit::AbstractSession::getState() const {
return _state;
}
std::shared_ptr<ffmpegkit::ReturnCode> ffmpegkit::AbstractSession::getReturnCode() const {
return _returnCode;
}
std::string ffmpegkit::AbstractSession::getFailStackTrace() const {
return _failStackTrace;
}
ffmpegkit::LogRedirectionStrategy ffmpegkit::AbstractSession::getLogRedirectionStrategy() const {
return _logRedirectionStrategy;
}
bool ffmpegkit::AbstractSession::thereAreAsynchronousMessagesInTransmit() const {
return (FFmpegKitConfig::messagesInTransmit(_sessionId) != 0);
}
void ffmpegkit::AbstractSession::addLog(const std::shared_ptr<ffmpegkit::Log> log) {
_logs->push_back(log);
}
void ffmpegkit::AbstractSession::startRunning() {
_state = SessionStateRunning;
_startTime = std::chrono::system_clock::now();
}
void ffmpegkit::AbstractSession::complete(const std::shared_ptr<ffmpegkit::ReturnCode> returnCode) {
_returnCode = returnCode;
_state = SessionStateCompleted;
_endTime = std::chrono::system_clock::now();
}
void ffmpegkit::AbstractSession::fail(const char* error) {
_failStackTrace = error;
_state = SessionStateFailed;
_endTime = std::chrono::system_clock::now();
}
bool ffmpegkit::AbstractSession::isFFmpeg() const {
// IMPLEMENTED IN SUBCLASSES
return false;
}
bool ffmpegkit::AbstractSession::isFFprobe() const {
// IMPLEMENTED IN SUBCLASSES
return false;
}
bool ffmpegkit::AbstractSession::isMediaInformation() const {
// IMPLEMENTED IN SUBCLASSES
return false;
}
void ffmpegkit::AbstractSession::cancel() {
if (_state == SessionStateRunning) {
FFmpegKit::cancel(_sessionId);
}
}
sessionIdGenerator
static std::atomic< long > sessionIdGenerator(1)
addSessionToSessionHistory
void addSessionToSessionHistory(const std::shared_ptr< ffmpegkit::Session > session)
Definition: FFmpegKitConfig.cpp:160
AbstractSession.h
FFmpegKit.h
logCallback
static ffmpegkit::LogCallback logCallback
Definition: FFmpegKitConfig.cpp:64
FFmpegKitConfig.h
LogCallback.h
ReturnCode.h
ffmpegkit::AbstractSession::fail
void fail(const char *error) override
Definition: AbstractSession.cpp:177
ffmpegkit::AbstractSession::isFFmpeg
virtual bool isFFmpeg() const override
Definition: AbstractSession.cpp:183
ffmpegkit::AbstractSession::addLog
void addLog(const std::shared_ptr< ffmpegkit::Log > log) override
Definition: AbstractSession.cpp:162
ffmpegkit::AbstractSession::getLogsAsString
std::string getLogsAsString() const override
Definition: AbstractSession.cpp:128
ffmpegkit::AbstractSession::getDuration
long getDuration() const override
Definition: AbstractSession.cpp:78
ffmpegkit::AbstractSession::getSessionId
long getSessionId() const override
Definition: AbstractSession.cpp:62
ffmpegkit::AbstractSession::getEndTime
std::chrono::time_point< std::chrono::system_clock > getEndTime() const override
Definition: AbstractSession.cpp:74
ffmpegkit::AbstractSession::getAllLogsAsString
std::string getAllLogsAsString() const override
Definition: AbstractSession.cpp:124
ffmpegkit::AbstractSession::getOutput
std::string getOutput() const override
Definition: AbstractSession.cpp:138
ffmpegkit::AbstractSession::getLogCallback
ffmpegkit::LogCallback getLogCallback() const override
Definition: AbstractSession.cpp:58
ffmpegkit::AbstractSession::getCreateTime
std::chrono::time_point< std::chrono::system_clock > getCreateTime() const override
Definition: AbstractSession.cpp:66
ffmpegkit::AbstractSession::AbstractSession
AbstractSession(const std::list< std::string > &arguments, const ffmpegkit::LogCallback logCallback, const LogRedirectionStrategy logRedirectionStrategy)
Definition: AbstractSession.cpp:36
ffmpegkit::AbstractSession::getReturnCode
std::shared_ptr< ffmpegkit::ReturnCode > getReturnCode() const override
Definition: AbstractSession.cpp:146
ffmpegkit::AbstractSession::waitForAsynchronousMessagesInTransmit
void waitForAsynchronousMessagesInTransmit(const int timeout) const
Definition: AbstractSession.cpp:47
ffmpegkit::AbstractSession::getAllLogsWithTimeout
std::shared_ptr< std::list< std::shared_ptr< ffmpegkit::Log > > > getAllLogsWithTimeout(const int waitTimeout) const override
Definition: AbstractSession.cpp:97
ffmpegkit::AbstractSession::startRunning
void startRunning() override
Definition: AbstractSession.cpp:166
ffmpegkit::AbstractSession::complete
void complete(const std::shared_ptr< ffmpegkit::ReturnCode > returnCode) override
Definition: AbstractSession.cpp:171
ffmpegkit::AbstractSession::getAllLogs
std::shared_ptr< std::list< std::shared_ptr< ffmpegkit::Log > > > getAllLogs() const override
Definition: AbstractSession.cpp:106
ffmpegkit::AbstractSession::getStartTime
std::chrono::time_point< std::chrono::system_clock > getStartTime() const override
Definition: AbstractSession.cpp:70
ffmpegkit::AbstractSession::getFailStackTrace
std::string getFailStackTrace() const override
Definition: AbstractSession.cpp:150
ffmpegkit::AbstractSession::getCommand
std::string getCommand() const override
Definition: AbstractSession.cpp:93
ffmpegkit::AbstractSession::getState
ffmpegkit::SessionState getState() const override
Definition: AbstractSession.cpp:142
ffmpegkit::AbstractSession::thereAreAsynchronousMessagesInTransmit
bool thereAreAsynchronousMessagesInTransmit() const override
Definition: AbstractSession.cpp:158
ffmpegkit::AbstractSession::getArguments
std::shared_ptr< std::list< std::string > > getArguments() const override
Definition: AbstractSession.cpp:89
ffmpegkit::AbstractSession::getLogs
std::shared_ptr< std::list< std::shared_ptr< ffmpegkit::Log > > > getLogs() const override
Definition: AbstractSession.cpp:110
ffmpegkit::AbstractSession::getAllLogsAsStringWithTimeout
std::string getAllLogsAsStringWithTimeout(const int waitTimeout) const override
Definition: AbstractSession.cpp:114
ffmpegkit::AbstractSession::getLogRedirectionStrategy
ffmpegkit::LogRedirectionStrategy getLogRedirectionStrategy() const override
Definition: AbstractSession.cpp:154
ffmpegkit::AbstractSession::isMediaInformation
virtual bool isMediaInformation() const override
Definition: AbstractSession.cpp:193
ffmpegkit::AbstractSession::DefaultTimeoutForAsynchronousMessagesInTransmit
static constexpr int DefaultTimeoutForAsynchronousMessagesInTransmit
Definition: AbstractSession.h:37
ffmpegkit::AbstractSession::cancel
void cancel() override
Definition: AbstractSession.cpp:198
ffmpegkit::AbstractSession::isFFprobe
virtual bool isFFprobe() const override
Definition: AbstractSession.cpp:188
ffmpegkit::FFmpegKitConfig::messagesInTransmit
static int messagesInTransmit(const long sessionId)
Definition: FFmpegKitConfig.cpp:1347
ffmpegkit::FFmpegKitConfig::argumentsToString
static std::string argumentsToString(std::shared_ptr< std::list< std::string > > arguments)
Definition: FFmpegKitConfig.cpp:1412
ffmpegkit::FFmpegKit::cancel
static void cancel()
Definition: FFmpegKit.cpp:68
ffmpegkit::Log
Definition: Log.h:31
ffmpegkit
Definition: AbstractSession.h:25
ffmpegkit::LogRedirectionStrategy
LogRedirectionStrategy
Definition: LogRedirectionStrategy.h:25
ffmpegkit::SessionState
SessionState
Definition: SessionState.h:25
ffmpegkit::SessionStateRunning
@ SessionStateRunning
Definition: SessionState.h:27
ffmpegkit::SessionStateFailed
@ SessionStateFailed
Definition: SessionState.h:28
ffmpegkit::SessionStateCreated
@ SessionStateCreated
Definition: SessionState.h:26
ffmpegkit::SessionStateCompleted
@ SessionStateCompleted
Definition: SessionState.h:29
ffmpegkit::LogCallback
std::function< void(const std::shared_ptr< ffmpegkit::Log > log)> LogCallback
Definition: LogCallback.h:35