Add camera resource manage into all display case
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_rm.c
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyuntae Kim <ht1211.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22  /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 ========================================================================================*/
25 #ifdef _MMCAMCORDER_RM_SUPPORT
26 #include <aul.h>
27 #include <rm_api.h>
28 #include "mm_camcorder_rm.h"
29 #include "mm_camcorder_internal.h"
30
31 static rm_cb_result __mmcamcorder_rm_callback(int handle, rm_callback_type event_src,
32         rm_device_request_s *info, void* cb_data)
33 {
34         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(cb_data);
35         int current_state = MM_CAMCORDER_STATE_NONE;
36         rm_cb_result cb_res = RM_CB_RESULT_OK;
37
38         mmf_return_val_if_fail((MMHandleType)hcamcorder, RM_CB_RESULT_OK);
39
40         current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
41
42         _mmcam_dbg_warn("current state %d (handle %p)", current_state, hcamcorder);
43
44         _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
45
46         /* set RM event code for sending it to application */
47         hcamcorder->interrupt_code = event_src;
48
49         _mmcam_dbg_log("RM conflict callback : event code 0x%x", event_src);
50         switch (event_src) {
51         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT:
52         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT_UD:
53                 __mmcamcorder_force_stop(hcamcorder, _MMCAMCORDER_STATE_CHANGE_BY_RM);
54                 break;
55         default:
56                 break;
57         }
58
59         _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
60
61         return cb_res;
62 }
63
64
65
66 int _mmcamcorder_rm_create(MMHandleType handle)
67 {
68         int ret = RM_OK;
69         rm_consumer_info rci;
70         int app_pid = 0;
71
72         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
73         if (!hcamcorder) {
74                 _mmcam_dbg_err("Not initialized");
75                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
76         }
77
78         mm_camcorder_get_attributes(handle, NULL,
79                 MMCAM_CLIENT_PID, &app_pid,
80                 NULL);
81         rci.app_pid = app_pid;
82         aul_app_get_appid_bypid(rci.app_pid, rci.app_id, sizeof(rci.app_id));
83
84         /* RM register */
85         if (hcamcorder->rm_handle == 0) {
86                 ret = rm_register((rm_resource_cb)__mmcamcorder_rm_callback, (void*)hcamcorder, &(hcamcorder->rm_handle), &rci);
87                 if (ret != RM_OK) {
88                         _mmcam_dbg_err("rm_register fail ret = %d",ret);
89                         return MM_ERROR_RESOURCE_INTERNAL;
90                 }
91         }
92         return MM_ERROR_NONE;
93 }
94
95 int _mmcamcorder_rm_allocate(MMHandleType handle)
96 {
97         int iret = RM_OK;
98         int preview_format = MM_PIXEL_FORMAT_NV12;
99         int qret = RM_OK;
100         int qret_avail = 0; /* 0: not available, 1: available */
101         int resource_count = 0;
102         int display_surface_type = MM_DISPLAY_SURFACE_OVERLAY;
103
104         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
105         if (!hcamcorder) {
106                 _mmcam_dbg_err("Not initialized");
107                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
108         }
109
110         mm_camcorder_get_attributes(handle, NULL,
111                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
112                 NULL);
113
114         if (display_surface_type != MM_DISPLAY_SURFACE_NULL) {
115                 mm_camcorder_get_attributes(handle, NULL,
116                         MMCAM_CAMERA_FORMAT, &preview_format,
117                         NULL);
118
119                 resource_count = 0;
120                 memset(&hcamcorder->request_resources, 0x0, sizeof(rm_category_request_s));
121                 memset(&hcamcorder->returned_devices, 0x0, sizeof(rm_device_return_s));
122
123                 if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
124                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
125                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_VIDEO_DECODER;
126
127                         _mmcam_dbg_log("request dec rsc - category 0x%x", RM_CATEGORY_VIDEO_DECODER);
128
129                         resource_count++;
130                 }
131
132                 if (display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) {
133                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
134                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER;
135
136                         _mmcam_dbg_log("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER);
137
138                         resource_count++;
139                 }
140
141                 hcamcorder->request_resources.request_num = resource_count;
142
143                 if (resource_count > 0) {
144                         qret = rm_query(hcamcorder->rm_handle, RM_QUERY_ALLOCATION, &(hcamcorder->request_resources), &qret_avail);
145                         if (qret != RM_OK || qret_avail != 1) {
146                                 _mmcam_dbg_log("rm query failed. retry with sub devices");
147
148                                 resource_count = 0;
149                                 memset(&hcamcorder->request_resources, 0x0, sizeof(rm_category_request_s));
150                                 memset(&hcamcorder->returned_devices, 0x0, sizeof(rm_device_return_s));
151
152                                 if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
153                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
154                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_VIDEO_DECODER_SUB;
155                                         _mmcam_dbg_log("request dec rsc - category 0x%x", RM_CATEGORY_VIDEO_DECODER_SUB);
156                                         resource_count++;
157                                 }
158
159                                 if (display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) {
160                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
161                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER_SUB;
162                                         _mmcam_dbg_log("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER_SUB);
163                                         resource_count++;
164                                 }
165                         }
166                 }
167         }
168
169         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
170         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_CAMERA;
171
172         hcamcorder->request_resources.request_num = resource_count + 1;
173         _mmcam_dbg_log("request camera rsc - category 0x%x", RM_CATEGORY_CAMERA);
174
175         iret = rm_allocate_resources(hcamcorder->rm_handle, &(hcamcorder->request_resources), &hcamcorder->returned_devices);
176         if (iret != RM_OK) {
177                 _mmcam_dbg_err("Resource allocation request failed ret = %d",iret);
178                 return MM_ERROR_RESOURCE_INTERNAL;
179         }
180
181         return MM_ERROR_NONE;
182 }
183
184
185 int _mmcamcorder_rm_deallocate(MMHandleType handle)
186 {
187         int rm_ret = RM_OK;
188         int idx = 0;
189         rm_device_request_s requested;
190
191         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
192
193         if (!hcamcorder->rm_handle) {
194                 _mmcam_dbg_err("Resource is not initialized ");
195                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
196         }
197
198         if (hcamcorder->returned_devices.allocated_num > 0) {
199                 memset(&requested, 0x0, sizeof(rm_device_request_s));
200                 requested.request_num = hcamcorder->returned_devices.allocated_num;
201                 for (idx = 0; idx < requested.request_num; idx++)
202                         requested.device_id[idx] = hcamcorder->returned_devices.device_id[idx];
203
204                 rm_ret = rm_deallocate_resources(hcamcorder->rm_handle, &requested);
205                 if (rm_ret != RM_OK)
206                         _mmcam_dbg_err("Resource deallocation request failed ");
207         }
208
209         return MM_ERROR_NONE;
210 }
211
212 int _mmcamcorder_rm_release(MMHandleType handle)
213 {
214         int rm_ret = RM_OK;
215
216         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
217
218         if (!hcamcorder->rm_handle) {
219                 _mmcam_dbg_err("Resource is not initialized ");
220                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
221         }
222
223         /* unregister RM */
224         rm_ret = rm_unregister(hcamcorder->rm_handle);
225         if (rm_ret != RM_OK)
226                 _mmcam_dbg_err("rm_unregister() failed");
227         hcamcorder->rm_handle = 0;
228
229         return MM_ERROR_NONE;
230 }
231
232 #endif /* _MMCAMCORDER_RM_SUPPORT*/
233