-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebcamCapture.h
More file actions
54 lines (43 loc) · 1.24 KB
/
Copy pathWebcamCapture.h
File metadata and controls
54 lines (43 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <atomic>
#include <cstdint>
#include <functional>
#include <string>
#include <thread>
#include <vector>
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
class WebcamCapture
{
public:
using FrameCallback = std::function<void(std::vector<uint8_t> jpegFrame)>;
WebcamCapture();
~WebcamCapture();
WebcamCapture(const WebcamCapture&) = delete;
WebcamCapture& operator=(const WebcamCapture&) = delete;
bool init(const std::string& device, int width, int height, int fps, int quality, bool reencode);
void start();
void stop();
void setFrameCallback(FrameCallback cb) { frameCallback_ = std::move(cb); }
int width() const { return width_; }
int height() const { return height_; }
private:
void readLoop();
void cleanup();
bool encodeFrame(AVFrame* decoded);
AVFormatContext* fmtCtx_ = nullptr;
AVCodecContext* decCtx_ = nullptr;
AVCodecContext* encCtx_ = nullptr;
SwsContext* swsCtx_ = nullptr;
int videoStreamIndex_ = -1;
bool reencode_ = false;
std::thread readThread_;
std::atomic<bool> running_{false};
FrameCallback frameCallback_;
int width_ = 0;
int height_ = 0;
};