test_main.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include "device_audios.h"
  2. #include "headers_mmdevice.h"
  3. #include "record_audio_factory.h"
  4. #include "record_desktop_factory.h"
  5. #include "encoder_aac.h"
  6. #include "filter_aresample.h"
  7. #include "resample_pcm.h"
  8. #include "muxer_define.h"
  9. #include "muxer_ffmpeg.h"
  10. #include "error_define.h"
  11. #include "hardware_acceleration.h"
  12. #include "log_helper.h"
  13. #include "system_version.h"
  14. #include "utils_string.h"
  15. #include "headers_ffmpeg.h"
  16. #include "remuxer_ffmpeg.h"
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #define USE_WASAPI 1
  22. #define V_FRAME_RATE 30
  23. #define V_BIT_RATE 1280 * 1000
  24. #define V_WIDTH GetSystemMetrics(SM_CXSCREEN)
  25. #define V_HEIGHT GetSystemMetrics(SM_CYSCREEN);
  26. #define V_QUALITY 100
  27. #define A_SAMPLE_CHANNEL 2
  28. #define A_SAMPLE_RATE 44100
  29. #define A_BIT_RATE 128000
  30. //for test muxer
  31. static am::record_audio *_recorder_speaker = nullptr;
  32. static am::record_audio *_recorder_microphone = nullptr;
  33. static am::record_desktop *_recorder_desktop = nullptr;
  34. static am::muxer_file *_muxer;
  35. static am::record_audio *audios[2];
  36. //for test audio record
  37. static am::record_audio *_recorder_audio = nullptr;
  38. static am::encoder_aac *_encoder_aac = nullptr;
  39. static am::resample_pcm *_resample_pcm = nullptr;
  40. static am::filter_aresample *_filter_aresample = nullptr;
  41. static int _sample_in = 0;
  42. static int _sample_size = 0;
  43. static int _resample_size = 0;
  44. static uint8_t *_sample_buffer = nullptr;
  45. static uint8_t *_resample_buffer = nullptr;
  46. int start_muxer()
  47. {
  48. std::string input_id, input_name, out_id, out_name;
  49. am::device_audios::get_default_input_device(input_id, input_name);
  50. al_info("use default input aduio device:%s", input_name.c_str());
  51. am::device_audios::get_default_ouput_device(out_id, out_name);
  52. al_info("use default output aduio device:%s", out_name.c_str());
  53. //first audio resrouce must be speaker,otherwise the audio pts may be not correct,may need to change the filter amix descriptions with duration & sync option
  54. #if !USE_WASAPI
  55. record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_DSHOW, &_recorder_speaker);
  56. _recorder_speaker->init(am::utils_string::ascii_utf8("audio=virtual-audio-capturer"),
  57. am::utils_string::ascii_utf8("audio=virtual-audio-capturer"),
  58. false);
  59. record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_DSHOW, &_recorder_microphone);
  60. _recorder_microphone->init(am::utils_string::ascii_utf8("audio=") + input_name, input_id, true);
  61. #else
  62. record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_WAS, &_recorder_speaker);
  63. _recorder_speaker->init(out_name, out_id, false);
  64. record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_WAS, &_recorder_microphone);
  65. _recorder_microphone->init(input_name, input_id, true);
  66. #endif // !USE_WASAPI
  67. record_desktop_new(RECORD_DESKTOP_TYPES::DT_DESKTOP_WIN_DUPLICATION, &_recorder_desktop);
  68. RECORD_DESKTOP_RECT rect;
  69. rect.left = 0;
  70. rect.top = 0;
  71. rect.right = V_WIDTH;
  72. rect.bottom = V_HEIGHT;
  73. _recorder_desktop->init(rect, V_FRAME_RATE);
  74. audios[0] = _recorder_microphone;
  75. audios[1] = _recorder_speaker;
  76. _muxer = new am::muxer_ffmpeg();
  77. am::MUX_SETTING setting;
  78. setting.v_frame_rate = V_FRAME_RATE;
  79. setting.v_bit_rate = V_BIT_RATE;
  80. setting.v_width = V_WIDTH;
  81. setting.v_height = V_HEIGHT;
  82. setting.v_qb = V_QUALITY;
  83. setting.v_encoder_id = am::EID_VIDEO_X264;
  84. setting.v_out_width = 1920;
  85. setting.v_out_height = 1080;
  86. setting.a_nb_channel = A_SAMPLE_CHANNEL;
  87. setting.a_sample_fmt = AV_SAMPLE_FMT_FLTP;
  88. setting.a_sample_rate = A_SAMPLE_RATE;
  89. setting.a_bit_rate = A_BIT_RATE;
  90. int error = _muxer->init(am::utils_string::ascii_utf8("..\\..\\save.mp4").c_str(),
  91. _recorder_desktop,
  92. audios,
  93. 2,
  94. setting);
  95. if (error != AE_NO) {
  96. return error;
  97. }
  98. _muxer->start();
  99. return error;
  100. }
  101. void stop_muxer()
  102. {
  103. _muxer->stop();
  104. delete _recorder_desktop;
  105. if (_recorder_speaker)
  106. delete _recorder_speaker;
  107. if (_recorder_microphone)
  108. delete _recorder_microphone;
  109. delete _muxer;
  110. }
  111. void test_recorder()
  112. {
  113. //av_log_set_level(AV_LOG_DEBUG);
  114. start_muxer();
  115. getchar();
  116. //stop have bug that sometime will stuck
  117. stop_muxer();
  118. al_info("record stoped...");
  119. }
  120. void show_devices()
  121. {
  122. std::list<am::DEVICE_AUDIOS> devices;
  123. am::device_audios::get_input_devices(devices);
  124. for (auto device : devices) {
  125. al_info("audio input name:%s id:%s", device.name.c_str(), device.id.c_str());
  126. }
  127. am::device_audios::get_output_devices(devices);
  128. for (auto device : devices) {
  129. al_info("audio output name:%s id:%s", device.name.c_str(), device.id.c_str());
  130. }
  131. }
  132. void on_aac_data(AVPacket *packet)
  133. {
  134. al_debug("on aac data :%d", packet->size);
  135. }
  136. void on_aac_error(int) {}
  137. void on_pcm_error(int, int) {}
  138. void on_pcm_data1(AVFrame *frame, int index)
  139. {
  140. int copied_len = 0;
  141. int sample_len = av_samples_get_buffer_size(NULL,
  142. ffmpeg_get_frame_channels(frame),
  143. frame->nb_samples,
  144. (AVSampleFormat) frame->format,
  145. 1);
  146. int remain_len = sample_len;
  147. int is_planner = av_sample_fmt_is_planar((AVSampleFormat) frame->format);
  148. while (remain_len > 0) { //should add is_planner codes
  149. //cache pcm
  150. copied_len = min(_sample_size - _sample_in, remain_len);
  151. if (copied_len) {
  152. memcpy(_sample_buffer + _sample_in,
  153. frame->data[0] + sample_len - remain_len,
  154. copied_len);
  155. _sample_in += copied_len;
  156. remain_len = remain_len - copied_len;
  157. }
  158. //got enough pcm to encoder,resample and mix
  159. if (_sample_in == _sample_size) {
  160. int ret = _resample_pcm->convert(_sample_buffer,
  161. _sample_size,
  162. _resample_buffer,
  163. _resample_size);
  164. if (ret > 0) {
  165. _encoder_aac->put(_resample_buffer, _resample_size, frame);
  166. } else {
  167. al_debug("resample audio %d failed,%d", index, ret);
  168. }
  169. _sample_in = 0;
  170. }
  171. }
  172. }
  173. void on_pcm_data(AVFrame *frame, int index)
  174. {
  175. _filter_aresample->add_frame(frame);
  176. }
  177. void on_aresample_data(AVFrame *frame, int index)
  178. {
  179. int copied_len = 0;
  180. int sample_len = av_samples_get_buffer_size(frame->linesize,
  181. ffmpeg_get_frame_channels(frame),
  182. frame->nb_samples,
  183. (AVSampleFormat) frame->format,
  184. 1);
  185. sample_len = av_samples_get_buffer_size(NULL,
  186. ffmpeg_get_frame_channels(frame),
  187. frame->nb_samples,
  188. (AVSampleFormat) frame->format,
  189. 1);
  190. int remain_len = sample_len;
  191. //for data is planar,should copy data[0] data[1] to correct buff pos
  192. if (av_sample_fmt_is_planar((AVSampleFormat) frame->format) == 0) {
  193. while (remain_len > 0) {
  194. //cache pcm
  195. copied_len = min(_sample_size - _sample_in, remain_len);
  196. if (copied_len) {
  197. memcpy(_resample_buffer + _sample_in,
  198. frame->data[0] + sample_len - remain_len,
  199. copied_len);
  200. _sample_in += copied_len;
  201. remain_len = remain_len - copied_len;
  202. }
  203. //got enough pcm to encoder,resample and mix
  204. if (_sample_in == _sample_size) {
  205. _encoder_aac->put(_resample_buffer, _sample_size, frame);
  206. _sample_in = 0;
  207. }
  208. }
  209. } else { //resample size is channels*frame->linesize[0],for 2 channels
  210. while (remain_len > 0) {
  211. copied_len = min(_sample_size - _sample_in, remain_len);
  212. if (copied_len) {
  213. memcpy(_resample_buffer + _sample_in / 2,
  214. frame->data[0] + (sample_len - remain_len) / 2,
  215. copied_len / 2);
  216. memcpy(_resample_buffer + _sample_size / 2 + _sample_in / 2,
  217. frame->data[1] + (sample_len - remain_len) / 2,
  218. copied_len / 2);
  219. _sample_in += copied_len;
  220. remain_len = remain_len - copied_len;
  221. }
  222. if (_sample_in == _sample_size) {
  223. _encoder_aac->put(_resample_buffer, _sample_size, frame);
  224. _sample_in = 0;
  225. }
  226. }
  227. }
  228. }
  229. void on_aresample_error(int error, int index) {}
  230. void save_aac()
  231. {
  232. std::string input_id, input_name, out_id, out_name;
  233. am::device_audios::get_default_input_device(input_id, input_name);
  234. am::device_audios::get_default_ouput_device(out_id, out_name);
  235. #if 0
  236. al_info("use default \r\noutput aduio device name:%s \r\noutput audio device id:%s ",
  237. out_name.c_str(), out_id.c_str());
  238. record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_WAS, &_recorder_audio);
  239. _recorder_audio->init(out_name, out_id, false);
  240. #else
  241. al_info("use default \r\ninput aduio device name:%s \r\ninput audio device id:%s ",
  242. input_name.c_str(),
  243. input_id.c_str());
  244. record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_WAS, &_recorder_audio);
  245. _recorder_audio->init(input_name, input_id, true);
  246. #endif
  247. _recorder_audio->registe_cb(on_pcm_data, on_pcm_error, 0);
  248. _encoder_aac = new am::encoder_aac();
  249. _encoder_aac->init(A_SAMPLE_CHANNEL, A_SAMPLE_RATE, AV_SAMPLE_FMT_FLTP, A_BIT_RATE);
  250. am::SAMPLE_SETTING src, dst = {0};
  251. src = {_encoder_aac->get_nb_samples(),
  252. (int64_t) ffmpeg_get_default_channel_layout(_recorder_audio->get_channel_num()),
  253. _recorder_audio->get_channel_num(),
  254. _recorder_audio->get_fmt(),
  255. _recorder_audio->get_sample_rate()};
  256. dst = {_encoder_aac->get_nb_samples(),
  257. (int64_t) ffmpeg_get_default_channel_layout(A_SAMPLE_CHANNEL),
  258. A_SAMPLE_CHANNEL,
  259. AV_SAMPLE_FMT_FLTP,
  260. A_SAMPLE_RATE};
  261. _resample_pcm = new am::resample_pcm();
  262. _resample_pcm->init(&src, &dst, &_resample_size);
  263. _resample_buffer = new uint8_t[_resample_size];
  264. _sample_size = av_samples_get_buffer_size(NULL,
  265. A_SAMPLE_CHANNEL,
  266. _encoder_aac->get_nb_samples(),
  267. _recorder_audio->get_fmt(),
  268. 1);
  269. _sample_buffer = new uint8_t[_sample_size];
  270. _filter_aresample = new am::filter_aresample();
  271. _filter_aresample->init({NULL,
  272. NULL,
  273. _recorder_audio->get_time_base(),
  274. _recorder_audio->get_sample_rate(),
  275. _recorder_audio->get_fmt(),
  276. _recorder_audio->get_channel_num(),
  277. (int64_t) ffmpeg_get_default_channel_layout(
  278. _recorder_audio->get_channel_num())},
  279. {NULL,
  280. NULL,
  281. {1, AV_TIME_BASE},
  282. A_SAMPLE_RATE,
  283. AV_SAMPLE_FMT_FLTP,
  284. A_SAMPLE_CHANNEL,
  285. (int64_t) ffmpeg_get_default_channel_layout(A_SAMPLE_CHANNEL)},
  286. 0);
  287. _filter_aresample->registe_cb(on_aresample_data, on_aresample_error);
  288. _filter_aresample->start();
  289. _encoder_aac->start();
  290. _recorder_audio->start();
  291. getchar();
  292. _recorder_audio->stop();
  293. _filter_aresample->stop();
  294. delete _recorder_audio;
  295. delete _encoder_aac;
  296. delete _resample_pcm;
  297. delete _filter_aresample;
  298. delete[] _sample_buffer;
  299. delete[] _resample_buffer;
  300. }
  301. void test_audio()
  302. {
  303. std::string input_id, input_name, out_id, out_name;
  304. am::device_audios::get_default_input_device(input_id, input_name);
  305. al_info("use default \r\ninput aduio device name:%s \r\ninput audio device id:%s ",
  306. input_name.c_str(),
  307. input_id.c_str());
  308. am::device_audios::get_default_ouput_device(out_id, out_name);
  309. al_info("use default \r\noutput aduio device name:%s \r\noutput audio device id:%s ",
  310. out_name.c_str(),
  311. out_id.c_str());
  312. record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_WAS, &_recorder_speaker);
  313. _recorder_speaker->init(am::utils_string::ascii_utf8("Default"),
  314. am::utils_string::ascii_utf8("Default"),
  315. false);
  316. //record_audio_new(RECORD_AUDIO_TYPES::AT_AUDIO_WAS, &_recorder_microphone);
  317. //_recorder_microphone->init(input_name,input_id, true);
  318. _recorder_speaker->start();
  319. //_recorder_microphone->start();
  320. getchar();
  321. _recorder_speaker->stop();
  322. //_recorder_microphone->stop();
  323. }
  324. void on_remux_progress(const char *src, int progress, int total)
  325. {
  326. al_debug("on remux progress:%s %d %d", src, progress, total);
  327. }
  328. void on_remux_state(const char *src, int state, int error)
  329. {
  330. al_debug("on remux state:%s %d %d", src, state, error);
  331. }
  332. void test_remux()
  333. {
  334. #if TEST_MULTI_THREAD
  335. for (int i = 0; i < 20; i++) {
  336. am::REMUXER_PARAM param = {0};
  337. sprintf_s(param.src, 260, "%d", i);
  338. am::remuxer_ffmpeg::instance()->create_remux(param);
  339. }
  340. #else
  341. am::REMUXER_PARAM param = {0};
  342. sprintf_s(param.src, 260, "%s", am::utils_string::ascii_utf8("..\\..\\save.mkv").c_str());
  343. sprintf_s(param.dst, 260, "%s", am::utils_string::ascii_utf8("..\\..\\save.mp4").c_str());
  344. param.cb_progress = on_remux_progress;
  345. param.cb_state = on_remux_state;
  346. am::remuxer_ffmpeg::instance()->create_remux(param);
  347. #endif
  348. }
  349. void test_scale()
  350. {
  351. static const double scaled_vals[]
  352. = {1.0, 1.25, (1.0 / 0.75), 1.5, (1.0 / 0.6), 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 0.0};
  353. auto get_valid_out_resolution =
  354. [](int src_width, int src_height, int *out_width, int *out_height) {
  355. int scale_cx = src_width;
  356. int scale_cy = src_height;
  357. int i = 0;
  358. while (((scale_cx * scale_cy) > (1920 * 1080)) && scaled_vals[i] > 0.0) {
  359. double scale = scaled_vals[i++];
  360. scale_cx = uint32_t(double(src_width) / scale);
  361. scale_cy = uint32_t(double(src_height) / scale);
  362. }
  363. if (scale_cx % 2 != 0) {
  364. scale_cx += 1;
  365. }
  366. if (scale_cy % 2 != 0) {
  367. scale_cy += 1;
  368. }
  369. *out_width = scale_cx;
  370. *out_height = scale_cy;
  371. al_info("get valid output resolution from %dx%d to %dx%d,with scale:%lf",
  372. src_width,
  373. src_height,
  374. scale_cx,
  375. scale_cy,
  376. scaled_vals[i]);
  377. };
  378. int src_width = 2736, src_height = 1824;
  379. int dst_width, dst_height;
  380. get_valid_out_resolution(src_width, src_height, &dst_width, &dst_height);
  381. }
  382. // int main(int argc, char **argv)
  383. // {
  384. // al_info("record start...");
  385. // am::winversion_info ver = {0};
  386. // am::system_version::get_win(&ver);
  387. // bool is_win8_or_above = am::system_version::is_win8_or_above();
  388. // bool is_ia32 = am::system_version::is_32();
  389. // al_info("win version: %d.%d.%d.%d", ver.major, ver.minor, ver.build, ver.revis);
  390. // al_info("is win8 or above: %s", is_win8_or_above ? "true" : "false");
  391. // //auto hw_encoders = am::hardware_acceleration::get_supported_video_encoders();
  392. // //show_devices();
  393. // //test_audio();
  394. // test_recorder();
  395. // //test_remux();
  396. // //save_aac();
  397. // //test_scale();
  398. // al_info("press any key to exit...");
  399. // system("pause");
  400. // return 0;
  401. // }