1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMPUTEPIPE_RUNNER_GRAPH_STREAM_SET_OBSERVER_H
16 #define COMPUTEPIPE_RUNNER_GRAPH_STREAM_SET_OBSERVER_H
17 
18 #include <condition_variable>
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <thread>
24 
25 #include "GrpcPrebuiltGraphService.grpc.pb.h"
26 #include "GrpcPrebuiltGraphService.pb.h"
27 #include "InputFrame.h"
28 #include "RunnerComponent.h"
29 #include "types/Status.h"
30 
31 namespace android {
32 namespace automotive {
33 namespace computepipe {
34 namespace graph {
35 
36 class GrpcGraph;
37 
38 class EndOfStreamReporter {
39   public:
40     virtual ~EndOfStreamReporter() = default;
41 
42     virtual void reportStreamClosed(int streamId) = 0;
43 };
44 
45 class StreamGraphInterface {
46   public:
47     virtual ~StreamGraphInterface() = default;
48 
49     virtual void dispatchPixelData(int streamId, int64_t timestamp_us,
50                                    const runner::InputFrame& frame) = 0;
51 
52     virtual void dispatchSerializedData(int streamId, int64_t timestamp_us,
53                                         std::string&& serialized_data) = 0;
54 
55     virtual void dispatchGraphTerminationMessage(Status, std::string&&) = 0;
56 
57     virtual proto::GrpcGraphService::Stub* getServiceStub() = 0;
58 };
59 
60 class SingleStreamObserver {
61   public:
62     SingleStreamObserver(int streamId, EndOfStreamReporter* endOfStreamReporter,
63                          StreamGraphInterface* streamGraphInterface);
64 
65     virtual ~SingleStreamObserver();
66 
67     Status startObservingStream();
68 
69     void stopObservingStream();
70   private:
71     int mStreamId;
72     EndOfStreamReporter* mEndOfStreamReporter;
73     StreamGraphInterface* mStreamGraphInterface;
74     std::thread mThread;
75     bool mStopped = true;
76     std::mutex mStopObservationLock;
77 };
78 
79 class StreamSetObserver : public EndOfStreamReporter {
80   public:
81     StreamSetObserver(const runner::ClientConfig& clientConfig,
82                       StreamGraphInterface* streamGraphInterface);
83 
84     Status startObservingStreams();
85 
86     void stopObservingStreams(bool stopImmediately);
87 
88     void reportStreamClosed(int streamId) override;
89   private:
90     const runner::ClientConfig& mClientConfig;
91     StreamGraphInterface* mStreamGraphInterface;
92     std::map<int, std::unique_ptr<SingleStreamObserver>> mStreamObservers;
93     std::mutex mLock;
94     std::condition_variable mStoppedCv;
95     bool mStopped = true;
96 };
97 
98 }  // namespace graph
99 }  // namespace computepipe
100 }  // namespace automotive
101 }  // namespace android
102 
103 #endif  // #define COMPUTEPIPE_RUNNER_GRAPH_STREAM_SET_OBSERVER_H
104