| #include "api/video/i420_buffer.h" |
| 6 |
| 7 | #include "AVIOContextImpl.h" |
| 8 | #include "platform/PlatformInterface.h" |
| 9 |
| 10 | #include <string> |
| 11 | #include <set> |
| 12 | #include <map> |
| 13 |
| 14 | namespace tgcalls { |
| 15 |
| 16 | namespace { |
| 17 |
| 18 | class MediaDataPacket { |
| 19 | public: |
| 20 | MediaDataPacket() : _packet(av_packet_alloc()) { |
| 21 | } |
| 22 |
| 23 | MediaDataPacket(MediaDataPacket &&other) : _packet(other._packet) { |
| 24 | other._packet = nullptr; |
| 25 | } |
| 26 |
| 27 | ~MediaDataPacket() { |
| 28 | if (_packet) { |
| 29 | av_packet_free(&_packet); |
| 30 | } |
| 31 | } |
| 32 |
| 33 | AVPacket *packet() { |
| 34 | return _packet; |
| 35 | } |
| 36 |
| 37 | private: |
| 38 | AVPacket *_packet = nullptr; |
| 39 | }; |
| 40 |
| 41 | class DecodableFrame { |
| 42 | public: |
| 43 | DecodableFrame(MediaDataPacket packet, int64_t pts, int64_t dts): |
| 44 | _packet(std::move(packet)), |
| 45 | _pts(pts), |
| 46 | _dts(dts) { |
| 47 | } |
| 48 |
| 49 | ~DecodableFrame() { |
| 50 | } |
| 51 |
| 52 | MediaDataPacket &packet() { |
| 53 | return _packet; |
| 54 | } |
| 55 |
| 56 | int64_t pts() { |
| 57 | return _pts; |
| 58 | } |
| 59 |
| 60 | int64_t dts() { |
| 61 | return _dts; |
| 62 | } |
| 63 |
| 64 | private: |
| 65 | MediaDataPacket _packet; |
| 66 | int64_t _pts = 0; |
| 67 | int64_t _dts = 0; |
| 68 | }; |
| 69 |
| 70 | class Frame { |
| 71 | public: |
| 72 | Frame() { |
| 73 | _frame = av_frame_alloc(); |
| 74 | } |
| 75 |
| 76 | Frame(Frame &&other) { |
| 77 | _frame = other._frame; |
| 78 | other._frame = nullptr; |
| 79 | } |
| 80 |
| 81 | ~Frame() { |
| 82 | if (_frame) { |
| 83 | av_frame_free(&_frame); |
| 84 | } |
| 85 | } |
| 86 |
| 87 | AVFrame *frame() { |
| 88 | return _frame; |
| 89 | } |
| 90 |
| 91 | double pts(AVStream *stream, double &firstFramePts) { |
| 92 | int64_t framePts = _frame->pts; |
| 93 | double spf = av_q2d(stream->time_base); |
| 94 | double value = ((double)framePts) * spf; |
| 95 |
| 96 | if (firstFramePts < 0.0) { |
| 97 | firstFramePts = value; |
| 98 | } |
| 99 |
| 100 | return value - firstFramePts; |
| 101 | } |
| 102 |
| 103 | private: |
| 104 | AVFrame *_frame = nullptr; |
| 105 | }; |
| 106 |
| 107 | struct VideoStreamEvent { |
| 108 | int32_t offset = 0; |
| 109 | std::string endpointId; |
| 110 | int32_t rotation = 0; |
| 111 | int32_t extra = 0; |
| 112 | }; |
| 113 |
| 114 | struct VideoStreamInfo { |
| 115 | std::string container; |
| 116 | int32_t activeMask = 0; |
| 117 | std::vector<VideoStreamEvent> events; |
| 118 | }; |
| 119 |
| 120 | absl::optional<int32_t> readInt32(std::vector<uint8_t> const &data, int &offset) { |
| 121 | if (offset + 4 > data.size()) { |
| 122 | return absl::nullopt; |
| 123 | } |
| 124 |
| 125 | int32_t value = 0; |
| 126 | memcpy(&value, data.data() + offset, 4); |
| 127 | offset += 4; |
| 128 |
| 129 | return value; |
| 130 | } |
| 131 |
| 132 | absl::optional<uint8_t> readBytesAsInt32(std::vector<uint8_t> const &data, int &offset, int count) { |
| 133 | if (offset + count > data.size()) { |
| 134 | return absl::nullopt; |
| 135 | } |
| 136 |
| 137 | if (count == 0) { |
| 138 | return absl::nullopt; |
| 139 | } |
| 140 |
| 141 | if (count <= 4) { |
| 142 | int32_t value = 0; |
| 143 | memcpy(&value, data.data() + offset, count); |
| 144 | offset += count; |
| 145 | return value; |
| 146 | } else { |
| 147 | return absl::nullopt; |
| 148 | } |
| 149 | } |
| 150 |
| 151 | int32_t roundUp(int32_t numToRound, int32_t multiple) { |
| 152 | if (multiple == 0) { |
| 153 | return numToRound; |
| 154 | } |
| 155 |
| 156 | int32_t remainder = numToRound % multiple; |
| 157 | if (remainder == 0) { |
| 158 | return numToRound; |
| 159 | } |
| 160 |
| 161 | return numToRound + multiple - remainder; |
| 162 | } |
| 163 |
| 164 | absl::optional<std::string> readSerializedString(std::vector<uint8_t> const &data, int &offset) { |
| 165 | if (const auto tmp = readBytesAsInt32(data, offset, 1)) { |
| 166 | int paddingBytes = 0; |
| 167 | int length = 0; |
| 168 | if (tmp.value() == 254) { |
| 169 | if (const auto len = readBytesAsInt32(data, offset, 3)) { |
| 170 | length = len.value(); |
| 171 | paddingBytes = roundUp(length, 4) - length; |
| 172 | } else { |
| 173 | return absl::nullopt; |
| 174 | } |
| 175 | } |
| 176 | else { |
| 177 | length = tmp.value(); |
| 178 | paddingBytes = roundUp(length + 1, 4) - (length + 1); |
| 179 | } |
| 180 |
| 181 | if (offset + length > data.size()) { |
| 182 | return absl::nullopt; |
| 183 | } |
| 184 |
| 185 | std::string result(data.data() + offset, data.data() + offset + length); |
| 186 |
| 187 | offset += length; |
| 188 | offset += paddingBytes; |
| 189 |
| 190 | return result; |
| 191 | } else { |
| 192 | return absl::nullopt; |
| 193 | } |
| 194 | } |
| 195 |
| 196 | absl::optional<VideoStreamEvent> readVideoStreamEvent(std::vector<uint8_t> const &data, int &offset) { |
| 197 | VideoStreamEvent event; |
| 198 |
| 199 | if (const auto offsetValue = readInt32(data, offset)) { |
| 200 | event.offset = offsetValue.value(); |
| 201 | } else { |
| 202 | return absl::nullopt; |
| 203 | } |
| 204 |
| 205 | if (const auto endpointId = readSerializedString(data, offset)) { |
| 206 | event.endpointId = endpointId.value(); |
| 207 | } else { |
| 208 | return absl::nullopt; |
| 209 | } |
| 210 |
| 211 | if (const auto rotation = readInt32(data, offset)) { |
| 212 | event.rotation = rotation.value(); |
| 213 | } else { |
| 214 | return absl::nullopt; |
| 215 | } |
| 216 |
| 217 | if (const auto extra = readInt32(data, offset)) { |
| 218 | event.extra = extra.value(); |
| 219 | } else { |
| 220 | return absl::nullopt; |
| 221 | } |
| 222 |
| 223 | return event; |
| 224 | } |
| 225 |
| 226 | absl::optional<VideoStreamInfo> consumeVideoStreamInfo(std::vector<uint8_t> &data) { |
| 227 | int offset = 0; |
| 228 | if (const auto signature = readInt32(data, offset)) { |
| 229 | if (signature.value() != 0xa12e810d) { |
| 230 | return absl::nullopt; |
| 231 | } |
| 232 | } else { |
| 233 | return absl::nullopt; |
| 234 | } |
| 235 |
| 236 | VideoStreamInfo info; |
| 237 |
| 238 | if (const auto container = readSerializedString(data, offset)) { |
| 239 | info.container = container.value(); |
| 240 | } else { |
| 241 | return absl::nullopt; |
| 242 | } |
| 243 |
| 244 | if (const auto activeMask = readInt32(data, offset)) { |
| 245 | info.activeMask = activeMask.value(); |
| 246 | } else { |
| 247 | return absl::nullopt; |
| 248 | } |
| 249 |
| 250 | if (const auto eventCount = readInt32(data, offset)) { |
| 251 | if (eventCount > 0) { |
| 252 | if (const auto event = readVideoStreamEvent(data, offset)) { |
| 253 | info.events.push_back(event.value()); |
| 254 | } else { |
| 255 | return absl::nullopt; |
| 256 | } |
| 257 | } else { |
| 258 | return absl::nullopt; |
| 259 | } |
| 260 | } else { |
| 261 | return absl::nullopt; |
| 262 | } |
| 263 |
| 264 | data.erase(data.begin(), data.begin() + offset); |
| 265 |
| 266 | return info; |
| 267 | } |
| 268 |
| 269 | bool areCodecParametersEqual(AVCodecParameters const &lhs, AVCodecParameters const &rhs) { |
| 270 | if (lhs.codec_id != rhs.codec_id) { |
| 271 | return false; |
| 272 | } |
| 273 | if (lhs.extradata_size != rhs.extradata_size) { |
| 274 | return false; |
| 275 | } |
| 276 | if (lhs.extradata_size != 0) { |
| 277 | if (memcmp(lhs.extradata, rhs.extradata, lhs.extradata_size)) { |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | if (lhs.format != rhs.format) { |
| 282 | return false; |
| 283 | } |
| 284 | if (lhs.profile != rhs.profile) { |
| 285 | return false; |
| 286 | } |
| 287 | if (lhs.level != rhs.level) { |
| 288 | return false; |
| 289 | } |
| 290 | if (lhs.width != rhs.width) { |
| 291 | return false; |
| 292 | } |
| 293 | if (lhs.height != rhs.height) { |
| 294 | return false; |
| 295 | } |
| 296 | if (lhs.sample_aspect_ratio.num != rhs.sample_aspect_ratio.num) { |
| 297 | return false; |
| 298 | } |
| 299 | if (lhs.sample_aspect_ratio.den != rhs.sample_aspect_ratio.den) { |
| 300 | return false; |
| 301 | } |
| 302 | if (lhs.field_order != rhs.field_order) { |
| 303 | return false; |
| 304 | } |
| 305 | if (lhs.color_range != rhs.color_range) { |
| 306 | return false; |
| 307 | } |
| 308 | if (lhs.color_primaries != rhs.color_primaries) { |
| 309 | return false; |
| 310 | } |
| 311 | if (lhs.color_trc != rhs.color_trc) { |
| 312 | return false; |
| 313 | } |
| 314 | if (lhs.color_space != rhs.color_space) { |
| 315 | return false; |
| 316 | } |
| 317 | if (lhs.chroma_location != rhs.chroma_location) { |
| 318 | return false; |
| 319 | } |
| 320 |
| 321 | return true; |
| 322 | } |
| 323 |
| 324 | class VideoStreamingDecoderState { |
| 325 | public: |
| 326 | static std::unique_ptr<VideoStreamingDecoderState> create( |
| 327 | AVCodecParameters const *codecParameters, |
| 328 | AVRational pktTimebase |
| 329 | ) { |
| 330 | AVCodec const *codec = nullptr; |
| 331 | if (!codec) { |
| 332 | codec = avcodec_find_decoder(codecParameters->codec_id); |
| 333 | } |
| 334 | if (!codec) { |
| 335 | return nullptr; |
| 336 | } |
| 337 | AVCodecContext *codecContext = avcodec_alloc_context3(codec); |
| 338 | int ret = avcodec_parameters_to_context(codecContext, codecParameters); |
| 339 | if (ret < 0) { |
| 340 | avcodec_free_context(&codecContext); |
| 341 | return nullptr; |
| 342 | } else { |
| 343 | codecContext->pkt_timebase = pktTimebase; |
| 344 |
| 345 | PlatformInterface::SharedInstance()->setupVideoDecoding(codecContext); |
| 346 |
| 347 | ret = avcodec_open2(codecContext, codec, nullptr); |
| 348 | if (ret < 0) { |
| 349 | avcodec_free_context(&codecContext); |
| 350 | return nullptr; |
| 351 | } |
| 352 | } |
| 353 |
| 354 | return std::make_unique<VideoStreamingDecoderState>( |
| 355 | codecContext, |
| 356 | codecParameters, |
| 357 | pktTimebase |
| 358 | ); |
| 359 | } |
| 360 |
| 361 | public: |
| 362 | VideoStreamingDecoderState( |
| 363 | AVCodecContext *codecContext, |
| 364 | AVCodecParameters const *codecParameters, |
| 365 | AVRational pktTimebase |
| 366 | ) { |
| 367 | _codecContext = codecContext; |
| 368 | _codecParameters = avcodec_parameters_alloc(); |
| 369 | avcodec_parameters_copy(_codecParameters, codecParameters); |
| 370 | _pktTimebase = pktTimebase; |
| 371 | } |
| 372 |
| 373 | ~VideoStreamingDecoderState() { |
| 374 | if (_codecContext) { |
| 375 | avcodec_free_context(&_codecContext); |
| 376 | } |
| 377 | if (_codecParameters) { |
| 378 | avcodec_parameters_free(&_codecParameters); |
| 379 | } |
| 380 | } |
| 381 |
| 382 | bool supportsDecoding( |
| 383 | AVCodecParameters const *codecParameters, |
| 384 | AVRational pktTimebase |
| 385 | ) const { |
| 386 | if (!areCodecParametersEqual(*_codecParameters, *codecParameters)) { |
| 387 | return false; |
| 388 | } |
| 389 | if (_pktTimebase.num != pktTimebase.num) { |
| 390 | return false; |
| 391 | } |
| 392 | if (_pktTimebase.den != pktTimebase.den) { |
| 393 | return false; |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 |
| 398 | int sendFrame(std::shared_ptr<DecodableFrame> frame) { |
| 399 | if (frame) { |
| 400 | int status = avcodec_send_packet(_codecContext, frame->packet().packet()); |
| 401 | return status; |
| 402 | } else { |
| 403 | int status = avcodec_send_packet(_codecContext, nullptr); |
| 404 | return status; |
| 405 | } |
| 406 | } |
| 407 |
| 408 | int receiveFrame(Frame &frame) { |
| 409 | int status = avcodec_receive_frame(_codecContext, frame.frame()); |
| 410 | return status; |
| 411 | } |
| 412 |
| 413 | void reset() { |
| 414 | avcodec_flush_buffers(_codecContext); |
| 415 | } |
| 416 |
| 417 | private: |
| 418 | AVCodecContext *_codecContext = nullptr; |
| 419 | AVCodecParameters *_codecParameters = nullptr; |
| 420 | AVRational _pktTimebase; |
| 421 | }; |
| 422 |
| 423 | } |
| 424 |
| 425 | class VideoStreamingSharedStateInternal { |
| 426 | public: |
| 427 | VideoStreamingSharedStateInternal() { |
| 428 | } |
| 429 |
| 430 | ~VideoStreamingSharedStateInternal() { |
| 431 | } |
| 432 |
| 433 | void updateDecoderState( |
| 434 | AVCodecParameters const *codecParameters, |
| 435 | AVRational pktTimebase |
| 436 | ) { |
| 437 | if (_decoderState && _decoderState->supportsDecoding(codecParameters, pktTimebase)) { |
| 438 | return; |
| 439 | } |
| 440 |
| 441 | _decoderState.reset(); |
| 442 | _decoderState = VideoStreamingDecoderState::create(codecParameters, pktTimebase); |
| 443 | } |
| 444 |
| 445 | int sendFrame(std::shared_ptr<DecodableFrame> frame) { |
| 446 | if (!_decoderState) { |
| 447 | return AVERROR(EIO); |
| 448 | } |
| 449 | return _decoderState->sendFrame(frame); |
| 450 | } |
| 451 |
| 452 | int receiveFrame(Frame &frame) { |
| 453 | if (!_decoderState) { |
| 454 | return AVERROR(EIO); |
| 455 | } |
| 456 | return _decoderState->receiveFrame(frame); |
| 457 | } |
| 458 |
| 459 | void reset() { |
| 460 | if (!_decoderState) { |
| 461 | return; |
| 462 | } |
| 463 | _decoderState->reset(); |
| 464 | } |
| 465 |
| 466 | private: |
| 467 | std::unique_ptr<VideoStreamingDecoderState> _decoderState; |
| 468 | }; |
| 469 |
| 470 | VideoStreamingSharedState::VideoStreamingSharedState() { |
| 471 | _impl = new VideoStreamingSharedStateInternal(); |
| 472 | } |
| 473 |
| 474 | VideoStreamingSharedState::~VideoStreamingSharedState() { |
| 475 | delete _impl; |
| 476 | } |
| 477 |
| 478 | class VideoStreamingPartInternal { |
| 479 | public: |
| 480 | VideoStreamingPartInternal(std::string endpointId, webrtc::VideoRotation rotation, std::vector<uint8_t> &&fileData, std::string const &container) : |
| 481 | _endpointId(endpointId), |
| 482 | _rotation(rotation) { |
| 483 | _avIoContext = std::make_unique<AVIOContextImpl>(std::move(fileData)); |
| 484 |
| 485 | int ret = 0; |
| 486 |
| 487 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 488 | const |
| 489 | #endif |
| 490 | AVInputFormat *inputFormat = av_find_input_format(container.c_str()); |
| 491 | if (!inputFormat) { |
| 492 | _didReadToEnd = true; |
| 493 | return; |
| 494 | } |
| 495 |
| 496 | _inputFormatContext = avformat_alloc_context(); |
| 497 | if (!_inputFormatContext) { |
| 498 | _didReadToEnd = true; |
| 499 | return; |
| 500 | } |
| 501 |
| 502 | _inputFormatContext->pb = _avIoContext->getContext(); |
| 503 |
| 504 | if ((ret = avformat_open_input(&_inputFormatContext, "", inputFormat, nullptr)) < 0) { |
| 505 | _didReadToEnd = true; |
| 506 | return; |
| 507 | } |
| 508 |
| 509 | if ((ret = avformat_find_stream_info(_inputFormatContext, nullptr)) < 0) { |
| 510 | _didReadToEnd = true; |
| 511 |
| 512 | avformat_close_input(&_inputFormatContext); |
| 513 | _inputFormatContext = nullptr; |
| 514 | return; |
| 515 | } |
| 516 |
| 517 | AVCodecParameters *videoCodecParameters = nullptr; |
| 518 | AVStream *videoStream = nullptr; |
| 519 | for (int i = 0; i < _inputFormatContext->nb_streams; i++) { |
| 520 | AVStream *inStream = _inputFormatContext->streams[i]; |
| 521 |
| 522 | AVCodecParameters *inCodecpar = inStream->codecpar; |
| 523 | if (inCodecpar->codec_type != AVMEDIA_TYPE_VIDEO) { |
| 524 | continue; |
| 525 | } |
| 526 | videoCodecParameters = inCodecpar; |
| 527 | videoStream = inStream; |
| 528 |
| 529 | break; |
| 530 | } |
| 531 |
| 532 | if (videoCodecParameters && videoStream) { |
| 533 | _videoCodecParameters = avcodec_parameters_alloc(); |
| 534 | avcodec_parameters_copy(_videoCodecParameters, videoCodecParameters); |
| 535 | _videoStream = videoStream; |
| 536 |
| 537 | /*const AVCodec *codec = avcodec_find_decoder(videoCodecParameters->codec_id); |
| 538 | if (codec) { |
| 539 | _codecContext = avcodec_alloc_context3(codec); |
| 540 | ret = avcodec_parameters_to_context(_codecContext, videoCodecParameters); |
| 541 | if (ret < 0) { |
| 542 | _didReadToEnd = true; |
| 543 |
| 544 | avcodec_free_context(&_codecContext); |
| 545 | _codecContext = nullptr; |
| 546 | } else { |
| 547 | _codecContext->pkt_timebase = videoStream->time_base; |
| 548 |
| 549 | ret = avcodec_open2(_codecContext, codec, nullptr); |
| 550 | if (ret < 0) { |
| 551 | _didReadToEnd = true; |
| 552 |
| 553 | avcodec_free_context(&_codecContext); |
| 554 | _codecContext = nullptr; |
| 555 | } else { |
| 556 | _videoStream = videoStream; |
| 557 | } |
| 558 | } |
| 559 | }*/ |
| 560 | } |
| 561 | } |
| 562 |
| 563 | ~VideoStreamingPartInternal() { |
| 564 | if (_videoCodecParameters) { |
| 565 | avcodec_parameters_free(&_videoCodecParameters); |
| 566 | } |
| 567 | if (_inputFormatContext) { |
| 568 | avformat_close_input(&_inputFormatContext); |
| 569 | } |
| 570 | } |
| 571 |
| 572 | std::string endpointId() { |
| 573 | return _endpointId; |
| 574 | } |
| 575 |
| 576 | absl::optional<MediaDataPacket> readPacket() { |
| 577 | if (_didReadToEnd) { |
| 578 | return absl::nullopt; |
| 579 | } |
| 580 | if (!_inputFormatContext) { |
| 581 | return absl::nullopt; |
| 582 | } |
| 583 |
| 584 | MediaDataPacket packet; |
| 585 | int result = av_read_frame(_inputFormatContext, packet.packet()); |
| 586 | if (result < 0) { |
| 587 | return absl::nullopt; |
| 588 | } |
| 589 |
| 590 | return packet; |
| 591 | } |
| 592 |
| 593 | std::shared_ptr<DecodableFrame> readNextDecodableFrame() { |
| 594 | while (true) { |
| 595 | absl::optional<MediaDataPacket> packet = readPacket(); |
| 596 | if (packet) { |
| 597 | if (_videoStream && packet->packet()->stream_index == _videoStream->index) { |
| 598 | return std::make_shared<DecodableFrame>(std::move(packet.value()), packet->packet()->pts, packet->packet()->dts); |
| 599 | } |
| 600 | } else { |
| 601 | return nullptr; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 |
| 606 | absl::optional<VideoStreamingPartFrame> convertCurrentFrame() { |
| 607 | auto platformFrameBuffer = PlatformInterface::SharedInstance()->createPlatformFrameFromData(_frame.frame()); |
| 608 | if (platformFrameBuffer) { |
| 609 | auto videoFrame = webrtc::VideoFrame::Builder() |
| 610 | .set_video_frame_buffer(platformFrameBuffer) |
| 611 | .set_rotation(_rotation) |
| 612 | .build(); |
| 613 |
| 614 | return VideoStreamingPartFrame(_endpointId, videoFrame, _frame.pts(_videoStream, _firstFramePts), _frameIndex); |
| 615 | } else { |
| 616 | webrtc::scoped_refptr<webrtc::I420Buffer> i420Buffer = webrtc::I420Buffer::Copy( |
| 617 | _frame.frame()->width, |
| 618 | _frame.frame()->height, |
| 619 | _frame.frame()->data[0], |
| 620 | _frame.frame()->linesize[0], |
| 621 | _frame.frame()->data[1], |
| 622 | _frame.frame()->linesize[1], |
| 623 | _frame.frame()->data[2], |
| 624 | _frame.frame()->linesize[2] |
| 625 | ); |
| 626 | if (i420Buffer) { |
| 627 | auto videoFrame = webrtc::VideoFrame::Builder() |
| 628 | .set_video_frame_buffer(i420Buffer) |
| 629 | .set_rotation(_rotation) |
| 630 | .build(); |
| 631 |
| 632 | return VideoStreamingPartFrame(_endpointId, videoFrame, _frame.pts(_videoStream, _firstFramePts), _frameIndex); |
| 633 | } else { |
| 634 | return absl::nullopt; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 |
| 639 | absl::optional<VideoStreamingPartFrame> getNextFrame(VideoStreamingSharedState const *sharedState) { |
| 640 | if (!_videoStream) { |
| 641 | return {}; |
| 642 | } |
| 643 | if (!_videoCodecParameters) { |
| 644 | return {}; |
| 645 | } |
| 646 |
| 647 | sharedState->impl()->updateDecoderState(_videoCodecParameters, _videoStream->time_base); |
| 648 |
| 649 | while (true) { |
| 650 | if (_didReadToEnd) { |
| 651 | if (!_finalFrames.empty()) { |
| 652 | auto frame = _finalFrames[0]; |
| 653 | _finalFrames.erase(_finalFrames.begin()); |
| 654 | return frame; |
| 655 | } else { |
| 656 | break; |
| 657 | } |
| 658 | } else { |
| 659 | const auto frame = readNextDecodableFrame(); |
| 660 | if (frame) { |
| 661 | int sendStatus = sharedState->impl()->sendFrame(frame); |
| 662 | if (sendStatus == 0) { |
| 663 | int receiveStatus = sharedState->impl()->receiveFrame(_frame); |
| 664 | if (receiveStatus == 0) { |
| 665 | auto convertedFrame = convertCurrentFrame(); |
| 666 | if (convertedFrame) { |
| 667 | _frameIndex++; |
| 668 | return convertedFrame; |
| 669 | } |
| 670 | } else if (receiveStatus == AVERROR(EAGAIN)) { |
| 671 | // more data needed |
| 672 | } else { |
| 673 | RTC_LOG(LS_ERROR) << "avcodec_receive_frame failed with result: " << receiveStatus; |
| 674 | _didReadToEnd = true; |
| 675 | break; |
| 676 | } |
| 677 | } else { |
| 678 | RTC_LOG(LS_ERROR) << "avcodec_send_packet failed with result: " << sendStatus; |
| 679 | _didReadToEnd = true; |
| 680 | return {}; |
| 681 | } |
| 682 | } else { |
| 683 | _didReadToEnd = true; |
| 684 | int sendStatus = sharedState->impl()->sendFrame(nullptr); |
| 685 | if (sendStatus == 0) { |
| 686 | while (true) { |
| 687 | int receiveStatus = sharedState->impl()->receiveFrame(_frame); |
| 688 | if (receiveStatus == 0) { |
| 689 | auto convertedFrame = convertCurrentFrame(); |
| 690 | if (convertedFrame) { |
| 691 | _frameIndex++; |
| 692 | _finalFrames.push_back(convertedFrame.value()); |
| 693 | } |
| 694 | } else { |
| 695 | if (receiveStatus != AVERROR_EOF) { |
| 696 | RTC_LOG(LS_ERROR) << "avcodec_receive_frame (drain) failed with result: " << receiveStatus; |
| 697 | } |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | } else { |
| 702 | RTC_LOG(LS_ERROR) << "avcodec_send_packet (drain) failed with result: " << sendStatus; |
| 703 | } |
| 704 | sharedState->impl()->reset(); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 |
| 709 | return {}; |
| 710 | } |
| 711 |
| 712 | private: |
| 713 | std::string _endpointId; |
| 714 | webrtc::VideoRotation _rotation = webrtc::VideoRotation::kVideoRotation_0; |
| 715 |
| 716 | std::unique_ptr<AVIOContextImpl> _avIoContext; |
| 717 |
| 718 | AVFormatContext *_inputFormatContext = nullptr; |
| 719 | AVStream *_videoStream = nullptr; |
| 720 | Frame _frame; |
| 721 |
| 722 | AVCodecParameters *_videoCodecParameters = nullptr; |
| 723 |
| 724 | std::vector<VideoStreamingPartFrame> _finalFrames; |
| 725 |
| 726 | int _frameIndex = 0; |
| 727 | double _firstFramePts = -1.0; |
| 728 | bool _didReadToEnd = false; |
| 729 | }; |
| 730 |
| 731 | class VideoStreamingPartState { |
| 732 | public: |
| 733 | VideoStreamingPartState(std::vector<uint8_t> &&data, VideoStreamingPart::ContentType contentType) { |
| 734 | _videoStreamInfo = consumeVideoStreamInfo(data); |
| 735 | if (!_videoStreamInfo) { |
| 736 | return; |
| 737 | } |
| 738 |
| 739 | for (size_t i = 0; i < _videoStreamInfo->events.size(); i++) { |
| 740 | if (_videoStreamInfo->events[i].offset < 0) { |
| 741 | continue; |
| 742 | } |
| 743 | size_t endOffset = 0; |
| 744 | if (i == _videoStreamInfo->events.size() - 1) { |
| 745 | endOffset = data.size(); |
| 746 | } else { |
| 747 | endOffset = _videoStreamInfo->events[i + 1].offset; |
| 748 | } |
| 749 | if (endOffset <= _videoStreamInfo->events[i].offset) { |
| 750 | continue; |
| 751 | } |
| 752 | if (endOffset > data.size()) { |
| 753 | continue; |
| 754 | } |
| 755 | std::vector<uint8_t> dataSlice(data.begin() + _videoStreamInfo->events[i].offset, data.begin() + endOffset); |
| 756 | webrtc::VideoRotation rotation = webrtc::VideoRotation::kVideoRotation_0; |
| 757 | switch (_videoStreamInfo->events[i].rotation) { |
| 758 | case 0: { |
| 759 | rotation = webrtc::VideoRotation::kVideoRotation_0; |
| 760 | break; |
| 761 | } |
| 762 | case 90: { |
| 763 | rotation = webrtc::VideoRotation::kVideoRotation_90; |
| 764 | break; |
| 765 | } |
| 766 | case 180: { |
| 767 | rotation = webrtc::VideoRotation::kVideoRotation_180; |
| 768 | break; |
| 769 | } |
| 770 | case 270: { |
| 771 | rotation = webrtc::VideoRotation::kVideoRotation_270; |
| 772 | break; |
| 773 | } |
| 774 | default: { |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 |
| 779 | switch (contentType) { |
| 780 | case VideoStreamingPart::ContentType::Audio: { |
| 781 | auto part = std::make_unique<AudioStreamingPart>(std::move(dataSlice), _videoStreamInfo->container, true); |
| 782 | _parsedAudioParts.push_back(std::move(part)); |
| 783 |
| 784 | break; |
| 785 | } |
| 786 | case VideoStreamingPart::ContentType::Video: { |
| 787 | auto part = std::make_unique<VideoStreamingPartInternal>(_videoStreamInfo->events[i].endpointId, rotation, std::move(dataSlice), _videoStreamInfo->container); |
| 788 | _parsedVideoParts.push_back(std::move(part)); |
| 789 |
| 790 | break; |
| 791 | } |
| 792 | default: { |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 |
| 799 | ~VideoStreamingPartState() { |
| 800 | } |
| 801 |
| 802 | absl::optional<VideoStreamingPartFrame> getFrameAtRelativeTimestamp(VideoStreamingSharedState const *sharedState, double timestamp) { |
| 803 | while (true) { |
| 804 | while (_availableFrames.size() >= 2) { |
| 805 | if (timestamp >= _availableFrames[1].pts) { |
| 806 | _availableFrames.erase(_availableFrames.begin()); |
| 807 | } else { |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 |
| 812 | if (_availableFrames.size() < 2) { |
| 813 | if (!_parsedVideoParts.empty()) { |
| 814 | auto result = _parsedVideoParts[0]->getNextFrame(sharedState); |
| 815 | if (result) { |
| 816 | _availableFrames.push_back(result.value()); |
| 817 | } else { |
| 818 | _parsedVideoParts.erase(_parsedVideoParts.begin()); |
| 819 | } |
| 820 | continue; |
| 821 | } |
| 822 | } |
| 823 |
| 824 | if (!_availableFrames.empty()) { |
| 825 | for (size_t i = 1; i < _availableFrames.size(); i++) { |
| 826 | if (timestamp < _availableFrames[i].pts) { |
| 827 | return _availableFrames[i - 1]; |
| 828 | } |
| 829 | } |
| 830 | return _availableFrames[_availableFrames.size() - 1]; |
| 831 | } else { |
| 832 | return absl::nullopt; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 |
| 837 | absl::optional<std::string> getActiveEndpointId() const { |
| 838 | if (!_parsedVideoParts.empty()) { |
| 839 | return _parsedVideoParts[0]->endpointId(); |
| 840 | } else { |
| 841 | return absl::nullopt; |
| 842 | } |
| 843 | } |
| 844 |
| 845 | bool hasRemainingFrames() { |
| 846 | return !_parsedVideoParts.empty() || getAudioRemainingMilliseconds() > 0; |
| 847 | } |
| 848 |
| 849 | int getAudioRemainingMilliseconds() { |
| 850 | while (!_parsedAudioParts.empty()) { |
| 851 | auto firstPartResult = _parsedAudioParts[0]->getRemainingMilliseconds(); |
| 852 | if (firstPartResult <= 0) { |
| 853 | _parsedAudioParts.erase(_parsedAudioParts.begin()); |
| 854 | } else { |
| 855 | return firstPartResult; |
| 856 | } |
| 857 | } |
| 858 | return 0; |
| 859 | } |
| 860 |
| 861 | std::vector<AudioStreamingPart::StreamingPartChannel> getAudio10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 862 | while (!_parsedAudioParts.empty()) { |
| 863 | auto firstPartResult = _parsedAudioParts[0]->get10msPerChannel(persistentDecoder); |
| 864 | if (firstPartResult.empty()) { |
| 865 | _parsedAudioParts.erase(_parsedAudioParts.begin()); |
| 866 | } else { |
| 867 | return firstPartResult; |
| 868 | } |
| 869 | } |
| 870 | return {}; |
| 871 | } |
| 872 |
| 873 | private: |
| 874 | absl::optional<VideoStreamInfo> _videoStreamInfo; |
| 875 | std::vector<std::unique_ptr<VideoStreamingPartInternal>> _parsedVideoParts; |
| 876 | std::vector<VideoStreamingPartFrame> _availableFrames; |
| 877 |
| 878 | std::vector<std::unique_ptr<AudioStreamingPart>> _parsedAudioParts; |
| 879 | }; |
| 880 |
| 881 | VideoStreamingPart::VideoStreamingPart(std::vector<uint8_t> &&data, VideoStreamingPart::ContentType contentType) { |
| 882 | if (!data.empty()) { |
| 883 | _state = new VideoStreamingPartState(std::move(data), contentType); |
| 884 | } |
| 885 | } |
| 886 |
| 887 | VideoStreamingPart::~VideoStreamingPart() { |
| 888 | if (_state) { |
| 889 | delete _state; |
| 890 | } |
| 891 | } |
| 892 |
| 893 | absl::optional<VideoStreamingPartFrame> VideoStreamingPart::getFrameAtRelativeTimestamp(VideoStreamingSharedState const *sharedState, double timestamp) { |
| 894 | return _state |
| 895 | ? _state->getFrameAtRelativeTimestamp(sharedState, timestamp) |
| 896 | : absl::nullopt; |
| 897 | } |
| 898 |
| 899 | absl::optional<std::string> VideoStreamingPart::getActiveEndpointId() const { |
| 900 | return _state |
| 901 | ? _state->getActiveEndpointId() |
| 902 | : absl::nullopt; |
| 903 | } |
| 904 |
| 905 | bool VideoStreamingPart::hasRemainingFrames() const { |
| 906 | return _state |
| 907 | ? _state->hasRemainingFrames() |
| 908 | : false; |
| 909 | } |
| 910 |
| 911 | int VideoStreamingPart::getAudioRemainingMilliseconds() { |
| 912 | return _state |
| 913 | ? _state->getAudioRemainingMilliseconds() |
| 914 | : 0; |
| 915 | } |
| 916 | std::vector<AudioStreamingPart::StreamingPartChannel> VideoStreamingPart::getAudio10msPerChannel(AudioStreamingPartPersistentDecoder &persistentDecoder) { |
| 917 | return _state |
| 918 | ? _state->getAudio10msPerChannel(persistentDecoder) |
| 919 | : std::vector<AudioStreamingPart::StreamingPartChannel>(); |
| 920 | } |
| 921 |
| 922 | } |
| 923 |