loss_notification_controller.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_VIDEO_CODING_LOSS_NOTIFICATION_CONTROLLER_H_
  11. #define MODULES_VIDEO_CODING_LOSS_NOTIFICATION_CONTROLLER_H_
  12. #include <stdint.h>
  13. #include <set>
  14. #include "absl/types/optional.h"
  15. #include "api/array_view.h"
  16. #include "modules/include/module_common_types.h"
  17. #include "rtc_base/synchronization/sequence_checker.h"
  18. #include "rtc_base/system/no_unique_address.h"
  19. namespace webrtc {
  20. class LossNotificationController {
  21. public:
  22. struct FrameDetails {
  23. bool is_keyframe;
  24. int64_t frame_id;
  25. rtc::ArrayView<const int64_t> frame_dependencies;
  26. };
  27. LossNotificationController(KeyFrameRequestSender* key_frame_request_sender,
  28. LossNotificationSender* loss_notification_sender);
  29. ~LossNotificationController();
  30. // An RTP packet was received from the network.
  31. // |frame| is non-null iff the packet is the first packet in the frame.
  32. void OnReceivedPacket(uint16_t rtp_seq_num, const FrameDetails* frame);
  33. // A frame was assembled from packets previously received.
  34. // (Should be called even if the frame was composed of a single packet.)
  35. void OnAssembledFrame(uint16_t first_seq_num,
  36. int64_t frame_id,
  37. bool discardable,
  38. rtc::ArrayView<const int64_t> frame_dependencies);
  39. private:
  40. void DiscardOldInformation();
  41. bool AllDependenciesDecodable(
  42. rtc::ArrayView<const int64_t> frame_dependencies) const;
  43. // When the loss of a packet or the non-decodability of a frame is detected,
  44. // produces a key frame request or a loss notification.
  45. // 1. |last_received_seq_num| is the last received sequence number.
  46. // 2. |decodability_flag| refers to the frame associated with the last packet.
  47. // It is set to |true| if and only if all of that frame's dependencies are
  48. // known to be decodable, and the frame itself is not yet known to be
  49. // unassemblable (i.e. no earlier parts of it were lost).
  50. // Clarifications:
  51. // a. In a multi-packet frame, the first packet reveals the frame's
  52. // dependencies, but it is not yet known whether all parts of the
  53. // current frame will be received.
  54. // b. In a multi-packet frame, if the first packet is missed, the
  55. // dependencies are unknown, but it is known that the frame itself
  56. // is unassemblable.
  57. void HandleLoss(uint16_t last_received_seq_num, bool decodability_flag);
  58. KeyFrameRequestSender* const key_frame_request_sender_
  59. RTC_GUARDED_BY(sequence_checker_);
  60. LossNotificationSender* const loss_notification_sender_
  61. RTC_GUARDED_BY(sequence_checker_);
  62. // Tracked to avoid processing repeated frames (buggy/malicious remote).
  63. absl::optional<int64_t> last_received_frame_id_
  64. RTC_GUARDED_BY(sequence_checker_);
  65. // Tracked to avoid processing repeated packets.
  66. absl::optional<uint16_t> last_received_seq_num_
  67. RTC_GUARDED_BY(sequence_checker_);
  68. // Tracked in order to correctly report the potential-decodability of
  69. // multi-packet frames.
  70. bool current_frame_potentially_decodable_ RTC_GUARDED_BY(sequence_checker_);
  71. // Loss notifications contain the sequence number of the first packet of
  72. // the last decodable-and-non-discardable frame. Since this is a bit of
  73. // a mouthful, last_decodable_non_discardable_.first_seq_num is used,
  74. // which hopefully is a bit easier for human beings to parse
  75. // than |first_seq_num_of_last_decodable_non_discardable_|.
  76. struct FrameInfo {
  77. explicit FrameInfo(uint16_t first_seq_num) : first_seq_num(first_seq_num) {}
  78. uint16_t first_seq_num;
  79. };
  80. absl::optional<FrameInfo> last_decodable_non_discardable_
  81. RTC_GUARDED_BY(sequence_checker_);
  82. // Track which frames are decodable. Later frames are also decodable if
  83. // all of their dependencies can be found in this container.
  84. // (Naturally, later frames must also be assemblable to be decodable.)
  85. std::set<int64_t> decodable_frame_ids_ RTC_GUARDED_BY(sequence_checker_);
  86. RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
  87. };
  88. } // namespace webrtc
  89. #endif // MODULES_VIDEO_CODING_LOSS_NOTIFICATION_CONTROLLER_H_