Apollo 10.0
自动驾驶开放平台
image_handler.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
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
18
19#include "cyber/common/log.h"
22
23#include "opencv2/opencv.hpp"
24
25namespace apollo {
26namespace dreamview {
27
30
31constexpr double ImageHandler::kImageScale;
32
33template <>
34void ImageHandler::OnImage(const std::shared_ptr<Image> &image) {
35 if (requests_ == 0) {
36 return;
37 }
38
39 cv::Mat mat(image->height(), image->width(), CV_8UC3,
40 const_cast<char *>(image->data().data()), image->step());
41 cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
42 cv::resize(
43 mat, mat,
44 cv::Size(static_cast<int>(image->width() * ImageHandler::kImageScale),
45 static_cast<int>(image->height() * ImageHandler::kImageScale)),
46 0, 0, cv::INTER_LINEAR);
47
48 std::unique_lock<std::mutex> lock(mutex_);
49 cv::imencode(".jpg", mat, send_buffer_, std::vector<int>() /* params */);
50 cvar_.notify_all();
51}
52
53template <>
54void ImageHandler::OnImage(
55 const std::shared_ptr<CompressedImage> &compressed_image) {
56 if (requests_ == 0 ||
57 compressed_image->format() == "h265" /* skip video format */) {
58 return;
59 }
60
61 std::vector<uint8_t> compressed_raw_data(compressed_image->data().begin(),
62 compressed_image->data().end());
63 cv::Mat mat_image = cv::imdecode(compressed_raw_data, cv::IMREAD_COLOR);
64
65 std::unique_lock<std::mutex> lock(mutex_);
66 cv::imencode(".jpg", mat_image, send_buffer_,
67 std::vector<int>() /* params */);
68 cvar_.notify_all();
69}
70
71void ImageHandler::OnImageFront(const std::shared_ptr<Image> &image) {
72 if (FLAGS_use_navigation_mode) {
73 // Navigation mode
74 OnImage(image);
75 }
76}
77
78void ImageHandler::OnImageShort(const std::shared_ptr<CompressedImage> &image) {
79 if (!FLAGS_use_navigation_mode) {
80 // Regular mode
81 OnImage(image);
82 }
83}
84
86 : requests_(0), node_(cyber::CreateNode("image_handler")) {
87 node_->CreateReader<Image>(
88 FLAGS_image_front_topic,
89 [this](const std::shared_ptr<Image> &image) { OnImageFront(image); });
90
91 node_->CreateReader<CompressedImage>(
92 FLAGS_image_short_topic,
93 [this](const std::shared_ptr<CompressedImage> &image) {
94 OnImageShort(image);
95 });
96}
97
98bool ImageHandler::handleGet(CivetServer *server, struct mg_connection *conn) {
99 requests_++;
100
101 mg_printf(conn,
102 "HTTP/1.1 200 OK\r\n"
103 "Connection: close\r\n"
104 "Max-Age: 0\r\n"
105 "Expires: 0\r\n"
106 "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
107 "Pragma: no-cache\r\n"
108 "Content-Type: multipart/x-mixed-replace; "
109 "boundary=--BoundaryString\r\n"
110 "\r\n");
111
112 std::vector<uchar> to_send;
113 while (true) {
114 {
115 std::unique_lock<std::mutex> lock(mutex_);
116 to_send = send_buffer_;
117 }
118
119 if (!to_send.empty()) {
120 // Sends the image data
121 mg_printf(conn,
122 "--BoundaryString\r\n"
123 "Content-type: image/jpeg\r\n"
124 "Content-Length: %zu\r\n"
125 "\r\n",
126 to_send.size());
127
128 if (mg_write(conn, &to_send[0], to_send.size()) <= 0) {
129 requests_--;
130 return false;
131 }
132 mg_printf(conn, "\r\n\r\n");
133 }
134
135 std::unique_lock<std::mutex> lock(mutex_);
136 cvar_.wait(lock);
137 }
138
139 requests_--;
140 return true;
141}
142
143} // namespace dreamview
144} // namespace apollo
bool handleGet(CivetServer *server, struct mg_connection *conn)
static constexpr double kImageScale
class register implement
Definition arena_queue.h:37