Apollo 10.0
自动驾驶开放平台
video_driver_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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/file.h"
20
21namespace apollo {
22namespace drivers {
23namespace video {
24
26
28 AINFO << "Initialize video driver component.";
29
30 CameraH265Config video_config;
31 if (!GetProtoConfig(&video_config)) {
32 return false;
33 }
34
35 AINFO << "Velodyne config: " << video_config.DebugString();
36
37 camera_deivce_.reset(new CameraDriver(&video_config));
38 camera_deivce_->Init();
39
40 if (camera_deivce_->Record()) {
41 // Use current directory to save record file if H265_SAVE_FOLDER environment
42 // is not set.
43 record_folder_ = cyber::common::GetEnv("H265_SAVE_FOLDER", ".");
44 AINFO << "Record folder: " << record_folder_;
45
46 struct stat st;
47 if (stat(record_folder_.c_str(), &st) < 0) {
48 bool ret = EnsureDirectory(record_folder_);
49 AINFO_IF(ret) << "Record folder is created successfully.";
50 }
51 }
52 pb_image_.reset(new CompressedImage);
53 pb_image_->mutable_data()->reserve(1920 * 1080 * 4);
54
55 writer_ = node_->CreateWriter<CompressedImage>(
56 video_config.compress_conf().output_channel());
57
58 runing_ = true;
59 video_thread_ = std::shared_ptr<std::thread>(
60 new std::thread(std::bind(&CompCameraH265Compressed::VideoPoll, this)));
61 video_thread_->detach();
62
63 return true;
64}
65
66void CompCameraH265Compressed::VideoPoll() {
67 std::ofstream fout;
68 if (camera_deivce_->Record()) {
69 char name[256];
70 snprintf(name, sizeof(name), "%s/encode_%d.h265", record_folder_.c_str(),
71 camera_deivce_->Port());
72 AINFO << "Output file: " << name;
73 fout.open(name, std::ios::binary);
74 if (!fout.good()) {
75 AERROR << "Failed to open output file: " << name;
76 }
77 }
78 int poll_failure_number = 0;
79 while (!apollo::cyber::IsShutdown()) {
80 if (!camera_deivce_->Poll(pb_image_)) {
81 AERROR << "H265 poll failed on port: " << camera_deivce_->Port();
82 static constexpr int kTolerance = 256;
83 if (++poll_failure_number > kTolerance) {
84 AERROR << "H265 poll keep failing for " << kTolerance << " times, Exit";
85 break;
86 }
87 continue;
88 }
89 poll_failure_number = 0;
90 pb_image_->mutable_header()->set_timestamp_sec(
91 cyber::Time::Now().ToSecond());
92 AINFO << "Send compressed image.";
93 writer_->Write(pb_image_);
94
95 if (camera_deivce_->Record()) {
96 fout.write(pb_image_->data().c_str(), pb_image_->data().size());
97 }
98 }
99
100 if (camera_deivce_->Record()) {
101 fout.close();
102 }
103}
104
105} // namespace video
106} // namespace drivers
107} // namespace apollo
bool GetProtoConfig(T *config) const
std::shared_ptr< Node > node_
static Time Now()
get the current time.
Definition time.cc:57
#define AERROR
Definition log.h:44
#define AINFO_IF(cond)
Definition log.h:72
#define AINFO
Definition log.h:42
std::string GetEnv(const std::string &var_name, const std::string &default_value="")
Definition environment.h:29
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists.
Definition file.cc:299
bool IsShutdown()
Definition state.h:46
class register implement
Definition arena_queue.h:37