Recorder.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. // Recorder.cpp : Defines the entry point for the console application.
  2. //
  3. #include <exception>
  4. #include <iostream>
  5. extern "C" {
  6. #include "common.h"
  7. }
  8. #include "headers_ffmpeg.h"
  9. void capture_screen()
  10. {
  11. try {
  12. ffmpeg_register_all();
  13. ffmpeg_register_devices();
  14. AVFormatContext *pFormatCtx = avformat_alloc_context();
  15. const AVInputFormat *ifmt = av_find_input_format("gdigrab");
  16. avformat_open_input(&pFormatCtx, "desktop", ifmt, NULL);
  17. if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
  18. throw std::exception("could not find stream information");
  19. }
  20. int videoIndex = -1;
  21. for (int i = 0; i < pFormatCtx->nb_streams; i++) {
  22. if (ffmpeg_get_codec_type(pFormatCtx->streams[i]) == AVMEDIA_TYPE_VIDEO) {
  23. videoIndex = i;
  24. break;
  25. }
  26. }
  27. if (videoIndex == -1)
  28. throw std::exception("could not find a video stream");
  29. AVCodecContext *pDecoderCtx = ffmpeg_get_codec_context(pFormatCtx->streams[videoIndex]);
  30. //init decoder
  31. AVCodec *pDecoder = avcodec_find_decoder(pDecoderCtx->codec_id);
  32. if (pDecoder == NULL)
  33. throw std::exception("could not find codec");
  34. if (avcodec_open2(pDecoderCtx, pDecoder, NULL) != 0) {
  35. throw std::exception("open codec ffailed");
  36. }
  37. //init transfer and buffer
  38. AVFrame *pFrameRGB, *pFrameYUV;
  39. pFrameRGB = av_frame_alloc();
  40. pFrameYUV = av_frame_alloc();
  41. uint8_t *out_buffer = (uint8_t *) av_malloc(
  42. avpicture_get_size(AV_PIX_FMT_YUV420P, pDecoderCtx->width, pDecoderCtx->height));
  43. avpicture_fill((AVPicture *) pFrameYUV,
  44. out_buffer,
  45. AV_PIX_FMT_YUV420P,
  46. pDecoderCtx->width,
  47. pDecoderCtx->height);
  48. int ret, gotPic = 0;
  49. AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));
  50. struct SwsContext *imgConvertCtx = NULL;
  51. imgConvertCtx = sws_getContext(pDecoderCtx->width,
  52. pDecoderCtx->height,
  53. pDecoderCtx->pix_fmt,
  54. pDecoderCtx->width,
  55. pDecoderCtx->height,
  56. AV_PIX_FMT_YUV420P,
  57. SWS_BICUBIC,
  58. NULL,
  59. NULL,
  60. NULL);
  61. //init encoder
  62. AVCodec *pEncoder = avcodec_find_encoder(AV_CODEC_ID_H264);
  63. if (pEncoder == NULL)
  64. throw std::exception("could not find encoder");
  65. AVCodecContext *pEncoderCtx = avcodec_alloc_context3(pEncoder);
  66. if (pEncoderCtx == NULL)
  67. throw std::exception("could not alloc context for encoder");
  68. pEncoderCtx->codec_id = AV_CODEC_ID_H264;
  69. pEncoderCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  70. pEncoderCtx->pix_fmt = AV_PIX_FMT_YUV420P;
  71. pEncoderCtx->width = pDecoderCtx->width;
  72. pEncoderCtx->height = pDecoderCtx->height;
  73. pEncoderCtx->time_base.num = 1;
  74. pEncoderCtx->time_base.den = 20; //֡��(��һ���Ӷ�����ͼƬ)
  75. pEncoderCtx->bit_rate = 4000000; //������(���������С���Ըı�������Ƶ������)
  76. pEncoderCtx->gop_size = 12;
  77. if (pEncoderCtx->flags & AVFMT_GLOBALHEADER)
  78. pEncoderCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  79. AVDictionary *param = 0;
  80. av_dict_set(&param, "preset", "superfast", 0);
  81. av_dict_set(&param, "tune", "zerolatency", 0);
  82. pEncoder = avcodec_find_encoder(pEncoderCtx->codec_id);
  83. if (pEncoder == NULL)
  84. throw std::exception("could not find encoder by context");
  85. if (avcodec_open2(pEncoderCtx, pEncoder, &param) != 0)
  86. throw std::exception("open encoder failed");
  87. AVFrame *pFrame264 = av_frame_alloc();
  88. int frame264Size = avpicture_get_size(pEncoderCtx->pix_fmt,
  89. pEncoderCtx->width,
  90. pEncoderCtx->height);
  91. uint8_t *frame264Buf = (uint8_t *) av_malloc(frame264Size);
  92. avpicture_fill((AVPicture *) pFrame264,
  93. frame264Buf,
  94. pEncoderCtx->pix_fmt,
  95. pEncoderCtx->width,
  96. pEncoderCtx->height);
  97. AVPacket pkt264;
  98. av_new_packet(&pkt264, frame264Size);
  99. int Y_size = pEncoderCtx->width * pEncoderCtx->height;
  100. for (;;) {
  101. if (av_read_frame(pFormatCtx, packet) < 0) {
  102. break;
  103. }
  104. if (packet->stream_index == videoIndex) {
  105. ret = avcodec_decode_video2(pDecoderCtx, pFrameRGB, &gotPic, packet);
  106. if (ret < 0) {
  107. throw std::exception("decode error");
  108. }
  109. //trans rgb to yuv420 in out_buffer
  110. if (gotPic) {
  111. sws_scale(imgConvertCtx,
  112. (const uint8_t *const *) pFrameRGB->data,
  113. pFrameRGB->linesize,
  114. 0,
  115. pDecoderCtx->height,
  116. pFrameYUV->data,
  117. pFrameYUV->linesize);
  118. gotPic = 0;
  119. pFrame264->data[0] = out_buffer; //Y
  120. pFrame264->data[1] = out_buffer + Y_size; //U
  121. pFrame264->data[2] = out_buffer + Y_size * 5 / 4; //V
  122. ret = avcodec_encode_video2(pEncoderCtx, &pkt264, pFrame264, &gotPic);
  123. if (gotPic == 1) {
  124. printf("264 packet:%d\r\n", pkt264.size);
  125. static FILE *fp = fopen("a.264", "wb+");
  126. fwrite(pkt264.data, 1, pkt264.size, fp);
  127. av_free_packet(&pkt264);
  128. }
  129. }
  130. // 移除固定睡眠以降低延迟,依靠事件/时间戳节奏
  131. }
  132. av_free_packet(packet);
  133. } //for(;;��
  134. av_free(pFrameYUV);
  135. avcodec_close(pDecoderCtx);
  136. avformat_close_input(&pFormatCtx);
  137. } catch (std::exception ex) {
  138. printf("%s\r\n", ex.what());
  139. }
  140. }
  141. #include <AudioClient.h>
  142. #include <AudioPolicy.h>
  143. #include <MMDeviceAPI.h>
  144. #define REFTIMES_PER_SEC 10000000
  145. #define REFTIMES_PER_MILLISEC 10000
  146. #define EXIT_ON_ERROR(hres) \
  147. if (FAILED(hres)) { \
  148. goto Exit; \
  149. }
  150. #define SAFE_RELEASE(punk) \
  151. if ((punk) != NULL) { \
  152. (punk)->Release(); \
  153. (punk) = NULL; \
  154. }
  155. const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  156. const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  157. const IID IID_IAudioClient = __uuidof(IAudioClient);
  158. const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
  159. #define MoveMemory RtlMoveMemory
  160. #define CopyMemory RtlCopyMemory
  161. #define FillMemory RtlFillMemory
  162. #define ZeroMemory RtlZeroMemory
  163. #define min(a, b) (((a) < (b)) ? (a) : (b))
  164. //
  165. // WAV file writer.
  166. //
  167. // This is a VERY simple .WAV file writer.
  168. //
  169. //
  170. // A wave file consists of:
  171. //
  172. // RIFF header: 8 bytes consisting of the signature "RIFF" followed by a 4 byte file length.
  173. // WAVE header: 4 bytes consisting of the signature "WAVE".
  174. // fmt header: 4 bytes consisting of the signature "fmt " followed by a WAVEFORMATEX
  175. // WAVEFORMAT: <n> bytes containing a waveformat structure.
  176. // DATA header: 8 bytes consisting of the signature "data" followed by a 4 byte file length.
  177. // wave data: <m> bytes containing wave data.
  178. //
  179. //
  180. // Header for a WAV file - we define a structure describing the first few fields in the header for convenience.
  181. //
  182. struct WAVEHEADER
  183. {
  184. DWORD dwRiff; // "RIFF"
  185. DWORD dwSize; // Size
  186. DWORD dwWave; // "WAVE"
  187. DWORD dwFmt; // "fmt "
  188. DWORD dwFmtSize; // Wave Format Size
  189. };
  190. // Static RIFF header, we'll append the format to it.
  191. const BYTE WaveHeader[] = {'R', 'I', 'F', 'F', 0x00, 0x00, 0x00, 0x00, 'W', 'A',
  192. 'V', 'E', 'f', 'm', 't', ' ', 0x00, 0x00, 0x00, 0x00};
  193. // Static wave DATA tag.
  194. const BYTE WaveData[] = {'d', 'a', 't', 'a'};
  195. //
  196. // Write the contents of a WAV file. We take as input the data to write and the format of that data.
  197. //
  198. bool WriteWaveFile(HANDLE FileHandle,
  199. const BYTE *Buffer,
  200. const size_t BufferSize,
  201. const WAVEFORMATEX *WaveFormat)
  202. {
  203. DWORD waveFileSize = sizeof(WAVEHEADER) + sizeof(WAVEFORMATEX) + WaveFormat->cbSize
  204. + sizeof(WaveData) + sizeof(DWORD) + static_cast<DWORD>(BufferSize);
  205. BYTE *waveFileData = new (std::nothrow) BYTE[waveFileSize];
  206. BYTE *waveFilePointer = waveFileData;
  207. WAVEHEADER *waveHeader = reinterpret_cast<WAVEHEADER *>(waveFileData);
  208. if (waveFileData == NULL) {
  209. printf("Unable to allocate %d bytes to hold output wave data\n", waveFileSize);
  210. return false;
  211. }
  212. //
  213. // Copy in the wave header - we'll fix up the lengths later.
  214. //
  215. CopyMemory(waveFilePointer, WaveHeader, sizeof(WaveHeader));
  216. waveFilePointer += sizeof(WaveHeader);
  217. //
  218. // Update the sizes in the header.
  219. //
  220. waveHeader->dwSize = waveFileSize - (2 * sizeof(DWORD));
  221. waveHeader->dwFmtSize = sizeof(WAVEFORMATEX) + WaveFormat->cbSize;
  222. //
  223. // Next copy in the WaveFormatex structure.
  224. //
  225. CopyMemory(waveFilePointer, WaveFormat, sizeof(WAVEFORMATEX) + WaveFormat->cbSize);
  226. waveFilePointer += sizeof(WAVEFORMATEX) + WaveFormat->cbSize;
  227. //
  228. // Then the data header.
  229. //
  230. CopyMemory(waveFilePointer, WaveData, sizeof(WaveData));
  231. waveFilePointer += sizeof(WaveData);
  232. *(reinterpret_cast<DWORD *>(waveFilePointer)) = static_cast<DWORD>(BufferSize);
  233. waveFilePointer += sizeof(DWORD);
  234. //
  235. // And finally copy in the audio data.
  236. //
  237. CopyMemory(waveFilePointer, Buffer, BufferSize);
  238. //
  239. // Last but not least, write the data to the file.
  240. //
  241. DWORD bytesWritten;
  242. if (!WriteFile(FileHandle, waveFileData, waveFileSize, &bytesWritten, NULL)) {
  243. printf("Unable to write wave file: %d\n", GetLastError());
  244. delete[] waveFileData;
  245. return false;
  246. }
  247. if (bytesWritten != waveFileSize) {
  248. printf("Failed to write entire wave file\n");
  249. delete[] waveFileData;
  250. return false;
  251. }
  252. delete[] waveFileData;
  253. return true;
  254. }
  255. //
  256. // Write the captured wave data to an output file so that it can be examined later.
  257. //
  258. void SaveWaveData(BYTE *CaptureBuffer, size_t BufferSize, const WAVEFORMATEX *WaveFormat)
  259. {
  260. HRESULT hr = NOERROR;
  261. SYSTEMTIME st;
  262. GetLocalTime(&st);
  263. char waveFileName[_MAX_PATH] = {0};
  264. sprintf(waveFileName,
  265. ".\\WAS_%04d-%02d-%02d_%02d_%02d_%02d_%02d.wav",
  266. st.wYear,
  267. st.wMonth,
  268. st.wDay,
  269. st.wHour,
  270. st.wMinute,
  271. st.wSecond,
  272. st.wMilliseconds);
  273. HANDLE waveHandle = CreateFile(waveFileName,
  274. GENERIC_WRITE,
  275. FILE_SHARE_READ,
  276. NULL,
  277. CREATE_ALWAYS,
  278. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  279. NULL);
  280. if (waveHandle != INVALID_HANDLE_VALUE) {
  281. if (WriteWaveFile(waveHandle, CaptureBuffer, BufferSize, WaveFormat)) {
  282. printf("Successfully wrote WAVE data to %s\n", waveFileName);
  283. } else {
  284. printf("Unable to write wave file\n");
  285. }
  286. CloseHandle(waveHandle);
  287. } else {
  288. printf("Unable to open output WAV file %s: %d\n", waveFileName, GetLastError());
  289. }
  290. }
  291. static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt);
  292. static int select_sample_rate(const AVCodec *codec);
  293. BOOL AdjustFormatTo16Bits(WAVEFORMATEX *pwfx)
  294. {
  295. BOOL bRet(FALSE);
  296. if (pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
  297. pwfx->wFormatTag = WAVE_FORMAT_PCM;
  298. pwfx->wBitsPerSample = 16;
  299. pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
  300. pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
  301. bRet = TRUE;
  302. } else if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
  303. PWAVEFORMATEXTENSIBLE pEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(pwfx);
  304. if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, pEx->SubFormat)) {
  305. pEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
  306. pEx->Samples.wValidBitsPerSample = 16;
  307. pwfx->wBitsPerSample = 16;
  308. pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
  309. pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
  310. bRet = TRUE;
  311. }
  312. }
  313. return bRet;
  314. }
  315. #define DEF_CAPTURE_MIC
  316. HRESULT capture_audio()
  317. {
  318. HRESULT hr;
  319. REFERENCE_TIME hnsRequestedDuration = REFTIMES_PER_SEC;
  320. REFERENCE_TIME hnsActualDuration;
  321. UINT32 bufferFrameCount;
  322. UINT32 numFramesAvailable;
  323. IMMDeviceEnumerator *pEnumerator = NULL;
  324. IMMDevice *pDevice = NULL;
  325. IAudioClient *pAudioClient = NULL;
  326. IAudioCaptureClient *pCaptureClient = NULL;
  327. WAVEFORMATEX *pwfx = NULL;
  328. UINT32 packetLength = 0;
  329. BOOL bDone = FALSE;
  330. BYTE *pData;
  331. DWORD flags;
  332. /*CoTaskMemFree(pwfx);
  333. SAFE_RELEASE(pEnumerator)
  334. SAFE_RELEASE(pDevice)
  335. SAFE_RELEASE(pAudioClient)
  336. SAFE_RELEASE(pCaptureClient)*/
  337. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  338. hr = CoCreateInstance(CLSID_MMDeviceEnumerator,
  339. NULL,
  340. CLSCTX_ALL,
  341. IID_IMMDeviceEnumerator,
  342. (void **) &pEnumerator);
  343. EXIT_ON_ERROR(hr)
  344. hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
  345. EXIT_ON_ERROR(hr)
  346. hr = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **) &pAudioClient);
  347. EXIT_ON_ERROR(hr)
  348. hr = pAudioClient->GetMixFormat(&pwfx);
  349. EXIT_ON_ERROR(hr)
  350. //AdjustFormatTo16Bits(pwfx);
  351. hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
  352. AUDCLNT_STREAMFLAGS_LOOPBACK | AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
  353. hnsRequestedDuration,
  354. 0,
  355. pwfx,
  356. NULL);
  357. EXIT_ON_ERROR(hr)
  358. size_t persample_size = (pwfx->wBitsPerSample / 8) * pwfx->nChannels;
  359. // Get the size of the allocated buffer.
  360. hr = pAudioClient->GetBufferSize(&bufferFrameCount);
  361. EXIT_ON_ERROR(hr)
  362. hr = pAudioClient->GetService(IID_IAudioCaptureClient, (void **) &pCaptureClient);
  363. EXIT_ON_ERROR(hr)
  364. // Calculate the actual duration of the allocated buffer.
  365. hnsActualDuration = (double) REFTIMES_PER_SEC * bufferFrameCount / pwfx->nSamplesPerSec;
  366. av_register_all();
  367. AVIOContext *output_io_context = NULL;
  368. AVFormatContext *output_format_context = NULL;
  369. AVCodecContext *output_codec_context = NULL;
  370. AVStream *output_stream = NULL;
  371. AVCodec *output_codec = NULL;
  372. const char *out_file = "tdjm.aac";
  373. if (avio_open(&output_io_context, out_file, AVIO_FLAG_READ_WRITE) < 0) {
  374. printf("Failed to open output file!\n");
  375. return -1;
  376. }
  377. output_format_context = avformat_alloc_context();
  378. if (output_format_context == NULL) {
  379. return -1;
  380. }
  381. output_format_context->pb = output_io_context;
  382. output_format_context->oformat = av_guess_format(NULL, out_file, NULL);
  383. output_format_context->url = av_strdup(out_file);
  384. if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC)))
  385. return -1;
  386. int is_support = check_sample_fmt(output_codec, AV_SAMPLE_FMT_S16);
  387. int selected_sample = select_sample_rate(output_codec);
  388. output_stream = avformat_new_stream(output_format_context, NULL);
  389. if (!output_stream)
  390. return -1;
  391. output_codec_context = avcodec_alloc_context3(output_codec);
  392. if (output_codec_context == NULL)
  393. return -1;
  394. output_codec_context->channels
  395. = 2; //av_get_channel_layout_nb_channels(pEncoderCtx->channel_layout);
  396. output_codec_context->channel_layout = ffmpeg_get_default_channel_layout(2); //AV_CH_LAYOUT_STEREO;
  397. output_codec_context->sample_rate = pwfx->nSamplesPerSec; //48000
  398. output_codec_context->sample_fmt = AV_SAMPLE_FMT_FLTP;
  399. output_codec_context->bit_rate = 96000;
  400. output_codec_context->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  401. output_codec_context->time_base.den = pwfx->nSamplesPerSec;
  402. output_codec_context->time_base.num = 1;
  403. if (output_format_context->oformat->flags & AVFMT_GLOBALHEADER) {
  404. output_format_context->oformat->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  405. }
  406. if (avcodec_open2(output_codec_context, output_codec, NULL) < 0)
  407. throw std::exception("open audio encoder failed");
  408. if (avcodec_parameters_from_context(output_stream->codecpar, output_codec_context) < 0)
  409. return -1;
  410. AVFrame *resampled_frame = av_frame_alloc();
  411. resampled_frame->nb_samples = output_codec_context->frame_size;
  412. resampled_frame->channel_layout = output_codec_context->channel_layout;
  413. resampled_frame->format = output_codec_context->sample_fmt;
  414. resampled_frame->sample_rate = output_codec_context->sample_rate;
  415. int resampled_size = av_samples_get_buffer_size(NULL,
  416. output_codec_context->channels,
  417. output_codec_context->frame_size,
  418. output_codec_context->sample_fmt,
  419. 0);
  420. uint8_t *resampled_buf = (uint8_t *) av_malloc(resampled_size);
  421. if (avcodec_fill_audio_frame(resampled_frame,
  422. output_codec_context->channels,
  423. output_codec_context->sample_fmt,
  424. resampled_buf,
  425. resampled_size,
  426. 0)
  427. < 0)
  428. return -1;
  429. AVFrame *sample_frame = av_frame_alloc();
  430. sample_frame->nb_samples = 1024;
  431. sample_frame->channel_layout = ffmpeg_get_default_channel_layout(pwfx->nChannels);
  432. sample_frame->format = AV_SAMPLE_FMT_FLT;
  433. sample_frame->sample_rate = pwfx->nSamplesPerSec;
  434. int sample_size = av_samples_get_buffer_size(NULL, pwfx->nChannels, 1024, AV_SAMPLE_FMT_FLT, 0);
  435. uint8_t *sample_buf = (uint8_t *) av_malloc(sample_size);
  436. avcodec_fill_audio_frame(sample_frame,
  437. pwfx->nChannels,
  438. AV_SAMPLE_FMT_FLT,
  439. sample_buf,
  440. sample_size,
  441. 0);
  442. AVPacket pkt;
  443. HANDLE hAudioSamplesReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  444. if (hAudioSamplesReadyEvent == NULL) {
  445. printf("Unable to create samples ready event: %d.\n", GetLastError());
  446. goto Exit;
  447. }
  448. hr = pAudioClient->SetEventHandle(hAudioSamplesReadyEvent);
  449. if (FAILED(hr)) {
  450. printf("Unable to set ready event: %x.\n", hr);
  451. return false;
  452. }
  453. //init resampler
  454. SwrContext *resample_ctx = swr_alloc_set_opts(NULL,
  455. AV_CH_LAYOUT_STEREO,
  456. AV_SAMPLE_FMT_FLTP,
  457. 48000,
  458. AV_CH_LAYOUT_STEREO,
  459. AV_SAMPLE_FMT_FLT,
  460. 48000,
  461. 0,
  462. NULL);
  463. int error = swr_init(resample_ctx);
  464. if (error < 0)
  465. return false;
  466. //init FIFO buffer
  467. //start recording
  468. avformat_write_header(output_format_context, NULL);
  469. hr = pAudioClient->Start();
  470. EXIT_ON_ERROR(hr)
  471. int pcmInBuffer = 0;
  472. int copiedPcm = 0;
  473. int gotPacket = 0;
  474. int index = 0;
  475. HANDLE waitArray[3];
  476. waitArray[0] = hAudioSamplesReadyEvent;
  477. uint64_t nextptx = 0;
  478. while (bDone == false) {
  479. DWORD waitResult = WaitForMultipleObjects(1, waitArray, FALSE, INFINITE);
  480. switch (waitResult) {
  481. case WAIT_OBJECT_0 + 0: // _AudioSamplesReadyEvent
  482. hr = pCaptureClient->GetNextPacketSize(&packetLength);
  483. while (packetLength != 0) {
  484. // Get the available data in the shared buffer.
  485. hr = pCaptureClient->GetBuffer(&pData, &numFramesAvailable, &flags, NULL, NULL);
  486. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  487. pData = NULL; // Tell CopyData to write silence.
  488. }
  489. // Copy the available capture data to the audio sink.
  490. if (pData != NULL) {
  491. copiedPcm = min(sample_size - pcmInBuffer, numFramesAvailable * persample_size);
  492. if (copiedPcm > 0) {
  493. memcpy(sample_buf + pcmInBuffer, pData, copiedPcm);
  494. pcmInBuffer += copiedPcm;
  495. }
  496. if (pcmInBuffer == sample_size) { //got one frame ,encode
  497. int ret = swr_convert(resample_ctx,
  498. resampled_frame->data,
  499. output_codec_context->frame_size,
  500. (const uint8_t **) sample_frame->data,
  501. 1024);
  502. if (ret <= 0)
  503. return -1;
  504. av_init_packet(&pkt);
  505. resampled_frame->pts = nextptx;
  506. nextptx += resampled_frame->nb_samples;
  507. int error = avcodec_send_frame(output_codec_context, resampled_frame);
  508. if (error == 0) {
  509. error = avcodec_receive_packet(output_codec_context, &pkt);
  510. if (error == 0) {
  511. av_write_frame(output_format_context, &pkt);
  512. index++;
  513. printf("index:%d\r\n", index);
  514. }
  515. av_packet_unref(&pkt);
  516. }
  517. pcmInBuffer = 0;
  518. }
  519. if (numFramesAvailable * persample_size - copiedPcm > 0) {
  520. memcpy(sample_buf + pcmInBuffer,
  521. pData + copiedPcm,
  522. numFramesAvailable * persample_size - copiedPcm);
  523. pcmInBuffer += numFramesAvailable * persample_size - copiedPcm;
  524. }
  525. if (index > 2000) {
  526. printf("pcm still in buffer:%d\r\n", pcmInBuffer);
  527. bDone = true;
  528. break;
  529. }
  530. }
  531. hr = pCaptureClient->ReleaseBuffer(numFramesAvailable);
  532. EXIT_ON_ERROR(hr)
  533. hr = pCaptureClient->GetNextPacketSize(&packetLength);
  534. EXIT_ON_ERROR(hr)
  535. }
  536. break;
  537. } // end of 'switch (waitResult)'
  538. } // end of 'while (stillPlaying)'
  539. av_write_trailer(output_format_context);
  540. if (output_codec_context)
  541. avcodec_free_context(&output_codec_context);
  542. if (output_format_context) {
  543. avio_closep(&output_format_context->pb);
  544. avformat_free_context(output_format_context);
  545. }
  546. hr = pAudioClient->Stop(); // Stop recording.
  547. EXIT_ON_ERROR(hr)
  548. Exit:
  549. CoTaskMemFree(pwfx);
  550. SAFE_RELEASE(pEnumerator)
  551. SAFE_RELEASE(pDevice)
  552. SAFE_RELEASE(pAudioClient)
  553. SAFE_RELEASE(pCaptureClient)
  554. return hr;
  555. }
  556. HRESULT cpature_wave()
  557. {
  558. HRESULT hr;
  559. IMMDeviceEnumerator *pEnumerator = NULL;
  560. IMMDevice *pDevice = NULL;
  561. IAudioClient *pAudioClient = NULL;
  562. IAudioCaptureClient *pCaptureClient = NULL;
  563. WAVEFORMATEX *pwfx = NULL;
  564. REFERENCE_TIME hnsRequestedDuration = REFTIMES_PER_SEC;
  565. UINT32 bufferFrameCount;
  566. UINT32 numFramesAvailable;
  567. BYTE *pData;
  568. UINT32 packetLength = 0;
  569. DWORD flags;
  570. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  571. if (FAILED(hr)) {
  572. printf("Unable to initialize COM in thread: %x\n", hr);
  573. return hr;
  574. }
  575. hr = CoCreateInstance(CLSID_MMDeviceEnumerator,
  576. NULL,
  577. CLSCTX_ALL,
  578. IID_IMMDeviceEnumerator,
  579. (void **) &pEnumerator);
  580. EXIT_ON_ERROR(hr)
  581. #ifdef DEF_CAPTURE_MIC
  582. hr = pEnumerator->GetDefaultAudioEndpoint(eCapture, eCommunications, &pDevice);
  583. //hr = pEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, &pDevice);
  584. #else
  585. hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
  586. #endif
  587. EXIT_ON_ERROR(hr)
  588. hr = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **) &pAudioClient);
  589. EXIT_ON_ERROR(hr)
  590. hr = pAudioClient->GetMixFormat(&pwfx);
  591. EXIT_ON_ERROR(hr)
  592. AdjustFormatTo16Bits(pwfx);
  593. #ifdef DEF_CAPTURE_MIC
  594. hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
  595. AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
  596. hnsRequestedDuration,
  597. 0,
  598. pwfx,
  599. NULL);
  600. #else
  601. /*
  602. The AUDCLNT_STREAMFLAGS_LOOPBACK flag enables loopback recording.
  603. In loopback recording, the audio engine copies the audio stream
  604. that is being played by a rendering endpoint device into an audio endpoint buffer
  605. so that a WASAPI client can capture the stream.
  606. If this flag is set, the IAudioClient::Initialize method attempts to open a capture buffer on the rendering device.
  607. This flag is valid only for a rendering device
  608. and only if the Initialize call sets the ShareMode parameter to AUDCLNT_SHAREMODE_SHARED.
  609. Otherwise the Initialize call will fail.
  610. If the call succeeds,
  611. the client can call the IAudioClient::GetService method
  612. to obtain an IAudioCaptureClient interface on the rendering device.
  613. For more information, see Loopback Recording.
  614. */
  615. hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
  616. AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_LOOPBACK,
  617. hnsRequestedDuration,
  618. 0,
  619. pwfx,
  620. NULL);
  621. #endif
  622. EXIT_ON_ERROR(hr)
  623. int nFrameSize = (pwfx->wBitsPerSample / 8) * pwfx->nChannels;
  624. REFERENCE_TIME hnsStreamLatency;
  625. hr = pAudioClient->GetStreamLatency(&hnsStreamLatency);
  626. EXIT_ON_ERROR(hr)
  627. REFERENCE_TIME hnsDefaultDevicePeriod;
  628. REFERENCE_TIME hnsMinimumDevicePeriod;
  629. hr = pAudioClient->GetDevicePeriod(&hnsDefaultDevicePeriod, &hnsMinimumDevicePeriod);
  630. EXIT_ON_ERROR(hr)
  631. hr = pAudioClient->GetBufferSize(&bufferFrameCount);
  632. EXIT_ON_ERROR(hr)
  633. std::cout << std::endl << "GetBufferSize : " << bufferFrameCount << std::endl;
  634. // SetEventHandle
  635. //////////////////////////////////////////////////////////////////////////
  636. HANDLE hAudioSamplesReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  637. if (hAudioSamplesReadyEvent == NULL) {
  638. printf("Unable to create samples ready event: %d.\n", GetLastError());
  639. goto Exit;
  640. }
  641. hr = pAudioClient->SetEventHandle(hAudioSamplesReadyEvent);
  642. if (FAILED(hr)) {
  643. printf("Unable to set ready event: %x.\n", hr);
  644. return false;
  645. }
  646. //////////////////////////////////////////////////////////////////////////
  647. hr = pAudioClient->GetService(IID_IAudioCaptureClient, (void **) &pCaptureClient);
  648. EXIT_ON_ERROR(hr)
  649. hr = pAudioClient->Start(); // Start recording.
  650. EXIT_ON_ERROR(hr)
  651. printf("\nAudio Capture begin...\n\n");
  652. int nCnt = 0;
  653. size_t nCaptureBufferSize = 8 * 1024 * 1024;
  654. size_t nCurrentCaptureIndex = 0;
  655. BYTE *pbyCaptureBuffer = new (std::nothrow) BYTE[nCaptureBufferSize];
  656. HANDLE waitArray[3];
  657. waitArray[0] = hAudioSamplesReadyEvent;
  658. bool stillPlaying = true;
  659. // Each loop fills about half of the shared buffer.
  660. while (stillPlaying) {
  661. DWORD waitResult = WaitForMultipleObjects(1, waitArray, FALSE, INFINITE);
  662. switch (waitResult) {
  663. case WAIT_OBJECT_0 + 0: // _AudioSamplesReadyEvent
  664. hr = pCaptureClient->GetNextPacketSize(&packetLength);
  665. EXIT_ON_ERROR(hr)
  666. printf("%06d # _AudioSamplesReadyEvent packetLength:%06u \n", nCnt, packetLength);
  667. while (packetLength != 0) {
  668. // Get the available data in the shared buffer.
  669. hr = pCaptureClient->GetBuffer(&pData, &numFramesAvailable, &flags, NULL, NULL);
  670. EXIT_ON_ERROR(hr)
  671. nCnt++;
  672. // test flags
  673. //////////////////////////////////////////////////////////////////////////
  674. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  675. printf("AUDCLNT_BUFFERFLAGS_SILENT \n");
  676. }
  677. if (flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY) {
  678. printf("%06d # AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY \n", nCnt);
  679. }
  680. //////////////////////////////////////////////////////////////////////////
  681. UINT32 framesToCopy = min(numFramesAvailable,
  682. static_cast<UINT32>(
  683. (nCaptureBufferSize - nCurrentCaptureIndex)
  684. / nFrameSize));
  685. if (framesToCopy != 0) {
  686. //
  687. // The flags on capture tell us information about the data.
  688. //
  689. // We only really care about the silent flag since we want to put frames of silence into the buffer
  690. // when we receive silence. We rely on the fact that a logical bit 0 is silence for both float and int formats.
  691. //
  692. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  693. //
  694. // Fill 0s from the capture buffer to the output buffer.
  695. //
  696. ZeroMemory(&pbyCaptureBuffer[nCurrentCaptureIndex],
  697. framesToCopy * nFrameSize);
  698. } else {
  699. //
  700. // Copy data from the audio engine buffer to the output buffer.
  701. //
  702. CopyMemory(&pbyCaptureBuffer[nCurrentCaptureIndex],
  703. pData,
  704. framesToCopy * nFrameSize);
  705. printf("Get: %d\r\n", framesToCopy * nFrameSize);
  706. }
  707. //
  708. // Bump the capture buffer pointer.
  709. //
  710. nCurrentCaptureIndex += framesToCopy * nFrameSize;
  711. }
  712. hr = pCaptureClient->ReleaseBuffer(numFramesAvailable);
  713. EXIT_ON_ERROR(hr)
  714. hr = pCaptureClient->GetNextPacketSize(&packetLength);
  715. EXIT_ON_ERROR(hr)
  716. UINT32 ui32NumPaddingFrames;
  717. hr = pAudioClient->GetCurrentPadding(&ui32NumPaddingFrames);
  718. EXIT_ON_ERROR(hr)
  719. if (0 != ui32NumPaddingFrames) {
  720. printf("GetCurrentPadding : %6u\n", ui32NumPaddingFrames);
  721. }
  722. //////////////////////////////////////////////////////////////////////////
  723. if (nCnt == 1000) {
  724. stillPlaying = false;
  725. break;
  726. }
  727. } // end of 'while (packetLength != 0)'
  728. break;
  729. } // end of 'switch (waitResult)'
  730. } // end of 'while (stillPlaying)'
  731. //
  732. // We've now captured our wave data. Now write it out in a wave file.
  733. //
  734. SaveWaveData(pbyCaptureBuffer, nCurrentCaptureIndex, pwfx);
  735. printf("\nAudio Capture Done.\n");
  736. hr = pAudioClient->Stop(); // Stop recording.
  737. EXIT_ON_ERROR(hr)
  738. Exit:
  739. CoTaskMemFree(pwfx);
  740. SAFE_RELEASE(pEnumerator)
  741. SAFE_RELEASE(pDevice)
  742. SAFE_RELEASE(pAudioClient)
  743. SAFE_RELEASE(pCaptureClient)
  744. CoUninitialize();
  745. if (pbyCaptureBuffer) {
  746. delete[] pbyCaptureBuffer;
  747. pbyCaptureBuffer = NULL;
  748. }
  749. if (hAudioSamplesReadyEvent) {
  750. CloseHandle(hAudioSamplesReadyEvent);
  751. hAudioSamplesReadyEvent = NULL;
  752. }
  753. }
  754. /* check that a given sample format is supported by the encoder */
  755. static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
  756. {
  757. const enum AVSampleFormat *p = codec->sample_fmts;
  758. while (*p != AV_SAMPLE_FMT_NONE) {
  759. if (*p == sample_fmt)
  760. return 1;
  761. p++;
  762. }
  763. return 0;
  764. }
  765. /* just pick the highest supported samplerate */
  766. static int select_sample_rate(const AVCodec *codec)
  767. {
  768. const int *p;
  769. int best_samplerate = 0;
  770. if (!codec->supported_samplerates)
  771. return 44100;
  772. p = codec->supported_samplerates;
  773. while (*p) {
  774. if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
  775. best_samplerate = *p;
  776. p++;
  777. }
  778. return best_samplerate;
  779. }
  780. /* select layout with the highest channel count */
  781. static int select_channel_layout(const AVCodec *codec)
  782. {
  783. const uint64_t *p;
  784. uint64_t best_ch_layout = 0;
  785. int best_nb_channels = 0;
  786. if (!codec->channel_layouts)
  787. return AV_CH_LAYOUT_STEREO;
  788. p = codec->channel_layouts;
  789. while (*p) {
  790. int nb_channels = av_get_channel_layout_nb_channels(*p);
  791. if (nb_channels > best_nb_channels) {
  792. best_ch_layout = *p;
  793. best_nb_channels = nb_channels;
  794. }
  795. p++;
  796. }
  797. return best_ch_layout;
  798. }
  799. int main1()
  800. {
  801. //capture_audio();
  802. //test_transcode();
  803. cpature_wave();
  804. system("pause");
  805. return 0;
  806. }