test_main.cpp 14 KB

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