| #include "api/video/i420_buffer.h" |
| 6 |
| 7 | #include "AVIOContextImpl.h" |
| 8 |
| 9 | #include <string> |
| 10 | #include <set> |
| 11 | #include <map> |
| 12 |
| 13 | namespace tgcalls { |
| 14 |
| 15 | namespace { |
| 16 |
| 17 | class MediaDataPacket { |
| 18 | public: |
| 19 | MediaDataPacket() : _packet(av_packet_alloc()) { |
| 20 | } |
| 21 |
| 22 | MediaDataPacket(MediaDataPacket &&other) : _packet(other._packet) { |
| 23 | other._packet = nullptr; |
| 24 | } |
| 25 |
| 26 | ~MediaDataPacket() { |
| 27 | if (_packet) { |
| 28 | av_packet_free(&_packet); |
| 29 | } |
| 30 | } |
| 31 |
| 32 | AVPacket *packet() { |
| 33 | return _packet; |
| 34 | } |
| 35 |
| 36 | private: |
| 37 | AVPacket *_packet = nullptr; |
| 38 | }; |
| 39 |
| 40 | class DecodableFrame { |
| 41 | public: |
| 42 | DecodableFrame(MediaDataPacket packet, int64_t pts, int64_t dts): |
| 43 | _packet(std::move(packet)), |
| 44 | _pts(pts), |
| 45 | _dts(dts) { |
| 46 | } |
| 47 |
| 48 | ~DecodableFrame() { |
| 49 | } |
| 50 |
| 51 | MediaDataPacket &packet() { |
| 52 | return _packet; |
| 53 | } |
| 54 |
| 55 | int64_t pts() { |
| 56 | return _pts; |
| 57 | } |
| 58 |
| 59 | int64_t dts() { |
| 60 | return _dts; |
| 61 | } |
| 62 |
| 63 | private: |
| 64 | MediaDataPacket _packet; |
| 65 | int64_t _pts = 0; |
| 66 | int64_t _dts = 0; |
| 67 | }; |
| 68 |
| 69 | class Frame { |
| 70 | public: |
| 71 | Frame() { |
| 72 | _frame = av_frame_alloc(); |
| 73 | } |
| 74 |
| 75 | Frame(Frame &&other) { |
| 76 | _frame = other._frame; |
| 77 | other._frame = nullptr; |
| 78 | } |
| 79 |
| 80 | ~Frame() { |
| 81 | if (_frame) { |
| 82 | av_frame_unref(_frame); |
| 83 | } |
| 84 | } |
| 85 |
| 86 | AVFrame *frame() { |
| 87 | return _frame; |
| 88 | } |
| 89 |
| 90 | double pts(AVStream *stream, double &firstFramePts) { |
| 91 | int64_t framePts = _frame->pts; |
| 92 | double spf = av_q2d(stream->time_base); |
| 93 | double value = ((double)framePts) * spf; |
| 94 |
| 95 | if (firstFramePts < 0.0) { |
| 96 | firstFramePts = value; |
| 97 | } |
| 98 |
| 99 | return value - firstFramePts; |
| 100 | } |
| 101 |
| 102 | private: |
| 103 | AVFrame *_frame = nullptr; |
| 104 | }; |
| 105 |
| 106 | struct VideoStreamEvent { |
| 107 | int32_t offset = 0; |
| 108 | std::string endpointId; |
| 109 | int32_t rotation = 0; |
| 110 | int32_t extra = 0; |
| 111 | }; |
| 112 |
| 113 | struct VideoStreamInfo { |
| 114 | std::string container; |
| 115 | int32_t activeMask = 0; |
| 116 | std::vector<VideoStreamEvent> events; |
| 117 | }; |
| 118 |
| 119 | absl::optional<int32_t> readInt32(std::vector<uint8_t> const &data, int &offset) { |
| 120 | if (offset + 4 > data.size()) { |
| 121 | return absl::nullopt; |
| 122 | } |
| 123 |
| 124 | int32_t value = 0; |
| 125 | memcpy(&value, data.data() + offset, 4); |
| 126 | offset += 4; |
| 127 |
| 128 | return value; |
| 129 | } |
| 130 |
| 131 | absl::optional<uint8_t> readBytesAsInt32(std::vector<uint8_t> const &data, int &offset, int count) { |
| 132 | if (offset + count > data.size()) { |
| 133 | return absl::nullopt; |
| 134 | } |
| 135 |
| 136 | if (count == 0) { |
| 137 | return absl::nullopt; |
| 138 | } |
| 139 |
| 140 | if (count <= 4) { |
| 141 | int32_t value = 0; |
| 142 | memcpy(&value, data.data() + offset, count); |
| 143 | offset += count; |
| 144 | return value; |
| 145 | } else { |
| 146 | return absl::nullopt; |
| 147 | } |
| 148 | } |
| 149 |
| 150 | int32_t roundUp(int32_t numToRound, int32_t multiple) { |
| 151 | if (multiple == 0) { |
| 152 | return numToRound; |
| 153 | } |
| 154 |
| 155 | int32_t remainder = numToRound % multiple; |
| 156 | if (remainder == 0) { |
| 157 | return numToRound; |
| 158 | } |
| 159 |
| 160 | return numToRound + multiple - remainder; |
| 161 | } |
| 162 |
| 163 | absl::optional<std::string> readSerializedString(std::vector<uint8_t> const &data, int &offset) { |
| 164 | if (const auto tmp = readBytesAsInt32(data, offset, 1)) { |
| 165 | int paddingBytes = 0; |
| 166 | int length = 0; |
| 167 | if (tmp.value() == 254) { |
| 168 | if (const auto len = readBytesAsInt32(data, offset, 3)) { |
| 169 | length = len.value(); |
| 170 | paddingBytes = roundUp(length, 4) - length; |
| 171 | } else { |
| 172 | return absl::nullopt; |
| 173 | } |
| 174 | } |
| 175 | else { |
| 176 | length = tmp.value(); |
| 177 | paddingBytes = roundUp(length + 1, 4) - (length + 1); |
| 178 | } |
| 179 |
| 180 | if (offset + length > data.size()) { |
| 181 | return absl::nullopt; |
| 182 | } |
| 183 |
| 184 | std::string result(data.data() + offset, data.data() + offset + length); |
| 185 |
| 186 | offset += length; |
| 187 | offset += paddingBytes; |
| 188 |
| 189 | return result; |
| 190 | } else { |
| 191 | return absl::nullopt; |
| 192 | } |
| 193 | } |
| 194 |
| 195 | absl::optional<VideoStreamEvent> readVideoStreamEvent(std::vector<uint8_t> const &data, int &offset) { |
| 196 | VideoStreamEvent event; |
| 197 |
| 198 | if (const auto offsetValue = readInt32(data, offset)) { |
| 199 | event.offset = offsetValue.value(); |
| 200 | } else { |
| 201 | return absl::nullopt; |
| 202 | } |
| 203 |
| 204 | if (const auto endpointId = readSerializedString(data, offset)) { |
| 205 | event.endpointId = endpointId.value(); |
| 206 | } else { |
| 207 | return absl::nullopt; |
| 208 | } |
| 209 |
| 210 | if (const auto rotation = readInt32(data, offset)) { |
| 211 | event.rotation = rotation.value(); |
| 212 | } else { |
| 213 | return absl::nullopt; |
| 214 | } |
| 215 |
| 216 | if (const auto extra = readInt32(data, offset)) { |
| 217 | event.extra = extra.value(); |
| 218 | } else { |
| 219 | return absl::nullopt; |
| 220 | } |
| 221 |
| 222 | return event; |
| 223 | } |
| 224 |
| 225 | absl::optional<VideoStreamInfo> consumeVideoStreamInfo(std::vector<uint8_t> &data) { |
| 226 | int offset = 0; |
| 227 | if (const auto signature = readInt32(data, offset)) { |
| 228 | if (signature.value() != 0xa12e810d) { |
| 229 | return absl::nullopt; |
| 230 | } |
| 231 | } else { |
| 232 | return absl::nullopt; |
| 233 | } |
| 234 |
| 235 | VideoStreamInfo info; |
| 236 |
| 237 | if (const auto container = readSerializedString(data, offset)) { |
| 238 | info.container = container.value(); |
| 239 | } else { |
| 240 | return absl::nullopt; |
| 241 | } |
| 242 |
| 243 | if (const auto activeMask = readInt32(data, offset)) { |
| 244 | info.activeMask = activeMask.value(); |
| 245 | } else { |
| 246 | return absl::nullopt; |
| 247 | } |
| 248 |
| 249 | if (const auto eventCount = readInt32(data, offset)) { |
| 250 | if (eventCount > 0) { |
| 251 | if (const auto event = readVideoStreamEvent(data, offset)) { |
| 252 | info.events.push_back(event.value()); |
| 253 | } else { |
| 254 | return absl::nullopt; |
| 255 | } |
| 256 | } else { |
| 257 | return absl::nullopt; |
| 258 | } |
| 259 | } else { |
| 260 | return absl::nullopt; |
| 261 | } |
| 262 |
| 263 | data.erase(data.begin(), data.begin() + offset); |
| 264 |
| 265 | return info; |
| 266 | } |
| 267 |
| 268 | } |
| 269 |
| 270 | class VideoStreamingPartInternal { |
| 271 | public: |
| 272 | VideoStreamingPartInternal(std::string endpointId, webrtc::VideoRotation rotation, std::vector<uint8_t> &&fileData, std::string const &container) : |
| 273 | _endpointId(endpointId), |
| 274 | _rotation(rotation) { |
| 275 | _avIoContext = std::make_unique<AVIOContextImpl>(std::move(fileData)); |
| 276 |
| 277 | int ret = 0; |
| 278 |
| 279 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 280 | const |
| 281 | #endif |
| 282 | AVInputFormat *inputFormat = av_find_input_format(container.c_str()); |
| 283 | if (!inputFormat) { |
| 284 | _didReadToEnd = true; |
| 285 | return; |
| 286 | } |
| 287 |
| 288 | _inputFormatContext = avformat_alloc_context(); |
| 289 | if (!_inputFormatContext) { |
| 290 | _didReadToEnd = true; |
| 291 | return; |
| 292 | } |
| 293 |
| 294 | _inputFormatContext->pb = _avIoContext->getContext(); |
| 295 |
| 296 | if ((ret = avformat_open_input(&_inputFormatContext, "", inputFormat, nullptr)) < 0) { |
| 297 | _didReadToEnd = true; |
| 298 | return; |
| 299 | } |
| 300 |
| 301 | if ((ret = avformat_find_stream_info(_inputFormatContext, nullptr)) < 0) { |
| 302 | _didReadToEnd = true; |
| 303 |
| 304 | avformat_close_input(&_inputFormatContext); |
| 305 | _inputFormatContext = nullptr; |
| 306 | return; |
| 307 | } |
| 308 |
| 309 | AVCodecParameters *videoCodecParameters = nullptr; |
| 310 | AVStream *videoStream = nullptr; |
| 311 | for (int i = 0; i < _inputFormatContext->nb_streams; i++) { |
| 312 | AVStream *inStream = _inputFormatContext->streams[i]; |
| 313 |
| 314 | AVCodecParameters *inCodecpar = inStream->codecpar; |
| 315 | if (inCodecpar->codec_type != AVMEDIA_TYPE_VIDEO) { |
| 316 | continue; |
| 317 | } |
| 318 | videoCodecParameters = inCodecpar; |
| 319 | videoStream = inStream; |
| 320 |
| 321 | break; |
| 322 | } |
| 323 |
| 324 | if (videoCodecParameters && videoStream) { |
| 325 | const AVCodec *codec = avcodec_find_decoder(videoCodecParameters->codec_id); |
| 326 | if (codec) { |
| 327 | _codecContext = avcodec_alloc_context3(codec); |
| 328 | ret = avcodec_parameters_to_context(_codecContext, videoCodecParameters); |
| 329 | if (ret < 0) { |
| 330 | _didReadToEnd = true; |
| 331 |
| 332 | avcodec_free_context(&_codecContext); |
| 333 | _codecContext = nullptr; |
| 334 | } else { |
| 335 | _codecContext->pkt_timebase = videoStream->time_base; |
| 336 |
| 337 | ret = avcodec_open2(_codecContext, codec, nullptr); |
| 338 | if (ret < 0) { |
| 339 | _didReadToEnd = true; |
| 340 |
| 341 | avcodec_free_context(&_codecContext); |
| 342 | _codecContext = nullptr; |
| 343 | } else { |
| 344 | _videoStream = videoStream; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 |
| 351 | ~VideoStreamingPartInternal() { |
| 352 | if (_codecContext) { |
| 353 | avcodec_close(_codecContext); |
| 354 | avcodec_free_context(&_codecContext); |
| 355 | } |
| 356 | if (_inputFormatContext) { |
| 357 | avformat_close_input(&_inputFormatContext); |
| 358 | } |
| 359 | } |
| 360 |
| 361 | std::string endpointId() { |
| 362 | return _endpointId; |
| 363 | } |
| 364 |
| 365 | absl::optional<MediaDataPacket> readPacket() { |
| 366 | if (_didReadToEnd) { |
| 367 | return absl::nullopt; |
| 368 | } |
| 369 | if (!_inputFormatContext) { |
| 370 | return absl::nullopt; |
| 371 | } |
| 372 |
| 373 | MediaDataPacket packet; |
| 374 | int result = av_read_frame(_inputFormatContext, packet.packet()); |
| 375 | if (result < 0) { |
| 376 | return absl::nullopt; |
| 377 | } |
| 378 |
| 379 | return packet; |
| 380 | } |
| 381 |
| 382 | std::shared_ptr<DecodableFrame> readNextDecodableFrame() { |
| 383 | while (true) { |
| 384 | absl::optional<MediaDataPacket> packet = readPacket(); |
| 385 | if (packet) { |
| 386 | if (_videoStream && packet->packet()->stream_index == _videoStream->index) { |
| 387 | return std::make_shared<DecodableFrame>(std::move(packet.value()), packet->packet()->pts, packet->packet()->dts); |
| 388 | } |
| 389 | } else { |
| 390 | return nullptr; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 |
| 395 | absl::optional<VideoStreamingPartFrame> convertCurrentFrame() { |
| 396 | rtc::scoped_refptr<webrtc::I420Buffer> i420Buffer = webrtc::I420Buffer::Copy( |
| 397 | _frame.frame()->width, |
| 398 | _frame.frame()->height, |
| 399 | _frame.frame()->data[0], |
| 400 | _frame.frame()->linesize[0], |
| 401 | _frame.frame()->data[1], |
| 402 | _frame.frame()->linesize[1], |
| 403 | _frame.frame()->data[2], |
| 404 | _frame.frame()->linesize[2] |
| 405 | ); |
| 406 | if (i420Buffer) { |
| 407 | auto videoFrame = webrtc::VideoFrame::Builder() |
| 408 | .set_video_frame_buffer(i420Buffer) |
| 409 | .set_rotation(_rotation) |
| 410 | .build(); |
| 411 |
| 412 | return VideoStreamingPartFrame(_endpointId, videoFrame, _frame.pts(_videoStream, _firstFramePts), _frameIndex); |
| 413 | } else { |
| 414 | return absl::nullopt; |
| 415 | } |
| 416 | } |
| 417 |
| 418 | absl::optional<VideoStreamingPartFrame> getNextFrame() { |
| 419 | if (!_codecContext) { |
| 420 | return {}; |
| 421 | } |
| 422 |
| 423 | while (true) { |
| 424 | if (_didReadToEnd) { |
| 425 | if (!_finalFrames.empty()) { |
| 426 | auto frame = _finalFrames[0]; |
| 427 | _finalFrames.erase(_finalFrames.begin()); |
| 428 | return frame; |
| 429 | } else { |
| 430 | break; |
| 431 | } |
| 432 | } else { |
| 433 | const auto frame = readNextDecodableFrame(); |
| 434 | if (frame) { |
| 435 | auto status = avcodec_send_packet(_codecContext, frame->packet().packet()); |
| 436 | if (status == 0) { |
| 437 | auto status = avcodec_receive_frame(_codecContext, _frame.frame()); |
| 438 | if (status == 0) { |
| 439 | auto convertedFrame = convertCurrentFrame(); |
| 440 | if (convertedFrame) { |
| 441 | _frameIndex++; |
| 442 | return convertedFrame; |
| 443 | } |
| 444 | } else if (status == AVERROR(EAGAIN)) { |
| 445 | // more data needed |
| 446 | } else { |
| 447 | _didReadToEnd = true; |
| 448 | break; |
| 449 | } |
| 450 | } else { |
| 451 | _didReadToEnd = true; |
| 452 | return {}; |
| 453 | } |
| 454 | } else { |
| 455 | _didReadToEnd = true; |
| 456 | int status = avcodec_send_packet(_codecContext, nullptr); |
| 457 | if (status == 0) { |
| 458 | while (true) { |
| 459 | auto status = avcodec_receive_frame(_codecContext, _frame.frame()); |
| 460 | if (status == 0) { |
| 461 | auto convertedFrame = convertCurrentFrame(); |
| 462 | if (convertedFrame) { |
| 463 | _frameIndex++; |
| 464 | _finalFrames.push_back(convertedFrame.value()); |
| 465 | } |
| 466 | } else { |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 |
| 475 | return {}; |
| 476 | } |
| 477 |
| 478 | private: |
| 479 | std::string _endpointId; |
| 480 | webrtc::VideoRotation _rotation = webrtc::VideoRotation::kVideoRotation_0; |
| 481 |
| 482 | std::unique_ptr<AVIOContextImpl> _avIoContext; |
| 483 |
| 484 | AVFormatContext *_inputFormatContext = nullptr; |
| 485 | AVCodecContext *_codecContext = nullptr; |
| 486 | AVStream *_videoStream = nullptr; |
| 487 | Frame _frame; |
| 488 |
| 489 | std::vector<VideoStreamingPartFrame> _finalFrames; |
| 490 |
| 491 | int _frameIndex = 0; |
| 492 | double _firstFramePts = -1.0; |
| 493 | bool _didReadToEnd = false; |
| 494 | }; |
| 495 |
| 496 | class VideoStreamingPartState { |
| 497 | public: |
| 498 | VideoStreamingPartState(std::vector<uint8_t> &&data, VideoStreamingPart::ContentType contentType) { |
| 499 | _videoStreamInfo = consumeVideoStreamInfo(data); |
| 500 | if (!_videoStreamInfo) { |
| 501 | return; |
| 502 | } |
| 503 |
| 504 | for (size_t i = 0; i < _videoStreamInfo->events.size(); i++) { |
| 505 | if (_videoStreamInfo->events[i].offset < 0) { |
| 506 | continue; |
| 507 | } |
| 508 | size_t endOffset = 0; |
| 509 | if (i == _videoStreamInfo->events.size() - 1) { |
| 510 | endOffset = data.size(); |
| 511 | } else { |
| 512 | endOffset = _videoStreamInfo->events[i + 1].offset; |
| 513 | } |
| 514 | if (endOffset <= _videoStreamInfo->events[i].offset) { |
| 515 | continue; |
| 516 | } |
| 517 | if (endOffset > data.size()) { |
| 518 | continue; |
| 519 | } |
| 520 | std::vector<uint8_t> dataSlice(data.begin() + _videoStreamInfo->events[i].offset, data.begin() + endOffset); |
| 521 | webrtc::VideoRotation rotation = webrtc::VideoRotation::kVideoRotation_0; |
| 522 | switch (_videoStreamInfo->events[i].rotation) { |
| 523 | case 0: { |
| 524 | rotation = webrtc::VideoRotation::kVideoRotation_0; |
| 525 | break; |
| 526 | } |
| 527 | case 90: { |
| 528 | rotation = webrtc::VideoRotation::kVideoRotation_90; |
| 529 | break; |
| 530 | } |
| 531 | case 180: { |
| 532 | rotation = webrtc::VideoRotation::kVideoRotation_180; |
| 533 | break; |
| 534 | } |
| 535 | case 270: { |
| 536 | rotation = webrtc::VideoRotation::kVideoRotation_270; |
| 537 | break; |
| 538 | } |
| 539 | default: { |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 |
| 544 | switch (contentType) { |
| 545 | case VideoStreamingPart::ContentType::Audio: { |
| 546 | auto part = std::make_unique<AudioStreamingPart>(std::move(dataSlice), _videoStreamInfo->container, true); |
| 547 | _parsedAudioParts.push_back(std::move(part)); |
| 548 |
| 549 | break; |
| 550 | } |
| 551 | case VideoStreamingPart::ContentType::Video: { |
| 552 | auto part = std::make_unique<VideoStreamingPartInternal>(_videoStreamInfo->events[i].endpointId, rotation, std::move(dataSlice), _videoStreamInfo->container); |
| 553 | _parsedVideoParts.push_back(std::move(part)); |
| 554 |
| 555 | break; |
| 556 | } |
| 557 | default: { |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 |
| 564 | ~VideoStreamingPartState() { |
| 565 | } |
| 566 |
| 567 | absl::optional<VideoStreamingPartFrame> getFrameAtRelativeTimestamp(double timestamp) { |
| 568 | while (true) { |
| 569 | while (_availableFrames.size() >= 2) { |
| 570 | if (timestamp >= _availableFrames[1].pts) { |
| 571 | _availableFrames.erase(_availableFrames.begin()); |
| 572 | } else { |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 |
| 577 | if (_availableFrames.size() < 2) { |
| 578 | if (!_parsedVideoParts.empty()) { |
| 579 | auto result = _parsedVideoParts[0]->getNextFrame(); |
| 580 | if (result) { |
| 581 | _availableFrames.push_back(result.value()); |
| 582 | } else { |
| 583 | _parsedVideoParts.erase(_parsedVideoParts.begin()); |
| 584 | } |
| 585 | continue; |
| 586 | } |
| 587 | } |
| 588 |
| 589 | if (!_availableFrames.empty()) { |
| 590 | for (size_t i = 1; i < _availableFrames.size(); i++) { |
| 591 | if (timestamp < _availableFrames[i].pts) { |
| 592 | return _availableFrames[i - 1]; |
| 593 | } |
| 594 | } |
| 595 | return _availableFrames[_availableFrames.size() - 1]; |
| 596 | } else { |
| 597 | return absl::nullopt; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 |
| 602 | absl::optional<std::string> getActiveEndpointId() const { |
| 603 | if (!_parsedVideoParts.empty()) { |
| 604 | return _parsedVideoParts[0]->endpointId(); |
| 605 | } else { |
| 606 | return absl::nullopt; |
| 607 | } |
| 608 | } |
| 609 |
| 610 | bool hasRemainingFrames() const { |
| 611 | return !_parsedVideoParts.empty(); |
| 612 | } |
| 613 |
| 614 | int getAudioRemainingMilliseconds() { |
| 615 | while (!_parsedAudioParts.empty()) { |
| 616 | auto firstPartResult = _parsedAudioParts[0]->getRemainingMilliseconds(); |
| 617 | if (firstPartResult <= 0) { |
| 618 | _parsedAudioParts.erase(_parsedAudioParts.begin()); |
| 619 | } else { |
| 620 | return firstPartResult; |
| 621 | } |
| 622 | } |
| 623 | return 0; |
| 624 | } |
| 625 |
| 626 | std::vector<AudioStreamingPart::StreamingPartChannel> getAudio10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 627 | while (!_parsedAudioParts.empty()) { |
| 628 | auto firstPartResult = _parsedAudioParts[0]->get10msPerChannel(persistentDecoder); |
| 629 | if (firstPartResult.empty()) { |
| 630 | _parsedAudioParts.erase(_parsedAudioParts.begin()); |
| 631 | } else { |
| 632 | return firstPartResult; |
| 633 | } |
| 634 | } |
| 635 | return {}; |
| 636 | } |
| 637 |
| 638 | private: |
| 639 | absl::optional<VideoStreamInfo> _videoStreamInfo; |
| 640 | std::vector<std::unique_ptr<VideoStreamingPartInternal>> _parsedVideoParts; |
| 641 | std::vector<VideoStreamingPartFrame> _availableFrames; |
| 642 |
| 643 | std::vector<std::unique_ptr<AudioStreamingPart>> _parsedAudioParts; |
| 644 | }; |
| 645 |
| 646 | VideoStreamingPart::VideoStreamingPart(std::vector<uint8_t> &&data, VideoStreamingPart::ContentType contentType) { |
| 647 | if (!data.empty()) { |
| 648 | _state = new VideoStreamingPartState(std::move(data), contentType); |
| 649 | } |
| 650 | } |
| 651 |
| 652 | VideoStreamingPart::~VideoStreamingPart() { |
| 653 | if (_state) { |
| 654 | delete _state; |
| 655 | } |
| 656 | } |
| 657 |
| 658 | absl::optional<VideoStreamingPartFrame> VideoStreamingPart::getFrameAtRelativeTimestamp(double timestamp) { |
| 659 | return _state |
| 660 | ? _state->getFrameAtRelativeTimestamp(timestamp) |
| 661 | : absl::nullopt; |
| 662 | } |
| 663 |
| 664 | absl::optional<std::string> VideoStreamingPart::getActiveEndpointId() const { |
| 665 | return _state |
| 666 | ? _state->getActiveEndpointId() |
| 667 | : absl::nullopt; |
| 668 | } |
| 669 |
| 670 | bool VideoStreamingPart::hasRemainingFrames() const { |
| 671 | return _state |
| 672 | ? _state->hasRemainingFrames() |
| 673 | : false; |
| 674 | } |
| 675 |
| 676 | int VideoStreamingPart::getAudioRemainingMilliseconds() { |
| 677 | return _state |
| 678 | ? _state->getAudioRemainingMilliseconds() |
| 679 | : 0; |
| 680 | } |
| 681 | std::vector<AudioStreamingPart::StreamingPartChannel> VideoStreamingPart::getAudio10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 682 | return _state |
| 683 | ? _state->getAudio10msPerChannel(persistentDecoder) |
| 684 | : std::vector<AudioStreamingPart::StreamingPartChannel>(); |
| 685 | } |
| 686 |
| 687 | } |
| 688 |