/** Interleaved playback function */ int sa_stream_write(sa_stream_t *s, const void *data, size_t nbytes) { int status = SA_SUCCESS; ERROR_IF_NO_INIT(s); status = writeAudio(s, (LPSTR)data, nbytes); return status; }
/** * @name Play MP3 * Questa funzione si occupa di riprodurre un frame Audio */ int playMp3(){ /* decode and play */ if (isPaused==0 && mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK) { writeAudio((char*)buffer,done); } else { if (isPaused==0) return 2; } return 0; }
DWORD WINAPI receiveMicThread(LPVOID iValue) { char micBuffer[BLOCK_SIZE]; int remote_len; char * play_byte = "1"; prepareMicPlay(); remote_len = sizeof(udp_remote); mic_play_beg(); for (;;) { if (1 != 1) { continue; } recvfrom(ci.udpSocket, micBuffer, BLOCK_SIZE, 0, (struct sockaddr *)&udp_remote, &remote_len); writeAudio(micBuffer, sizeof(micBuffer)); Sleep(225); } }
//_______________________________________________ uint8_t ADM_ogmWrite::save(const char *name) { uint8_t *bufr; uint32_t len,flags; uint8_t error=0; _fd=fopen(name,"wb"); if(!_fd) { GUI_Error_HIG(QT_TR_NOOP("File error"), QT_TR_NOOP("Cannot open \"%s\" for writing."), name); return 0; } videoStream=new ogm_page(_fd,1); encoding_gui=new DIA_encoding(25000); encoding_gui->setContainer("OGM"); //______________ Write headers..._____________________ if(!initVideo(name)) { fclose(_fd); _fd=NULL; GUI_Error_HIG(QT_TR_NOOP("Could not initialize video"), NULL); return 0; } if(!initAudio()) { fclose(_fd); _fd=NULL; GUI_Error_HIG(QT_TR_NOOP("Could not initialize audio"), NULL); return 0; } encoding_gui->setFps(_fps1000); encoding_gui->reset(); // ___________________Then body_______________________ uint32_t j=0; for( j=0;j<_togo && !error;j++) { if(!encoding_gui->isAlive()) { error=1; continue; } if(!writeVideo(j)) error=1; if(!writeAudio(j)) error=1; } if(abs(j-_togo)<3 && error) error=0; // might be caused by late B frame delete encoding_gui; encoding_gui=NULL; //________________ Flush______________________ videoStream->flush(); endAudio(); //deleteAudioFilters(); // ____________Close____________________ fclose(_fd); _fd=NULL; return !error; }
void Q3E_GetAudio(void) { int offset = (dmapos * bytes_per_sample) & (buf_size - 1); writeAudio(offset, chunkSizeBytes); dmapos+=dma.submission_chunk; }
void UDPread(UDPinfo *UI, int filesize) { WSABUF UDPread; int bytesRecv = 0; DWORD flags = 0; int err; WAVEHDR* waveBlocks; FILE *fp; WAVEFORMATEX wfx; wavheader WH; HWAVEOUT hWaveOut; char data[BUFSIZE]; int i = 0; UDPread.buf = (char *) malloc (BUFSIZE); UDPread.len = BUFSIZE; waveBlocks = allocateBlocks(BLOCK_SIZE, BLOCK_COUNT); waveFreeBlockCount = BLOCK_COUNT; waveCurrentBlock = 0; InitializeCriticalSection(&waveCriticalSection); fp = wavOpen(&WH); wfx.nSamplesPerSec = WH.sampleRate; wfx.wBitsPerSample = 16; wfx.nChannels = 2; wfx.cbSize = 0; wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.nBlockAlign = (wfx.wBitsPerSample * wfx.nChannels) >> 3; wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec; if((err = waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx,(DWORD_PTR)waveOutProc, (DWORD_PTR)&waveFreeBlockCount, CALLBACK_FUNCTION)) != MMSYSERR_NOERROR) { fprintf(stdout, "unable to open wave mapper device"); fprintf(stdout, "%d", err); ExitProcess(1); } while(bytesRecv < filesize) { if(recvfrom(UI->sd, UDPread.buf, BUFSIZE, flags,NULL,NULL) == SOCKET_ERROR) { err = WSAGetLastError(); if(err != ERROR_IO_PENDING) { // error during read } } memcpy(data,UDPread.buf, BUFSIZE); writeAudio(hWaveOut, data, BUFSIZE, waveBlocks, waveCriticalSection); bytesRecv += BUFSIZE; } /* * unprepare any blocks that are still prepared */ for(i = 0; i < waveFreeBlockCount; i++) { if(waveBlocks[i].dwFlags & WHDR_PREPARED) { waveOutUnprepareHeader(hWaveOut, &waveBlocks[i], sizeof(WAVEHDR)); } } DeleteCriticalSection(&waveCriticalSection); freeBlocks(waveBlocks); waveOutClose(hWaveOut); }
/** \fn runH264 \brief Index H264 stream */ bool TsIndexer::runH264(const char *file,ADM_TS_TRACK *videoTrac) { bool seq_found=false; bool firstSps=true; TSVideo video; indexerData data; dmxPacketInfo tmpInfo; TS_PESpacket SEI_nal(0); bool result=false; bool bAppend=false; beginConsuming=0; listOfUnits.clear(); printf("Starting H264 indexer\n"); if(!videoTrac) return false; if(videoTrac[0].trackType!=ADM_TS_H264) { printf("[Ts Indexer] Only H264 video supported\n"); return false; } video.pid=videoTrac[0].trackPid; memset(&data,0,sizeof(data)); data.picStructure=pictureFrame; string indexName=string(file); indexName=indexName+string(".idx2"); index=qfopen(indexName,(const char*)"wt"); if(!index) { printf("[PsIndex] Cannot create %s\n",indexName.c_str()); return false; } pkt=new tsPacketLinearTracker(videoTrac->trackPid, audioTracks); FP_TYPE append=FP_DONT_APPEND; if(true==ADM_probeSequencedFile(file)) { if(true==GUI_Question("There are several files with sequential file names. Should they be all loaded ?")) bAppend=true; } if(bAppend==true) append=FP_APPEND; writeSystem(file,bAppend); pkt->open(file,append); data.pkt=pkt; fullSize=pkt->getSize(); gui=createProcessing("Indexing",pkt->getSize()); int lastRefIdc=0; bool keepRunning=true; //****************** // 1 search SPS //****************** while(keepRunning) { int startCode=pkt->findStartCode(); if(startCode&0x80) continue; // Marker missing startCode&=0x1f; if(startCode!=NAL_SPS) continue; // Got SPS! uint32_t xA,xR; // Get info pkt->getInfo(&tmpInfo); // Read just enough... { SEI_nal.empty(); uint32_t code=0xffff+0xffff0000; while(((code&0xffffff)!=1) && pkt->stillOk()) { uint8_t r=pkt->readi8(); code=(code<<8)+r; SEI_nal.pushByte(r); } if(!pkt->stillOk()) break;; pkt->seek(tmpInfo.startAt,tmpInfo.offset-5); if (extractSPSInfo(SEI_nal.payload, SEI_nal.payloadSize-4,&spsInfo)) { ADM_info("[TsIndexer] Found video %"PRIu32"x%"PRIu32", fps=%"PRIu32"\n",video.w,video.h,video.fps); ADM_info("[TsIndexer] SPS says %"PRIu32"x%"PRIu32"\n",spsInfo.width,spsInfo.height); seq_found=1; video.w=spsInfo.width; video.h=spsInfo.height; video.fps=spsInfo.fps1000; xA=spsInfo.darNum; xR=spsInfo.darDen; writeVideo(&video,ADM_TS_H264); writeAudio(); qfprintf(index,"[Data]"); // Rewind break; }; } } if(!seq_found) goto the_end; decodingImage=false; //****************** // 2 Index //****************** bool fourBytes; while(keepRunning) { fourBytes=false; int startCode=pkt->findStartCode2(fourBytes); resume: if(!pkt->stillOk()) break; int startCodeLength=4; if(fourBytes==true) startCodeLength++; // 1:0 2:Nal ref idc 5:Nal Type if(startCode&0x80) { printf("[Ts] Nal Marker missing:%x\n",startCode); continue; // Marker missing } int fullStartCode=startCode; int ref=(startCode>>5)&3; startCode&=0x1f; // Ignore nal ref IDR aprintf("[%02x] Nal :0x%x,ref=%d,lastRef=%d at : %d \n",fullStartCode,startCode,ref,lastRefIdc,pkt->getConsumed()-beginConsuming); // Ignore multiple chunk of the same pic if((startCode==NAL_NON_IDR || startCode==NAL_IDR)&&decodingImage ) { aprintf("Still capturing, ignore\n"); continue; } switch(startCode) { case NAL_AU_DELIMITER: { aprintf("AU DELIMITER\n"); decodingImage = false; } break; case NAL_SEI: { // Load the whole NAL SEI_nal.empty(); uint32_t code=0xffff+0xffff0000; while(((0xffffff&code)!=1) && pkt->stillOk()) { uint8_t r=pkt->readi8(); code=(code<<8)+r; SEI_nal.pushByte(r); } if(!pkt->stillOk()) goto resume; aprintf("[SEI] Nal size :%d\n",SEI_nal.payloadSize); if(SEI_nal.payloadSize>=7) decodeSEI(SEI_nal.payloadSize-4, SEI_nal.payload,&(thisUnit.recoveryCount),&(thisUnit.imageStructure)); else printf("[SEI] Too short size+4=%d\n",*(SEI_nal.payload)); startCode=pkt->readi8(); decodingImage=false; pkt->getInfo(&thisUnit.packetInfo); thisUnit.consumedSoFar=pkt->getConsumed(); if(!addUnit(data,unitTypeSei,thisUnit,startCodeLength+SEI_nal.payloadSize+1)) keepRunning=false; fourBytes=true; goto resume; } break; case NAL_SPS: decodingImage=false; pkt->getInfo(&thisUnit.packetInfo); if(firstSps) { pkt->setConsumed(startCodeLength); // reset consume counter firstSps=false; } thisUnit.consumedSoFar=pkt->getConsumed(); if(!addUnit(data,unitTypeSps,thisUnit,startCodeLength)) keepRunning=false; break; case NAL_IDR: case NAL_NON_IDR: { #define NON_IDR_PRE_READ 8 aprintf("Pic start last ref:%d cur ref:%d nb=%d\n",lastRefIdc,ref,data.nbPics); lastRefIdc=ref; uint8_t bufr[NON_IDR_PRE_READ+4]; uint8_t header[NON_IDR_PRE_READ+4]; pkt->read(NON_IDR_PRE_READ,bufr); // unescape... ADM_unescapeH264(NON_IDR_PRE_READ,bufr,header); // getBits bits(NON_IDR_PRE_READ,header); int first_mb_in_slice,slice_type; first_mb_in_slice= bits.getUEG(); slice_type= bits.getUEG31(); if(slice_type>9) { printf("[TsIndexer] Bad slice type\n"); } if(slice_type>4) slice_type-=5; switch(slice_type) { case 0 : thisUnit.imageType=2;break; // P case 1 : thisUnit.imageType=3;break; // B case 2 : thisUnit.imageType=1;break; // I default : thisUnit.imageType=2;break; // SP/SI } if(startCode==NAL_IDR) thisUnit.imageType=4; // IDR aprintf("[>>>>>>>>] Pic Type %"PRIu32" Recovery %"PRIu32"\n",thisUnit.imageType,recoveryCount); if(thisUnit.imageType==1 && !thisUnit.recoveryCount) thisUnit.imageType=4; //I + Recovery=0 = IDR! data.nbPics++; decodingImage=true; pkt->getInfo(&thisUnit.packetInfo); thisUnit.consumedSoFar=pkt->getConsumed(); if(!addUnit(data,unitTypePic,thisUnit,startCodeLength+NON_IDR_PRE_READ)) keepRunning=false; // reset to default thisUnit.imageStructure=pictureFrame; thisUnit.recoveryCount=0xff; pkt->invalidatePtsDts(); } break; default: break; } } // End while result=true; the_end: printf("\n"); qfprintf(index,"\n[End]\n"); qfclose(index); index=NULL; audioTracks=NULL; delete pkt; pkt=NULL; return result; }
/** \fn runVC1 \brief Index VC1 stream */ bool TsIndexer::runVC1(const char *file,ADM_TS_TRACK *videoTrac) { uint32_t temporal_ref,val; uint8_t buffer[50*1024]; bool seq_found=false; TSVideo video; indexerData data; dmxPacketInfo info; beginConsuming=0; listOfUnits.clear(); if(!videoTrac) return false; if(videoTrac[0].trackType!=ADM_TS_VC1) { printf("[Ts Indexer] Only VC1 video supported\n"); return false; } video.pid=videoTrac[0].trackPid; memset(&data,0,sizeof(data)); data.picStructure=pictureFrame; string indexName=string(file); indexName=indexName+string(".idx2"); index=qfopen(indexName,"wt"); if(!index) { printf("[PsIndex] Cannot create %s\n",indexName.c_str()); return false; } writeSystem(file,false); pkt=new tsPacketLinearTracker(videoTrac->trackPid, audioTracks); FP_TYPE append=FP_APPEND; pkt->open(file,append); data.pkt=pkt; fullSize=pkt->getSize(); int startCode; decodingImage=false; #define likely(x) x #define unlikely(x) x while(1) { startCode=pkt->findStartCode(); if(!pkt->stillOk()) break; switch(startCode) { case 0x0f: // sequence start if(seq_found) { pkt->getInfo(&thisUnit.packetInfo); thisUnit.consumedSoFar=pkt->getConsumed(); addUnit(data,unitTypeSps,thisUnit,4); decodingImage=false; break; } // Verify it is high/advanced profile { int seqSize=0; tsGetBits bits(pkt); if(!bits.peekBits(1)) continue; // simple/main profile if(!decodeVC1Seq(bits,video)) continue; seqSize=bits.getConsumed(); video.extraDataLength=seqSize+4+1; memcpy(video.extraData+4,bits.data,seqSize); // Add info so that ffmpeg is happy video.extraData[0]=0; video.extraData[1]=0; video.extraData[2]=1; video.extraData[3]=0xf; video.extraData[seqSize+4+0]=0; seq_found=1; // Hi Profile printf("[VC1] Found seq start with %d x %d video\n",(int)video.w,(int)video.h); printf("[VC1] FPS : %d\n",(int)video.fps); printf("[VC1] sequence header is %d bytes\n",(int)seqSize); writeVideo(&video,ADM_TS_VC1); writeAudio(); qfprintf(index,"[Data]"); pkt->getInfo(&thisUnit.packetInfo); thisUnit.consumedSoFar=pkt->getConsumed(); addUnit(data,unitTypeSps,thisUnit,seqSize+4); decodingImage=false; continue; } break; case 0x0D: // Picture start { int type; uint8_t buffer[4]; uint32_t fType,sType; if(!seq_found) { continue; printf("[TsIndexer]No sequence start yet, skipping..\n"); } pkt->getInfo(&thisUnit.packetInfo); thisUnit.consumedSoFar=pkt->getConsumed(); tsGetBits bits(pkt); if(!decodeVC1Pic(bits,fType,sType)) continue; thisUnit.imageType=fType; updatePicStructure(video,sType); addUnit(data,unitTypePic,thisUnit,4); decodingImage=true; data.nbPics++; } break; default: break; } } printf("\n"); // Mark(&data,&info,2); qfprintf(index,"\n[End]\n"); qfprintf(index,"\n# Found %"PRIu32" images \n",data.nbPics); // Size qfprintf(index,"# Found %"PRIu32" frame pictures\n",video.frameCount); // Size qfprintf(index,"# Found %"PRIu32" field pictures\n",video.fieldCount); // Size qfclose(index); index=NULL; audioTracks=NULL; delete pkt; pkt=NULL; return 1; }
/*----------------------------------------------------------------------------- -- FUNCTION: receiveStream -- -- DATE: 2009-04-06 -- -- REVISIONS: 2009-04-06 - Jaymz, Took out the TCP connection stuff since -- we already have that at this point. Also added -- a parameter WPARAM sd, which is the socket -- from which we are receiving the data. -- - Jaymz, Miscellaneous code touch-ups (mainly -- formatting and removing of test printf()'s) -- -- DESIGNER(S): David Overton -- PROGRAMMER(S): David Overton, Jaymz Boilard, Steffen L. Norgren -- -- INTERFACE: receiveStream(LPVOID iValue) -- -- RETURNS: void -- -- NOTES: The main function to receive a UDP stream of data and process -- that information. -----------------------------------------------------------------------------*/ DWORD WINAPI receiveStream(LPVOID iValue) { WAVEFORMATEX wfx; char buffer[BLOCK_SIZE]; /* intermediate buffer for reading */ int i, n, remote_len; DWORD outBytes = 0; char * play_byte = "1"; BOOL firstRun = TRUE; remote_len = sizeof(udp_remote); /* initialise the module variables */ waveBlocks = allocateBlocks(BLOCK_SIZE, BLOCK_COUNT); waveFreeBlockCount = BLOCK_COUNT; waveCurrentBlock = 0; InitializeCriticalSection(&waveCriticalSection); /* playback loop - read from socket */ while (TRUE) { if (ci.request != MULTI_STREAM) { /* send play signal */ sendto(ci.udpSocket, play_byte, sizeof(play_byte), 0, (struct sockaddr *)&udp_remote, remote_len); } if ((n = recvfrom(ci.udpSocket, buffer, sizeof(buffer), 0, (struct sockaddr *)&udp_remote, &remote_len)) <= 0) { waveOutClose(hWaveOut); ExitThread(0); } /* first 4 bytes in a file, so set the header information */ if(strncmp(buffer, "RIFF", 4) == 0) { memcpy(&wfx, buffer+20, sizeof(wfx)); if (ci.request != MULTI_STREAM || firstRun == TRUE) { waveOutClose(hWaveOut); if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, (DWORD_PTR)waveOutProc, (DWORD_PTR)&waveFreeBlockCount, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) { MessageBox(NULL, "Unable to open mapper device.", "Error", MB_OK); ExitProcess(1); } firstRun = FALSE; } } if(n == 0) break; else if(n < sizeof(buffer) && n != WAVE_HEAD_SIZE) { memset(buffer + n, 0, sizeof(buffer) - n); writeAudio(buffer, n); break; } writeAudio(buffer, n); } /* wait for all blocks to complete */ while(waveFreeBlockCount < BLOCK_COUNT) Sleep(10); /* unprepare any blocks that are still prepared */ for(i = 0; i < waveFreeBlockCount; i++) { if(waveBlocks[i].dwFlags & WHDR_PREPARED) waveOutUnprepareHeader(hWaveOut, &waveBlocks[i], sizeof(WAVEHDR)); } DeleteCriticalSection(&waveCriticalSection); freeBlocks(waveBlocks); waveOutClose(hWaveOut); streamInProgress = FALSE; ExitThread(0); }
void Output::run() { mutex()->lock (); if (!bytes_per_millisecond_) { qWarning("Output: invalid audio parameters"); mutex()->unlock (); return; } mutex()->unlock (); bool done = false; Buffer *b = 0; qint64 l, m = 0; dispatch(PlayerUtils::Playing); while (!done) { mutex()->lock(); recycler()->mutex()->lock(); done = user_stop_; while (!done && (recycler()->empty() || pause_)) { mutex()->unlock(); recycler()->cond()->wakeOne(); recycler()->cond()->wait(recycler()->mutex()); mutex()->lock (); done = user_stop_; } status(); if (!b) { b = recycler()->next(); if (b && b->rate) { kbps_ = b->rate; } } recycler()->cond()->wakeOne(); recycler()->mutex()->unlock(); mutex()->unlock(); if (b) { changeVolume(b->data, b->nbytes, channels_); l = 0; m = 0; if (is_seeking_) { enable(b->seeking_finished); is_seeking_ = !b->seeking_finished; } while (l < b->nbytes) { m = writeAudio(b->data + l, b->nbytes - l); if(m >= 0) { total_written_ += m; l+= m; } else { break; } } if(m < 0) { break; } } mutex()->lock(); //force buffer change recycler()->mutex()->lock (); recycler()->done(); recycler()->mutex()->unlock(); b = 0; mutex()->unlock(); } mutex()->lock (); //write remaining data if(finish_) { flush(); qDebug("Output: total written %lld", total_written_); } dispatch(PlayerUtils::Stopped); mutex()->unlock(); }