hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#ifndef EASYMEDIA_V4L2_STREAM_H_
#define EASYMEDIA_V4L2_STREAM_H_
 
#include <assert.h>
 
#include <mutex>
 
#include "stream.h"
#include "v4l2_utils.h"
 
#define RKISP_SUBDEV_NAME "rkisp-isp-subdev"
#define RKIISPP_SUBDEV_NAME "rkispp-subdev"
 
namespace easymedia
{
 
    class V4L2Context
    {
      public:
        V4L2Context(enum v4l2_buf_type cap_type, v4l2_io io_func, const std::string device);
        ~V4L2Context();
        int GetDeviceFd()
        {
            return fd;
        }
        // val: if true, VIDIOC_STREAMON, else VIDIOC_STREAMOFF
        bool SetStarted(bool val);
        int IoCtrl(unsigned long int request, void* arg);
 
      private:
        int fd;
        enum v4l2_buf_type capture_type;
        v4l2_io vio;
        std::mutex mtx;
        volatile bool started;
        std::string path;
    };
 
    class V4L2MediaCtl
    {
      public:
        V4L2MediaCtl();
        ~V4L2MediaCtl();
        int InitHwInfos();
        int SetupLink(std::string devname, bool enable);
    };
 
    class V4L2Stream : public Stream
    {
      public:
        V4L2Stream(const char* param);
        virtual ~V4L2Stream()
        {
            V4L2Stream::Close();
        }
        virtual size_t Read(void* ptr _UNUSED, size_t size _UNUSED, size_t nmemb _UNUSED) final
        {
            return 0;
        }
        virtual int Seek(int64_t offset _UNUSED, int whence _UNUSED) final
        {
            return -1;
        }
        virtual long Tell() final
        {
            return -1;
        }
        virtual size_t Write(const void* ptr _UNUSED, size_t size _UNUSED, size_t nmemb _UNUSED) final
        {
            return 0;
        }
        virtual int IoCtrl(unsigned long int request, ...) override;
 
#define v4l2_open vio.open_f
#define v4l2_close vio.close_f
#define v4l2_dup vio.dup_f
#define v4l2_ioctl vio.ioctl_f
#define v4l2_read vio.read_f
#define v4l2_mmap vio.mmap_f
#define v4l2_munmap vio.munmap_f
 
      protected:
        virtual int Open() override;
        virtual int Close() override;
 
        bool use_libv4l2;
        v4l2_io vio;
        std::string devname;
        std::string device;
        std::string sub_device;
        int fd; // just for convenience
        // static sub_device_controller;
        enum v4l2_buf_type capture_type;
        std::shared_ptr<V4L2Context> v4l2_ctx;
        std::shared_ptr<V4L2MediaCtl> v4l2_medctl;
    };
 
} // namespace easymedia
 
#endif // #ifndef EASYMEDIA_V4L2_STREAM_H_