| 150 | } |
| 151 | |
| 152 | bool CameraFeedAndroid::activate_feed() { |
| 153 | ERR_FAIL_COND_V_MSG(selected_format == -1, false, "CameraFeed format needs to be set before activating."); |
| 154 | if (is_active()) { |
| 155 | deactivate_feed(); |
| 156 | }; |
| 157 | |
| 158 | // Request permission |
| 159 | if (!OS::get_singleton()->request_permission("CAMERA")) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // Open device |
| 164 | static ACameraDevice_stateCallbacks deviceCallbacks = { |
| 165 | .context = this, |
| 166 | .onDisconnected = onDisconnected, |
| 167 | .onError = onError, |
| 168 | }; |
| 169 | camera_status_t c_status = ACameraManager_openCamera(manager, camera_id.utf8().get_data(), &deviceCallbacks, &device); |
| 170 | if (c_status != ACAMERA_OK) { |
| 171 | onError(this, device, c_status); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // Create image reader |
| 176 | const FeedFormat &feed_format = formats[selected_format]; |
| 177 | media_status_t m_status = AImageReader_new(feed_format.width, feed_format.height, feed_format.pixel_format, 1, &reader); |
| 178 | if (m_status != AMEDIA_OK) { |
| 179 | onError(this, device, m_status); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // Get image listener |
| 184 | static AImageReader_ImageListener listener{ |
| 185 | .context = this, |
| 186 | .onImageAvailable = onImage, |
| 187 | }; |
| 188 | m_status = AImageReader_setImageListener(reader, &listener); |
| 189 | if (m_status != AMEDIA_OK) { |
| 190 | onError(this, device, m_status); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // Get image surface |
| 195 | ANativeWindow *surface; |
| 196 | m_status = AImageReader_getWindow(reader, &surface); |
| 197 | if (m_status != AMEDIA_OK) { |
| 198 | onError(this, device, m_status); |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // Prepare session outputs |
| 203 | ACaptureSessionOutput *output = nullptr; |
| 204 | c_status = ACaptureSessionOutput_create(surface, &output); |
| 205 | if (c_status != ACAMERA_OK) { |
| 206 | onError(this, device, c_status); |
| 207 | return false; |
| 208 | } |
| 209 |
nothing calls this directly
no test coverage detected