|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Controllers\Camera; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +class Add extends Camera |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Add a new camera |
| 11 | + */ |
| 12 | + public function add(array $params) : void |
| 13 | + { |
| 14 | + if (!IS_ADMIN) { |
| 15 | + throw new Exception('You are not allowed to add a new camera'); |
| 16 | + } |
| 17 | + |
| 18 | + $motionParams = []; |
| 19 | + $go2rtcStreams = []; |
| 20 | + $ffmpeg = false; |
| 21 | + $ffmpegParams = ''; |
| 22 | + |
| 23 | + /** |
| 24 | + * Check that minimal required parameters are set |
| 25 | + */ |
| 26 | + Param\Name::check($params['name']); |
| 27 | + Param\Device::check($params['main-stream-device']); |
| 28 | + Param\Resolution::check($params['main-stream-resolution']); |
| 29 | + Param\Framerate::check($params['main-stream-framerate']); |
| 30 | + Param\BasicAuthUsername::check($params['username']); |
| 31 | + Param\BasicAuthPassword::check($params['password']); |
| 32 | + |
| 33 | + /** |
| 34 | + * Get camera configuration template |
| 35 | + */ |
| 36 | + $configuration = $this->cameraConfigController->getTemplate(); |
| 37 | + |
| 38 | + /** |
| 39 | + * Set camera global configuration |
| 40 | + */ |
| 41 | + $configuration['name'] = $params['name']; |
| 42 | + $configuration['main-stream']['device'] = $params['main-stream-device']; |
| 43 | + $configuration['main-stream']['width'] = explode('x', $params['main-stream-resolution'])[0]; |
| 44 | + $configuration['main-stream']['height'] = explode('x', $params['main-stream-resolution'])[1]; |
| 45 | + $configuration['main-stream']['framerate'] = $params['main-stream-framerate']; |
| 46 | + $configuration['authentication']['username'] = \Controllers\Common::validateData($params['username']); |
| 47 | + $configuration['authentication']['password'] = \Controllers\Common::validateData($params['password']); |
| 48 | + $configuration['motion-detection']['enable'] = $params['motion-detection-enable']; |
| 49 | + // If camera is a http:// camera, use mjpeg mode for streaming |
| 50 | + if (preg_match('#^https?://#', $configuration['main-stream']['device'])) { |
| 51 | + $configuration['stream']['technology'] = 'mjpeg'; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Add camera in database |
| 56 | + */ |
| 57 | + $this->model->add(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Get new camera Id from database |
| 61 | + */ |
| 62 | + $id = $this->model->getLastInsertRowID(); |
| 63 | + |
| 64 | + if (empty($id)) { |
| 65 | + throw new Exception('Could not retrieve camera Id'); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get camera motion configuration template |
| 70 | + */ |
| 71 | + $motionConfiguration = $this->motionTemplateController->get($id); |
| 72 | + |
| 73 | + /** |
| 74 | + * Prepare motion configuration parameters |
| 75 | + */ |
| 76 | + $motionConfiguration['device_id'] = ['enabled' => true, 'value' => $id]; |
| 77 | + $motionConfiguration['device_name'] = ['enabled' => true, 'value' => $params['name']]; |
| 78 | + $motionConfiguration['width'] = ['enabled' => true, 'value' => explode('x', $params['main-stream-resolution'])[0]]; |
| 79 | + $motionConfiguration['height'] = ['enabled' => true, 'value' => explode('x', $params['main-stream-resolution'])[1]]; |
| 80 | + $motionConfiguration['framerate'] = ['enabled' => true, 'value' => $params['main-stream-framerate']]; |
| 81 | + |
| 82 | + // If auth username and password are set |
| 83 | + if (!empty($configuration['authentication']['username']) and !empty($configuration['authentication']['password'])) { |
| 84 | + $motionConfiguration['netcam_userpass']['value'] = $configuration['authentication']['username'] . ':' . $configuration['authentication']['password']; |
| 85 | + $motionConfiguration['netcam_userpass']['enabled'] = true; |
| 86 | + } |
| 87 | + |
| 88 | + // Case the URL is http(s):// |
| 89 | + if (preg_match('#^https?://#', $configuration['main-stream']['device'])) { |
| 90 | + $motionConfiguration['netcam_url'] = ['enabled' => true, 'value' => $configuration['main-stream']['device']]; |
| 91 | + $motionConfiguration['movie_passthrough'] = ['enabled' => false, 'value' => 'on']; |
| 92 | + $motionConfiguration['v4l2_device'] = ['enabled' => false, 'value' => '']; |
| 93 | + // Case the URL is rtsp:// |
| 94 | + } else if (preg_match('#^rtsp?://#', $configuration['main-stream']['device'])) { |
| 95 | + $motionConfiguration['netcam_url'] = ['enabled' => true, 'value' => $configuration['main-stream']['device']]; |
| 96 | + $motionConfiguration['movie_passthrough'] = ['enabled' => true, 'value' => 'on']; |
| 97 | + $motionConfiguration['v4l2_device'] = ['enabled' => false, 'value' => '']; |
| 98 | + // Case the URL is /dev/video |
| 99 | + } else if (preg_match('#^/dev/video#', $configuration['main-stream']['device'])) { |
| 100 | + // /dev/videoX devices cannot be used by both go2rtc and motion at the same time (device is locked), so force the use of the go2rtc stream |
| 101 | + $motionConfiguration['netcam_url'] = ['enabled' => true, 'value' => 'rtsp://127.0.0.1:8554/camera_' . $id . '?mp4']; |
| 102 | + $motionConfiguration['movie_passthrough'] = ['enabled' => false, 'value' => 'on']; |
| 103 | + $motionConfiguration['v4l2_device'] = ['enabled' => true, 'value' => $configuration['main-stream']['device']]; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Encode global configuration to JSON |
| 108 | + */ |
| 109 | + try { |
| 110 | + $configurationJson = json_encode($configuration, JSON_THROW_ON_ERROR); |
| 111 | + } catch (JsonException $e) { |
| 112 | + throw new Exception('Could not encode camera configuration to JSON'); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Encode motion configuration to JSON |
| 117 | + */ |
| 118 | + try { |
| 119 | + $motionConfigurationJson = json_encode($motionConfiguration, JSON_THROW_ON_ERROR); |
| 120 | + } catch (JsonException $e) { |
| 121 | + throw new Exception('Could not encode camera configuration to JSON'); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Save configurations to database |
| 126 | + */ |
| 127 | + $this->model->saveGlobalConfiguration($id, $configurationJson); |
| 128 | + $this->model->saveMotionConfiguration($id, $motionConfigurationJson); |
| 129 | + |
| 130 | + /** |
| 131 | + * Edit motion configuration file for this camera |
| 132 | + */ |
| 133 | + $this->motionConfigController->write(CAMERAS_MOTION_CONF_AVAILABLE_DIR . '/camera-' . $id . '.conf', $motionConfiguration); |
| 134 | + |
| 135 | + /** |
| 136 | + * Enable / disable motion configuration file |
| 137 | + */ |
| 138 | + if ($configuration['motion-detection']['enable'] == 'true') { |
| 139 | + $this->motionConfigController->enable($id); |
| 140 | + } else { |
| 141 | + $this->motionConfigController->disable($id); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Define proper stream URLs for go2rtc |
| 146 | + */ |
| 147 | + $go2rtcStreams = $this->generateGo2rtcStreams($id, $configuration); |
| 148 | + |
| 149 | + /** |
| 150 | + * Add a new stream in go2rtc |
| 151 | + */ |
| 152 | + $this->go2rtcController->addStream($id, $go2rtcStreams); |
| 153 | + |
| 154 | + unset($configuration, $configurationJson, $motionConfiguration, $motionConfigurationJson); |
| 155 | + } |
| 156 | +} |
0 commit comments