mjpeg resource allcation routine changes according to resource information
[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 <ri-api.h>
29 #include <ri-module-api.h>
30 #include "mm_camcorder_rm.h"
31 #include "mm_camcorder_internal.h"
32
33 static rm_cb_result __mmcamcorder_rm_callback(int handle, rm_callback_type event_src,
34         rm_device_request_s *info, void* cb_data)
35 {
36         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(cb_data);
37         int current_state = MM_CAMCORDER_STATE_NONE;
38         rm_cb_result cb_res = RM_CB_RESULT_OK;
39
40         mmf_return_val_if_fail((MMHandleType)hcamcorder, RM_CB_RESULT_OK);
41
42         current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
43
44         _mmcam_dbg_warn("current state %d (handle %p)", current_state, hcamcorder);
45
46         _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
47
48         /* set RM event code for sending it to application */
49         hcamcorder->interrupt_code = event_src;
50
51         _mmcam_dbg_log("RM conflict callback : event code 0x%x", event_src);
52         switch (event_src) {
53         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT:
54         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT_UD:
55                 __mmcamcorder_force_stop(hcamcorder, _MMCAMCORDER_STATE_CHANGE_BY_RM);
56                 break;
57         default:
58                 break;
59         }
60
61         _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
62
63         return cb_res;
64 }
65
66
67
68 int _mmcamcorder_rm_create(MMHandleType handle)
69 {
70         int ret = RM_OK;
71         rm_consumer_info rci;
72         int app_pid = 0;
73
74         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
75         if (!hcamcorder) {
76                 _mmcam_dbg_err("Not initialized");
77                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
78         }
79
80         memset(&rci, 0x00, sizeof(rm_consumer_info));
81
82         mm_camcorder_get_attributes(handle, NULL,
83                 MMCAM_CLIENT_PID, &app_pid,
84                 NULL);
85         rci.app_pid = app_pid;
86         aul_app_get_appid_bypid(rci.app_pid, rci.app_id, sizeof(rci.app_id));
87
88         /* RM register */
89         if (hcamcorder->rm_handle == 0) {
90                 ret = rm_register((rm_resource_cb)__mmcamcorder_rm_callback,
91                         (void*)hcamcorder,
92                         &(hcamcorder->rm_handle),
93                         (rci.app_id[0] != '\0') ? &rci : NULL);
94                 if (ret != RM_OK) {
95                         _mmcam_dbg_err("rm_register fail ret = %d",ret);
96                         return MM_ERROR_RESOURCE_INTERNAL;
97                 }
98         }
99         return MM_ERROR_NONE;
100 }
101
102 int _mmcamcorder_rm_allocate(MMHandleType handle)
103 {
104         int iret = RM_OK;
105         int preview_format = MM_PIXEL_FORMAT_NV12;
106         int qret = RM_OK;
107         int qret_avail = 0; /* 0: not available, 1: available */
108         int resource_count = 0;
109         int display_surface_type = MM_DISPLAY_SURFACE_OVERLAY;
110         int camera_width = 0;
111
112         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
113         if (!hcamcorder) {
114                 _mmcam_dbg_err("Not initialized");
115                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
116         }
117
118         mm_camcorder_get_attributes(handle, NULL,
119                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
120                 NULL);
121
122         if (display_surface_type != MM_DISPLAY_SURFACE_NULL) {
123                 mm_camcorder_get_attributes(handle, NULL,
124                         MMCAM_CAMERA_FORMAT, &preview_format,
125                         NULL);
126
127                 resource_count = 0;
128                 memset(&hcamcorder->request_resources, 0x0, sizeof(rm_category_request_s));
129                 memset(&hcamcorder->returned_devices, 0x0, sizeof(rm_device_return_s));
130
131                 if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
132                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
133                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_VIDEO_DECODER;
134
135                         _mmcam_dbg_log("request dec rsc - category 0x%x", RM_CATEGORY_VIDEO_DECODER);
136
137                         resource_count++;
138                 } else if (preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
139                         mm_camcorder_get_attributes(handle, NULL,
140                                 MMCAM_CAMERA_WIDTH, &camera_width,
141                                 NULL);
142
143                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
144                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_MJPEG_DECODER;
145                         hcamcorder->request_resources.category_option[resource_count] = ri_get_jpeg_category_id("MJPEG", camera_width);
146                         _mmcam_dbg_log("request dec rsc - category 0x%x, option width [%d]", RM_CATEGORY_MJPEG_DECODER, camera_width);
147                         resource_count++;
148                 }
149                 if (display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) {
150                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
151                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER;
152                         _mmcam_dbg_log("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER);
153                         resource_count++;
154                 }
155
156                 hcamcorder->request_resources.request_num = resource_count;
157
158                 if (resource_count > 0) {
159                         qret = rm_query(hcamcorder->rm_handle, RM_QUERY_ALLOCATION, &(hcamcorder->request_resources), &qret_avail);
160                         if (qret != RM_OK || qret_avail != 1) {
161                                 _mmcam_dbg_log("rm query failed. retry with sub devices");
162
163                                 resource_count = 0;
164                                 memset(&hcamcorder->request_resources, 0x0, sizeof(rm_category_request_s));
165                                 memset(&hcamcorder->returned_devices, 0x0, sizeof(rm_device_return_s));
166
167                                 if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
168                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
169                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_VIDEO_DECODER_SUB;
170                                         _mmcam_dbg_log("request dec rsc - category 0x%x", RM_CATEGORY_VIDEO_DECODER_SUB);
171                                         resource_count++;
172                                 } else if (preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
173                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
174                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_MJPEG_DECODER;
175                                         hcamcorder->request_resources.category_option[resource_count] = ri_get_jpeg_category_id("MJPEG", camera_width);
176                                         _mmcam_dbg_log("request dec rsc - category 0x%x", RM_CATEGORY_MJPEG_DECODER);
177                                         resource_count++;
178                                 }
179                                 if (display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) {
180                                         if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
181                                                 hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
182                                                 hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER_SUB;
183                                                 _mmcam_dbg_log("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER_SUB);
184                                                 resource_count++;
185                                         } else if (preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
186                                                 hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
187                                                 hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER;
188                                                 _mmcam_dbg_log("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER);
189                                                 resource_count++;
190                                         }
191                                 }
192                         }
193                 }
194         }
195
196         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
197         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_CAMERA;
198
199         hcamcorder->request_resources.request_num = resource_count + 1;
200         _mmcam_dbg_log("request camera rsc - category 0x%x", RM_CATEGORY_CAMERA);
201
202         iret = rm_allocate_resources(hcamcorder->rm_handle, &(hcamcorder->request_resources), &hcamcorder->returned_devices);
203         if (iret != RM_OK) {
204                 _mmcam_dbg_err("Resource allocation request failed ret = %d",iret);
205                 return MM_ERROR_RESOURCE_INTERNAL;
206         }
207
208         return MM_ERROR_NONE;
209 }
210
211
212 int _mmcamcorder_rm_deallocate(MMHandleType handle)
213 {
214         int rm_ret = RM_OK;
215         int idx = 0;
216         rm_device_request_s requested;
217
218         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
219
220         if (!hcamcorder->rm_handle) {
221                 _mmcam_dbg_err("Resource is not initialized ");
222                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
223         }
224
225         if (hcamcorder->returned_devices.allocated_num > 0) {
226                 memset(&requested, 0x0, sizeof(rm_device_request_s));
227                 requested.request_num = hcamcorder->returned_devices.allocated_num;
228                 for (idx = 0; idx < requested.request_num; idx++)
229                         requested.device_id[idx] = hcamcorder->returned_devices.device_id[idx];
230
231                 rm_ret = rm_deallocate_resources(hcamcorder->rm_handle, &requested);
232                 if (rm_ret != RM_OK)
233                         _mmcam_dbg_err("Resource deallocation request failed ");
234         }
235
236         return MM_ERROR_NONE;
237 }
238
239 int _mmcamcorder_rm_release(MMHandleType handle)
240 {
241         int rm_ret = RM_OK;
242
243         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
244
245         if (!hcamcorder->rm_handle) {
246                 _mmcam_dbg_err("Resource is not initialized ");
247                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
248         }
249
250         /* unregister RM */
251         rm_ret = rm_unregister(hcamcorder->rm_handle);
252         if (rm_ret != RM_OK)
253                 _mmcam_dbg_err("rm_unregister() failed");
254         hcamcorder->rm_handle = 0;
255
256         return MM_ERROR_NONE;
257 }
258
259 #endif /* _MMCAMCORDER_RM_SUPPORT*/
260