1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef C2_COMPONENT_WRAPPER_H_
18 #define C2_COMPONENT_WRAPPER_H_
19 
20 #include <C2Component.h>
21 
22 #include "SimpleMethodState.h"
23 
24 namespace android {
25 
26 /**
27  * Creates a Wrapper around the class C2Component and its methods. The wrapper is used to
28  * simulate errors in the android media components by fault injection technique.
29  * This is done to check how the framework handles the error situation.
30  */
31 class C2ComponentWrapper
32         : public C2Component, public std::enable_shared_from_this<C2ComponentWrapper> {
33 public:
34     class Injecter {
35     public:
36         explicit Injecter(C2ComponentWrapper *thiz);
37 
38         SimpleMethodState::Injecter start();
39     private:
40         C2ComponentWrapper *const mThiz;
41     };
42 
43     /**
44      * A wrapper around the listener class inside C2Component class.
45      */
46     class Listener : public C2Component::Listener {
47     public:
48         explicit Listener(const std::shared_ptr<C2Component::Listener> &listener);
49         virtual ~Listener() = default;
50 
51         void onWorkDone_nb(std::weak_ptr<C2Component> component,
52                            std::list<std::unique_ptr<C2Work>> workItems) override;
53         void onTripped_nb(std::weak_ptr<C2Component> component,
54                           std::vector<std::shared_ptr<C2SettingResult>> settingResult) override;
55         void onError_nb(std::weak_ptr<C2Component> component, uint32_t errorCode) override;
56 
57     private:
58         std::shared_ptr<C2Component::Listener> mListener;
59     };
60 
61     explicit C2ComponentWrapper(const std::shared_ptr<C2Component> &comp);
62     virtual ~C2ComponentWrapper() = default;
63 
64     virtual c2_status_t setListener_vb(
65             const std::shared_ptr<C2Component::Listener> &listener,
66             c2_blocking_t mayBlock) override;
67     virtual c2_status_t queue_nb(std::list<std::unique_ptr<C2Work>>* const items) override;
68     virtual c2_status_t announce_nb(const std::vector<C2WorkOutline> &items) override;
69     virtual c2_status_t flush_sm(
70             flush_mode_t mode, std::list<std::unique_ptr<C2Work>>* const flushedWork) override;
71     virtual c2_status_t drain_nb(drain_mode_t mode) override;
72     virtual c2_status_t start() override;
73     virtual c2_status_t stop() override;
74     virtual c2_status_t reset() override;
75     virtual c2_status_t release() override;
76     virtual std::shared_ptr<C2ComponentInterface> intf() override;
77 
78     Injecter inject();
79 
80 private:
81     std::shared_ptr<Listener> mListener;
82     std::shared_ptr<C2Component> mComp;
83 
84     SimpleMethodState mStartState;
85 };
86 
87 }  // namespace android
88 
89 #endif // C2_COMPONENT_WRAPPER_H_
90