Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "fileutils.h"
19 : #include "logger.h"
20 : #include "manager.h"
21 : #include "recordable.h"
22 :
23 : namespace jami {
24 :
25 433 : Recordable::Recordable()
26 433 : : recorder_(std::make_shared<MediaRecorder>())
27 433 : {}
28 :
29 433 : Recordable::~Recordable() {}
30 :
31 : std::string
32 14 : Recordable::getPath() const
33 : {
34 14 : if (recorder_)
35 14 : return recorder_->getPath();
36 : else
37 0 : return "";
38 : }
39 :
40 : bool
41 17 : Recordable::toggleRecording()
42 : {
43 17 : if (!recorder_) {
44 0 : JAMI_ERROR("Unable to toggle recording, non existent recorder");
45 0 : return false;
46 : }
47 :
48 17 : if (!recording_) {
49 10 : const auto& audioPath = Manager::instance().audioPreference.getRecordPath();
50 10 : auto dir = audioPath.empty() ? fileutils::get_home_dir() : std::filesystem::path(audioPath);
51 10 : dhtnet::fileutils::check_dir(dir);
52 10 : auto timeStamp = fmt::format("{:%Y%m%d-%H%M%S}", std::chrono::system_clock::now());
53 10 : startRecording((dir / timeStamp).string());
54 10 : } else {
55 7 : stopRecording();
56 : }
57 17 : return recording_;
58 : }
59 :
60 : bool
61 10 : Recordable::startRecording(const std::string& path)
62 : {
63 10 : std::lock_guard lk {apiMutex_};
64 10 : if (!recorder_) {
65 0 : JAMI_ERROR("Unable to start recording, non existent recorder");
66 0 : return false;
67 : }
68 :
69 10 : if (!recording_) {
70 10 : if (path.empty()) {
71 0 : JAMI_ERROR("Unable to start recording, path is empty");
72 0 : return false;
73 : }
74 :
75 10 : recorder_->audioOnly(isAudioOnly_);
76 10 : recorder_->setPath(path);
77 10 : recorder_->startRecording();
78 10 : recording_ = recorder_->isRecording();
79 : }
80 :
81 10 : return recording_;
82 10 : }
83 :
84 : void
85 10 : Recordable::stopRecording()
86 : {
87 10 : std::lock_guard lk {apiMutex_};
88 10 : if (!recorder_) {
89 0 : JAMI_WARNING("Unable to stop recording, non existent recorder");
90 0 : return;
91 : }
92 :
93 10 : if (not recording_) {
94 0 : JAMI_WARNING("Unable to stop non-running recording");
95 0 : return;
96 : }
97 :
98 10 : recorder_->stopRecording();
99 10 : recording_ = false;
100 10 : }
101 :
102 : bool
103 0 : Recordable::isAudioOnly() const
104 : {
105 0 : return isAudioOnly_;
106 : }
107 :
108 : } // namespace jami
|