blob: 09f4fef180d587418958cf8ca700042e9df08e16 [file] [log] [blame]
Elad Alon10874b22019-02-21 16:25:40 +01001/*
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
11#ifndef MODULES_VIDEO_CODING_LOSS_NOTIFICATION_CONTROLLER_H_
12#define MODULES_VIDEO_CODING_LOSS_NOTIFICATION_CONTROLLER_H_
13
14#include <set>
15
16#include "absl/types/optional.h"
17#include "modules/include/module_common_types.h"
18#include "modules/video_coding/packet.h"
19#include "rtc_base/numerics/sequence_number_util.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020020#include "rtc_base/synchronization/sequence_checker.h"
Elad Alon10874b22019-02-21 16:25:40 +010021
22namespace webrtc {
23
24class LossNotificationController {
25 public:
26 LossNotificationController(KeyFrameRequestSender* key_frame_request_sender,
27 LossNotificationSender* loss_notification_sender);
28 ~LossNotificationController();
29
30 // An RTP packet was received from the network.
31 void OnReceivedPacket(const VCMPacket& packet);
32
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 uint16_t frame_id,
37 bool discardable,
38 rtc::ArrayView<const uint16_t> frame_dependency_diffs);
39
40 private:
41 void DiscardOldInformation();
42
43 bool AllDependenciesDecodable(
Philip Eliasson1f850a62019-03-19 12:15:00 +000044 int64_t unwrapped_frame_id,
Elad Alon10874b22019-02-21 16:25:40 +010045 rtc::ArrayView<const uint16_t> frame_dependency_diffs) const;
46
47 // When the loss of a packet or the non-decodability of a frame is detected,
48 // produces a key frame request or a loss notification.
49 // 1. |last_received_seq_num| is the last received sequence number.
50 // 2. |decodability_flag| refers to the frame associated with the last packet.
51 // It is set to |true| if and only if all of that frame's dependencies are
52 // known to be decodable, and the frame itself is not yet known to be
53 // unassemblable (i.e. no earlier parts of it were lost).
54 // Clarifications:
55 // a. In a multi-packet frame, the first packet reveals the frame's
56 // dependencies, but it is not yet known whether all parts of the
57 // current frame will be received.
58 // b. In a multi-packet frame, if the first packet is missed, the
59 // dependencies are unknown, but it is known that the frame itself
60 // is unassemblable.
61 void HandleLoss(uint16_t last_received_seq_num, bool decodability_flag);
62
63 KeyFrameRequestSender* const key_frame_request_sender_
Sebastian Janssonb55015e2019-04-09 13:44:04 +020064 RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +010065
66 LossNotificationSender* const loss_notification_sender_
Sebastian Janssonb55015e2019-04-09 13:44:04 +020067 RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +010068
69 SeqNumUnwrapper<uint16_t> frame_id_unwrapper_
Sebastian Janssonb55015e2019-04-09 13:44:04 +020070 RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +010071
72 // Tracked to avoid processing repeated frames (buggy/malicious remote).
Philip Eliasson1f850a62019-03-19 12:15:00 +000073 absl::optional<int64_t> last_received_unwrapped_frame_id_
Sebastian Janssonb55015e2019-04-09 13:44:04 +020074 RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +010075
76 // Tracked to avoid processing repeated packets.
77 absl::optional<uint16_t> last_received_seq_num_
Sebastian Janssonb55015e2019-04-09 13:44:04 +020078 RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +010079
80 // Tracked in order to correctly report the potential-decodability of
81 // multi-packet frames.
Sebastian Janssonb55015e2019-04-09 13:44:04 +020082 bool current_frame_potentially_decodable_ RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +010083
84 // Loss notifications contain the sequence number of the first packet of
85 // the last decodable-and-non-discardable frame. Since this is a bit of
86 // a mouthful, last_decodable_non_discardable_.first_seq_num is used,
87 // which hopefully is a bit easier for human beings to parse
88 // than |first_seq_num_of_last_decodable_non_discardable_|.
89 struct FrameInfo {
90 explicit FrameInfo(uint16_t first_seq_num) : first_seq_num(first_seq_num) {}
91 uint16_t first_seq_num;
92 };
93 absl::optional<FrameInfo> last_decodable_non_discardable_
Sebastian Janssonb55015e2019-04-09 13:44:04 +020094 RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +010095
96 // Track which frames are decodable. Later frames are also decodable if
97 // all of their dependencies can be found in this container.
98 // (Naturally, later frames must also be assemblable to be decodable.)
Philip Eliasson1f850a62019-03-19 12:15:00 +000099 std::set<int64_t> decodable_unwrapped_frame_ids_
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200100 RTC_GUARDED_BY(sequence_checker_);
Elad Alon10874b22019-02-21 16:25:40 +0100101
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200102 SequenceChecker sequence_checker_;
Elad Alon10874b22019-02-21 16:25:40 +0100103};
104
105} // namespace webrtc
106
107#endif // MODULES_VIDEO_CODING_LOSS_NOTIFICATION_CONTROLLER_H_