media_audio_capture.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "media/audio/media_audio_capture.h"
  8. #include "media/audio/media_audio_capture_common.h"
  9. #include "media/audio/media_audio_ffmpeg_loader.h"
  10. #include "media/audio/media_audio_track.h"
  11. #include "ffmpeg/ffmpeg_utility.h"
  12. #include "base/timer.h"
  13. #include <al.h>
  14. #include <alc.h>
  15. #include <numeric>
  16. namespace Media {
  17. namespace Capture {
  18. namespace {
  19. constexpr auto kCaptureFrequency = Player::kDefaultFrequency;
  20. constexpr auto kCaptureSkipDuration = crl::time(400);
  21. constexpr auto kCaptureFadeInDuration = crl::time(300);
  22. constexpr auto kCaptureBufferSlice = 256 * 1024;
  23. constexpr auto kCaptureUpdateDelta = crl::time(100);
  24. Instance *CaptureInstance = nullptr;
  25. bool ErrorHappened(ALCdevice *device) {
  26. ALenum errCode;
  27. if ((errCode = alcGetError(device)) != ALC_NO_ERROR) {
  28. LOG(("Audio Capture Error: %1, %2").arg(errCode).arg((const char *)alcGetString(device, errCode)));
  29. return true;
  30. }
  31. return false;
  32. }
  33. [[nodiscard]] VoiceWaveform CollectWaveform(
  34. const QVector<uchar> &waveformVector) {
  35. if (waveformVector.isEmpty()) {
  36. return {};
  37. }
  38. auto waveform = VoiceWaveform();
  39. auto count = int64(waveformVector.size());
  40. auto sum = int64(0);
  41. if (count >= Player::kWaveformSamplesCount) {
  42. auto peaks = QVector<uint16>();
  43. peaks.reserve(Player::kWaveformSamplesCount);
  44. auto peak = uint16(0);
  45. for (auto i = int32(0); i < count; ++i) {
  46. auto sample = uint16(waveformVector.at(i)) * 256;
  47. if (peak < sample) {
  48. peak = sample;
  49. }
  50. sum += Player::kWaveformSamplesCount;
  51. if (sum >= count) {
  52. sum -= count;
  53. peaks.push_back(peak);
  54. peak = 0;
  55. }
  56. }
  57. auto sum = std::accumulate(peaks.cbegin(), peaks.cend(), 0LL);
  58. peak = qMax(int32(sum * 1.8 / peaks.size()), 2500);
  59. waveform.resize(peaks.size());
  60. for (int32 i = 0, l = peaks.size(); i != l; ++i) {
  61. waveform[i] = char(qMin(
  62. 31U,
  63. uint32(qMin(peaks.at(i), peak)) * 31 / peak));
  64. }
  65. }
  66. return waveform;
  67. }
  68. } // namespace
  69. class Instance::Inner final : public QObject {
  70. public:
  71. Inner(QThread *thread);
  72. ~Inner();
  73. void start(
  74. Webrtc::DeviceResolvedId id,
  75. Fn<void(Update)> updated,
  76. Fn<void()> error,
  77. Fn<void(Chunk)> externalProcessing);
  78. void stop(Fn<void(Result&&)> callback = nullptr);
  79. void pause(bool value, Fn<void(Result&&)> callback);
  80. private:
  81. void process();
  82. bool initializeFFmpeg();
  83. [[nodiscard]] bool processFrame(int32 offset, int32 framesize);
  84. void fail();
  85. [[nodiscard]] bool writeFrame(AVFrame *frame);
  86. // Writes the packets till EAGAIN is got from av_receive_packet()
  87. // Returns number of packets written or -1 on error
  88. [[nodiscard]] int writePackets();
  89. Fn<void(Chunk)> _externalProcessing;
  90. Fn<void(Update)> _updated;
  91. Fn<void()> _error;
  92. struct Private;
  93. const std::unique_ptr<Private> d;
  94. base::Timer _timer;
  95. QByteArray _captured;
  96. bool _paused = false;
  97. };
  98. void Start() {
  99. Assert(CaptureInstance == nullptr);
  100. CaptureInstance = new Instance();
  101. instance()->check();
  102. }
  103. void Finish() {
  104. delete base::take(CaptureInstance);
  105. }
  106. Instance::Instance() : _inner(std::make_unique<Inner>(&_thread)) {
  107. CaptureInstance = this;
  108. _thread.start();
  109. }
  110. void Instance::start(Fn<void(Chunk)> externalProcessing) {
  111. _updates.fire_done();
  112. const auto id = Audio::Current().captureDeviceId();
  113. InvokeQueued(_inner.get(), [=] {
  114. _inner->start(id, [=](Update update) {
  115. crl::on_main(this, [=] {
  116. _updates.fire_copy(update);
  117. });
  118. }, [=] {
  119. crl::on_main(this, [=] {
  120. _updates.fire_error(Error::Other);
  121. });
  122. }, externalProcessing);
  123. crl::on_main(this, [=] {
  124. _started = true;
  125. });
  126. });
  127. }
  128. void Instance::stop(Fn<void(Result&&)> callback) {
  129. InvokeQueued(_inner.get(), [=] {
  130. if (!callback) {
  131. _inner->stop();
  132. crl::on_main(this, [=] { _started = false; });
  133. return;
  134. }
  135. _inner->stop([=](Result &&result) {
  136. crl::on_main([=, result = std::move(result)]() mutable {
  137. callback(std::move(result));
  138. _started = false;
  139. });
  140. });
  141. });
  142. }
  143. void Instance::pause(bool value, Fn<void(Result&&)> callback) {
  144. InvokeQueued(_inner.get(), [=] {
  145. auto done = callback
  146. ? [=](Result &&result) {
  147. crl::on_main([=, result = std::move(result)]() mutable {
  148. callback(std::move(result));
  149. });
  150. }
  151. : std::move(callback);
  152. _inner->pause(value, std::move(done));
  153. });
  154. }
  155. void Instance::check() {
  156. _available = false;
  157. if (auto device = alcGetString(0, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER)) {
  158. if (!QString::fromUtf8(device).isEmpty()) {
  159. _available = true;
  160. return;
  161. }
  162. }
  163. LOG(("Audio Error: No capture device found!"));
  164. }
  165. Instance::~Instance() {
  166. // Send _inner to it's thread for destruction.
  167. if (const auto context = _inner.get()) {
  168. InvokeQueued(context, [copy = base::take(_inner)]{});
  169. }
  170. // And wait for it to finish.
  171. _thread.quit();
  172. _thread.wait();
  173. }
  174. Instance *instance() {
  175. return CaptureInstance;
  176. }
  177. struct Instance::Inner::Private {
  178. ALCdevice *device = nullptr;
  179. AVOutputFormat *fmt = nullptr;
  180. uchar *ioBuffer = nullptr;
  181. AVIOContext *ioContext = nullptr;
  182. AVFormatContext *fmtContext = nullptr;
  183. AVStream *stream = nullptr;
  184. const AVCodec *codec = nullptr;
  185. AVCodecContext *codecContext = nullptr;
  186. int channels = 0;
  187. bool opened = false;
  188. bool processing = false;
  189. int srcSamples = 0;
  190. int dstSamples = 0;
  191. int maxDstSamples = 0;
  192. int dstSamplesSize = 0;
  193. int fullSamples = 0;
  194. uint8_t **srcSamplesData = nullptr;
  195. uint8_t **dstSamplesData = nullptr;
  196. SwrContext *swrContext = nullptr;
  197. int32 lastUpdate = 0;
  198. uint16 levelMax = 0;
  199. QByteArray data;
  200. int32 dataPos = 0;
  201. int64 waveformMod = 0;
  202. int64 waveformEach = (kCaptureFrequency / 100);
  203. uint16 waveformPeak = 0;
  204. QVector<uchar> waveform;
  205. static int ReadData(void *opaque, uint8_t *buf, int buf_size) {
  206. auto l = reinterpret_cast<Private*>(opaque);
  207. int32 nbytes = qMin(l->data.size() - l->dataPos, int32(buf_size));
  208. if (nbytes <= 0) {
  209. return AVERROR_EOF;
  210. }
  211. memcpy(buf, l->data.constData() + l->dataPos, nbytes);
  212. l->dataPos += nbytes;
  213. return nbytes;
  214. }
  215. #if DA_FFMPEG_CONST_WRITE_CALLBACK
  216. static int WriteData(void *opaque, const uint8_t *buf, int buf_size) {
  217. #else
  218. static int WriteData(void *opaque, uint8_t *buf, int buf_size) {
  219. #endif
  220. auto l = reinterpret_cast<Private*>(opaque);
  221. if (buf_size <= 0) return 0;
  222. if (l->dataPos + buf_size > l->data.size()) l->data.resize(l->dataPos + buf_size);
  223. memcpy(l->data.data() + l->dataPos, buf, buf_size);
  224. l->dataPos += buf_size;
  225. return buf_size;
  226. }
  227. static int64_t SeekData(void *opaque, int64_t offset, int whence) {
  228. auto l = reinterpret_cast<Private*>(opaque);
  229. int32 newPos = -1;
  230. switch (whence) {
  231. case SEEK_SET: newPos = offset; break;
  232. case SEEK_CUR: newPos = l->dataPos + offset; break;
  233. case SEEK_END: newPos = l->data.size() + offset; break;
  234. case AVSEEK_SIZE: {
  235. // Special whence for determining filesize without any seek.
  236. return l->data.size();
  237. } break;
  238. }
  239. if (newPos < 0) {
  240. return -1;
  241. }
  242. l->dataPos = newPos;
  243. return l->dataPos;
  244. }
  245. };
  246. Instance::Inner::Inner(QThread *thread)
  247. : d(std::make_unique<Private>())
  248. , _timer(thread, [=] { process(); }) {
  249. moveToThread(thread);
  250. }
  251. Instance::Inner::~Inner() {
  252. stop();
  253. }
  254. void Instance::Inner::fail() {
  255. stop();
  256. if (const auto error = base::take(_error)) {
  257. InvokeQueued(this, error);
  258. }
  259. }
  260. void Instance::Inner::start(
  261. Webrtc::DeviceResolvedId id,
  262. Fn<void(Update)> updated,
  263. Fn<void()> error,
  264. Fn<void(Chunk)> externalProcessing) {
  265. _externalProcessing = std::move(externalProcessing);
  266. _updated = std::move(updated);
  267. _error = std::move(error);
  268. if (_paused) {
  269. _paused = false;
  270. }
  271. // Start OpenAL Capture
  272. const auto utf = id.isDefault() ? std::string() : id.value.toStdString();
  273. d->device = alcCaptureOpenDevice(
  274. utf.empty() ? nullptr : utf.c_str(),
  275. kCaptureFrequency,
  276. AL_FORMAT_MONO16,
  277. kCaptureFrequency / 5);
  278. if (!d->device) {
  279. LOG(("Audio Error: capture device not present!"));
  280. fail();
  281. return;
  282. }
  283. alcCaptureStart(d->device);
  284. if (ErrorHappened(d->device)) {
  285. alcCaptureCloseDevice(d->device);
  286. d->device = nullptr;
  287. fail();
  288. return;
  289. } else if (!_externalProcessing) {
  290. if (!initializeFFmpeg()) {
  291. fail();
  292. return;
  293. }
  294. }
  295. _timer.callEach(50);
  296. _captured.clear();
  297. _captured.reserve(kCaptureBufferSlice);
  298. DEBUG_LOG(("Audio Capture: started!"));
  299. }
  300. bool Instance::Inner::initializeFFmpeg() {
  301. // Create encoding context
  302. d->ioBuffer = (uchar*)av_malloc(FFmpeg::kAVBlockSize);
  303. d->ioContext = avio_alloc_context(d->ioBuffer, FFmpeg::kAVBlockSize, 1, static_cast<void*>(d.get()), &Private::ReadData, &Private::WriteData, &Private::SeekData);
  304. int res = 0;
  305. char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
  306. const AVOutputFormat *fmt = nullptr;
  307. void *i = nullptr;
  308. while ((fmt = av_muxer_iterate(&i))) {
  309. if (fmt->name == u"opus"_q) {
  310. break;
  311. }
  312. }
  313. if (!fmt) {
  314. LOG(("Audio Error: Unable to find opus AVOutputFormat for capture"));
  315. return false;
  316. }
  317. if ((res = avformat_alloc_output_context2(&d->fmtContext, (AVOutputFormat*)fmt, 0, 0)) < 0) {
  318. LOG(("Audio Error: Unable to avformat_alloc_output_context2 for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  319. return false;
  320. }
  321. d->fmtContext->pb = d->ioContext;
  322. d->fmtContext->flags |= AVFMT_FLAG_CUSTOM_IO;
  323. d->opened = true;
  324. // Add audio stream
  325. d->codec = avcodec_find_encoder(fmt->audio_codec);
  326. if (!d->codec) {
  327. LOG(("Audio Error: Unable to avcodec_find_encoder for capture"));
  328. return false;
  329. }
  330. d->stream = avformat_new_stream(d->fmtContext, d->codec);
  331. if (!d->stream) {
  332. LOG(("Audio Error: Unable to avformat_new_stream for capture"));
  333. return false;
  334. }
  335. d->stream->id = d->fmtContext->nb_streams - 1;
  336. d->codecContext = avcodec_alloc_context3(d->codec);
  337. if (!d->codecContext) {
  338. LOG(("Audio Error: Unable to avcodec_alloc_context3 for capture"));
  339. return false;
  340. }
  341. av_opt_set_int(d->codecContext, "refcounted_frames", 1, 0);
  342. d->codecContext->sample_fmt = AV_SAMPLE_FMT_FLTP;
  343. d->codecContext->bit_rate = 32000;
  344. #if DA_FFMPEG_NEW_CHANNEL_LAYOUT
  345. d->codecContext->ch_layout = AV_CHANNEL_LAYOUT_MONO;
  346. d->channels = d->codecContext->ch_layout.nb_channels;
  347. #else // DA_FFMPEG_NEW_CHANNEL_LAYOUT
  348. d->codecContext->channel_layout = AV_CH_LAYOUT_MONO;
  349. d->channels = d->codecContext->channels = 1;
  350. #endif // DA_FFMPEG_NEW_CHANNEL_LAYOUT
  351. d->codecContext->sample_rate = kCaptureFrequency;
  352. if (d->fmtContext->oformat->flags & AVFMT_GLOBALHEADER) {
  353. d->codecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  354. }
  355. // Open audio stream
  356. if ((res = avcodec_open2(d->codecContext, d->codec, nullptr)) < 0) {
  357. LOG(("Audio Error: Unable to avcodec_open2 for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  358. return false;
  359. }
  360. // Alloc source samples
  361. d->srcSamples = (d->codecContext->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) ? 10000 : d->codecContext->frame_size;
  362. //if ((res = av_samples_alloc_array_and_samples(&d->srcSamplesData, 0, d->codecContext->channels, d->srcSamples, d->codecContext->sample_fmt, 0)) < 0) {
  363. // LOG(("Audio Error: Unable to av_samples_alloc_array_and_samples for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  364. // onStop(false);
  365. // emit error();
  366. // return;
  367. //}
  368. // Using _captured directly
  369. // Prepare resampling
  370. #if DA_FFMPEG_NEW_CHANNEL_LAYOUT
  371. res = swr_alloc_set_opts2(
  372. &d->swrContext,
  373. &d->codecContext->ch_layout,
  374. d->codecContext->sample_fmt,
  375. d->codecContext->sample_rate,
  376. &d->codecContext->ch_layout,
  377. AV_SAMPLE_FMT_S16,
  378. d->codecContext->sample_rate,
  379. 0,
  380. nullptr);
  381. #else // DA_FFMPEG_NEW_CHANNEL_LAYOUT
  382. d->swrContext = swr_alloc_set_opts(
  383. d->swrContext,
  384. d->codecContext->channel_layout,
  385. d->codecContext->sample_fmt,
  386. d->codecContext->sample_rate,
  387. d->codecContext->channel_layout,
  388. AV_SAMPLE_FMT_S16,
  389. d->codecContext->sample_rate,
  390. 0,
  391. nullptr);
  392. res = 0;
  393. #endif // DA_FFMPEG_NEW_CHANNEL_LAYOUT
  394. if (res < 0 || !d->swrContext) {
  395. LOG(("Audio Error: Unable to swr_alloc_set_opts2 for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  396. return false;
  397. } else if ((res = swr_init(d->swrContext)) < 0) {
  398. LOG(("Audio Error: Unable to swr_init for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  399. return false;
  400. }
  401. d->maxDstSamples = d->srcSamples;
  402. if ((res = av_samples_alloc_array_and_samples(&d->dstSamplesData, 0, d->channels, d->maxDstSamples, d->codecContext->sample_fmt, 0)) < 0) {
  403. LOG(("Audio Error: Unable to av_samples_alloc_array_and_samples for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  404. return false;
  405. }
  406. d->dstSamplesSize = av_samples_get_buffer_size(0, d->channels, d->maxDstSamples, d->codecContext->sample_fmt, 0);
  407. if ((res = avcodec_parameters_from_context(d->stream->codecpar, d->codecContext)) < 0) {
  408. LOG(("Audio Error: Unable to avcodec_parameters_from_context for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  409. return false;
  410. }
  411. // Write file header
  412. if ((res = avformat_write_header(d->fmtContext, 0)) < 0) {
  413. LOG(("Audio Error: Unable to avformat_write_header for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  414. return false;
  415. }
  416. return true;
  417. }
  418. void Instance::Inner::pause(bool value, Fn<void(Result&&)> callback) {
  419. _paused = value;
  420. if (!_paused) {
  421. return;
  422. }
  423. if (callback) {
  424. callback({
  425. .bytes = d->fullSamples ? d->data : QByteArray(),
  426. .waveform = (d->fullSamples
  427. ? CollectWaveform(d->waveform)
  428. : VoiceWaveform()),
  429. .duration = ((d->fullSamples * crl::time(1000))
  430. / int64(kCaptureFrequency)),
  431. });
  432. }
  433. }
  434. void Instance::Inner::stop(Fn<void(Result&&)> callback) {
  435. if (!_timer.isActive()) {
  436. return; // in stop() already
  437. }
  438. _paused = false;
  439. _timer.cancel();
  440. const auto needResult = (callback != nullptr);
  441. const auto hadDevice = (d->device != nullptr);
  442. if (hadDevice) {
  443. alcCaptureStop(d->device);
  444. if (d->processing) {
  445. Assert(!needResult); // stop in the middle of processing - error.
  446. } else {
  447. process(); // get last data
  448. }
  449. alcCaptureCloseDevice(d->device);
  450. d->device = nullptr;
  451. }
  452. // Write what is left
  453. if (needResult && !_captured.isEmpty()) {
  454. auto fadeSamples = kCaptureFadeInDuration * kCaptureFrequency / 1000;
  455. auto capturedSamples = static_cast<int>(_captured.size() / sizeof(short));
  456. if ((_captured.size() % sizeof(short)) || (d->fullSamples + capturedSamples < kCaptureFrequency) || (capturedSamples < fadeSamples)) {
  457. d->fullSamples = 0;
  458. d->dataPos = 0;
  459. d->data.clear();
  460. d->waveformMod = 0;
  461. d->waveformPeak = 0;
  462. d->waveform.clear();
  463. } else {
  464. float64 coef = 1. / fadeSamples, fadedFrom = 0;
  465. for (short *ptr = ((short*)_captured.data()) + capturedSamples, *end = ptr - fadeSamples; ptr != end; ++fadedFrom) {
  466. --ptr;
  467. *ptr = qRound(fadedFrom * coef * *ptr);
  468. }
  469. if (capturedSamples % d->srcSamples) {
  470. int32 s = _captured.size();
  471. _captured.resize(s + (d->srcSamples - (capturedSamples % d->srcSamples)) * sizeof(short));
  472. memset(_captured.data() + s, 0, _captured.size() - s);
  473. }
  474. int32 framesize = d->srcSamples * d->channels * sizeof(short), encoded = 0;
  475. while (_captured.size() >= encoded + framesize) {
  476. if (!processFrame(encoded, framesize)) {
  477. break;
  478. }
  479. encoded += framesize;
  480. }
  481. // Drain the codec.
  482. if (!writeFrame(nullptr) || encoded != _captured.size()) {
  483. d->fullSamples = 0;
  484. d->dataPos = 0;
  485. d->data.clear();
  486. d->waveformMod = 0;
  487. d->waveformPeak = 0;
  488. d->waveform.clear();
  489. }
  490. }
  491. }
  492. DEBUG_LOG(("Audio Capture: "
  493. "stopping (need result: %1), size: %2, samples: %3"
  494. ).arg(Logs::b(callback != nullptr)
  495. ).arg(d->data.size()
  496. ).arg(d->fullSamples));
  497. _captured = QByteArray();
  498. // Finish stream
  499. if (needResult && hadDevice && d->fmtContext) {
  500. av_write_trailer(d->fmtContext);
  501. }
  502. QByteArray result = d->fullSamples ? d->data : QByteArray();
  503. VoiceWaveform waveform;
  504. qint32 samples = d->fullSamples;
  505. if (needResult && samples && !d->waveform.isEmpty()) {
  506. waveform = CollectWaveform(d->waveform);
  507. }
  508. if (hadDevice) {
  509. if (d->codecContext) {
  510. avcodec_free_context(&d->codecContext);
  511. d->codecContext = nullptr;
  512. }
  513. if (d->srcSamplesData) {
  514. if (d->srcSamplesData[0]) {
  515. av_freep(&d->srcSamplesData[0]);
  516. }
  517. av_freep(&d->srcSamplesData);
  518. }
  519. if (d->dstSamplesData) {
  520. if (d->dstSamplesData[0]) {
  521. av_freep(&d->dstSamplesData[0]);
  522. }
  523. av_freep(&d->dstSamplesData);
  524. }
  525. d->fullSamples = 0;
  526. if (d->swrContext) {
  527. swr_free(&d->swrContext);
  528. d->swrContext = nullptr;
  529. }
  530. if (d->opened) {
  531. avformat_close_input(&d->fmtContext);
  532. d->opened = false;
  533. }
  534. if (d->ioContext) {
  535. av_freep(&d->ioContext->buffer);
  536. av_freep(&d->ioContext);
  537. d->ioBuffer = nullptr;
  538. } else if (d->ioBuffer) {
  539. av_freep(&d->ioBuffer);
  540. }
  541. if (d->fmtContext) {
  542. avformat_free_context(d->fmtContext);
  543. d->fmtContext = nullptr;
  544. }
  545. d->fmt = nullptr;
  546. d->stream = nullptr;
  547. d->codec = nullptr;
  548. d->lastUpdate = 0;
  549. d->levelMax = 0;
  550. d->dataPos = 0;
  551. d->data.clear();
  552. d->waveformMod = 0;
  553. d->waveformPeak = 0;
  554. d->waveform.clear();
  555. }
  556. if (needResult) {
  557. callback({
  558. .bytes = result,
  559. .waveform = waveform,
  560. .duration = (samples * crl::time(1000)) / kCaptureFrequency,
  561. });
  562. }
  563. }
  564. void Instance::Inner::process() {
  565. Expects(!d->processing);
  566. if (_paused) {
  567. return;
  568. }
  569. d->processing = true;
  570. const auto guard = gsl::finally([&] { d->processing = false; });
  571. if (!d->device) {
  572. _timer.cancel();
  573. return;
  574. }
  575. ALint samples;
  576. alcGetIntegerv(d->device, ALC_CAPTURE_SAMPLES, 1, &samples);
  577. if (ErrorHappened(d->device)) {
  578. fail();
  579. return;
  580. }
  581. if (samples > 0) {
  582. // Get samples from OpenAL
  583. auto s = _captured.size();
  584. auto news = s + static_cast<int>(samples * sizeof(short));
  585. if (news / kCaptureBufferSlice > s / kCaptureBufferSlice) {
  586. _captured.reserve(((news / kCaptureBufferSlice) + 1) * kCaptureBufferSlice);
  587. }
  588. _captured.resize(news);
  589. alcCaptureSamples(d->device, (ALCvoid *)(_captured.data() + s), samples);
  590. if (ErrorHappened(d->device)) {
  591. fail();
  592. return;
  593. } else if (_externalProcessing) {
  594. _externalProcessing({
  595. .finished = crl::now(),
  596. .samples = base::take(_captured),
  597. .frequency = kCaptureFrequency,
  598. });
  599. return;
  600. }
  601. // Count new recording level and update view
  602. auto skipSamples = kCaptureSkipDuration * kCaptureFrequency / 1000;
  603. auto fadeSamples = kCaptureFadeInDuration * kCaptureFrequency / 1000;
  604. auto levelindex = d->fullSamples + static_cast<int>(s / sizeof(short));
  605. for (auto ptr = (const short*)(_captured.constData() + s), end = (const short*)(_captured.constData() + news); ptr < end; ++ptr, ++levelindex) {
  606. if (levelindex > skipSamples) {
  607. uint16 value = qAbs(*ptr);
  608. if (levelindex < skipSamples + fadeSamples) {
  609. value = qRound(value * float64(levelindex - skipSamples) / fadeSamples);
  610. }
  611. if (d->levelMax < value) {
  612. d->levelMax = value;
  613. }
  614. }
  615. }
  616. qint32 samplesFull = d->fullSamples + _captured.size() / sizeof(short), samplesSinceUpdate = samplesFull - d->lastUpdate;
  617. if (samplesSinceUpdate > kCaptureUpdateDelta * kCaptureFrequency / 1000) {
  618. _updated(Update{ .samples = samplesFull, .level = d->levelMax });
  619. d->lastUpdate = samplesFull;
  620. d->levelMax = 0;
  621. }
  622. // Write frames
  623. int32 framesize = d->srcSamples * d->channels * sizeof(short), encoded = 0;
  624. while (uint32(_captured.size()) >= encoded + framesize + fadeSamples * sizeof(short)) {
  625. if (!processFrame(encoded, framesize)) {
  626. return;
  627. }
  628. encoded += framesize;
  629. }
  630. // Collapse the buffer
  631. if (encoded > 0) {
  632. int32 goodSize = _captured.size() - encoded;
  633. memmove(_captured.data(), _captured.constData() + encoded, goodSize);
  634. _captured.resize(goodSize);
  635. }
  636. } else {
  637. DEBUG_LOG(("Audio Capture: no samples to capture."));
  638. }
  639. }
  640. bool Instance::Inner::processFrame(int32 offset, int32 framesize) {
  641. // Prepare audio frame
  642. if (framesize % sizeof(short)) { // in the middle of a sample
  643. LOG(("Audio Error: Bad framesize in writeFrame() for capture, framesize %1, %2").arg(framesize));
  644. fail();
  645. return false;
  646. }
  647. auto samplesCnt = static_cast<int>(framesize / sizeof(short));
  648. int res = 0;
  649. char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
  650. auto srcSamplesDataChannel = (short*)(_captured.data() + offset);
  651. auto srcSamplesData = &srcSamplesDataChannel;
  652. // memcpy(d->srcSamplesData[0], _captured.constData() + offset, framesize);
  653. auto skipSamples = static_cast<int>(kCaptureSkipDuration * kCaptureFrequency / 1000);
  654. auto fadeSamples = static_cast<int>(kCaptureFadeInDuration * kCaptureFrequency / 1000);
  655. if (d->fullSamples < skipSamples + fadeSamples) {
  656. int32 fadedCnt = qMin(samplesCnt, skipSamples + fadeSamples - d->fullSamples);
  657. float64 coef = 1. / fadeSamples, fadedFrom = d->fullSamples - skipSamples;
  658. short *ptr = srcSamplesDataChannel, *zeroEnd = ptr + qMin(samplesCnt, qMax(0, skipSamples - d->fullSamples)), *end = ptr + fadedCnt;
  659. for (; ptr != zeroEnd; ++ptr, ++fadedFrom) {
  660. *ptr = 0;
  661. }
  662. for (; ptr != end; ++ptr, ++fadedFrom) {
  663. *ptr = qRound(fadedFrom * coef * *ptr);
  664. }
  665. }
  666. d->waveform.reserve(d->waveform.size() + (samplesCnt / d->waveformEach) + 1);
  667. for (short *ptr = srcSamplesDataChannel, *end = ptr + samplesCnt; ptr != end; ++ptr) {
  668. uint16 value = qAbs(*ptr);
  669. if (d->waveformPeak < value) {
  670. d->waveformPeak = value;
  671. }
  672. if (++d->waveformMod == d->waveformEach) {
  673. d->waveformMod -= d->waveformEach;
  674. d->waveform.push_back(uchar(d->waveformPeak / 256));
  675. d->waveformPeak = 0;
  676. }
  677. }
  678. // Convert to final format
  679. d->dstSamples = av_rescale_rnd(swr_get_delay(d->swrContext, d->codecContext->sample_rate) + d->srcSamples, d->codecContext->sample_rate, d->codecContext->sample_rate, AV_ROUND_UP);
  680. if (d->dstSamples > d->maxDstSamples) {
  681. d->maxDstSamples = d->dstSamples;
  682. av_freep(&d->dstSamplesData[0]);
  683. if ((res = av_samples_alloc(d->dstSamplesData, 0, d->channels, d->dstSamples, d->codecContext->sample_fmt, 1)) < 0) {
  684. LOG(("Audio Error: Unable to av_samples_alloc for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  685. fail();
  686. return false;
  687. }
  688. d->dstSamplesSize = av_samples_get_buffer_size(0, d->channels, d->maxDstSamples, d->codecContext->sample_fmt, 0);
  689. }
  690. if ((res = swr_convert(d->swrContext, d->dstSamplesData, d->dstSamples, (const uint8_t **)srcSamplesData, d->srcSamples)) < 0) {
  691. LOG(("Audio Error: Unable to swr_convert for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  692. fail();
  693. return false;
  694. }
  695. // Write audio frame
  696. AVFrame *frame = av_frame_alloc();
  697. frame->format = d->codecContext->sample_fmt;
  698. #if DA_FFMPEG_NEW_CHANNEL_LAYOUT
  699. av_channel_layout_copy(&frame->ch_layout, &d->codecContext->ch_layout);
  700. #else // DA_FFMPEG_NEW_CHANNEL_LAYOUT
  701. frame->channels = d->codecContext->channels;
  702. frame->channel_layout = d->codecContext->channel_layout;
  703. #endif // DA_FFMPEG_NEW_CHANNEL_LAYOUT
  704. frame->sample_rate = d->codecContext->sample_rate;
  705. frame->nb_samples = d->dstSamples;
  706. frame->pts = av_rescale_q(d->fullSamples, AVRational { 1, d->codecContext->sample_rate }, d->codecContext->time_base);
  707. avcodec_fill_audio_frame(frame, d->channels, d->codecContext->sample_fmt, d->dstSamplesData[0], d->dstSamplesSize, 0);
  708. if (!writeFrame(frame)) {
  709. return false;
  710. }
  711. d->fullSamples += samplesCnt;
  712. av_frame_free(&frame);
  713. return true;
  714. }
  715. bool Instance::Inner::writeFrame(AVFrame *frame) {
  716. int res = 0;
  717. char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
  718. res = avcodec_send_frame(d->codecContext, frame);
  719. if (res == AVERROR(EAGAIN)) {
  720. const auto packetsWritten = writePackets();
  721. if (packetsWritten < 0) {
  722. if (frame && packetsWritten == AVERROR_EOF) {
  723. LOG(("Audio Error: EOF in packets received when EAGAIN was got in avcodec_send_frame()"));
  724. fail();
  725. return false;
  726. }
  727. return true;
  728. } else if (!packetsWritten) {
  729. LOG(("Audio Error: No packets received when EAGAIN was got in avcodec_send_frame()"));
  730. fail();
  731. return false;
  732. }
  733. res = avcodec_send_frame(d->codecContext, frame);
  734. }
  735. if (res < 0) {
  736. LOG(("Audio Error: Unable to avcodec_send_frame for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  737. fail();
  738. return false;
  739. }
  740. if (!frame) { // drain
  741. if ((res = writePackets()) != AVERROR_EOF) {
  742. LOG(("Audio Error: not EOF in packets received when draining the codec, result %1").arg(res));
  743. fail();
  744. return false;
  745. }
  746. }
  747. return true;
  748. }
  749. int Instance::Inner::writePackets() {
  750. AVPacket *pkt = av_packet_alloc();
  751. const auto guard = gsl::finally([&] { av_packet_free(&pkt); });
  752. int res = 0;
  753. char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
  754. int written = 0;
  755. do {
  756. if ((res = avcodec_receive_packet(d->codecContext, pkt)) < 0) {
  757. if (res == AVERROR(EAGAIN)) {
  758. return written;
  759. } else if (res == AVERROR_EOF) {
  760. return res;
  761. }
  762. LOG(("Audio Error: Unable to avcodec_receive_packet for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  763. fail();
  764. return res;
  765. }
  766. av_packet_rescale_ts(pkt, d->codecContext->time_base, d->stream->time_base);
  767. pkt->stream_index = d->stream->index;
  768. if ((res = av_interleaved_write_frame(d->fmtContext, pkt)) < 0) {
  769. LOG(("Audio Error: Unable to av_interleaved_write_frame for capture, error %1, %2").arg(res).arg(av_make_error_string(err, sizeof(err), res)));
  770. fail();
  771. return -1;
  772. }
  773. ++written;
  774. av_packet_unref(pkt);
  775. } while (true);
  776. return written;
  777. }
  778. } // namespace Capture
  779. } // namespace Media