/////////////////////////////////////////////////////// Get audio data from the client until playback is stopped ///////////////////////////////////////////////////////
| 115 | /// |
| 116 | //////////////////////////////////////////////////////////// |
| 117 | void receiveLoop() |
| 118 | { |
| 119 | while (!m_hasFinished) |
| 120 | { |
| 121 | // Get waiting audio data from the network |
| 122 | sf::Packet packet; |
| 123 | if (m_client.receive(packet) != sf::Socket::Status::Done) |
| 124 | break; |
| 125 | |
| 126 | // Extract the message ID |
| 127 | std::uint8_t id = 0; |
| 128 | packet >> id; |
| 129 | |
| 130 | if (id == serverAudioData) |
| 131 | { |
| 132 | // Extract audio samples from the packet, and append it to our samples buffer |
| 133 | const std::size_t sampleCount = (packet.getDataSize() - 1) / sizeof(std::int16_t); |
| 134 | |
| 135 | // Don't forget that the other thread can access the sample array at any time |
| 136 | // (so we protect any operation on it with the mutex) |
| 137 | { |
| 138 | const std::lock_guard lock(m_mutex); |
| 139 | const auto* begin = static_cast<const char*>(packet.getData()) + 1; |
| 140 | const auto* end = begin + sampleCount * sizeof(std::int16_t); |
| 141 | m_samples.insert(m_samples.end(), begin, end); |
| 142 | } |
| 143 | } |
| 144 | else if (id == serverEndOfStream) |
| 145 | { |
| 146 | // End of stream reached: we stop receiving audio data |
| 147 | std::cout << "Audio data has been 100% received!" << std::endl; |
| 148 | m_hasFinished = true; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | // Something's wrong... |
| 153 | std::cout << "Invalid packet received..." << std::endl; |
| 154 | m_hasFinished = true; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | //////////////////////////////////////////////////////////// |
| 160 | // Member data |
nothing calls this directly
no test coverage detected