Line data Source code
1 : /*
2 : * Copyright (C) 2004-2025 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 : #ifndef DENABLE_VIDEOMANAGERI_H
19 : #define DENABLE_VIDEOMANAGERI_H
20 :
21 : #include "jami.h"
22 : #ifdef HAVE_CONFIG_H
23 : #include "config.h"
24 : #endif // HAVE_CONFIG_H
25 :
26 : extern "C" {
27 : struct AVFrame;
28 : struct AVPacket;
29 : void av_frame_free(AVFrame** frame);
30 : void av_packet_free(AVPacket** frame);
31 : }
32 :
33 : #include "def.h"
34 :
35 : #include <memory>
36 : #include <vector>
37 : #include <map>
38 : #include <string>
39 : #include <functional>
40 : #include <chrono>
41 : #include <cstdint>
42 : #include <cstdlib>
43 :
44 : #ifdef __APPLE__
45 : #import "TargetConditionals.h"
46 : #endif
47 :
48 : namespace jami {
49 : struct AudioFormat;
50 : }
51 :
52 : namespace libjami {
53 :
54 : [[deprecated("Replaced by registerSignalHandlers")]] LIBJAMI_PUBLIC void registerVideoHandlers(
55 : const std::map<std::string, std::shared_ptr<CallbackWrapperBase>>&);
56 :
57 : struct LIBJAMI_PUBLIC AVFrame_deleter
58 : {
59 35279 : void operator()(AVFrame* frame) const { av_frame_free(&frame); }
60 : };
61 :
62 : typedef std::unique_ptr<AVFrame, AVFrame_deleter> FrameBuffer;
63 :
64 : struct LIBJAMI_PUBLIC AVPacket_deleter
65 : {
66 5861 : void operator()(AVPacket* pkt) const { av_packet_free(&pkt); }
67 : };
68 :
69 : typedef std::unique_ptr<AVPacket, AVPacket_deleter> PacketBuffer;
70 :
71 : class LIBJAMI_PUBLIC MediaFrame
72 : {
73 : public:
74 : // Construct an empty MediaFrame
75 : MediaFrame();
76 : MediaFrame(const MediaFrame&) = delete;
77 : MediaFrame& operator=(const MediaFrame& o) = delete;
78 : MediaFrame(MediaFrame&& o) = delete;
79 : MediaFrame& operator=(MediaFrame&& o) = delete;
80 :
81 34331 : virtual ~MediaFrame() = default;
82 :
83 : // Return a pointer on underlaying buffer
84 26908 : const AVFrame* pointer() const noexcept { return frame_.get(); }
85 64272 : AVFrame* pointer() noexcept { return frame_.get(); }
86 21152 : AVPacket* packet() const noexcept { return packet_.get(); }
87 :
88 : // Fill this MediaFrame with data from o
89 : void copyFrom(const MediaFrame& o);
90 : void setPacket(PacketBuffer&& pkt);
91 :
92 : // Reset internal buffers (return to an empty MediaFrame)
93 : virtual void reset() noexcept;
94 :
95 0 : FrameBuffer getFrame() { return std::move(frame_); }
96 :
97 : protected:
98 : FrameBuffer frame_;
99 : PacketBuffer packet_;
100 : };
101 :
102 : class LIBJAMI_PUBLIC AudioFrame : public MediaFrame
103 : {
104 : public:
105 153 : AudioFrame()
106 153 : : MediaFrame()
107 153 : {}
108 : AudioFrame(const jami::AudioFormat& format, size_t nb_samples = 0);
109 10201 : ~AudioFrame() {};
110 : void mix(const AudioFrame& o);
111 : float calcRMS() const;
112 : jami::AudioFormat getFormat() const;
113 : size_t getFrameSize() const;
114 : bool has_voice {false};
115 :
116 : private:
117 : void setFormat(const jami::AudioFormat& format);
118 : void reserve(size_t nb_samples = 0);
119 : };
120 :
121 : class LIBJAMI_PUBLIC VideoFrame : public MediaFrame
122 : {
123 : public:
124 : // Construct an empty VideoFrame
125 24237 : VideoFrame()
126 24237 : : MediaFrame()
127 24223 : {}
128 : ~VideoFrame();
129 :
130 : // Reset internal buffers (return to an empty VideoFrame)
131 : void reset() noexcept override;
132 :
133 : // Fill this VideoFrame with data from o
134 : void copyFrom(const VideoFrame& o);
135 :
136 : // Return frame size in bytes
137 : std::size_t size() const noexcept;
138 :
139 : // Return pixel format
140 : int format() const noexcept;
141 :
142 : // Return frame width in pixels
143 : int width() const noexcept;
144 :
145 : // Return frame height in pixels
146 : int height() const noexcept;
147 :
148 : // Allocate internal pixel buffers following given specifications
149 : void reserve(int format, int width, int height);
150 :
151 : // Return orientation (in degrees) stored in the frame metadata, or 0 by default.
152 : int getOrientation() const;
153 :
154 : // Set internal pixel buffers on given memory buffer
155 : // This buffer must follow given specifications.
156 : void setFromMemory(uint8_t* data, int format, int width, int height) noexcept;
157 : void setFromMemory(
158 : uint8_t* data, int format, int width, int height, const std::function<void(uint8_t*)>& cb) noexcept;
159 : void setReleaseCb(const std::function<void(uint8_t*)>& cb) noexcept;
160 :
161 : void noise();
162 :
163 : private:
164 : std::function<void(uint8_t*)> releaseBufferCb_ {};
165 : uint8_t* ptr_ {nullptr};
166 : bool allocated_ {false};
167 : void setGeometry(int format, int width, int height) noexcept;
168 : };
169 :
170 : struct LIBJAMI_PUBLIC SinkTarget
171 : {
172 : std::function<FrameBuffer()> pull;
173 : std::function<void(FrameBuffer)> push;
174 : int /* AVPixelFormat */ preferredFormat {-1 /* AV_PIX_FMT_NONE */};
175 : };
176 :
177 : using VideoCapabilities = std::map<std::string, std::map<std::string, std::vector<std::string>>>;
178 :
179 : LIBJAMI_PUBLIC std::vector<std::string> getDeviceList();
180 : LIBJAMI_PUBLIC VideoCapabilities getCapabilities(const std::string& deviceId);
181 : LIBJAMI_PUBLIC std::map<std::string, std::string> getSettings(const std::string& deviceId);
182 : LIBJAMI_PUBLIC void applySettings(const std::string& deviceId, const std::map<std::string, std::string>& settings);
183 : LIBJAMI_PUBLIC void setDefaultDevice(const std::string& deviceId);
184 : LIBJAMI_PUBLIC void setDeviceOrientation(const std::string& deviceId, int angle);
185 : LIBJAMI_PUBLIC std::map<std::string, std::string> getDeviceParams(const std::string& deviceId);
186 : LIBJAMI_PUBLIC std::string getDefaultDevice();
187 : LIBJAMI_PUBLIC void startAudioDevice();
188 : LIBJAMI_PUBLIC void stopAudioDevice();
189 :
190 : LIBJAMI_PUBLIC std::string openVideoInput(const std::string& path);
191 : LIBJAMI_PUBLIC bool closeVideoInput(const std::string& id);
192 :
193 : LIBJAMI_PUBLIC std::string createMediaPlayer(const std::string& path);
194 : LIBJAMI_PUBLIC bool closeMediaPlayer(const std::string& id);
195 : LIBJAMI_PUBLIC bool pausePlayer(const std::string& id, const bool& pause);
196 : LIBJAMI_PUBLIC bool mutePlayerAudio(const std::string& id, const bool& mute);
197 : LIBJAMI_PUBLIC bool playerSeekToTime(const std::string& id, const int& time);
198 : LIBJAMI_PUBLIC int64_t getPlayerPosition(const std::string& id);
199 : LIBJAMI_PUBLIC int64_t getPlayerDuration(const std::string& id);
200 : LIBJAMI_PUBLIC void setAutoRestart(const std::string& id, const bool& restart);
201 :
202 : LIBJAMI_PUBLIC bool registerSinkTarget(const std::string& sinkId, SinkTarget target);
203 : #ifdef ENABLE_SHM
204 : LIBJAMI_PUBLIC void startShmSink(const std::string& sinkId, bool value);
205 : #endif
206 : LIBJAMI_PUBLIC std::map<std::string, std::string> getRenderer(const std::string& callId);
207 :
208 : LIBJAMI_PUBLIC std::string startLocalMediaRecorder(const std::string& videoInputId, const std::string& filepath);
209 : LIBJAMI_PUBLIC void stopLocalRecorder(const std::string& filepath);
210 :
211 : #if defined(__ANDROID__) || (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
212 : LIBJAMI_PUBLIC void addVideoDevice(const std::string& node,
213 : const std::vector<std::map<std::string, std::string>>& devInfo = {});
214 : LIBJAMI_PUBLIC void removeVideoDevice(const std::string& node);
215 : LIBJAMI_PUBLIC VideoFrame* getNewFrame(std::string_view id);
216 : LIBJAMI_PUBLIC void publishFrame(std::string_view id);
217 : #endif
218 :
219 : LIBJAMI_PUBLIC bool getDecodingAccelerated();
220 : LIBJAMI_PUBLIC void setDecodingAccelerated(bool state);
221 : LIBJAMI_PUBLIC bool getEncodingAccelerated();
222 : LIBJAMI_PUBLIC void setEncodingAccelerated(bool state);
223 :
224 : // player signal type definitions
225 : struct LIBJAMI_PUBLIC MediaPlayerSignal
226 : {
227 : struct LIBJAMI_PUBLIC FileOpened
228 : {
229 : constexpr static const char* name = "FileOpened";
230 : using cb_type = void(const std::string& /*playerId*/, std::map<std::string, std::string> /*playerInfo*/);
231 : };
232 : };
233 :
234 : // Video signal type definitions
235 : struct LIBJAMI_PUBLIC VideoSignal
236 : {
237 : struct LIBJAMI_PUBLIC DeviceEvent
238 : {
239 : constexpr static const char* name = "DeviceEvent";
240 : using cb_type = void(void);
241 : };
242 : struct LIBJAMI_PUBLIC DecodingStarted
243 : {
244 : constexpr static const char* name = "DecodingStarted";
245 : using cb_type = void(
246 : const std::string& /*id*/, const std::string& /*shm_path*/, int /*w*/, int /*h*/, bool /*is_mixer*/ id);
247 : };
248 : struct LIBJAMI_PUBLIC DecodingStopped
249 : {
250 : constexpr static const char* name = "DecodingStopped";
251 : using cb_type = void(const std::string& /*id*/, const std::string& /*shm_path*/, bool /*is_mixer*/);
252 : };
253 : #ifdef __ANDROID__
254 : struct LIBJAMI_PUBLIC SetParameters
255 : {
256 : constexpr static const char* name = "SetParameters";
257 : using cb_type = void(
258 : const std::string& device, const int format, const int width, const int height, const int rate);
259 : };
260 : struct LIBJAMI_PUBLIC GetCameraInfo
261 : {
262 : constexpr static const char* name = "GetCameraInfo";
263 : using cb_type = void(const std::string& device,
264 : std::vector<int>* formats,
265 : std::vector<unsigned>* sizes,
266 : std::vector<unsigned>* rates);
267 : };
268 : struct LIBJAMI_PUBLIC RequestKeyFrame
269 : {
270 : constexpr static const char* name = "RequestKeyFrame";
271 : using cb_type = void(const std::string& /*device*/);
272 : };
273 : struct LIBJAMI_PUBLIC SetBitrate
274 : {
275 : constexpr static const char* name = "SetBitrate";
276 : using cb_type = void(const std::string& /*device*/, const int bitrate);
277 : };
278 : #endif
279 : struct LIBJAMI_PUBLIC StartCapture
280 : {
281 : constexpr static const char* name = "StartCapture";
282 : using cb_type = void(const std::string& /*device*/);
283 : };
284 : struct LIBJAMI_PUBLIC StopCapture
285 : {
286 : constexpr static const char* name = "StopCapture";
287 : using cb_type = void(const std::string& /*device*/);
288 : };
289 : struct LIBJAMI_PUBLIC DeviceAdded
290 : {
291 : constexpr static const char* name = "DeviceAdded";
292 : using cb_type = void(const std::string& /*device*/);
293 : };
294 : struct LIBJAMI_PUBLIC ParametersChanged
295 : {
296 : constexpr static const char* name = "ParametersChanged";
297 : using cb_type = void(const std::string& /*device*/);
298 : };
299 : };
300 :
301 : } // namespace libjami
302 :
303 : #endif // DENABLE_VIDEOMANAGERI_H
|