问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

iOS 即时的音频怎么发送和接收

发布网友 发布时间:2022-04-22 06:11

我来回答

1个回答

热心网友 时间:2023-09-29 14:01

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 call to
// ntohl
destip = ntohl(destip);

std::cout << "Enter the destination port" << std::endl;
std::cin >> destport;

std::cout << std::endl;
std::cout << "Number of packets you wish to be sent:" << std::endl;
std::cin >> num;

// Now, we'll create a RTP session, set the destination, send some
// packets and poll for incoming data.

RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;

// IMPORTANT: The local timestamp unit MUST be set, otherwise
// RTCP Sender Report info will be calculated wrong
// In this case, we'll be sending 10 samples each second, so we'll
// put the timestamp unit to (1.0/10.0)
sessparams.SetOwnTimestampUnit(1.0/10.0);

sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status = sess.Create(sessparams,&transparams);
checkerror(status);

RTPIPv4Address addr(destip,destport);

status = sess.AddDestination(addr);
checkerror(status);

for (i = 1 ; i <= num ; i++)
{
printf("\nSending packet %d/%d\n",i,num);

// send the packet
status = sess.SendPacket((void *)"1234567890",10,0,false,10);
checkerror(status);

sess.BeginDataAccess();

// check incoming packets
if (sess.GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;

while ((pack = sess.GetNextPacket()) != NULL)
{
// You can examine the data here
printf("Got packet !\n");

// we don't longer need the packet, so
// we'll delete it
sess.DeletePacket(pack);
}
} while (sess.GotoNextSourceWithData());
}

sess.EndDataAccess();

#ifndef RTP_SUPPORT_THREAD
status = sess.Poll();
checkerror(status);
#endif // RTP_SUPPORT_THREAD

RTPTime::Wait(RTPTime(1,0));
}

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}
(2) receive.cpp
[cpp] view plaincopy
#include "rtpsession.h"
#include "rtppacket.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#ifndef WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif // WIN32
#include "rtpsourcedata.h"
#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 new class routine
//
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
找专业防水队做完还漏水怎么维权 法院会受理房屋漏水造成的纠纷吗? 巴西龟最长活多久,家养!!! 养胃的药最好的是什么啊 婴儿积食发烧不愿吃药怎么办 板门穴位在哪个部位 手机设置放偷看的方法? 凝结水回收器生产厂家? 个人账户养老金预测公式:现有5万元,缴费20年,能领多少钱? 临沂比较有名的男装品牌 毕业论文写作计划,怎么写啊? RTP+ES流方式 jrtplib如何设置视频和音频的两个tim... 在安装双显卡下,怎么样可以设置多个虚拟桌面,一... 关于socket和JRTPLIB的问题 win10怎么增加显卡虚拟内存 jrtplib中怎么实现rtcp传输,怎么提取rtcp信息 显卡 虚拟显存是什么 怎么看显卡怎么看虚拟内存? 有种技术是叫虚拟桌面吗?就是显卡设置分辨率超过... 桌面虚拟化显卡K1,K2,还有M10和M6那个适合 自己测试虚拟桌面,用什么显卡比较好 安卓slg游戏杂音 手机可以玩打造世界吗? 打造世界可以在手机上玩吗? SLG手游在哪里 好玩的SLG手游一览 我把儿子看黄色书籍的事情告诉了我老公...... slg游戏解压后怎么玩 中学生为什么不能看黄色书刊或录像求答案 SLG手游战争之轮联盟循环赛怎么玩? 无限制沙滩手机怎么玩 我们的金蝶软件K3进不去了,是怎么回事呀?哪位高... 内存1G,独立显卡,如何设置虚拟内存? 求一份c语言的RTP音频传输源码 金蝶K3拒绝访问 对于桌面虚拟化,我想问,如果是对显卡要求高的,... 论文工作计划和预期目标摘要怎么写 jrtplib跟mfc不兼容吗 金蝶k3显示用户登录失败怎么回事? 虚拟显卡要怎么弄 !高分 ! 您好。不知道你对RTSP协议熟不熟,但我还是想问一... 桌面PC中如何在虚拟机中调用独立显卡工作 VC++链接出错!!救命啊! 毕业论文进度安排表怎么写 kvm 桌面虚拟化 对服务器显卡有要求吗 widnows 下怎么采集声音编码然后用RTP协议发出去? 如何启用gpu虚拟化 什么虚拟机软件可以虚拟的显卡比较好 论文是什么具体要做什么准备,写论文的意义是什么 无限制格斗UFC与MMA规则有什么区别, 金蝶K3登录不上,显示 系统正忙,请稍后再尝试登录...