Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 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 33114 : 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 5371 : 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 32132 : virtual ~MediaFrame() = default;
82 :
83 : // Return a pointer on underlaying buffer
84 25145 : const AVFrame* pointer() const noexcept { return frame_.get(); }
85 60017 : AVFrame* pointer() noexcept { return frame_.get(); }
86 19844 : 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 152 : AudioFrame()
106 152 : : MediaFrame()
107 152 : {}
108 : AudioFrame(const jami::AudioFormat& format, size_t nb_samples = 0);
109 9415 : ~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 22817 : VideoFrame()
126 22817 : : MediaFrame()
127 22808 : {}
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(uint8_t* data,
158 : int format,
159 : int width,
160 : int height,
161 : const std::function<void(uint8_t*)>& cb) noexcept;
162 : void setReleaseCb(const std::function<void(uint8_t*)>& cb) noexcept;
163 :
164 : void noise();
165 :
166 : private:
167 : std::function<void(uint8_t*)> releaseBufferCb_ {};
168 : uint8_t* ptr_ {nullptr};
169 : bool allocated_ {false};
170 : void setGeometry(int format, int width, int height) noexcept;
171 : };
172 :
173 : struct LIBJAMI_PUBLIC SinkTarget
174 : {
175 : std::function<FrameBuffer()> pull;
176 : std::function<void(FrameBuffer)> push;
177 : int /* AVPixelFormat */ preferredFormat {-1 /* AV_PIX_FMT_NONE */};
178 : };
179 :
180 : using VideoCapabilities = std::map<std::string, std::map<std::string, std::vector<std::string>>>;
181 :
182 : LIBJAMI_PUBLIC std::vector<std::string> getDeviceList();
183 : LIBJAMI_PUBLIC VideoCapabilities getCapabilities(const std::string& deviceId);
184 : LIBJAMI_PUBLIC std::map<std::string, std::string> getSettings(const std::string& deviceId);
185 : LIBJAMI_PUBLIC void applySettings(const std::string& deviceId,
186 : const std::map<std::string, std::string>& settings);
187 : LIBJAMI_PUBLIC void setDefaultDevice(const std::string& deviceId);
188 : LIBJAMI_PUBLIC void setDeviceOrientation(const std::string& deviceId, int angle);
189 : LIBJAMI_PUBLIC std::map<std::string, std::string> getDeviceParams(const std::string& deviceId);
190 : LIBJAMI_PUBLIC std::string getDefaultDevice();
191 : LIBJAMI_PUBLIC void startAudioDevice();
192 : LIBJAMI_PUBLIC void stopAudioDevice();
193 :
194 : LIBJAMI_PUBLIC std::string openVideoInput(const std::string& path);
195 : LIBJAMI_PUBLIC bool closeVideoInput(const std::string& id);
196 :
197 : LIBJAMI_PUBLIC std::string createMediaPlayer(const std::string& path);
198 : LIBJAMI_PUBLIC bool closeMediaPlayer(const std::string& id);
199 : LIBJAMI_PUBLIC bool pausePlayer(const std::string& id, const bool& pause);
200 : LIBJAMI_PUBLIC bool mutePlayerAudio(const std::string& id, const bool& mute);
201 : LIBJAMI_PUBLIC bool playerSeekToTime(const std::string& id, const int& time);
202 : LIBJAMI_PUBLIC int64_t getPlayerPosition(const std::string& id);
203 : LIBJAMI_PUBLIC int64_t getPlayerDuration(const std::string& id);
204 : LIBJAMI_PUBLIC void setAutoRestart(const std::string& id, const bool& restart);
205 :
206 : LIBJAMI_PUBLIC bool registerSinkTarget(const std::string& sinkId, SinkTarget target);
207 : #ifdef ENABLE_SHM
208 : LIBJAMI_PUBLIC void startShmSink(const std::string& sinkId, bool value);
209 : #endif
210 : LIBJAMI_PUBLIC std::map<std::string, std::string> getRenderer(const std::string& callId);
211 :
212 : LIBJAMI_PUBLIC std::string startLocalMediaRecorder(const std::string& videoInputId,
213 : const std::string& filepath);
214 : LIBJAMI_PUBLIC void stopLocalRecorder(const std::string& filepath);
215 :
216 : #if defined(__ANDROID__) || (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
217 : LIBJAMI_PUBLIC void addVideoDevice(
218 : const std::string& node, const std::vector<std::map<std::string, std::string>>& devInfo = {});
219 : LIBJAMI_PUBLIC void removeVideoDevice(const std::string& node);
220 : LIBJAMI_PUBLIC VideoFrame* getNewFrame(std::string_view id);
221 : LIBJAMI_PUBLIC void publishFrame(std::string_view id);
222 : #endif
223 :
224 : LIBJAMI_PUBLIC bool getDecodingAccelerated();
225 : LIBJAMI_PUBLIC void setDecodingAccelerated(bool state);
226 : LIBJAMI_PUBLIC bool getEncodingAccelerated();
227 : LIBJAMI_PUBLIC void setEncodingAccelerated(bool state);
228 :
229 : // player signal type definitions
230 : struct LIBJAMI_PUBLIC MediaPlayerSignal
231 : {
232 : struct LIBJAMI_PUBLIC FileOpened
233 : {
234 : constexpr static const char* name = "FileOpened";
235 : using cb_type = void(const std::string& /*playerId*/,
236 : std::map<std::string, std::string> /*playerInfo*/);
237 : };
238 : };
239 :
240 : // Video signal type definitions
241 : struct LIBJAMI_PUBLIC VideoSignal
242 : {
243 : struct LIBJAMI_PUBLIC DeviceEvent
244 : {
245 : constexpr static const char* name = "DeviceEvent";
246 : using cb_type = void(void);
247 : };
248 : struct LIBJAMI_PUBLIC DecodingStarted
249 : {
250 : constexpr static const char* name = "DecodingStarted";
251 : using cb_type = void(const std::string& /*id*/,
252 : const std::string& /*shm_path*/,
253 : int /*w*/,
254 : int /*h*/,
255 : bool /*is_mixer*/ id);
256 : };
257 : struct LIBJAMI_PUBLIC DecodingStopped
258 : {
259 : constexpr static const char* name = "DecodingStopped";
260 : using cb_type = void(const std::string& /*id*/,
261 : const std::string& /*shm_path*/,
262 : bool /*is_mixer*/);
263 : };
264 : #ifdef __ANDROID__
265 : struct LIBJAMI_PUBLIC SetParameters
266 : {
267 : constexpr static const char* name = "SetParameters";
268 : using cb_type = void(const std::string& device,
269 : const int format,
270 : const int width,
271 : const int height,
272 : const int rate);
273 : };
274 : struct LIBJAMI_PUBLIC GetCameraInfo
275 : {
276 : constexpr static const char* name = "GetCameraInfo";
277 : using cb_type = void(const std::string& device,
278 : std::vector<int>* formats,
279 : std::vector<unsigned>* sizes,
280 : std::vector<unsigned>* rates);
281 : };
282 : struct LIBJAMI_PUBLIC RequestKeyFrame
283 : {
284 : constexpr static const char* name = "RequestKeyFrame";
285 : using cb_type = void(const std::string& /*device*/);
286 : };
287 : struct LIBJAMI_PUBLIC SetBitrate
288 : {
289 : constexpr static const char* name = "SetBitrate";
290 : using cb_type = void(const std::string& /*device*/, const int bitrate);
291 : };
292 : #endif
293 : struct LIBJAMI_PUBLIC StartCapture
294 : {
295 : constexpr static const char* name = "StartCapture";
296 : using cb_type = void(const std::string& /*device*/);
297 : };
298 : struct LIBJAMI_PUBLIC StopCapture
299 : {
300 : constexpr static const char* name = "StopCapture";
301 : using cb_type = void(const std::string& /*device*/);
302 : };
303 : struct LIBJAMI_PUBLIC DeviceAdded
304 : {
305 : constexpr static const char* name = "DeviceAdded";
306 : using cb_type = void(const std::string& /*device*/);
307 : };
308 : struct LIBJAMI_PUBLIC ParametersChanged
309 : {
310 : constexpr static const char* name = "ParametersChanged";
311 : using cb_type = void(const std::string& /*device*/);
312 : };
313 : };
314 :
315 : } // namespace libjami
316 :
317 : #endif // DENABLE_VIDEOMANAGERI_H
|