|
| 1 | +#include <audioapi/core/OfflineAudioContext.h> |
| 2 | +#include <audioapi/core/destinations/AudioDestinationNode.h> |
| 3 | +#include <audioapi/core/sources/AudioScheduledSourceNode.h> |
| 4 | +#include <audioapi/core/utils/worklets/SafeIncludes.h> |
| 5 | +#include <audioapi/utils/AudioBus.h> |
| 6 | +#include <gtest/gtest.h> |
| 7 | +#include <test/src/MockAudioEventHandlerRegistry.h> |
| 8 | + |
| 9 | +using namespace audioapi; |
| 10 | +static constexpr int SAMPLE_RATE = 44100; |
| 11 | +static constexpr int RENDER_QUANTUM = 128; |
| 12 | +static constexpr double RENDER_QUANTUM_TIME = |
| 13 | + static_cast<double>(RENDER_QUANTUM) / SAMPLE_RATE; |
| 14 | + |
| 15 | +class AudioScheduledSourceTest : public ::testing::Test { |
| 16 | + protected: |
| 17 | + std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry; |
| 18 | + std::unique_ptr<OfflineAudioContext> context; |
| 19 | + |
| 20 | + void SetUp() override { |
| 21 | + eventRegistry = std::make_shared<MockAudioEventHandlerRegistry>(); |
| 22 | + context = std::make_unique<OfflineAudioContext>( |
| 23 | + 2, 5 * SAMPLE_RATE, SAMPLE_RATE, eventRegistry, RuntimeRegistry{}); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +class TestableAudioScheduledSourceNode : public AudioScheduledSourceNode { |
| 28 | + public: |
| 29 | + explicit TestableAudioScheduledSourceNode(BaseAudioContext *context) |
| 30 | + : AudioScheduledSourceNode(context) { |
| 31 | + isInitialized_ = true; |
| 32 | + } |
| 33 | + |
| 34 | + void updatePlaybackInfo( |
| 35 | + const std::shared_ptr<AudioBus> &processingBus, |
| 36 | + int framesToProcess, |
| 37 | + size_t &startOffset, |
| 38 | + size_t &nonSilentFramesToProcess) { |
| 39 | + AudioScheduledSourceNode::updatePlaybackInfo( |
| 40 | + processingBus, framesToProcess, startOffset, nonSilentFramesToProcess); |
| 41 | + } |
| 42 | + |
| 43 | + std::shared_ptr<AudioBus> processNode(const std::shared_ptr<AudioBus> &, int) |
| 44 | + override { |
| 45 | + return nullptr; |
| 46 | + } |
| 47 | + |
| 48 | + PlaybackState getPlaybackState() const { |
| 49 | + return playbackState_; |
| 50 | + } |
| 51 | + |
| 52 | + void playFrames(int frames) { |
| 53 | + size_t startOffset = 0; |
| 54 | + size_t nonSilentFramesToProcess = 0; |
| 55 | + auto processingBus = |
| 56 | + std::make_shared<AudioBus>(128, 2, static_cast<float>(SAMPLE_RATE)); |
| 57 | + updatePlaybackInfo( |
| 58 | + processingBus, frames, startOffset, nonSilentFramesToProcess); |
| 59 | + context_->getDestination()->renderAudio(processingBus, frames); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +TEST_F(AudioScheduledSourceTest, IsUnscheduledStateSetCorrectly) { |
| 64 | + auto sourceNode = TestableAudioScheduledSourceNode(context.get()); |
| 65 | + EXPECT_EQ( |
| 66 | + sourceNode.getPlaybackState(), |
| 67 | + AudioScheduledSourceNode::PlaybackState::UNSCHEDULED); |
| 68 | + |
| 69 | + sourceNode.start(RENDER_QUANTUM_TIME); |
| 70 | + EXPECT_NE( |
| 71 | + sourceNode.getPlaybackState(), |
| 72 | + AudioScheduledSourceNode::PlaybackState::UNSCHEDULED); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(AudioScheduledSourceTest, IsScheduledStateSetCorrectly) { |
| 76 | + auto sourceNode = TestableAudioScheduledSourceNode(context.get()); |
| 77 | + sourceNode.start(RENDER_QUANTUM_TIME); |
| 78 | + EXPECT_EQ( |
| 79 | + sourceNode.getPlaybackState(), |
| 80 | + AudioScheduledSourceNode::PlaybackState::SCHEDULED); |
| 81 | + |
| 82 | + sourceNode.playFrames(RENDER_QUANTUM); |
| 83 | + EXPECT_EQ( |
| 84 | + sourceNode.getPlaybackState(), |
| 85 | + AudioScheduledSourceNode::PlaybackState::SCHEDULED); |
| 86 | + |
| 87 | + sourceNode.playFrames(1); |
| 88 | + EXPECT_NE( |
| 89 | + sourceNode.getPlaybackState(), |
| 90 | + AudioScheduledSourceNode::PlaybackState::SCHEDULED); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(AudioScheduledSourceTest, IsPlayingStateSetCorrectly) { |
| 94 | + auto sourceNode = TestableAudioScheduledSourceNode(context.get()); |
| 95 | + sourceNode.start(0); |
| 96 | + sourceNode.stop(RENDER_QUANTUM_TIME); |
| 97 | + |
| 98 | + sourceNode.playFrames(RENDER_QUANTUM); |
| 99 | + EXPECT_EQ( |
| 100 | + sourceNode.getPlaybackState(), |
| 101 | + AudioScheduledSourceNode::PlaybackState::PLAYING); |
| 102 | + |
| 103 | + sourceNode.playFrames(1); |
| 104 | + EXPECT_NE( |
| 105 | + sourceNode.getPlaybackState(), |
| 106 | + AudioScheduledSourceNode::PlaybackState::PLAYING); |
| 107 | +} |
| 108 | + |
| 109 | +TEST_F(AudioScheduledSourceTest, IsStopScheduledStateSetCorrectly) { |
| 110 | + auto sourceNode = TestableAudioScheduledSourceNode(context.get()); |
| 111 | + sourceNode.start(0); |
| 112 | + sourceNode.stop(RENDER_QUANTUM_TIME); |
| 113 | + sourceNode.playFrames(1); // start playing |
| 114 | + sourceNode.playFrames(RENDER_QUANTUM); |
| 115 | + EXPECT_EQ( |
| 116 | + sourceNode.getPlaybackState(), |
| 117 | + AudioScheduledSourceNode::PlaybackState::STOP_SCHEDULED); |
| 118 | + |
| 119 | + sourceNode.playFrames(1); |
| 120 | + EXPECT_NE( |
| 121 | + sourceNode.getPlaybackState(), |
| 122 | + AudioScheduledSourceNode::PlaybackState::STOP_SCHEDULED); |
| 123 | +} |
| 124 | + |
| 125 | +TEST_F(AudioScheduledSourceTest, IsFinishedStateSetCorrectly) { |
| 126 | + auto sourceNode = TestableAudioScheduledSourceNode(context.get()); |
| 127 | + sourceNode.start(0); |
| 128 | + sourceNode.stop(RENDER_QUANTUM_TIME); |
| 129 | + sourceNode.playFrames(1); // start playing |
| 130 | + |
| 131 | + EXPECT_CALL( |
| 132 | + *eventRegistry, |
| 133 | + invokeHandlerWithEventBody("ended", testing::_, testing::_)) |
| 134 | + .Times(1); |
| 135 | + sourceNode.playFrames(RENDER_QUANTUM); |
| 136 | + sourceNode.playFrames(1); |
| 137 | + EXPECT_TRUE(sourceNode.isFinished()); |
| 138 | +} |
0 commit comments