AudioCodecPluginAgnostic.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2023, Gregory Bertilson <zaggy1024@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/MemoryStream.h>
  7. #include <AK/WeakPtr.h>
  8. #include <LibAudio/Loader.h>
  9. #include <LibCore/EventLoop.h>
  10. #include <LibCore/ThreadedPromise.h>
  11. #include <LibCore/Timer.h>
  12. #include "AudioCodecPluginAgnostic.h"
  13. namespace Web::Platform {
  14. constexpr int update_interval = 50;
  15. static Duration timestamp_from_samples(i64 samples, u32 sample_rate)
  16. {
  17. return Duration::from_milliseconds(samples * 1000 / sample_rate);
  18. }
  19. static Duration get_loader_timestamp(NonnullRefPtr<Audio::Loader> const& loader)
  20. {
  21. return timestamp_from_samples(loader->loaded_samples(), loader->sample_rate());
  22. }
  23. ErrorOr<NonnullOwnPtr<AudioCodecPluginAgnostic>> AudioCodecPluginAgnostic::create(NonnullRefPtr<Audio::Loader> const& loader)
  24. {
  25. auto duration = timestamp_from_samples(loader->total_samples(), loader->sample_rate());
  26. auto update_timer = TRY(Core::Timer::try_create());
  27. update_timer->set_interval(update_interval);
  28. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) AudioCodecPluginAgnostic(loader, duration, move(update_timer))));
  29. constexpr u32 latency_ms = 100;
  30. // FIXME: Audio loaders are hard-coded to output stereo audio. Once that changes, the channel count provided
  31. // below should be retrieved from the audio loader instead of being hard-coded to 2.
  32. RefPtr<Audio::PlaybackStream> output = TRY(Audio::PlaybackStream::create(
  33. Audio::OutputState::Suspended, loader->sample_rate(), /* channels = */ 2, latency_ms,
  34. [&plugin = *plugin, loader](Bytes buffer, Audio::PcmSampleFormat format, size_t sample_count) -> ReadonlyBytes {
  35. VERIFY(format == Audio::PcmSampleFormat::Float32);
  36. auto samples = loader->get_more_samples(sample_count).release_value_but_fixme_should_propagate_errors();
  37. VERIFY(samples.size() <= sample_count);
  38. FixedMemoryStream writing_stream { buffer };
  39. for (auto& sample : samples) {
  40. MUST(writing_stream.write_value(sample.left));
  41. MUST(writing_stream.write_value(sample.right));
  42. }
  43. // FIXME: Check if we have loaded samples past the current known duration, and if so, update it
  44. // and notify the media element.
  45. return buffer.trim(writing_stream.offset());
  46. }));
  47. output->set_underrun_callback([&plugin = *plugin, loader, output]() {
  48. auto new_device_time = output->total_time_played().release_value_but_fixme_should_propagate_errors();
  49. auto new_media_time = timestamp_from_samples(loader->loaded_samples(), loader->sample_rate());
  50. plugin.m_main_thread_event_loop.deferred_invoke([&plugin, new_device_time, new_media_time]() {
  51. plugin.m_last_resume_in_device_time = new_device_time;
  52. plugin.m_last_resume_in_media_time = new_media_time;
  53. });
  54. });
  55. plugin->m_output = move(output);
  56. return plugin;
  57. }
  58. AudioCodecPluginAgnostic::AudioCodecPluginAgnostic(NonnullRefPtr<Audio::Loader> loader, Duration duration, NonnullRefPtr<Core::Timer> update_timer)
  59. : m_loader(move(loader))
  60. , m_duration(duration)
  61. , m_main_thread_event_loop(Core::EventLoop::current())
  62. , m_update_timer(move(update_timer))
  63. {
  64. m_update_timer->on_timeout = [this]() {
  65. update_timestamp();
  66. };
  67. }
  68. void AudioCodecPluginAgnostic::resume_playback()
  69. {
  70. m_paused = false;
  71. m_output->resume()
  72. ->when_resolved([this](Duration new_device_time) {
  73. m_main_thread_event_loop.deferred_invoke([this, new_device_time]() {
  74. m_last_resume_in_device_time = new_device_time;
  75. m_update_timer->start();
  76. });
  77. })
  78. .when_rejected([](Error&&) {
  79. // FIXME: Propagate errors.
  80. });
  81. }
  82. void AudioCodecPluginAgnostic::pause_playback()
  83. {
  84. m_paused = true;
  85. m_output->drain_buffer_and_suspend()
  86. ->when_resolved([this]() -> ErrorOr<void> {
  87. auto new_media_time = timestamp_from_samples(m_loader->loaded_samples(), m_loader->sample_rate());
  88. auto new_device_time = TRY(m_output->total_time_played());
  89. m_main_thread_event_loop.deferred_invoke([this, new_media_time, new_device_time]() {
  90. m_last_resume_in_media_time = new_media_time;
  91. m_last_resume_in_device_time = new_device_time;
  92. m_update_timer->stop();
  93. update_timestamp();
  94. });
  95. return {};
  96. })
  97. .when_rejected([](Error&&) {
  98. // FIXME: Propagate errors.
  99. });
  100. }
  101. void AudioCodecPluginAgnostic::set_volume(double volume)
  102. {
  103. m_output->set_volume(volume)->when_rejected([](Error&&) {
  104. // FIXME: Propagate errors.
  105. });
  106. }
  107. void AudioCodecPluginAgnostic::seek(double position)
  108. {
  109. m_output->discard_buffer_and_suspend()
  110. ->when_resolved([this, position, was_paused = m_paused]() -> ErrorOr<void> {
  111. auto sample_position = static_cast<i32>(position * m_loader->sample_rate());
  112. auto seek_result = m_loader->seek(sample_position);
  113. if (seek_result.is_error())
  114. return Error::from_string_literal("Seeking in audio loader failed");
  115. auto new_media_time = get_loader_timestamp(m_loader);
  116. auto new_device_time = m_output->total_time_played().release_value_but_fixme_should_propagate_errors();
  117. m_main_thread_event_loop.deferred_invoke([this, was_paused, new_device_time, new_media_time]() {
  118. m_last_resume_in_device_time = new_device_time;
  119. m_last_resume_in_media_time = new_media_time;
  120. if (was_paused) {
  121. update_timestamp();
  122. } else {
  123. m_output->resume()->when_rejected([](Error&&) {
  124. // FIXME: Propagate errors.
  125. });
  126. }
  127. });
  128. return {};
  129. })
  130. .when_rejected([](Error&&) {
  131. // FIXME: Propagate errors.
  132. });
  133. }
  134. Duration AudioCodecPluginAgnostic::duration()
  135. {
  136. return m_duration;
  137. }
  138. void AudioCodecPluginAgnostic::update_timestamp()
  139. {
  140. auto current_device_time_result = m_output->total_time_played();
  141. if (!current_device_time_result.is_error())
  142. m_last_good_device_time = current_device_time_result.release_value();
  143. auto current_device_time_delta = m_last_good_device_time - m_last_resume_in_device_time;
  144. auto current_media_time = m_last_resume_in_media_time + current_device_time_delta;
  145. current_media_time = min(current_media_time, m_duration);
  146. on_playback_position_updated(current_media_time);
  147. }
  148. }