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 STAGEFRIGHT_CODEC2_COMPONENT_FACTORY_H_
18 #define STAGEFRIGHT_CODEC2_COMPONENT_FACTORY_H_
19 
20 #include <C2Component.h>
21 
22 #include <functional>
23 #include <memory>
24 
25 /**
26  * Component factory object that enables to create a component and/or interface from a dynamically
27  * linked library. This is needed because the component/interfaces are managed objects, but we
28  * cannot safely create a managed object and pass it in C.
29  *
30  * Components/interfaces typically inherit from std::enable_shared_from_this, but C requires
31  * passing simple pointer, and shared_ptr constructor needs to know the class to be constructed
32  * derives from enable_shared_from_this.
33  *
34  */
35 class C2ComponentFactory {
36 public:
37     typedef std::function<void(::C2Component*)> ComponentDeleter;
38     typedef std::function<void(::C2ComponentInterface*)> InterfaceDeleter;
39 
40     /**
41      * Creates a component.
42      *
43      * This method SHALL return within 100ms.
44      *
45      * \param id        component ID for the created component
46      * \param component shared pointer where the created component is stored. Cleared on
47      *                  failure and updated on success.
48      *
49      * \retval C2_OK        the component was created successfully
50      * \retval C2_TIMED_OUT could not create the component within the time limit (unexpected)
51      * \retval C2_CORRUPTED some unknown error prevented the creation of the component (unexpected)
52      *
53      * \retval C2_NO_MEMORY not enough memory to create the component
54      */
55     virtual c2_status_t createComponent(
56             c2_node_id_t id, std::shared_ptr<C2Component>* const component,
57             ComponentDeleter deleter = std::default_delete<C2Component>()) = 0;
58 
59     /**
60      * Creates a component interface.
61      *
62      * This method SHALL return within 100ms.
63      *
64      * \param id        component interface ID for the created interface
65      * \param interface shared pointer where the created interface is stored. Cleared on
66      *                  failure and updated on success.
67      *
68      * \retval C2_OK        the component interface was created successfully
69      * \retval C2_TIMED_OUT could not create the component interface within the time limit
70      *                      (unexpected)
71      * \retval C2_CORRUPTED some unknown error prevented the creation of the component interface
72      *                      (unexpected)
73      *
74      * \retval C2_NO_MEMORY not enough memory to create the component interface
75      */
76     virtual c2_status_t createInterface(
77             c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
78             InterfaceDeleter deleter = std::default_delete<C2ComponentInterface>()) = 0;
79 
80     virtual ~C2ComponentFactory() = default;
81 
82     typedef ::C2ComponentFactory* (*CreateCodec2FactoryFunc)(void);
83     typedef void (*DestroyCodec2FactoryFunc)(::C2ComponentFactory*);
84 };
85 
86 
87 #endif // STAGEFRIGHT_CODEC2_COMPONENT_FACTORY_H_
88