JRTPlib发送G711音频数据

  • 将采集的pcm文件转码发送

- (void)DidGetAudioData:(void *const)buffer size:(int)dataSize andPts:(long long)pts
{
    if (self.silence)
    {
        return;
    }
    
    TEST *hehe=[[TEST alloc]init];
    
    int d=0;
    
    //pcmu 编码
    unsigned char requestBuf[dataSize / 2];
        
//    G711Encoder(buffer, requestBuf, (int)dataSize / 2, 1);
//转g711
    [hehe G711Encoder:buffer with:requestBuf with:(int)dataSize/2 with:1];

    long m=(sizeof(requestBuf)/320)+1;
    
    for (int i =0; i < m ;i++) {
        
        unsigned char buf[320];
        
        memset(buf, 0, sizeof(buf));
        
        for (int x= i*320; x < (i+1)*320 && x < dataSize / 2; x++) {

            buf[x-i*320]=requestBuf[x];

        }
        
        [RTPPack SendVideoPacket:buf Size:320];
        
        NSLog(@"%d---%d",dataSize,d++);
        
    }

    
    //解码
//    short decodeBuf[dataSize];
//
////    G711Decode(decodeBuf, requestBuf,dataSize);
//
//    [hehe G711Decode:decodeBuf with:requestBuf with:dataSize];
//
//    //播放
//    [self.pcmPlayer play:decodeBuf length:dataSize];
    
}

参考别人封装的

//发送数据
(void)SendVideoPacket:(unsigned char*)videoData
                   Size:(size_t)videoDataSize;

c++代码看不太懂,参考乔乐的代码

.h

#include "rtpsession.h"
#include "rtppacket.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"

@interface VideoRTPPack : NSObject

{
    
    jrtplib::RTPSession m_RTPSession;

}

@property(nonatomic,strong)NSMutableDictionary *dicDataBuffer;

- (id)initWithVideoRTP:(NSString *)hostIP onPort:(unsigned short)port;

- (void)createVideoRTP:(NSString *)host onPort:(unsigned short)port;

- (void)SendVideoPacket:(unsigned char*)videoData
                   Size:(size_t)videoDataSize;

- (void)resendSubPacketData:(int)frameID withSubPack:(int)packID;

- (void)closeVideoRTP;

.mm

- (id)initWithVideoRTP:(NSString *)hostIP onPort:(unsigned short)port
{
    if (self = [super init])
    {
        [self createVideoRTP:hostIP onPort:port];
        
        _dicDataBuffer = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}

- (void)dealloc
{
    
}

- (void)createVideoRTP:(NSString *)host onPort:(unsigned short)port
{
    jrtplib::RTPUDPv4TransmissionParams m_VideoTransparams;
    jrtplib::RTPSessionParams m_VideoSessionparams;
    
    m_VideoSessionparams.SetOwnTimestampUnit(1.0/8000.0);
    m_VideoSessionparams.SetAcceptOwnPackets(TRUE);
    m_VideoSessionparams.SetUsePollThread(0);
    m_VideoTransparams.SetPortbase(port);
    
    int nStatus = m_RTPSession.Create(m_VideoSessionparams, &m_VideoTransparams);
    
    if(nStatus < 0)
    {
        
        NSLog(@"create rtp faild!");
        
        return;
        
    }
    
    unsigned long ipAddress = ntohl(inet_addr([host UTF8String]));
    
    jrtplib::RTPIPv4Address JRTPVideoAddr;
    
    JRTPVideoAddr.SetIP(ipAddress);
    JRTPVideoAddr.SetPort(port);
    
    nStatus = m_RTPSession.AddDestination(JRTPVideoAddr);
    
    if(nStatus < 0)
    {
        
        NSLog(@"add destination faild!");
        
        return;
        
    }
    
    m_RTPSession.SetDefaultPayloadType(97);
    m_RTPSession.SetDefaultMark(false);
    m_RTPSession.SetDefaultTimestampIncrement(320);
    
}

- (void)SendVideoPacket:(unsigned char*)videoData
                   Size:(size_t)videoDataSize
{
   
//    m_RTPSession.SendPacket(videoData, videoDataSize);
    

    m_RTPSession.SendPacket(videoData, videoDataSize, 97, false, 320);
    
}

//重新发送
- (void)resendSubPacketData:(int)frameID withSubPack:(int)packID
{
    NSArray *lostArray = (NSArray *)[self.dicDataBuffer objectForKey:[NSNumber numberWithInt:frameID]];
    NSData *lostData = [lostArray objectAtIndex:packID-1];
    
    int nStatus = m_RTPSession.SendPacket([lostData bytes], [lostData length]);
    
    if (nStatus == 0)
    NSLog(@"ReSend Packet Data Succeed!");
}

- (void)closeVideoRTP
{
    if (m_RTPSession.IsActive())
    {
        m_RTPSession.Destroy();
    }
}

你可能感兴趣的:(JRTPlib发送G711音频数据)