Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1-only */
2 : /**
3 : * GStreamer / NNStreamer gRPC/protobuf support
4 : * Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
5 : */
6 : /**
7 : * @file nnstreamer_grpc_protobuf.h
8 : * @date 21 Oct 2020
9 : * @brief gRPC/Protobuf wrappers for nnstreamer
10 : * @see https://github.com/nnstreamer/nnstreamer
11 : * @author Dongju Chae <dongju.chae@samsung.com>
12 : * @bug No known bugs except for NYI items
13 : */
14 :
15 : #ifndef __NNS_GRPC_PROTOBUF_H__
16 : #define __NNS_GRPC_PROTOBUF_H__
17 :
18 : #include "nnstreamer_grpc_common.h"
19 : #include "nnstreamer.grpc.pb.h" /* Generated by `protoc` */
20 :
21 : using nnstreamer::protobuf::TensorService;
22 : using nnstreamer::protobuf::Tensors;
23 : using nnstreamer::protobuf::Tensor;
24 :
25 : using google::protobuf::Empty;
26 :
27 : namespace grpc {
28 :
29 : /**
30 : * @brief NNStreamer gRPC protobuf service impl.
31 : */
32 : class ServiceImplProtobuf : public NNStreamerRPC
33 : {
34 : public:
35 : ServiceImplProtobuf (const grpc_config * config);
36 :
37 : void parse_tensors (Tensors &tensors);
38 : gboolean fill_tensors (Tensors &tensors);
39 :
40 : protected:
41 : template <typename T>
42 : grpc::Status _write_tensors (T writer);
43 :
44 : template <typename T>
45 : grpc::Status _read_tensors (T reader);
46 :
47 : void _get_tensors_from_buffer (GstBuffer *buffer, Tensors &tensors);
48 : void _get_buffer_from_tensors (Tensors &tensors, GstBuffer **buffer);
49 :
50 : std::unique_ptr<nnstreamer::protobuf::TensorService::Stub> client_stub_;
51 : };
52 :
53 : /**
54 : * @brief NNStreamer gRPC protobuf sync service impl.
55 : */
56 : class SyncServiceImplProtobuf final
57 : : public ServiceImplProtobuf, public TensorService::Service
58 : {
59 : public:
60 : SyncServiceImplProtobuf (const grpc_config * config);
61 :
62 : Status SendTensors (ServerContext *context, ServerReader<Tensors> *reader,
63 : Empty *reply) override;
64 :
65 : Status RecvTensors (ServerContext *context, const Empty *request,
66 : ServerWriter<Tensors> *writer) override;
67 :
68 : private:
69 : gboolean start_server (std::string address) override;
70 : gboolean start_client (std::string address) override;
71 :
72 : void _client_thread ();
73 : };
74 :
75 : class AsyncCallData;
76 :
77 : /**
78 : * @brief NNStreamer gRPC protobuf async service impl.
79 : */
80 : class AsyncServiceImplProtobuf final
81 : : public ServiceImplProtobuf, public TensorService::AsyncService
82 : {
83 : public:
84 : AsyncServiceImplProtobuf (const grpc_config * config);
85 : ~AsyncServiceImplProtobuf ();
86 :
87 : /** @brief set the last call data */
88 4 : void set_last_call (AsyncCallData * call) { last_call_ = call; }
89 :
90 : private:
91 : gboolean start_server (std::string address) override;
92 : gboolean start_client (std::string address) override;
93 :
94 : void _server_thread ();
95 : void _client_thread ();
96 :
97 : AsyncCallData * last_call_;
98 : };
99 :
100 : /** @brief Internal base class to serve a request */
101 : class AsyncCallData {
102 : public:
103 : /** @brief Constructor of AsyncCallData */
104 8 : AsyncCallData (AsyncServiceImplProtobuf *service)
105 8 : : service_ (service), state_ (CREATE), count_ (0)
106 : {
107 8 : }
108 :
109 : /** @brief Destructor of AsyncCallData */
110 2 : virtual ~AsyncCallData () {}
111 :
112 : /** @brief FSM-based state handling function */
113 0 : virtual void RunState (bool ok) {}
114 :
115 : protected:
116 : enum CallState { CREATE, PROCESS, FINISH, DESTROY };
117 :
118 : AsyncServiceImplProtobuf *service_;
119 : CallState state_;
120 : guint count_;
121 :
122 : Tensors rpc_tensors_;
123 : Empty rpc_empty_;
124 : };
125 :
126 : }; /* namespace grpc */
127 :
128 : #endif /* __NNS_GRPC_PROTOBUF_H__ */
|