Ring Daemon 16.0.0
Loading...
Searching...
No Matches
video_input.h
Go to the documentation of this file.
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#pragma once
18
19#include "noncopyable.h"
20#include "threadloop.h"
21#include "media/media_stream.h"
22#include "media/media_device.h" // DeviceParams
24#include "media/media_codec.h"
25
26#include <map>
27#include <atomic>
28#include <future>
29#include <string>
30#include <mutex>
31#include <condition_variable>
32#include <array>
33
34#if __APPLE__
35#import "TargetConditionals.h"
36#endif
37
38namespace jami {
39class MediaDecoder;
40class MediaDemuxer;
41} // namespace jami
42
43namespace jami {
44namespace video {
45
46class SinkClient;
47
49
51{
52public:
54 const std::string& resource = "local",
55 const std::string& sink = "");
57
58 // as VideoGenerator
59 const std::string& getName() const { return resource_; }
60 int getWidth() const;
61 int getHeight() const;
63
64 const DeviceParams& getConfig() const {
65 return decOpts_;
66 }
67 std::shared_future<DeviceParams> getParams() const {
68 return futureDecOpts_;
69 }
70
71 MediaStream getInfo() const;
72
73 void setSink(const std::string& sinkId);
75 void configureFilePlayback(const std::string& path,
76 std::shared_ptr<MediaDemuxer>& demuxer,
77 int index);
78 void flushBuffers();
79 void setPaused(bool paused) { paused_ = paused; }
81 void setupSink(const int width, const int height);
82 void stopSink();
83
84 void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb);
85
86 void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb)
87 {
88 onSuccessfulSetup_ = cb;
89 }
90
96 void restart();
97
98private:
100
101 std::shared_future<DeviceParams> switchInput(const std::string& resource);
102
103 std::string resource_;
104 std::atomic<bool> switchPending_ = {false};
105 std::atomic_bool isStopped_ = {false};
106
107 DeviceParams decOpts_;
108 std::promise<DeviceParams> foundDecOpts_;
109 std::shared_future<DeviceParams> futureDecOpts_;
110 bool emulateRate_ = false;
111
112 std::atomic_bool decOptsFound_ {false};
113 void foundDecOpts(const DeviceParams& params);
114
115 void clearOptions();
116
117 // true if decOpts_ is ready to use, false if using promise/future
118 bool initCamera(const std::string& device);
119 bool initFile(std::string path);
120
121#ifdef __APPLE__
122 bool initAVFoundation(const std::string& display);
123#elif defined(WIN32)
124 bool initWindowsGrab(const std::string& display);
125#else
126 bool initLinuxGrab(const std::string& display);
127#endif
128
129 bool isCapturing() const noexcept;
130 void startLoop();
131
132 void switchDevice();
133 bool capturing_ {false};
134 void createDecoder();
135 void deleteDecoder();
136 std::unique_ptr<MediaDecoder> decoder_;
137 std::shared_ptr<SinkClient> sink_;
138 ThreadLoop loop_;
139
140 // for ThreadLoop
141 bool setup();
142 void process();
143 void cleanup();
144
145 bool captureFrame();
146
147 int rotation_ {0};
148 std::shared_ptr<AVBufferRef> displayMatrix_;
149 void setRotation(int angle);
150 VideoInputMode inputMode_;
151 inline bool videoManagedByClient() const
152 {
153 return inputMode_ == VideoInputMode::ManagedByClient;
154 }
155 bool playingFile_ = false;
156 std::atomic_bool paused_ {true};
157
158 std::function<void(MediaType, bool)> onSuccessfulSetup_;
159 std::function<void(const MediaStream& ms)> recorderCallback_;
160};
161
162} // namespace video
163} // namespace jami
void updateStartTime(int64_t startTime)
void setSeekTime(int64_t time)
std::shared_future< DeviceParams > getParams() const
Definition video_input.h:67
void setupSink(const int width, const int height)
void restart()
Restart stopped video input.
void configureFilePlayback(const std::string &path, std::shared_ptr< MediaDemuxer > &demuxer, int index)
void setRecorderCallback(const std::function< void(const MediaStream &ms)> &cb)
AVPixelFormat getPixelFormat() const
MediaStream getInfo() const
void setPaused(bool paused)
Definition video_input.h:79
const DeviceParams & getConfig() const
Definition video_input.h:64
const std::string & getName() const
Definition video_input.h:59
void setSink(const std::string &sinkId)
void setSuccessfulSetupCb(const std::function< void(MediaType, bool)> &cb)
Definition video_input.h:86
void emitSignal(Args... args)
Definition ring_signal.h:64
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
DeviceParams Parameters used by MediaDecoder and MediaEncoder to open a LibAV device/stream.