AudioCodecPluginAgnostic.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 = Core::Timer::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_result = loader->get_more_samples(sample_count);
  37. if (samples_result.is_error()) {
  38. plugin.on_decoder_error(MUST(String::formatted("Decoding failure: {}", samples_result.error())));
  39. return buffer.trim(0);
  40. }
  41. auto samples = samples_result.release_value();
  42. VERIFY(samples.size() <= sample_count);
  43. FixedMemoryStream writing_stream { buffer };
  44. for (auto& sample : samples) {
  45. MUST(writing_stream.write_value(sample.left));
  46. MUST(writing_stream.write_value(sample.right));
  47. }
  48. // FIXME: Check if we have loaded samples past the current known duration, and if so, update it
  49. // and notify the media element.
  50. return buffer.trim(writing_stream.offset());
  51. }));
  52. output->set_underrun_callback([&plugin = *plugin, loader, output]() {
  53. auto new_device_time = output->total_time_played().release_value_but_fixme_should_propagate_errors();
  54. auto new_media_time = timestamp_from_samples(loader->loaded_samples(), loader->sample_rate());
  55. plugin.m_main_thread_event_loop.deferred_invoke([&plugin, new_device_time, new_media_time]() {
  56. plugin.m_last_resume_in_device_time = new_device_time;
  57. plugin.m_last_resume_in_media_time = new_media_time;
  58. });
  59. });
  60. plugin->m_output = move(output);
  61. return plugin;
  62. }
  63. AudioCodecPluginAgnostic::AudioCodecPluginAgnostic(NonnullRefPtr<Audio::Loader> loader, Duration duration, NonnullRefPtr<Core::Timer> update_timer)
  64. : m_loader(move(loader))
  65. , m_duration(duration)
  66. , m_main_thread_event_loop(Core::EventLoop::current())
  67. , m_update_timer(move(update_timer))
  68. {
  69. m_update_timer->on_timeout = [this]() {
  70. update_timestamp();
  71. };
  72. }
  73. void AudioCodecPluginAgnostic::resume_playback()
  74. {
  75. m_paused = false;
  76. m_output->resume()
  77. ->when_resolved([this](Duration new_device_time) {
  78. m_main_thread_event_loop.deferred_invoke([this, new_device_time]() {
  79. m_last_resume_in_device_time = new_device_time;
  80. m_update_timer->start();
  81. });
  82. })
  83. .when_rejected([](Error&&) {
  84. // FIXME: Propagate errors.
  85. });
  86. }
  87. void AudioCodecPluginAgnostic::pause_playback()
  88. {
  89. m_paused = true;
  90. m_output->drain_buffer_and_suspend()
  91. ->when_resolved([this]() -> ErrorOr<void> {
  92. auto new_media_time = timestamp_from_samples(m_loader->loaded_samples(), m_loader->sample_rate());
  93. auto new_device_time = TRY(m_output->total_time_played());
  94. m_main_thread_event_loop.deferred_invoke([this, new_media_time, new_device_time]() {
  95. m_last_resume_in_media_time = new_media_time;
  96. m_last_resume_in_device_time = new_device_time;
  97. m_update_timer->stop();
  98. update_timestamp();
  99. });
  100. return {};
  101. })
  102. .when_rejected([](Error&&) {
  103. // FIXME: Propagate errors.
  104. });
  105. }
  106. void AudioCodecPluginAgnostic::set_volume(double volume)
  107. {
  108. m_output->set_volume(volume)->when_rejected([](Error&&) {
  109. // FIXME: Propagate errors.
  110. });
  111. }
  112. void AudioCodecPluginAgnostic::seek(double position)
  113. {
  114. m_output->discard_buffer_and_suspend()
  115. ->when_resolved([this, position, was_paused = m_paused]() -> ErrorOr<void> {
  116. auto sample_position = static_cast<i32>(position * m_loader->sample_rate());
  117. auto seek_result = m_loader->seek(sample_position);
  118. if (seek_result.is_error())
  119. return Error::from_string_literal("Seeking in audio loader failed");
  120. auto new_media_time = get_loader_timestamp(m_loader);
  121. auto new_device_time = m_output->total_time_played().release_value_but_fixme_should_propagate_errors();
  122. m_main_thread_event_loop.deferred_invoke([this, was_paused, new_device_time, new_media_time]() {
  123. m_last_resume_in_device_time = new_device_time;
  124. m_last_resume_in_media_time = new_media_time;
  125. if (was_paused) {
  126. update_timestamp();
  127. } else {
  128. m_output->resume()->when_rejected([](Error&&) {
  129. // FIXME: Propagate errors.
  130. });
  131. }
  132. });
  133. return {};
  134. })
  135. .when_rejected([](Error&&) {
  136. // FIXME: Propagate errors.
  137. });
  138. }
  139. Duration AudioCodecPluginAgnostic::duration()
  140. {
  141. return m_duration;
  142. }
  143. void AudioCodecPluginAgnostic::update_timestamp()
  144. {
  145. auto current_device_time_result = m_output->total_time_played();
  146. if (!current_device_time_result.is_error())
  147. m_last_good_device_time = current_device_time_result.release_value();
  148. auto current_device_time_delta = m_last_good_device_time - m_last_resume_in_device_time;
  149. auto current_media_time = m_last_resume_in_media_time + current_device_time_delta;
  150. current_media_time = min(current_media_time, m_duration);
  151. on_playback_position_updated(current_media_time);
  152. }
  153. }