blob: a2d640fe23dd3c72b2beaa3340bf2ff7736cb21e [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"
20#include "rtc_base/sequenced_task_checker.h"
21
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(
44 uint64_t unwrapped_frame_id,
45 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_
64 RTC_GUARDED_BY(sequenced_task_checker_);
65
66 LossNotificationSender* const loss_notification_sender_
67 RTC_GUARDED_BY(sequenced_task_checker_);
68
69 SeqNumUnwrapper<uint16_t> frame_id_unwrapper_
70 RTC_GUARDED_BY(sequenced_task_checker_);
71
72 // Tracked to avoid processing repeated frames (buggy/malicious remote).
73 absl::optional<uint64_t> last_received_unwrapped_frame_id_
74 RTC_GUARDED_BY(sequenced_task_checker_);
75
76 // Tracked to avoid processing repeated packets.
77 absl::optional<uint16_t> last_received_seq_num_
78 RTC_GUARDED_BY(sequenced_task_checker_);
79
80 // Tracked in order to correctly report the potential-decodability of
81 // multi-packet frames.
82 bool current_frame_potentially_decodable_
83 RTC_GUARDED_BY(sequenced_task_checker_);
84
85 // Loss notifications contain the sequence number of the first packet of
86 // the last decodable-and-non-discardable frame. Since this is a bit of
87 // a mouthful, last_decodable_non_discardable_.first_seq_num is used,
88 // which hopefully is a bit easier for human beings to parse
89 // than |first_seq_num_of_last_decodable_non_discardable_|.
90 struct FrameInfo {
91 explicit FrameInfo(uint16_t first_seq_num) : first_seq_num(first_seq_num) {}
92 uint16_t first_seq_num;
93 };
94 absl::optional<FrameInfo> last_decodable_non_discardable_
95 RTC_GUARDED_BY(sequenced_task_checker_);
96
97 // Track which frames are decodable. Later frames are also decodable if
98 // all of their dependencies can be found in this container.
99 // (Naturally, later frames must also be assemblable to be decodable.)
100 std::set<uint64_t> decodable_unwrapped_frame_ids_
101 RTC_GUARDED_BY(sequenced_task_checker_);
102
103 rtc::SequencedTaskChecker sequenced_task_checker_;
104};
105
106} // namespace webrtc
107
108#endif // MODULES_VIDEO_CODING_LOSS_NOTIFICATION_CONTROLLER_H_