Пример #1
0
void StreamPacketsProc(void * inClientData,
                       UInt32 inNumberBytes,
                       UInt32 inNumberPackets,
                       const void * inInputData,
                       AudioStreamPacketDescription	*inPacketDescriptions)
{
    // this is called by audio file stream when it finds packets of audio
    struct audioPlayer* player = (struct audioPlayer*)inClientData;

    // the following code assumes we're streaming VBR data. for CBR data, you'd need another code branch here.


    for (int i = 0; i < inNumberPackets; ++i) {
        SInt64 packetOffset = inPacketDescriptions[i].mStartOffset;
        SInt64 packetSize   = inPacketDescriptions[i].mDataByteSize;

        player->processedPacketsSize += packetSize;
        player->processedPacketCount += 1;

        // if the space remaining in the buffer is not enough for this packet, then enqueue the buffer.
        size_t bufSpaceRemaining = kAQBufSize - player->bytesFilled;
        if (bufSpaceRemaining < packetSize) {
            EnqueueBuffer(player);
            WaitForFreeBuffer(player);
        }

        // copy data to the audio queue buffer
        AudioQueueBufferRef fillBuf = player->audioQueueBuffer[player->fillBufferIndex];
        memcpy((char*)fillBuf->mAudioData + player->bytesFilled, (const char*)inInputData + packetOffset, packetSize);
        // fill out packet description
        player->packetDescs[player->packetsFilled] = inPacketDescriptions[i];
        player->packetDescs[player->packetsFilled].mStartOffset = player->bytesFilled;
        // keep track of bytes filled and packets filled
        player->bytesFilled += packetSize;
        player->packetsFilled += 1;
        player->dSongPlayed += player->packetDuration * 1000.0f;
        player->songPlayed = (unsigned long int)player->dSongPlayed;
        // if that was the last free packet description, then enqueue the buffer.
        size_t packetsDescsRemaining = kAQMaxPacketDescs - player->packetsFilled;
        if (packetsDescsRemaining == 0) {
            EnqueueBuffer(player);
            WaitForFreeBuffer(player);
        }
    }
}
void MyPacketsProc(				void *							inClientData,
								UInt32							inNumberBytes,
								UInt32							inNumberPackets,
								const void *					inInputData,
								AudioStreamPacketDescription	*inPacketDescriptions)
{
	// this is called by audio file stream when it finds packets of audio
	MyData* myData = (MyData*)inClientData;
	printf("got data.  bytes: %d  packets: %d\n", inNumberBytes, inNumberPackets);

	// the following code assumes we're streaming VBR data. for CBR data, you'd need another code branch here.

	for (int i = 0; i < inNumberPackets; ++i) {
		SInt64 packetOffset = inPacketDescriptions[i].mStartOffset;
		SInt64 packetSize   = inPacketDescriptions[i].mDataByteSize;
		
		// if the space remaining in the buffer is not enough for this packet, then enqueue the buffer.
		size_t bufSpaceRemaining = kAQBufSize - myData->bytesFilled;
		if (bufSpaceRemaining < packetSize) {
			MyEnqueueBuffer(myData);
			WaitForFreeBuffer(myData);
		}
		
		// copy data to the audio queue buffer
		AudioQueueBufferRef fillBuf = myData->audioQueueBuffer[myData->fillBufferIndex];
		memcpy((char*)fillBuf->mAudioData + myData->bytesFilled, (const char*)inInputData + packetOffset, packetSize);
		// fill out packet description
		myData->packetDescs[myData->packetsFilled] = inPacketDescriptions[i];
		myData->packetDescs[myData->packetsFilled].mStartOffset = myData->bytesFilled;
		// keep track of bytes filled and packets filled
		myData->bytesFilled += packetSize;
		myData->packetsFilled += 1;
		
		// if that was the last free packet description, then enqueue the buffer.
		size_t packetsDescsRemaining = kAQMaxPacketDescs - myData->packetsFilled;
		if (packetsDescsRemaining == 0) {
			MyEnqueueBuffer(myData);
			WaitForFreeBuffer(myData);
		}
	}	
}