123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include "nodes/base/base.hpp"
- #include "nodes/stream/streamNode.hpp"
- namespace Node
- {
- void StreamNode::work()
- {
- printf("StreamNode %s\n", name_.c_str());
- printf("stram url: %s\n", stream_url_.c_str());
- printf("Decode type: %d\n", static_cast<int>(decode_type_));
- if (decode_type_ == DecodeType::CPU)
- {
- work_cpu();
- }
- else
- {
- work_gpu();
- }
- }
- void StreamNode::work_cpu()
- {
- while (running_)
- {
- cv::Mat frame;
- cap_->read(frame);
- frame_count_++;
- if (frame_count_ % skip_frame_ != 0)
- {
- // printf("Skip frame %d\n", frame_count_);
- continue;
- }
- if (frame.empty())
- {
- std::cerr << "Error: cannot read frame" << std::endl;
- break;
- }
- auto metaData = std::make_shared<meta::MetaData>();
- metaData->image = frame;
- metaData->from = name_;
- for (auto& output_buffer : output_buffers_)
- {
- output_buffer.second->push(metaData);
- // printf("Node %s push data to %s\n", name_.c_str(), output_buffer.first.c_str());
- }
- }
- }
- void StreamNode::work_gpu()
- {
- uint8_t* packet_data = nullptr;
- int packet_size = 0;
- int64_t pts = 0;
- demuxer_->get_extra_data(&packet_data, &packet_size);
- decoder_->decode(packet_data, packet_size);
- printf("packet_size = %d\n", packet_size);
- unsigned int frame_index = 0;
- while(running_)
- {
- demuxer_->demux(&packet_data, &packet_size, &pts);
- if (packet_size <= 0 || !running_)
- {
- break;
- }
- int ndecoded_frame = decoder_->decode(packet_data, packet_size, pts);
- for(int i = 0; i < ndecoded_frame; ++i){
- /* 因为decoder获取的frame内存,是YUV-NV12格式的。储存内存大小是 [height * 1.5] * width byte
- 因此构造一个height * 1.5, width 大小的空间
- 然后由opencv函数,把YUV-NV12转换到BGR,转换后的image则是正常的height, width, CV_8UC3
- */
- cv::Mat frame(decoder_->get_height(), decoder_->get_width(), CV_8UC3, decoder_->get_frame(&pts, &frame_index));
- //cv::cvtColor(image, image, cv::COLOR_YUV2BGR_NV12);
- frame_index = frame_index + 1;
- // INFO("write imgs/img_%05d.jpg %dx%d", frame_index, decoder->get_width(), decoder->get_height());
-
- frame_count_++;
- if (frame_count_ % skip_frame_ != 0)
- {
- // printf("Skip frame %d\n", frame_count_);
- continue;
- }
- auto metaData = std::make_shared<meta::MetaData>();
- metaData->image = frame;
- metaData->from = name_;
- for (auto& output_buffer : output_buffers_)
- {
- output_buffer.second->push(metaData);
- // printf("Node %s push data to %s\n", name_.c_str(), output_buffer.first.c_str());
- }
- }
- };
- printf("C++ Demo: %d frames\n", frame_index);
- }
- } // namespace Node
|