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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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_STREAM_H_
#define EASYMEDIA_STREAM_H_
 
#include <stddef.h>
#include <stdint.h>
 
#ifdef __cplusplus
extern "C" {
 
typedef struct {
    int (*close)(void* stream);
    size_t (*read)(void* ptr, size_t size, size_t nmemb, void* stream);
    size_t (*write)(const void* ptr, size_t size, size_t nmemb, void* stream);
    int (*seek)(void* stream, int64_t offset, int whence);
    long (*tell)(void* stream);
} StreamOperation;
}
#endif
 
#include "control.h"
#include "image.h"
#include "media_reflector.h"
#include "utils.h"
 
namespace easymedia
{
 
    DECLARE_FACTORY(Stream)
 
    // usage: REFLECTOR(Stream)::Create<T>(streamname, param)
    //        T must be the final class type exposed to user
    DECLARE_REFLECTOR(Stream)
 
#define DEFINE_STREAM_FACTORY(REAL_PRODUCT, FINAL_EXPOSE_PRODUCT)                                                      \
    DEFINE_MEDIA_CHILD_FACTORY(REAL_PRODUCT, REAL_PRODUCT::GetStreamName(), FINAL_EXPOSE_PRODUCT, Stream)              \
    DEFINE_MEDIA_CHILD_FACTORY_EXTRA(REAL_PRODUCT)                                                                     \
    DEFINE_MEDIA_NEW_PRODUCT_BY(REAL_PRODUCT, FINAL_EXPOSE_PRODUCT, Open() < 0)
 
    class MediaBuffer;
#ifdef IN_EASYMEDIA_STREAM_CC
    static int local_close(void* stream);
#endif
    // interface
    class _API Stream
    {
      public:
        static StreamOperation c_operations;
 
        Stream() : readable(false), writeable(false), seekable(false)
        {
        }
        virtual ~Stream() = default;
 
        virtual size_t Read(void* ptr, size_t size, size_t nmemb) = 0;
        virtual size_t Write(const void* ptr, size_t size, size_t nmemb) = 0;
        // whence: SEEK_SET, SEEK_CUR, SEEK_END
        virtual int Seek(int64_t offset, int whence) = 0;
        virtual long Tell() = 0;
 
        virtual int NewStream(std::string new_path _UNUSED)
        {
            return -1;
        };
        virtual size_t WriteAndClose(const void* ptr _UNUSED, size_t size _UNUSED, size_t nmemb _UNUSED)
        {
            return -1;
        };
        virtual int ReName(std::string old_path _UNUSED, std::string new_path _UNUSED)
        {
            return -1;
        };
 
        virtual bool Readable()
        {
            return readable;
        }
        virtual bool Writeable()
        {
            return writeable;
        }
        virtual bool Seekable()
        {
            return seekable;
        }
 
        void SetReadable(bool able)
        {
            readable = able;
        }
        void SetWriteable(bool able)
        {
            writeable = able;
        }
        void SetSeekable(bool able)
        {
            seekable = able;
        }
 
        virtual bool Eof()
        {
            return false;
        }
 
        // No need size input. For some device, such as V4L2, always return specific
        // buffer
        virtual std::shared_ptr<MediaBuffer> Read()
        {
            return nullptr;
        }
        virtual bool Write(std::shared_ptr<MediaBuffer>)
        {
            return false;
        }
        // The IoCtrl must be called in the same thread of Read()/Write()
        virtual int IoCtrl(unsigned long int request _UNUSED, ...)
        {
            return -1;
        }
        virtual int SubIoCtrl(unsigned long int request _UNUSED, void* arg, int size = 0)
        {
            SubRequest subreq = {request, size, arg};
            return IoCtrl(S_SUB_REQUEST, &subreq);
        }
 
        // read data as image by ImageInfo
        bool ReadImage(void* ptr, const ImageInfo& info);
 
      protected:
        virtual int Open() = 0;
        virtual int Close() = 0;
 
        friend int local_close(void* stream);
 
      private:
        bool readable;
        bool writeable;
        bool seekable;
 
        DECLARE_PART_FINAL_EXPOSE_PRODUCT(Stream)
    };
 
} // namespace easymedia
 
#endif // #ifndef EASYMEDIA_STREAM_H_