| FFmpegKit Linux API 6.0

Loading...
Searching...
No Matches
AbstractSession.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Taner Sener
3 *
4 * This file is part of FFmpegKit.
5 *
6 * FFmpegKit is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpegKit is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General License
17 * along with FFmpegKit. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "AbstractSession.h"
21#include "FFmpegKit.h"
22#include "FFmpegKitConfig.h"
23#include "LogCallback.h"
24#include "ReturnCode.h"
25#include <mutex>
26#include <thread>
27#include <iostream>
28#include <atomic>
29#include <algorithm>
30#include <condition_variable>
31
32 static std::atomic<long> sessionIdGenerator(1);
33
34 extern void addSessionToSessionHistory(const std::shared_ptr<ffmpegkit::Session> session);
35
36 ffmpegkit::AbstractSession::AbstractSession(const std::list<std::string>& arguments, const ffmpegkit::LogCallback logCallback, const LogRedirectionStrategy logRedirectionStrategy) :
37 _arguments{std::make_shared<std::list<std::string>>(arguments)},
38 _sessionId{sessionIdGenerator++},
39 _logCallback{logCallback},
40 _createTime{std::chrono::system_clock::now()},
41 _logs{std::make_shared<std::list<std::shared_ptr<ffmpegkit::Log>>>()},
42 _state{SessionStateCreated},
43 _returnCode{nullptr},
44 _logRedirectionStrategy{logRedirectionStrategy} {
45}
46
47 void ffmpegkit::AbstractSession::waitForAsynchronousMessagesInTransmit(const int timeout) const {
48 std::mutex mutex;
49 std::unique_lock<std::mutex> lock(mutex);
50 std::condition_variable condition_variable;
51 const std::chrono::time_point<std::chrono::system_clock> expireTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout);
52
53 while (this->thereAreAsynchronousMessagesInTransmit() && (std::chrono::system_clock::now() < expireTime)) {
54 condition_variable.wait_for(lock, std::chrono::milliseconds(100));
55 }
56}
57
58 ffmpegkit::LogCallback ffmpegkit::AbstractSession::getLogCallback() const {
59 return _logCallback;
60}
61
62 long ffmpegkit::AbstractSession::getSessionId() const {
63 return _sessionId;
64}
65
66std::chrono::time_point<std::chrono::system_clock> ffmpegkit::AbstractSession::getCreateTime() const {
67 return _createTime;
68}
69
70std::chrono::time_point<std::chrono::system_clock> ffmpegkit::AbstractSession::getStartTime() const {
71 return _startTime;
72}
73
74std::chrono::time_point<std::chrono::system_clock> ffmpegkit::AbstractSession::getEndTime() const {
75 return _endTime;
76}
77
78 long ffmpegkit::AbstractSession::getDuration() const {
79 const std::chrono::time_point<std::chrono::system_clock> startTime = _startTime;
80 const std::chrono::time_point<std::chrono::system_clock> endTime = _endTime;
81
82 if (startTime.time_since_epoch() != std::chrono::microseconds(0) && endTime.time_since_epoch() != std::chrono::microseconds(0)) {
83 return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
84 }
85
86 return 0;
87}
88
89std::shared_ptr<std::list<std::string>> ffmpegkit::AbstractSession::getArguments() const {
90 return _arguments;
91}
92
93std::string ffmpegkit::AbstractSession::getCommand() const {
94 return ffmpegkit::FFmpegKitConfig::argumentsToString(_arguments);
95}
96
97std::shared_ptr<std::list<std::shared_ptr<ffmpegkit::Log>>> ffmpegkit::AbstractSession::getAllLogsWithTimeout(const int waitTimeout) const {
98 this->waitForAsynchronousMessagesInTransmit(waitTimeout);
99
100 if (this->thereAreAsynchronousMessagesInTransmit()) {
101 std::cout << "getAllLogsWithTimeout was called to return all logs but there are still logs being transmitted for session id " << _sessionId << "." << std::endl;
102 }
103
104 return this->getLogs();
105}
106std::shared_ptr<std::list<std::shared_ptr<ffmpegkit::Log>>> ffmpegkit::AbstractSession::getAllLogs() const {
107 return this->getAllLogsWithTimeout(ffmpegkit::AbstractSession::DefaultTimeoutForAsynchronousMessagesInTransmit);
108}
109
110std::shared_ptr<std::list<std::shared_ptr<ffmpegkit::Log>>> ffmpegkit::AbstractSession::getLogs() const {
111 return _logs;
112}
113
114std::string ffmpegkit::AbstractSession::getAllLogsAsStringWithTimeout(const int waitTimeout) const {
115 this->waitForAsynchronousMessagesInTransmit(waitTimeout);
116
117 if (this->thereAreAsynchronousMessagesInTransmit()) {
118 std::cout << "getAllLogsAsStringWithTimeout was called to return all logs but there are still logs being transmitted for session id " << _sessionId << "." << std::endl;
119 }
120
121 return this->getLogsAsString();
122}
123
124std::string ffmpegkit::AbstractSession::getAllLogsAsString() const {
125 return this->getAllLogsAsStringWithTimeout(ffmpegkit::AbstractSession::DefaultTimeoutForAsynchronousMessagesInTransmit);
126}
127
128std::string ffmpegkit::AbstractSession::getLogsAsString() const {
129 std::string concatenatedString;
130
131 std::for_each(_logs->cbegin(), _logs->cend(), [&](std::shared_ptr<ffmpegkit::Log> log) {
132 concatenatedString.append(log->getMessage());
133 });
134
135 return concatenatedString;
136}
137
138std::string ffmpegkit::AbstractSession::getOutput() const {
139 return this->getAllLogsAsString();
140}
141
142 ffmpegkit::SessionState ffmpegkit::AbstractSession::getState() const {
143 return _state;
144}
145
146std::shared_ptr<ffmpegkit::ReturnCode> ffmpegkit::AbstractSession::getReturnCode() const {
147 return _returnCode;
148}
149
150std::string ffmpegkit::AbstractSession::getFailStackTrace() const {
151 return _failStackTrace;
152}
153
154 ffmpegkit::LogRedirectionStrategy ffmpegkit::AbstractSession::getLogRedirectionStrategy() const {
155 return _logRedirectionStrategy;
156}
157
158 bool ffmpegkit::AbstractSession::thereAreAsynchronousMessagesInTransmit() const {
159 return (FFmpegKitConfig::messagesInTransmit(_sessionId) != 0);
160}
161
162 void ffmpegkit::AbstractSession::addLog(const std::shared_ptr<ffmpegkit::Log> log) {
163 _logs->push_back(log);
164}
165
166 void ffmpegkit::AbstractSession::startRunning() {
167 _state = SessionStateRunning;
168 _startTime = std::chrono::system_clock::now();
169}
170
171 void ffmpegkit::AbstractSession::complete(const std::shared_ptr<ffmpegkit::ReturnCode> returnCode) {
172 _returnCode = returnCode;
173 _state = SessionStateCompleted;
174 _endTime = std::chrono::system_clock::now();
175}
176
177 void ffmpegkit::AbstractSession::fail(const char* error) {
178 _failStackTrace = error;
179 _state = SessionStateFailed;
180 _endTime = std::chrono::system_clock::now();
181}
182
183 bool ffmpegkit::AbstractSession::isFFmpeg() const {
184 // IMPLEMENTED IN SUBCLASSES
185 return false;
186}
187
188 bool ffmpegkit::AbstractSession::isFFprobe() const {
189 // IMPLEMENTED IN SUBCLASSES
190 return false;
191}
192
193 bool ffmpegkit::AbstractSession::isMediaInformation() const {
194 // IMPLEMENTED IN SUBCLASSES
195 return false;
196}
197
198 void ffmpegkit::AbstractSession::cancel() {
199 if (_state == SessionStateRunning) {
200 FFmpegKit::cancel(_sessionId);
201 }
202}
sessionIdGenerator
static std::atomic< long > sessionIdGenerator(1)
addSessionToSessionHistory
void addSessionToSessionHistory(const std::shared_ptr< ffmpegkit::Session > session)
Definition FFmpegKitConfig.cpp:165
AbstractSession.h
FFmpegKit.h
logCallback
static ffmpegkit::LogCallback logCallback
Definition FFmpegKitConfig.cpp:69
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:1352
ffmpegkit::FFmpegKitConfig::argumentsToString
static std::string argumentsToString(std::shared_ptr< std::list< std::string > > arguments)
Definition FFmpegKitConfig.cpp:1417
ffmpegkit::FFmpegKit::cancel
static void cancel()
Definition FFmpegKit.cpp:69
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