| 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 | class AudioStreamingPartState { |
| 16 | struct ChannelMapping { |
| 17 | uint32_t ssrc = 0; |
| 18 | int channelIndex = 0; |
| 19 |
| 20 | ChannelMapping(uint32_t ssrc_, int channelIndex_) : |
| 21 | ssrc(ssrc_), channelIndex(channelIndex_) { |
| 22 | } |
| 23 | }; |
| 24 |
| 25 | public: |
| 26 | AudioStreamingPartState(std::vector<uint8_t> &&data, std::string const &container, bool isSingleChannel) : |
| 27 | _isSingleChannel(isSingleChannel), |
| 28 | _parsedPart(std::move(data), container) { |
| 29 | if (_parsedPart.getChannelUpdates().size() == 0 && !isSingleChannel) { |
| 30 | _didReadToEnd = true; |
| 31 | return; |
| 32 | } |
| 33 |
| 34 | _remainingMilliseconds = _parsedPart.getDurationInMilliseconds(); |
| 35 |
| 36 | for (const auto &it : _parsedPart.getChannelUpdates()) { |
| 37 | _allSsrcs.insert(it.ssrc); |
| 38 | } |
| 39 | } |
| 40 |
| 41 | ~AudioStreamingPartState() { |
| 42 | } |
| 43 |
| 44 | std::map<std::string, int32_t> getEndpointMapping() const { |
| 45 | return _parsedPart.getEndpointMapping(); |
| 46 | } |
| 47 |
| 48 | int getRemainingMilliseconds() const { |
| 49 | return _remainingMilliseconds; |
| 50 | } |
| 51 |
| 52 | std::vector<AudioStreamingPart::StreamingPartChannel> get10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 53 | if (_didReadToEnd) { |
| 54 | return {}; |
| 55 | } |
| 56 |
| 57 | for (const auto &update : _parsedPart.getChannelUpdates()) { |
| 58 | if (update.frameIndex == _frameIndex) { |
| 59 | updateCurrentMapping(update.ssrc, update.id); |
| 60 | } |
| 61 | } |
| 62 |
| 63 | auto readResult = _parsedPart.readPcm(persistentDecoder, _pcm10ms); |
| 64 | if (readResult.numSamples <= 0) { |
| 65 | _didReadToEnd = true; |
| 66 | return {}; |
| 67 | } |
| 68 |
| 69 | std::vector<AudioStreamingPart::StreamingPartChannel> resultChannels; |
| 70 |
| 71 | if (_isSingleChannel) { |
| 72 | for (int i = 0; i < readResult.numChannels; i++) { |
| 73 | AudioStreamingPart::StreamingPartChannel emptyPart; |
| 74 | emptyPart.ssrc = i + 1; |
| 75 | resultChannels.push_back(emptyPart); |
| 76 | } |
| 77 |
| 78 | for (int i = 0; i < readResult.numChannels; i++) { |
| 79 | auto channel = resultChannels.begin() + i; |
| 80 | int sourceChannelIndex = i; |
| 81 | for (int j = 0; j < readResult.numSamples; j++) { |
| 82 | channel->pcmData.push_back(_pcm10ms[sourceChannelIndex + j * readResult.numChannels]); |
| 83 | } |
| 84 | channel->numSamples += readResult.numSamples; |
| 85 | } |
| 86 | } else { |
| 87 | for (const auto ssrc : _allSsrcs) { |
| 88 | AudioStreamingPart::StreamingPartChannel emptyPart; |
| 89 | emptyPart.ssrc = ssrc; |
| 90 | resultChannels.push_back(emptyPart); |
| 91 | } |
| 92 |
| 93 | for (auto &channel : resultChannels) { |
| 94 | auto mappedChannelIndex = getCurrentMappedChannelIndex(channel.ssrc); |
| 95 |
| 96 | if (mappedChannelIndex) { |
| 97 | int sourceChannelIndex = mappedChannelIndex.value(); |
| 98 | for (int j = 0; j < readResult.numSamples; j++) { |
| 99 | channel.pcmData.push_back(_pcm10ms[sourceChannelIndex + j * readResult.numChannels]); |
| 100 | } |
| 101 | channel.numSamples += readResult.numSamples; |
| 102 | } else { |
| 103 | for (int j = 0; j < readResult.numSamples; j++) { |
| 104 | channel.pcmData.push_back(0); |
| 105 | } |
| 106 | channel.numSamples += readResult.numSamples; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 |
| 111 | _remainingMilliseconds -= 10; |
| 112 | if (_remainingMilliseconds < 0) { |
| 113 | _remainingMilliseconds = 0; |
| 114 | } |
| 115 | _frameIndex++; |
| 116 |
| 117 | return resultChannels; |
| 118 | } |
| 119 |
| 120 | private: |
| 121 | absl::optional<int> getCurrentMappedChannelIndex(uint32_t ssrc) { |
| 122 | for (const auto &it : _currentChannelMapping) { |
| 123 | if (it.ssrc == ssrc) { |
| 124 | return it.channelIndex; |
| 125 | } |
| 126 | } |
| 127 | return absl::nullopt; |
| 128 | } |
| 129 |
| 130 | void updateCurrentMapping(uint32_t ssrc, int channelIndex) { |
| 131 | for (int i = (int)_currentChannelMapping.size() - 1; i >= 0; i--) { |
| 132 | const auto &entry = _currentChannelMapping[i]; |
| 133 | if (entry.ssrc == ssrc && entry.channelIndex == channelIndex) { |
| 134 | return; |
| 135 | } else if (entry.ssrc == ssrc || entry.channelIndex == channelIndex) { |
| 136 | _currentChannelMapping.erase(_currentChannelMapping.begin() + i); |
| 137 | } |
| 138 | } |
| 139 | _currentChannelMapping.emplace_back(ssrc, channelIndex); |
| 140 | } |
| 141 |
| 142 | private: |
| 143 | bool _isSingleChannel = false; |
| 144 | AudioStreamingPartInternal _parsedPart; |
| 145 | std::set<uint32_t> _allSsrcs; |
| 146 |
| 147 | std::vector<int16_t> _pcm10ms; |
| 148 | std::vector<ChannelMapping> _currentChannelMapping; |
| 149 | int _frameIndex = 0; |
| 150 | int _remainingMilliseconds = 0; |
| 151 |
| 152 | bool _didReadToEnd = false; |
| 153 | }; |
| 154 |
| 155 | AudioStreamingPart::AudioStreamingPart(std::vector<uint8_t> &&data, std::string const &container, bool isSingleChannel) { |
| 156 | if (!data.empty()) { |
| 157 | _state = new AudioStreamingPartState(std::move(data), container, isSingleChannel); |
| 158 | } |
| 159 | } |
| 160 |
| 161 | AudioStreamingPart::~AudioStreamingPart() { |
| 162 | if (_state) { |
| 163 | delete _state; |
| 164 | } |
| 165 | } |
| 166 |
| 167 | std::map<std::string, int32_t> AudioStreamingPart::getEndpointMapping() const { |
| 168 | return _state ? _state->getEndpointMapping() : std::map<std::string, int32_t>(); |
| 169 | } |
| 170 |
| 171 | int AudioStreamingPart::getRemainingMilliseconds() const { |
| 172 | return _state ? _state->getRemainingMilliseconds() : 0; |
| 173 | } |
| 174 |
| 175 | std::vector<AudioStreamingPart::StreamingPartChannel> AudioStreamingPart::get10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 176 | return _state |
| 177 | ? _state->get10msPerChannel(persistentDecoder) |
| 178 | : std::vector<AudioStreamingPart::StreamingPartChannel>(); |
| 179 | } |
| 180 |
| 181 | } |
| 182 |