ios的Ice cast源客户端

ios的Ice cast源客户端,ios,audio-streaming,asyncsocket,icecast,Ios,Audio Streaming,Asyncsocket,Icecast,我一直在尝试为ios的ice cast创建源客户端。我已经能够使用asyncsocket连接到套接字。我还能够将数据写入服务器。icecast配置是针对mp3格式完成的。但写入服务器的mp3文件已损坏。我提供了一些代码片段 标题: NSString *string = @"SOURCE /sync HTTP/1.0\r\n" "Authorization: Basic c291cmNlOmhhY2ttZQ==\r\n" "User-Agent: butt-0.1

我一直在尝试为ios的ice cast创建源客户端。我已经能够使用asyncsocket连接到套接字。我还能够将数据写入服务器。icecast配置是针对mp3格式完成的。但写入服务器的mp3文件已损坏。我提供了一些代码片段

标题:

 NSString *string = @"SOURCE /sync HTTP/1.0\r\n"
        "Authorization: Basic c291cmNlOmhhY2ttZQ==\r\n"
        "User-Agent: butt-0.1.12\r\n"
        "User-Agent: butt-0.1.12\r\n"
        "content-type: audio/mpeg\r\n"
        "ice-name:  sync's Stream\r\n"
        "ice-public: 0\r\n"
        "ice-genre: Rock\r\n"
        "ice-description: This is my server description\r\n"
        "Connection: keep-alive\r\n"
        "ice-audio-info: ice-samplerate=44100;ice-bitrate=48;ice-channels=2\r\n\r\n";

     NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

    //sending http request to write the header
    NSLog(@"Sending HTTP Request.");
    [socket writeData:data withTimeout:-1 tag:1];

    //write buffer data to server
    [socket writeData:self.dataBuffer withTimeout:-1 tag:1];
对于录制,我使用aqrecorder使用以下代码进行录制

void AQRecorder::MyInputBufferHandler(  void *                              inUserData,
                                        AudioQueueRef                       inAQ,
                                        AudioQueueBufferRef                 inBuffer,
                                        const AudioTimeStamp *              inStartTime,
                                        UInt32                              inNumPackets,
                                        const AudioStreamPacketDescription* inPacketDesc)
{
    AQRecorder *aqr = (AQRecorder *)inUserData;
    try {
        if (inNumPackets > 0) {
            // write packets to file
            XThrowIfError(AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize,
                                             inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData),
                       "AudioFileWritePackets failed");
            aqr->mRecordPacket += inNumPackets;

           NSLog(@"size = %u",(unsigned int)inBuffer->mAudioDataByteSize);

            data = [[[NSData alloc]initWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize]retain];

            server *srv = [[server alloc]init];
            srv.dataBuffer=data;
            [srv connecting];

        }

        // if we're not stopping, re-enqueue the buffe so that it gets filled again
        if (aqr->IsRunning())
            XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
    } catch (CAXException e) {
        char buf[256];
        fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
    }
}



