| 6 | #include "rtc_base/third_party/base64/base64.h" |
| 7 |
| 8 | #include <string> |
| 9 | #include <bitset> |
| 10 | #include <set> |
| 11 | #include <map> |
| 12 |
| 13 | namespace tgcalls { |
| 14 |
| 15 | namespace { |
| 16 |
| 17 | uint32_t stringToUInt32(std::string const &string) { |
| 18 | std::stringstream stringStream(string); |
| 19 | uint32_t value = 0; |
| 20 | stringStream >> value; |
| 21 | return value; |
| 22 | } |
| 23 |
| 24 | template <typename Out> |
| 25 | void splitString(const std::string &s, char delim, Out result) { |
| 26 | std::istringstream iss(s); |
| 27 | std::string item; |
| 28 | while (std::getline(iss, item, delim)) { |
| 29 | *result++ = item; |
| 30 | } |
| 31 | } |
| 32 |
| 33 | std::vector<std::string> splitString(const std::string &s, char delim) { |
| 34 | std::vector<std::string> elems; |
| 35 | splitString(s, delim, std::back_inserter(elems)); |
| 36 | return elems; |
| 37 | } |
| 38 |
| 39 | static absl::optional<uint32_t> readInt32(std::string const &data, int &offset) { |
| 40 | if (offset + 4 > data.length()) { |
| 41 | return absl::nullopt; |
| 42 | } |
| 43 |
| 44 | int32_t value = 0; |
| 45 | memcpy(&value, data.data() + offset, 4); |
| 46 | offset += 4; |
| 47 |
| 48 | return value; |
| 49 | } |
| 50 |
| 51 | struct ChannelUpdate { |
| 52 | int frameIndex = 0; |
| 53 | int id = 0; |
| 54 | uint32_t ssrc = 0; |
| 55 | }; |
| 56 |
| 57 | static std::vector<ChannelUpdate> parseChannelUpdates(std::string const &data, int &offset) { |
| 58 | std::vector<ChannelUpdate> result; |
| 59 |
| 60 | auto channels = readInt32(data, offset); |
| 61 | if (!channels) { |
| 62 | return {}; |
| 63 | } |
| 64 |
| 65 | auto count = readInt32(data, offset); |
| 66 | if (!count) { |
| 67 | return {}; |
| 68 | } |
| 69 |
| 70 | for (int i = 0; i < count.value(); i++) { |
| 71 | auto frameIndex = readInt32(data, offset); |
| 72 | if (!frameIndex) { |
| 73 | return {}; |
| 74 | } |
| 75 |
| 76 | auto channelId = readInt32(data, offset); |
| 77 | if (!channelId) { |
| 78 | return {}; |
| 79 | } |
| 80 |
| 81 | auto ssrc = readInt32(data, offset); |
| 82 | if (!ssrc) { |
| 83 | return {}; |
| 84 | } |
| 85 |
| 86 | ChannelUpdate update; |
| 87 | update.frameIndex = frameIndex.value(); |
| 88 | update.id = channelId.value(); |
| 89 | update.ssrc = ssrc.value(); |
| 90 |
| 91 | result.push_back(update); |
| 92 | } |
| 93 |
| 94 | return result; |
| 95 | } |
| 96 |
| 97 | class AVIOContextImpl { |
| 98 | public: |
| 99 | AVIOContextImpl(std::vector<uint8_t> &&fileData) : |
| 100 | _fileData(std::move(fileData)) { |
| 101 | _buffer.resize(4 * 1024); |
| 102 | _context = avio_alloc_context(_buffer.data(), (int)_buffer.size(), 0, this, &AVIOContextImpl::read, NULL, &AVIOContextImpl::seek); |
| 103 | } |
| 104 |
| 105 | ~AVIOContextImpl() { |
| 106 | av_free(_context); |
| 107 | } |
| 108 |
| 109 | static int read(void *opaque, unsigned char *buffer, int bufferSize) { |
| 110 | AVIOContextImpl *instance = static_cast<AVIOContextImpl *>(opaque); |
| 111 |
| 112 | int bytesToRead = std::min(bufferSize, ((int)instance->_fileData.size()) - instance->_fileReadPosition); |
| 113 | if (bytesToRead < 0) { |
| 114 | bytesToRead = 0; |
| 115 | } |
| 116 |
| 117 | if (bytesToRead > 0) { |
| 118 | memcpy(buffer, instance->_fileData.data() + instance->_fileReadPosition, bytesToRead); |
| 119 | instance->_fileReadPosition += bytesToRead; |
| 120 |
| 121 | return bytesToRead; |
| 122 | } else { |
| 123 | return AVERROR_EOF; |
| 124 | } |
| 125 | } |
| 126 |
| 127 | static int64_t seek(void *opaque, int64_t offset, int whence) { |
| 128 | AVIOContextImpl *instance = static_cast<AVIOContextImpl *>(opaque); |
| 129 |
| 130 | if (whence == 0x10000) { |
| 131 | return (int64_t)instance->_fileData.size(); |
| 132 | } else { |
| 133 | int64_t seekOffset = std::min(offset, (int64_t)instance->_fileData.size()); |
| 134 | if (seekOffset < 0) { |
| 135 | seekOffset = 0; |
| 136 | } |
| 137 | instance->_fileReadPosition = (int)seekOffset; |
| 138 | return seekOffset; |
| 139 | } |
| 140 | } |
| 141 |
| 142 | AVIOContext *getContext() { |
| 143 | return _context; |
| 144 | } |
| 145 |
| 146 | private: |
| 147 | std::vector<uint8_t> _fileData; |
| 148 | int _fileReadPosition = 0; |
| 149 |
| 150 | std::vector<uint8_t> _buffer; |
| 151 | AVIOContext *_context = nullptr; |
| 152 | }; |
| 153 |
| 154 | } |
| 155 |
| 156 | struct ReadPcmResult { |
| 157 | int numSamples = 0; |
| 158 | int numChannels = 0; |
| 159 | }; |
| 160 |
| 161 | class AudioStreamingPartState { |
| 162 | struct ChannelMapping { |
| 163 | uint32_t ssrc = 0; |
| 164 | int channelIndex = 0; |
| 165 |
| 166 | ChannelMapping(uint32_t ssrc_, int channelIndex_) : |
| 167 | ssrc(ssrc_), channelIndex(channelIndex_) { |
| 168 | } |
| 169 | }; |
| 170 |
| 171 | public: |
| 172 | AudioStreamingPartState(std::vector<uint8_t> &&data, std::string const &container, bool isSingleChannel) : |
| 173 | _isSingleChannel(isSingleChannel), |
| 174 | _parsedPart(std::move(data), container) { |
| 175 | if (_parsedPart.getChannelUpdates().size() == 0 && !isSingleChannel) { |
| 176 | _didReadToEnd = true; |
| 177 | return; |
| 178 | } |
| 179 |
| 180 | _remainingMilliseconds = _parsedPart.getDurationInMilliseconds(); |
| 181 |
| 182 | for (const auto &it : _parsedPart.getChannelUpdates()) { |
| 183 | _allSsrcs.insert(it.ssrc); |
| 184 | } |
| 185 | } |
| 186 |
| 187 | ~AudioStreamingPartState() { |
| 188 | } |
| 189 |
| 190 | std::map<std::string, int32_t> getEndpointMapping() const { |
| 191 | return _parsedPart.getEndpointMapping(); |
| 192 | } |
| 193 |
| 194 | int getRemainingMilliseconds() const { |
| 195 | return _remainingMilliseconds; |
| 196 | } |
| 197 |
| 198 | std::vector<AudioStreamingPart::StreamingPartChannel> get10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 199 | if (_didReadToEnd) { |
| 200 | return {}; |
| 201 | } |
| 202 |
| 203 | for (const auto &update : _parsedPart.getChannelUpdates()) { |
| 204 | if (update.frameIndex == _frameIndex) { |
| 205 | updateCurrentMapping(update.ssrc, update.id); |
| 206 | } |
| 207 | } |
| 208 |
| 209 | auto readResult = _parsedPart.readPcm(persistentDecoder, _pcm10ms); |
| 210 | if (readResult.numSamples <= 0) { |
| 211 | _didReadToEnd = true; |
| 212 | return {}; |
| 213 | } |
| 214 |
| 215 | std::vector<AudioStreamingPart::StreamingPartChannel> resultChannels; |
| 216 |
| 217 | if (_isSingleChannel) { |
| 218 | for (int i = 0; i < readResult.numChannels; i++) { |
| 219 | AudioStreamingPart::StreamingPartChannel emptyPart; |
| 220 | emptyPart.ssrc = i + 1; |
| 221 | resultChannels.push_back(emptyPart); |
| 222 | } |
| 223 |
| 224 | for (int i = 0; i < readResult.numChannels; i++) { |
| 225 | auto channel = resultChannels.begin() + i; |
| 226 | int sourceChannelIndex = i; |
| 227 | for (int j = 0; j < readResult.numSamples; j++) { |
| 228 | channel->pcmData.push_back(_pcm10ms[sourceChannelIndex + j * readResult.numChannels]); |
| 229 | } |
| 230 | channel->numSamples += readResult.numSamples; |
| 231 | } |
| 232 | } else { |
| 233 | for (const auto ssrc : _allSsrcs) { |
| 234 | AudioStreamingPart::StreamingPartChannel emptyPart; |
| 235 | emptyPart.ssrc = ssrc; |
| 236 | resultChannels.push_back(emptyPart); |
| 237 | } |
| 238 |
| 239 | for (auto &channel : resultChannels) { |
| 240 | auto mappedChannelIndex = getCurrentMappedChannelIndex(channel.ssrc); |
| 241 |
| 242 | if (mappedChannelIndex) { |
| 243 | int sourceChannelIndex = mappedChannelIndex.value(); |
| 244 | for (int j = 0; j < readResult.numSamples; j++) { |
| 245 | channel.pcmData.push_back(_pcm10ms[sourceChannelIndex + j * readResult.numChannels]); |
| 246 | } |
| 247 | channel.numSamples += readResult.numSamples; |
| 248 | } else { |
| 249 | for (int j = 0; j < readResult.numSamples; j++) { |
| 250 | channel.pcmData.push_back(0); |
| 251 | } |
| 252 | channel.numSamples += readResult.numSamples; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 |
| 257 | _remainingMilliseconds -= 10; |
| 258 | if (_remainingMilliseconds < 0) { |
| 259 | _remainingMilliseconds = 0; |
| 260 | } |
| 261 | _frameIndex++; |
| 262 |
| 263 | return resultChannels; |
| 264 | } |
| 265 |
| 266 | private: |
| 267 | absl::optional<int> getCurrentMappedChannelIndex(uint32_t ssrc) { |
| 268 | for (const auto &it : _currentChannelMapping) { |
| 269 | if (it.ssrc == ssrc) { |
| 270 | return it.channelIndex; |
| 271 | } |
| 272 | } |
| 273 | return absl::nullopt; |
| 274 | } |
| 275 |
| 276 | void updateCurrentMapping(uint32_t ssrc, int channelIndex) { |
| 277 | for (int i = (int)_currentChannelMapping.size() - 1; i >= 0; i--) { |
| 278 | const auto &entry = _currentChannelMapping[i]; |
| 279 | if (entry.ssrc == ssrc && entry.channelIndex == channelIndex) { |
| 280 | return; |
| 281 | } else if (entry.ssrc == ssrc || entry.channelIndex == channelIndex) { |
| 282 | _currentChannelMapping.erase(_currentChannelMapping.begin() + i); |
| 283 | } |
| 284 | } |
| 285 | _currentChannelMapping.emplace_back(ssrc, channelIndex); |
| 286 | } |
| 287 |
| 288 | private: |
| 289 | bool _isSingleChannel = false; |
| 290 | AudioStreamingPartInternal _parsedPart; |
| 291 | std::set<uint32_t> _allSsrcs; |
| 292 |
| 293 | std::vector<int16_t> _pcm10ms; |
| 294 | std::vector<ChannelMapping> _currentChannelMapping; |
| 295 | int _frameIndex = 0; |
| 296 | int _remainingMilliseconds = 0; |
| 297 |
| 298 | bool _didReadToEnd = false; |
| 299 | }; |
| 300 |
| 301 | AudioStreamingPart::AudioStreamingPart(std::vector<uint8_t> &&data, std::string const &container, bool isSingleChannel) { |
| 302 | if (!data.empty()) { |
| 303 | _state = new AudioStreamingPartState(std::move(data), container, isSingleChannel); |
| 304 | } |
| 305 | } |
| 306 |
| 307 | AudioStreamingPart::~AudioStreamingPart() { |
| 308 | if (_state) { |
| 309 | delete _state; |
| 310 | } |
| 311 | } |
| 312 |
| 313 | std::map<std::string, int32_t> AudioStreamingPart::getEndpointMapping() const { |
| 314 | return _state ? _state->getEndpointMapping() : std::map<std::string, int32_t>(); |
| 315 | } |
| 316 |
| 317 | int AudioStreamingPart::getRemainingMilliseconds() const { |
| 318 | return _state ? _state->getRemainingMilliseconds() : 0; |
| 319 | } |
| 320 |
| 321 | std::vector<AudioStreamingPart::StreamingPartChannel> AudioStreamingPart::get10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 322 | return _state |
| 323 | ? _state->get10msPerChannel(persistentDecoder) |
| 324 | : std::vector<AudioStreamingPart::StreamingPartChannel>(); |
| 325 | } |
| 326 |
| 327 | } |
| 328 |