• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2022 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 VIDEO_VIDEO_RECEIVE_STREAM_TIMEOUT_TRACKER_H_
12 #define VIDEO_VIDEO_RECEIVE_STREAM_TIMEOUT_TRACKER_H_
13 
14 #include <functional>
15 
16 #include "api/task_queue/task_queue_base.h"
17 #include "api/units/time_delta.h"
18 #include "api/units/timestamp.h"
19 #include "rtc_base/task_utils/repeating_task.h"
20 #include "system_wrappers/include/clock.h"
21 
22 namespace webrtc {
23 
24 class VideoReceiveStreamTimeoutTracker {
25  public:
26   struct Timeouts {
27     TimeDelta max_wait_for_keyframe;
28     TimeDelta max_wait_for_frame;
29   };
30 
31   using TimeoutCallback = std::function<void(TimeDelta wait)>;
32   VideoReceiveStreamTimeoutTracker(Clock* clock,
33                                    TaskQueueBase* const bookkeeping_queue,
34                                    const Timeouts& timeouts,
35                                    TimeoutCallback callback);
36   ~VideoReceiveStreamTimeoutTracker();
37   VideoReceiveStreamTimeoutTracker(const VideoReceiveStreamTimeoutTracker&) =
38       delete;
39   VideoReceiveStreamTimeoutTracker& operator=(
40       const VideoReceiveStreamTimeoutTracker&) = delete;
41 
42   bool Running() const;
43   void Start(bool waiting_for_keyframe);
44   void Stop();
45   void SetWaitingForKeyframe();
46   void OnEncodedFrameReleased();
47   TimeDelta TimeUntilTimeout() const;
48 
49   void SetTimeouts(Timeouts timeouts);
50 
51  private:
TimeoutForNextFrame()52   TimeDelta TimeoutForNextFrame() const RTC_RUN_ON(bookkeeping_queue_) {
53     return waiting_for_keyframe_ ? timeouts_.max_wait_for_keyframe
54                                  : timeouts_.max_wait_for_frame;
55   }
56   TimeDelta HandleTimeoutTask();
57 
58   Clock* const clock_;
59   TaskQueueBase* const bookkeeping_queue_;
60   Timeouts timeouts_ RTC_GUARDED_BY(bookkeeping_queue_);
61   const TimeoutCallback timeout_cb_;
62   RepeatingTaskHandle timeout_task_;
63 
64   Timestamp last_frame_ = Timestamp::MinusInfinity();
65   Timestamp timeout_ = Timestamp::MinusInfinity();
66   bool waiting_for_keyframe_;
67 };
68 }  // namespace webrtc
69 
70 #endif  // VIDEO_VIDEO_RECEIVE_STREAM_TIMEOUT_TRACKER_H_
71