void AQRecorder::StartRecord(CFStringRef inRecordFile)
{
//    server *srv=[[server alloc]init];
//    [srv connecting];

    int i, bufferByteSize;
    UInt32 size;
    CFURLRef url = nil;

    try {       
        mFileName = CFStringCreateCopy(kCFAllocatorDefault, inRecordFile);

//      // specify the recording format
//      SetupAudioFormat(kAudioFormatMPEG4AAC);

        // specify the recording format, use hardware AAC if available
        // otherwise use IMA4
        if(IsAACHardwareEncoderAvailable())
            SetupAudioFormat(kAudioFormatMPEG4AAC);
        else
            SetupAudioFormat(kAudioFormatAppleIMA4);

        // create the queue
        XThrowIfError(AudioQueueNewInput(
                                      &mRecordFormat,
                                      MyInputBufferHandler,
                                      this /* userData */,
                                      NULL /* run loop */, NULL /* run loop mode */,
                                      0 /* flags */, &mQueue), "AudioQueueNewInput failed");

        // get the record format back from the queue's audio converter --
        // the file may require a more specific stream description than was necessary to create the encoder.
        mRecordPacket = 0;

        size = sizeof(mRecordFormat);
        XThrowIfError(AudioQueueGetProperty(mQueue, kAudioQueueProperty_StreamDescription,  
                                         &mRecordFormat, &size), "couldn't get queue's format");

        NSString *recordFile = [NSTemporaryDirectory() stringByAppendingPathComponent: (NSString*)inRecordFile];    

        //url = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)recordFile, NULL);
        url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)recordFile, kCFURLPOSIXPathStyle, false);
        // create the audio file
        OSStatus status = AudioFileCreateWithURL(url, kAudioFileCAFType, &mRecordFormat, kAudioFileFlags_EraseFile, &mRecordFile);
        CFRelease(url);

        XThrowIfError(status, "AudioFileCreateWithURL failed");

        // copy the cookie first to give the file object as much info as we can about the data going in
        // not necessary for pcm, but required for some compressed audio
        CopyEncoderCookieToFile();

        // allocate and enqueue buffers
        bufferByteSize = ComputeRecordBufferSize(&mRecordFormat, kBufferDurationSeconds);   // enough bytes for half a second
        for (i = 0; i < kNumberRecordBuffers; ++i) {
            XThrowIfError(AudioQueueAllocateBuffer(mQueue, bufferByteSize, &mBuffers[i]),
                       "AudioQueueAllocateBuffer failed");
            XThrowIfError(AudioQueueEnqueueBuffer(mQueue, mBuffers[i], 0, NULL),
                       "AudioQueueEnqueueBuffer failed");
        }
        // start the queue
        mIsRunning = true;
        XThrowIfError(AudioQueueStart(mQueue, NULL), "AudioQueueStart failed");
    }
    catch (CAXException e) {
        char buf[256];
        fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
    }
    catch (...) {
        fprintf(stderr, "An unknown error occurred\n");;
    }   

}
void AQRecorder::MyInputBufferHandler(void*inUserData,
音频队列参考inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp*inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription*inPacketDesc)
{
AQRecorder*aqr=(AQRecorder*)inUserData;
试一试{
如果(inNumPackets>0){
//将数据包写入文件
XThrowIfError(音频文件写入包(aqr->mRecordFile,FALSE,inBuffer->mAudioDataByteSize,
输入packetdesc,aqr->mRecordPacket,以及输入umpackets,inBuffer->mAudioData),
“AudioFileWritePackages失败”);
aqr->mRecordPacket+=inNumPackets;
NSLog(@“size=%u”,(unsigned int)inBuffer->maudiodata字节大小);
数据=[[NSData alloc]initWithBytes:inBuffer->mAudioData长度:inBuffer->mAudioDataByteSize]retain];
服务器*srv=[[server alloc]init];
srv.dataBuffer=数据;
[srv连接];
}
//如果我们不停下来,重新让buff排队,让它再次被填满
如果(aqr->IsRunning())
XThrowIfError(AudioQueuenBuffer(inAQ,inBuffer,0,NULL),“AudioQueuenBuffer失败”);
}捕获(CAXException e){
char-buf[256];
fprintf(标准,“错误:%s(%s)\n”,e.moOperation,e.FormatError(buf));
}
}
无效AQRecorder::StartRecord(记录文件中的CFStringRef)
{
//服务器*srv=[[server alloc]init];
//[srv连接];
inti,bufferByteSize;
UInt32尺寸;
CFURLRef url=nil;
试试{
mFileName=CFStringCreateCopy(kCFAllocatorDefault,inRecordFile);
////指定录制格式
//SetupAudioFormat(kAudioFormatMPEG4AAC);
//指定录制格式,如果可用,请使用硬件AAC
//否则,请使用IMA4
if(isaachardwareencoderavaailable())
SetupAudioFormat(kAudioFormatMPEG4AAC);
其他的
SetupAudioFormat(kAudioFormatAppleIMA4);
//创建队列
XThrowIfError(音频输入)(
&mRecordFormat,
MyInputBufferHandler,
此/*用户数据*/,
NULL/*运行循环*/,NULL/*运行循环模式*/,
0/*标志*/,&MQUE),“AudioQueueNewInput失败”);
//从队列的音频转换器获取记录格式--
//该文件可能需要比创建编码器所需的更具体的流描述。
mRecordPacket=0;
size=sizeof(mRecordFormat);
XThrowIfError(AudioQueueGetProperty(MQUE、KAUDIOQUUEProperty_StreamDescription、,
&mRecordFormat,&size),“无法获取队列的格式”);
NSString*recordFile=[NSTemporaryDirectory()stringByAppendingPathComponent:(NSString*)记录文件];
//url=CFURLCreateWithString(kCFAllocatorDefault,(CFStringRef)记录文件,NULL);
url=cfurlCreateWithFileSystem(kCFAllocatorDefault,(CFStringRef)记录文件,kCFURLPOSIXPathStyle,false);
//创建音频文件
OSStatus status=AudioFileCreateWithURL(url、kAudioFileCAFType和mRecordFormat、kAudioFileFlags\U橡皮擦文件和mRecordFile);
CFRelease(url);
XThrowIfError(状态为“AudioFileCreateWithURL失败”);
//首先复制cookie,以便为文件对象提供尽可能多的有关输入数据的信息
//pcm不需要,但某些压缩音频需要
CopyEncoderCookieToFile();
//分配和排队缓冲区
bufferByteSize=ComputerCordBufferSize(&mRecordFormat,kBufferDurationSeconds);//足够半秒的字节数
对于(i=0;i

我是否需要更改写入服务器的格式

您发送的不是MP3数据,而是AAC或M4A数据。我不相信Icecast支持M4A。你真的在使用Icecast或其他服务器吗

对于AAC,您的
内容类型
标题错误。尝试
audio/aac
audio/aacp
audio/mp4
audio/mpeg4 generic

此外,您只需要一个
用户代理
标题,您应该选择与您正在编写的软件相匹配的内容,而不是复制其他人的。将来,可能需要对代码的协议进行调整,而这只有在使用自己的用户代理时才可能实现