Home Database Mysql Tutorial 使用 live555 直播来自 v4l2 的摄像头图像

使用 live555 直播来自 v4l2 的摄像头图像

Jun 07, 2016 pm 03:48 PM
c use image Camera live streaming

结合前面的 采集 v4l2 视频, 使用 live555, 通过 rtsp 发布实时流. capture.h, capture.cpp, vcompress.h, vcompress.cpp 需要参考前面几片文章. 这里仅仅贴出 v4l2_x264_service.cpp [cpp] view plaincopy #includestdio.h #includestdlib.h #includeunistd

结合前面的 采集 v4l2 视频, 使用 live555, 通过 rtsp 发布实时流. capture.h, capture.cpp, vcompress.h, vcompress.cpp 需要参考前面几片文章. 这里仅仅贴出 v4l2_x264_service.cpp

[cpp] view plaincopy

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6. #include   
  7. #include   
  8. #include   
  9.   
  10. #include   
  11. #include   
  12.   
  13. #include "capture.h"  
  14. #include "vcompress.h"  
  15.   
  16. static UsageEnvironment *_env = 0;  
  17.   
  18. #define SINK_PORT 3030  
  19.   
  20. #define VIDEO_WIDTH 320  
  21. #define VIDEO_HEIGHT 240  
  22. #define FRAME_PER_SEC 5.0  
  23.   
  24. pid_t gettid()  
  25. {  
  26.     return syscall(SYS_gettid);  
  27. }  
  28.   
  29.   
  30. // 使用 webcam + x264  
  31. class WebcamFrameSource : public FramedSource  
  32. {  
  33.     void *mp_capture, *mp_compress; // v4l2 + x264 encoder  
  34.     int m_started;  
  35.     void *mp_token;  
  36.   
  37. public:  
  38.     WebcamFrameSource (UsageEnvironment &env)  
  39.         : FramedSource(env)  
  40.     {  
  41.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  42.         mp_capture = capture_open("/dev/video0", VIDEO_WIDTH, VIDEO_HEIGHT, PIX_FMT_YUV420P);  
  43.         if (!mp_capture) {  
  44.             fprintf(stderr, "%s: open /dev/video0 err\n", __func__);  
  45.             exit(-1);  
  46.         }  
  47.   
  48.         mp_compress = vc_open(VIDEO_WIDTH, VIDEO_HEIGHT, FRAME_PER_SEC);  
  49.         if (!mp_compress) {  
  50.             fprintf(stderr, "%s: open x264 err\n", __func__);  
  51.             exit(-1);  
  52.         }  
  53.   
  54.         m_started = 0;  
  55.         mp_token = 0;  
  56.     }  
  57.   
  58.     ~WebcamFrameSource ()  
  59.     {  
  60.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  61.           
  62.         if (m_started) {  
  63.             envir().taskScheduler().unscheduleDelayedTask(mp_token);  
  64.         }  
  65.   
  66.         if (mp_compress)  
  67.             vc_close(mp_compress);  
  68.         if (mp_capture)  
  69.             capture_close(mp_capture);  
  70.     }  
  71.   
  72. protected:  
  73.     virtual void doGetNextFrame ()  
  74.     {  
  75.         if (m_started) return;  
  76.         m_started = 1;  
  77.   
  78.         // 根据 fps, 计算等待时间  
  79.         double delay = 1000.0 / FRAME_PER_SEC;  
  80.         int to_delay = delay * 1000;    // us  
  81.   
  82.         mp_token = envir().taskScheduler().scheduleDelayedTask(to_delay,  
  83.                 getNextFrame, this);  
  84.     }  

[cpp] view plaincopy

  1. virtual unsigned maxFrameSize() const        // 这个很重要, 如果不设置, 可能导致 getNextFrame() 出现 fMaxSize 小于实际编码帧的情况, 导致图像不完整  

[cpp] view plaincopy

  1. {    return 100*1024; }  

[cpp] view plaincopy

  1. private:  
  2.     static void getNextFrame (void *ptr)  
  3.     {  
  4.         ((WebcamFrameSource*)ptr)->getNextFrame1();  
  5.     }  
  6.   
  7.     void getNextFrame1 ()  
  8.     {  
  9.         // capture:  
  10.         Picture pic;  
  11.         if (capture_get_picture(mp_capture, &pic) 
  12.             fprintf(stderr, "==== %s: capture_get_picture err\n", __func__);  
  13.             m_started = 0;  
  14.             return;  
  15.         }  
  16.   
  17.         // compress  
  18.         const void *outbuf;  
  19.         int outlen;  
  20.         if (vc_compress(mp_compress, pic.data, pic.stride, &outbuf, &outlen) 
  21.             fprintf(stderr, "==== %s: vc_compress err\n", __func__);  
  22.             m_started = 0;  
  23.             return;  
  24.         }  
  25.   
  26.         int64_t pts, dts;  
  27.         int key;  
  28.         vc_get_last_frame_info(mp_compress, &key, &pts, &dts);  
  29.   
  30.         // save outbuf  
  31.         gettimeofday(&fPresentationTime, 0);  
  32.         fFrameSize = outlen;  
  33.         if (fFrameSize > fMaxSize) {  
  34.             fNumTruncatedBytes = fFrameSize - fMaxSize;  
  35.             fFrameSize = fMaxSize;  
  36.         }  
  37.         else {  
  38.             fNumTruncatedBytes = 0;  
  39.         }  
  40.   
  41.         memmove(fTo, outbuf, fFrameSize);  
  42.   
  43.         // notify  
  44.         afterGetting(this);  
  45.   
  46.         m_started = 0;  
  47.     }  
  48. };  
  49.   
  50. class WebcamOndemandMediaSubsession : public OnDemandServerMediaSubsession  
  51. {  
  52. public:  
  53.     static WebcamOndemandMediaSubsession *createNew (UsageEnvironment &env, FramedSource *source)  
  54.     {  
  55.         return new WebcamOndemandMediaSubsession(env, source);  
  56.     }  
  57.   
  58. protected:  
  59.     WebcamOndemandMediaSubsession (UsageEnvironment &env, FramedSource *source)  
  60.         : OnDemandServerMediaSubsession(env, True) // reuse the first source  
  61.     {  
  62.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  63.         mp_source = source;  
  64.         mp_sdp_line = 0;  
  65.     }  
  66.   
  67.     ~WebcamOndemandMediaSubsession ()  
  68.     {  
  69.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  70.         if (mp_sdp_line) free(mp_sdp_line);  
  71.     }  
  72.   
  73. private:  
  74.     static void afterPlayingDummy (void *ptr)  
  75.     {  
  76.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  77.         // ok  
  78.         WebcamOndemandMediaSubsession *This = (WebcamOndemandMediaSubsession*)ptr;  
  79.         This->m_done = 0xff;  
  80.     }  
  81.   
  82.     static void chkForAuxSDPLine (void *ptr)  
  83.     {  
  84.         WebcamOndemandMediaSubsession *This = (WebcamOndemandMediaSubsession *)ptr;  
  85.         This->chkForAuxSDPLine1();  
  86.     }  
  87.   
  88.     void chkForAuxSDPLine1 ()  
  89.     {  
  90.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  91.         if (mp_dummy_rtpsink->auxSDPLine())  
  92.             m_done = 0xff;  
  93.         else {  
  94.             int delay = 100*1000;   // 100ms  
  95.             nextTask() = envir().taskScheduler().scheduleDelayedTask(delay,  
  96.                     chkForAuxSDPLine, this);  
  97.         }  
  98.     }  
  99.   
  100. protected:  
  101.     virtual const char *getAuxSDPLine (RTPSink *sink, FramedSource *source)  
  102.     {  
  103.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  104.         if (mp_sdp_line) return mp_sdp_line;  
  105.   
  106.         mp_dummy_rtpsink = sink;  
  107.         mp_dummy_rtpsink->startPlaying(*source, 0, 0);  
  108.         //mp_dummy_rtpsink->startPlaying(*source, afterPlayingDummy, this);  
  109.         chkForAuxSDPLine(this);  
  110.         m_done = 0;  
  111.         envir().taskScheduler().doEventLoop(&m_done);  
  112.         mp_sdp_line = strdup(mp_dummy_rtpsink->auxSDPLine());  
  113.         mp_dummy_rtpsink->stopPlaying();  
  114.   
  115.         return mp_sdp_line;  
  116.     }  
  117.   
  118.     virtual RTPSink *createNewRTPSink(Groupsock *rtpsock, unsigned char type, FramedSource *source)  
  119.     {  
  120.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  121.         return H264VideoRTPSink::createNew(envir(), rtpsock, type);  
  122.     }  
  123.   
  124.     virtual FramedSource *createNewStreamSource (unsigned sid, unsigned &bitrate)  
  125.     {  
  126.         fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);  
  127.         bitrate = 500;  
  128.         return H264VideoStreamFramer::createNew(envir(), new WebcamFrameSource(envir()));  
  129.     }  
  130.   
  131. private:  
  132.     FramedSource *mp_source;    // 对应 WebcamFrameSource  
  133.     char *mp_sdp_line;  
  134.     RTPSink *mp_dummy_rtpsink;  
  135.     char m_done;  
  136. };  
  137.   
  138. static void test_task (void *ptr)  
  139. {  
  140.     fprintf(stderr, "test: task ....\n");  
  141.     _env->taskScheduler().scheduleDelayedTask(100000, test_task, 0);  
  142. }  
  143.   
  144. static void test (UsageEnvironment &env)  
  145. {  
  146.     fprintf(stderr, "test: begin...\n");  
  147.   
  148.     char done = 0;  
  149.     int delay = 100 * 1000;  
  150.     env.taskScheduler().scheduleDelayedTask(delay, test_task, 0);  
  151.     env.taskScheduler().doEventLoop(&done);  
  152.   
  153.     fprintf(stderr, "test: end..\n");  
  154. }  
  155.   
  156. int main (int argc, char **argv)  
  157. {  
  158.     // env  
  159.     TaskScheduler *scheduler = BasicTaskScheduler::createNew();  
  160.     _env = BasicUsageEnvironment::createNew(*scheduler);  
  161.   
  162.     // test  
  163.     //test(*_env);  
  164.   
  165.     // rtsp server  
  166.     RTSPServer *rtspServer = RTSPServer::createNew(*_env, 8554);  
  167.     if (!rtspServer) {  
  168.         fprintf(stderr, "ERR: create RTSPServer err\n");  
  169.         ::exit(-1);  
  170.     }  
  171.   
  172.     // add live stream  
  173.     do {  
  174.         WebcamFrameSource *webcam_source = 0;  
  175.   
  176.         ServerMediaSession *sms = ServerMediaSession::createNew(*_env, "webcam", 0, "Session from /dev/video0");   
  177.         sms->addSubsession(WebcamOndemandMediaSubsession::createNew(*_env, webcam_source));  
  178.         rtspServer->addServerMediaSession(sms);  
  179.   
  180.         char *url = rtspServer->rtspURL(sms);  
  181.         *_env "using url \"" "\"\n";  
  182.         delete [] url;  
  183.     } while (0);  
  184.   
  185.     // run loop  
  186.     _env->taskScheduler().doEventLoop();  
  187.   
  188.     return 1;  
  189. }  

