|
解决了,系统是debian10 ,解决不了可以发邮寄给我:li.pandeng@163.com你参考下,- #include <stdio.h>
- #include <unistd.h>
- #include <iostream>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <thread>
- #include <memory.h>
- #include <sys/time.h>
- #include <queue>
- using namespace std;
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <rockchip/rockchip_rtsp.h>
- #include <rockchip/rockchip_mpp.h>
- extern "C" {
- #include <rockchip/rockchip_rga.h>
- }
- #define RTSP_URL "rtsp://192.168.1.200/h264/ch1/main/av_stream"
- #define RTSP_USER "admin"
- #define RTSP_PWD "123456"
- static MppDecoder *mpp_dec = NULL;
- static std::queue<DecFrame *> frame_queue;
- static int run_flag = 0;
- unsigned long get_time(void)
- {
- struct timeval ts;
- gettimeofday(&ts, NULL);
- return (ts.tv_sec * 1000 + ts.tv_usec / 1000);
- }
- void onRtspHandle(unsigned char *buf, size_t len)
- {
- std::cout << "frame recived " << len << std::endl;
- mpp_dec->ops->enqueue(mpp_dec, buf, len);
- }
- void inference_thread(RockchipRga *rga, int width, int height)
- {
- int ret;
- int resize_w = 1920, resize_h = 1080;
- static int frame_size = 0;
- unsigned char *frame_rgb = NULL;
- rga->ops->initCtx(rga);
- rga->ops->setRotate(rga, RGA_ROTATE_NONE);
- rga->ops->setSrcFormat(rga, V4L2_PIX_FMT_NV12, width, height);
- rga->ops->setDstFormat(rga, V4L2_PIX_FMT_BGR24, resize_w, resize_h);
-
- frame_size = resize_w * resize_h * 3;
- frame_rgb = (unsigned char *)malloc(frame_size);
- cv::Mat img(resize_h , resize_w , CV_8UC3, frame_rgb);
- if (!frame_rgb)
- goto exit;
-
- rga->ops->setDstBufferPtr(rga, frame_rgb);
- while (run_flag) {
- if (frame_queue.empty()) {
- usleep(1000);
- continue;
- }
- auto frame = frame_queue.front();
- rga->ops->setSrcBufferPtr(rga, frame->data);
- ret = rga->ops->go(rga);
- printf("inference_thread ................\n");
- if (!ret) {
- ///do something with frame_rgb
- cv::imshow("test", img);
- cv::waitKey(10);
- }
- frame_queue.pop();
- mpp_dec->ops->freeFrame(frame);
- }
- exit:
- run_flag = 0;
- while (!frame_queue.empty()) {
- auto frame = frame_queue.front();
- mpp_dec->ops->freeFrame(frame);
- frame_queue.pop();
- }
- if (frame_rgb)
- free(frame_rgb);
- }
- void decode_thread(RockchipRga *rga)
- {
- int ret;
- int first_frame = 0;
- std::thread t_inference;
- while (run_flag) {
- DecFrame *frame = mpp_dec->ops->dequeue_timeout(mpp_dec, 300);
- if (frame != NULL) {
- std::cout << "decode frame" << frame->width << "x" << frame->height << std::endl;
-
- if (!first_frame) {
- std::cout << "first_frame" << std::endl;
- t_inference = std::thread(inference_thread, rga, frame->width, frame->height);
- first_frame = 1;
- }
- if (frame_queue.size() < 30)
- frame_queue.push(frame);
- else
- mpp_dec->ops->freeFrame(frame);
- }
- }
- exit:
- run_flag = 0;
- t_inference.join();
- while (!frame_queue.empty()) {
- auto frame = frame_queue.front();
- mpp_dec->ops->freeFrame(frame);
- frame_queue.pop();
- }
- }
- int main(int argc, char **argv)
- {
- int ret;
- RockchipRga *rga;
- unsigned char ready[5] = {'r', 'e', 'a', 'd', 'y'};
- RtspClient rtsp_client(RTSP_URL, RTSP_USER, RTSP_PWD);
- rtsp_client.setDataCallback(onRtspHandle);
- mpp_dec = MppDecoderCreate(DECODE_TYPE_H264);
- if (!mpp_dec) {
- std::cout << "MppDecoderCreate error!\n" << std::endl;
- return -1;
- }
- rga = RgaCreate();
- if (!rga) {
- MppDecoderDestroy(mpp_dec);
- std::cout << "rgaCreate error!\n" << std::endl;
- return -1;
- }
- run_flag = 1;
- rtsp_client.enable();
- std::thread t_decode(decode_thread, rga);
- while (run_flag) {
- usleep(10000);
- }
- rtsp_client.disable();
- run_flag = 0;
- t_decode.join();
- RgaDestroy(rga);
- MppDecoderDestroy(mpp_dec);
- return 0;
- }
|
|