| 6 | extern "C" { |
| 7 | #include <libavutil/timestamp.h> |
| 8 | #include <libavformat/avformat.h> |
| 9 | #include <libavcodec/avcodec.h> |
| 10 | } |
| 11 |
| 12 | #include <string> |
| 13 | #include <bitset> |
| 14 | #include <set> |
| 15 | #include <map> |
| 16 |
| 17 | namespace tgcalls { |
| 18 |
| 19 | namespace { |
| 20 |
| 21 | int16_t sampleFloatToInt16(float sample) { |
| 22 | return av_clip_int16 (static_cast<int32_t>(lrint(sample*32767))); |
| 23 | } |
| 24 |
| 25 | uint32_t stringToUInt32(std::string const &string) { |
| 26 | std::stringstream stringStream(string); |
| 27 | uint32_t value = 0; |
| 28 | stringStream >> value; |
| 29 | return value; |
| 30 | } |
| 31 |
| 32 | template <typename Out> |
| 33 | void splitString(const std::string &s, char delim, Out result) { |
| 34 | std::istringstream iss(s); |
| 35 | std::string item; |
| 36 | while (std::getline(iss, item, delim)) { |
| 37 | *result++ = item; |
| 38 | } |
| 39 | } |
| 40 |
| 41 | std::vector<std::string> splitString(const std::string &s, char delim) { |
| 42 | std::vector<std::string> elems; |
| 43 | splitString(s, delim, std::back_inserter(elems)); |
| 44 | return elems; |
| 45 | } |
| 46 |
| 47 | static absl::optional<uint32_t> readInt32(std::string const &data, int &offset) { |
| 48 | if (offset + 4 > data.length()) { |
| 49 | return absl::nullopt; |
| 50 | } |
| 51 |
| 52 | int32_t value = 0; |
| 53 | memcpy(&value, data.data() + offset, 4); |
| 54 | offset += 4; |
| 55 |
| 56 | return value; |
| 57 | } |
| 58 |
| 59 | std::vector<AudioStreamingPartInternal::ChannelUpdate> parseChannelUpdates(std::string const &data, int &offset) { |
| 60 | std::vector<AudioStreamingPartInternal::ChannelUpdate> result; |
| 61 |
| 62 | auto channels = readInt32(data, offset); |
| 63 | if (!channels) { |
| 64 | return {}; |
| 65 | } |
| 66 |
| 67 | auto count = readInt32(data, offset); |
| 68 | if (!count) { |
| 69 | return {}; |
| 70 | } |
| 71 |
| 72 | for (int i = 0; i < count.value(); i++) { |
| 73 | auto frameIndex = readInt32(data, offset); |
| 74 | if (!frameIndex) { |
| 75 | return {}; |
| 76 | } |
| 77 |
| 78 | auto channelId = readInt32(data, offset); |
| 79 | if (!channelId) { |
| 80 | return {}; |
| 81 | } |
| 82 |
| 83 | auto ssrc = readInt32(data, offset); |
| 84 | if (!ssrc) { |
| 85 | return {}; |
| 86 | } |
| 87 |
| 88 | AudioStreamingPartInternal::ChannelUpdate update; |
| 89 | update.frameIndex = frameIndex.value(); |
| 90 | update.id = channelId.value(); |
| 91 | update.ssrc = ssrc.value(); |
| 92 |
| 93 | result.push_back(update); |
| 94 | } |
| 95 |
| 96 | return result; |
| 97 | } |
| 98 |
| 99 | } |
| 100 |
| 101 | AudioStreamingPartInternal::AudioStreamingPartInternal(std::vector<uint8_t> &&fileData, std::string const &container) : |
| 102 | _avIoContext(std::move(fileData)) { |
| 103 | int ret = 0; |
| 104 |
| 105 | _frame = av_frame_alloc(); |
| 106 |
| 107 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 108 | const |
| 109 | #endif |
| 110 | AVInputFormat *inputFormat = av_find_input_format(container.c_str()); |
| 111 | if (!inputFormat) { |
| 112 | _didReadToEnd = true; |
| 113 | return; |
| 114 | } |
| 115 |
| 116 | _inputFormatContext = avformat_alloc_context(); |
| 117 | if (!_inputFormatContext) { |
| 118 | _didReadToEnd = true; |
| 119 | return; |
| 120 | } |
| 121 |
| 122 | _inputFormatContext->pb = _avIoContext.getContext(); |
| 123 |
| 124 | if ((ret = avformat_open_input(&_inputFormatContext, "", inputFormat, nullptr)) < 0) { |
| 125 | _didReadToEnd = true; |
| 126 | return; |
| 127 | } |
| 128 |
| 129 | if ((ret = avformat_find_stream_info(_inputFormatContext, nullptr)) < 0) { |
| 130 | _didReadToEnd = true; |
| 131 |
| 132 | avformat_close_input(&_inputFormatContext); |
| 133 | _inputFormatContext = nullptr; |
| 134 | return; |
| 135 | } |
| 136 |
| 137 | for (int i = 0; i < _inputFormatContext->nb_streams; i++) { |
| 138 | AVStream *inStream = _inputFormatContext->streams[i]; |
| 139 |
| 140 | AVCodecParameters *inCodecpar = inStream->codecpar; |
| 141 | if (inCodecpar->codec_type != AVMEDIA_TYPE_AUDIO) { |
| 142 | continue; |
| 143 | } |
| 144 |
| 145 | _audioCodecParameters = avcodec_parameters_alloc(); |
| 146 | avcodec_parameters_copy(_audioCodecParameters, inCodecpar); |
| 147 |
| 148 | _streamId = i; |
| 149 |
| 150 | _durationInMilliseconds = (int)(inStream->duration * av_q2d(inStream->time_base) * 1000); |
| 151 |
| 152 | if (inStream->metadata) { |
| 153 | AVDictionaryEntry *entry = av_dict_get(inStream->metadata, "TG_META", nullptr, 0); |
| 154 | if (entry && entry->value) { |
| 155 | std::string result; |
| 156 | size_t data_used = 0; |
| 157 | std::string sourceBase64 = (const char *)entry->value; |
| 158 | rtc::Base64::Decode(sourceBase64, rtc::Base64::DO_LAX, &result, &data_used); |
| 159 |
| 160 | if (result.size() != 0) { |
| 161 | int offset = 0; |
| 162 | _channelUpdates = parseChannelUpdates(result, offset); |
| 163 | } |
| 164 | } |
| 165 |
| 166 | uint32_t videoChannelMask = 0; |
| 167 | entry = av_dict_get(inStream->metadata, "ACTIVE_MASK", nullptr, 0); |
| 168 | if (entry && entry->value) { |
| 169 | std::string sourceString = (const char *)entry->value; |
| 170 | videoChannelMask = stringToUInt32(sourceString); |
| 171 | } |
| 172 |
| 173 | std::vector<std::string> endpointList; |
| 174 | entry = av_dict_get(inStream->metadata, "ENDPOINTS", nullptr, 0); |
| 175 | if (entry && entry->value) { |
| 176 | std::string sourceString = (const char *)entry->value; |
| 177 | endpointList = splitString(sourceString, ' '); |
| 178 | } |
| 179 |
| 180 | std::bitset<32> videoChannels(videoChannelMask); |
| 181 | size_t endpointIndex = 0; |
| 182 | if (videoChannels.count() == endpointList.size()) { |
| 183 | for (size_t i = 0; i < videoChannels.size(); i++) { |
| 184 | if (videoChannels[i]) { |
| 185 | _endpointMapping.insert(std::make_pair(endpointList[endpointIndex], i)); |
| 186 | endpointIndex++; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 |
| 192 | break; |
| 193 | } |
| 194 |
| 195 | if (_streamId == -1) { |
| 196 | _didReadToEnd = true; |
| 197 | } |
| 198 | } |
| 199 |
| 200 | AudioStreamingPartInternal::~AudioStreamingPartInternal() { |
| 201 | if (_frame) { |
| 202 | av_frame_free(&_frame); |
| 203 | } |
| 204 | if (_inputFormatContext) { |
| 205 | avformat_close_input(&_inputFormatContext); |
| 206 | } |
| 207 | if (_audioCodecParameters) { |
| 208 | avcodec_parameters_free(&_audioCodecParameters); |
| 209 | } |
| 210 | } |
| 211 |
| 212 | AudioStreamingPartInternal::ReadPcmResult AudioStreamingPartInternal::readPcm(AudioStreamingPartPersistentDecoder &persistentDecoder, std::vector<int16_t> &outPcm) { |
| 213 | if (_didReadToEnd) { |
| 214 | return AudioStreamingPartInternal::ReadPcmResult(); |
| 215 | } |
| 216 |
| 217 | int outPcmSampleOffset = 0; |
| 218 | ReadPcmResult result; |
| 219 |
| 220 | if (_pcmBufferSampleOffset >= _pcmBufferSampleSize) { |
| 221 | fillPcmBuffer(persistentDecoder); |
| 222 | } |
| 223 |
| 224 | if (outPcm.size() != 480 * _channelCount) { |
| 225 | outPcm.resize(480 * _channelCount); |
| 226 | } |
| 227 | int readSamples = 0; |
| 228 | if (_channelCount != 0) { |
| 229 | readSamples = (int)outPcm.size() / _channelCount; |
| 230 | } |
| 231 |
| 232 | while (outPcmSampleOffset < readSamples) { |
| 233 | if (_pcmBufferSampleOffset >= _pcmBufferSampleSize) { |
| 234 | fillPcmBuffer(persistentDecoder); |
| 235 |
| 236 | if (_pcmBufferSampleOffset >= _pcmBufferSampleSize) { |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 |
| 241 | int readFromPcmBufferSamples = std::min(_pcmBufferSampleSize - _pcmBufferSampleOffset, readSamples - outPcmSampleOffset); |
| 242 | if (readFromPcmBufferSamples != 0) { |
| 243 | std::copy(_pcmBuffer.begin() + _pcmBufferSampleOffset * _channelCount, _pcmBuffer.begin() + _pcmBufferSampleOffset * _channelCount + readFromPcmBufferSamples * _channelCount, outPcm.begin() + outPcmSampleOffset * _channelCount); |
| 244 | _pcmBufferSampleOffset += readFromPcmBufferSamples; |
| 245 | outPcmSampleOffset += readFromPcmBufferSamples; |
| 246 | result.numSamples += readFromPcmBufferSamples; |
| 247 | _readSampleCount += readFromPcmBufferSamples; |
| 248 | } |
| 249 | } |
| 250 |
| 251 | result.numChannels = _channelCount; |
| 252 |
| 253 | // Uncomment for debugging incomplete frames |
| 254 | /*if (result.numSamples != 480 && result.numSamples != 0) { |
| 255 | RTC_LOG(LS_INFO) << "result.numSamples = " << result.numSamples << ", _readSampleCount = " << _readSampleCount << ", duration = " << _inputFormatContext->streams[_streamId]->duration; |
| 256 | }*/ |
| 257 |
| 258 | return result; |
| 259 | } |
| 260 |
| 261 | int AudioStreamingPartInternal::getDurationInMilliseconds() const { |
| 262 | return _durationInMilliseconds; |
| 263 | } |
| 264 |
| 265 | std::vector<AudioStreamingPartInternal::ChannelUpdate> const &AudioStreamingPartInternal::getChannelUpdates() const { |
| 266 | return _channelUpdates; |
| 267 | } |
| 268 |
| 269 | std::map<std::string, int32_t> AudioStreamingPartInternal::getEndpointMapping() const { |
| 270 | return _endpointMapping; |
| 271 | } |
| 272 |
| 273 | void AudioStreamingPartInternal::fillPcmBuffer(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 274 | _pcmBufferSampleSize = 0; |
| 275 | _pcmBufferSampleOffset = 0; |
| 276 |
| 277 | if (_didReadToEnd) { |
| 278 | return; |
| 279 | } |
| 280 | if (!_inputFormatContext) { |
| 281 | _didReadToEnd = true; |
| 282 | return; |
| 283 | } |
| 284 |
| 285 | int ret = 0; |
| 286 | while (true) { |
| 287 | ret = av_read_frame(_inputFormatContext, &_packet); |
| 288 | if (ret < 0) { |
| 289 | _didReadToEnd = true; |
| 290 | return; |
| 291 | } |
| 292 |
| 293 | if (_packet.stream_index != _streamId) { |
| 294 | av_packet_unref(&_packet); |
| 295 | continue; |
| 296 | } |
| 297 |
| 298 | ret = persistentDecoder.decode(_audioCodecParameters, _inputFormatContext->streams[_streamId]->time_base, _packet, _frame); |
| 299 | av_packet_unref(&_packet); |
| 300 |
| 301 | if (ret == AVERROR(EAGAIN)) { |
| 302 | continue; |
| 303 | } |
| 304 |
| 305 | break; |
| 306 | } |
| 307 |
| 308 | if (ret != 0) { |
| 309 | _didReadToEnd = true; |
| 310 | return; |
| 311 | } |
| 312 |
| 313 | if (_channelCount == 0) { |
| 314 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 315 | _channelCount = _frame->ch_layout.nb_channels; |
| 316 | #else |
| 317 | _channelCount = _frame->channels; |
| 318 | #endif |
| 319 | } |
| 320 |
| 321 | if (_channelCount == 0) { |
| 322 | _didReadToEnd = true; |
| 323 | return; |
| 324 | } |
| 325 |
| 326 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 327 | if (_frame->ch_layout.nb_channels != _channelCount || _frame->ch_layout.nb_channels > 8) { |
| 328 | #else |
| 329 | if (_frame->channels != _channelCount || _frame->channels > 8) { |
| 330 | #endif |
| 331 | _didReadToEnd = true; |
| 332 | return; |
| 333 | } |
| 334 |
| 335 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 336 | if (_pcmBuffer.size() < _frame->nb_samples * _frame->ch_layout.nb_channels) { |
| 337 | _pcmBuffer.resize(_frame->nb_samples * _frame->ch_layout.nb_channels); |
| 338 | } |
| 339 | #else |
| 340 | if (_pcmBuffer.size() < _frame->nb_samples * _frame->channels) { |
| 341 | _pcmBuffer.resize(_frame->nb_samples * _frame->channels); |
| 342 | } |
| 343 | #endif |
| 344 |
| 345 | switch (_frame->format) { |
| 346 | case AV_SAMPLE_FMT_S16: { |
| 347 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 348 | memcpy(_pcmBuffer.data(), _frame->data[0], _frame->nb_samples * 2 * _frame->ch_layout.nb_channels); |
| 349 | #else |
| 350 | memcpy(_pcmBuffer.data(), _frame->data[0], _frame->nb_samples * 2 * _frame->channels); |
| 351 | #endif |
| 352 | } break; |
| 353 |
| 354 | case AV_SAMPLE_FMT_S16P: { |
| 355 | int16_t *to = _pcmBuffer.data(); |
| 356 | for (int sample = 0; sample < _frame->nb_samples; ++sample) { |
| 357 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 358 | for (int channel = 0; channel < _frame->ch_layout.nb_channels; ++channel) { |
| 359 | #else |
| 360 | for (int channel = 0; channel < _frame->channels; ++channel) { |
| 361 | #endif |
| 362 | int16_t *shortChannel = (int16_t*)_frame->data[channel]; |
| 363 | *to++ = shortChannel[sample]; |
| 364 | } |
| 365 | } |
| 366 | } break; |
| 367 |
| 368 | case AV_SAMPLE_FMT_FLT: { |
| 369 | float *floatData = (float *)&_frame->data[0]; |
| 370 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 371 | for (int i = 0; i < _frame->nb_samples * _frame->ch_layout.nb_channels; i++) { |
| 372 | _pcmBuffer[i] = sampleFloatToInt16(floatData[i]); |
| 373 | } |
| 374 | #else |
| 375 | for (int i = 0; i < _frame->nb_samples * _frame->channels; i++) { |
| 376 | _pcmBuffer[i] = sampleFloatToInt16(floatData[i]); |
| 377 | } |
| 378 | #endif |
| 379 | } break; |
| 380 |
| 381 | case AV_SAMPLE_FMT_FLTP: { |
| 382 | int16_t *to = _pcmBuffer.data(); |
| 383 | for (int sample = 0; sample < _frame->nb_samples; ++sample) { |
| 384 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 385 | for (int channel = 0; channel < _frame->ch_layout.nb_channels; ++channel) { |
| 386 | #else |
| 387 | for (int channel = 0; channel < _frame->channels; ++channel) { |
| 388 | #endif |
| 389 | float *floatChannel = (float*)_frame->data[channel]; |
| 390 | *to++ = sampleFloatToInt16(floatChannel[sample]); |
| 391 | } |
| 392 | } |
| 393 | } break; |
| 394 |
| 395 | default: { |
| 396 | RTC_FATAL() << "Unexpected sample_fmt"; |
| 397 | } break; |
| 398 | } |
| 399 |
| 400 | _pcmBufferSampleSize = _frame->nb_samples; |
| 401 | _pcmBufferSampleOffset = 0; |
| 402 | } |
| 403 |
| 404 | } |
| 405 |