需要 live555 + libavcodec + libswscale + libx264, client 使用 vlc, mplayer, quicktime, .....

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to start a live broadcast on Xiaohongshu How to start a live broadcast on Xiaohongshu How to start a live broadcast on Xiaohongshu How to start a live broadcast on Xiaohongshu Mar 28, 2024 pm 01:50 PM

Xiaohongshu is a life community platform application that you are very familiar with. It has many functions and allows everyone to see a variety of information content at any time. There are many notes with pictures and texts. All of them can make everyone very satisfied, and sometimes you can see some live broadcast rooms, so everyone also wants to start a live broadcast and chat with everyone, but they don’t know how to start a live broadcast. The editor below I can also give you specific operation methods, I hope it can help you. How to start live streaming in Xiaohongshu: 1. First open Xiaohongshu and click + at the bottom of the homepage. 2. Then switch to live broadcast and click the start live broadcast entrance.

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to use Xiaomi Auto app How to use Xiaomi Auto app Apr 01, 2024 pm 09:19 PM

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

How to watch live broadcast on PotPlayer? -PotPlayer to watch live tutorials How to watch live broadcast on PotPlayer? -PotPlayer to watch live tutorials Mar 19, 2024 pm 10:04 PM

Friends, do you know how to watch live broadcasts with PotPlayer? Today I will explain the tutorial for watching live broadcasts with PotPlayer. If you are interested, come and take a look with me. I hope it can help everyone. First we open PotPlayer, then select the multi-functional sidebar in the lower right corner of the software, and then we click; refer to the picture below and a playlist will pop up. We can select the "Add" option, where we can adjust the live broadcast settings and Add to. At this time, in the drop-down box that pops up, we choose to add a link. Of course, if we have a live broadcast source file, we can directly choose to add the file and then import the file. Then in the address box that pops up, we enter what we want to watch

See all articles