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