当前位置:编程学习 > C/C++ >>

RTP实时音视频数据传输,发送端和接收端

1.项目前期工作(配置好环境)
2.发送端文件编写(见下面的send.cpp)
3.接收端文件编写(见下面的receive.cpp)
4.编译文件
(1)发送端
                      g++ -o send send.cpp -I /usr/local/include/jrtplib3/ -ljrtp
(2)接收端
                      g++ -o receive  receive.cpp -I /usr/local/include/jrtplib3/ -ljrtp
附录:
(1)send.cpp
[cpp]
#include "rtpsession.h" 
#include "rtppacket.h" 
#include "rtpudpv4transmitter.h" 
#include "rtpipv4address.h" 
#include "rtpsessionparams.h" 
#include "rtperrors.h" 
#include "rtpmemorymanager.h" 
#ifndef WIN32 
    #include <netinet/in.h> 
    #include <arpa/inet.h> 
#else 
    #include <winsock2.h> 
#endif // WIN32 
#include <stdlib.h> 
#include <stdio.h> 
#include <iostream> 
#include <string> 
 
// 
// This function checks if there was a RTP error. If so, it displays an error 
// message and exists. 
// 
 
void checkerror(int rtperr) 

    if (rtperr < 0) 
    { 
        std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl; 
        exit(-1); 
    } 

 
// 
// The main routine 
// 
 
#ifdef RTP_SUPPORT_THREAD 
 
class MyMemoryManager : public RTPMemoryManager 

public: 
    MyMemoryManager()  
    {  
        mutex.Init(); 
        alloccount = 0;  
        freecount = 0;  
    } 
    ~MyMemoryManager()  
    {  
        std::cout << "alloc: " << alloccount << " free: " << freecount << std::endl;  
    } 
    void *AllocateBuffer(size_t numbytes, int memtype) 
    { 
        mutex.Lock(); 
        void *buf = malloc(numbytes); 
        std::cout << "Allocated " << numbytes << " bytes at location " << buf << " (memtype = " << memtype << ")" << std::endl; 
        alloccount++; 
        mutex.Unlock(); 
        return buf; 
    } 
 
    void FreeBuffer(void *p) 
    { 
        mutex.Lock(); 
        std::cout << "Freeing block " << p << std::endl; 
        freecount++; 
        free(p); 
        mutex.Unlock(); 
    } 
private: 
    int alloccount,freecount; 
    JMutex mutex; 
}; 
 
#else 
 
class MyMemoryManager : public RTPMemoryManager 

public: 
    MyMemoryManager()  
    {  
        alloccount = 0;  
        freecount = 0;  
    } 
    ~MyMemoryManager()  
    {  
        std::cout << "alloc: " << alloccount << " free: " << freecount << std::endl;  
    } 
    void *AllocateBuffer(size_t numbytes, int memtype) 
    { 
        void *buf = malloc(numbytes); 
        std::cout << "Allocated " << numbytes << " bytes at location " << buf << " (memtype = " << memtype << ")" << std::endl; 
        alloccount++; 
        return buf; 
    } 
 
    void FreeBuffer(void *p) 
    { 
        std::cout << "Freeing block " << p << std::endl; 
        freecount++; 
        free(p); 
    } 
private: 
    int alloccount,freecount; 
}; 
 
#endif // RTP_SUPPORT_THREAD 
 
int main(void) 

#ifdef WIN32 
    WSADATA dat; 
    WSAStartup(MAKEWORD(2,2),&dat); 
#endif // WIN32 
     
    MyMemoryManager mgr; 
    RTPSession sess(&mgr); 
    uint16_t portbase,destport; 
    uint32_t destip; 
    std::string ipstr; 
    int status,i,num; 
 
        // First, we'll ask for the necessary information 
         
    std::cout << "Enter local portbase:" << std::endl; 
    std::cin >> portbase; 
    std::cout << std::endl; 
     
    std::cout << "Enter the destination IP address" << std::endl; 
    std::cin >> ipstr; 
    destip = inet_addr(ipstr.c_str()); 
    if (destip == INADDR_NONE) 
    { 
        std::cerr << "Bad IP address specified" << std::endl; 
        return -1; 
    } 
     
    // The inet_addr function returns a value in network byte order, but 
    // we need the IP address in host byte order, so we use a cal

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,