summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/test/pc/e2e/analyzer/video/default_video_quality_analyzer_stream_state.h
blob: 829a79c7bf60bf4a8c6e4bff919025d790e81d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_STREAM_STATE_H_
#define TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_STREAM_STATE_H_

#include <limits>
#include <map>
#include <set>

#include "absl/types/optional.h"
#include "api/units/timestamp.h"
#include "test/pc/e2e/analyzer/video/multi_reader_queue.h"

namespace webrtc {

// Represents a current state of video stream inside
// DefaultVideoQualityAnalyzer.
//
// Maintains the sequence of frames for each video stream and keeps track about
// which frames were seen by each of the possible stream receiver.
//
// Keeps information about which frames are alive and which are dead. Frame is
// alive if it contains VideoFrame payload for corresponding FrameInFlight
// object inside DefaultVideoQualityAnalyzer, otherwise frame is considered
// dead.
//
// Supports peer indexes from 0 to max(size_t) - 1.
class StreamState {
 public:
  StreamState(size_t sender,
              std::set<size_t> receivers,
              Timestamp stream_started_time);

  size_t sender() const { return sender_; }
  Timestamp stream_started_time() const { return stream_started_time_; }

  void PushBack(uint16_t frame_id) { frame_ids_.PushBack(frame_id); }
  // Crash if state is empty.
  uint16_t PopFront(size_t peer);
  bool IsEmpty(size_t peer) const { return frame_ids_.IsEmpty(peer); }
  // Crash if state is empty.
  uint16_t Front(size_t peer) const { return frame_ids_.Front(peer).value(); }

  // Adds a new peer to the state. All currently alive frames will be expected
  // to be received by the newly added peer.
  void AddPeer(size_t peer);

  // Removes peer from the state. Frames that were expected to be received by
  // this peer will be removed from it. On the other hand last rendered frame
  // time for the removed peer will be preserved, because
  // DefaultVideoQualityAnalyzer still may request it for stats processing.
  void RemovePeer(size_t peer);

  size_t GetAliveFramesCount() const {
    return frame_ids_.size(kAliveFramesQueueIndex);
  }
  uint16_t MarkNextAliveFrameAsDead();

  void SetLastRenderedFrameTime(size_t peer, Timestamp time);
  absl::optional<Timestamp> last_rendered_frame_time(size_t peer) const;

 private:
  // Index of the `frame_ids_` queue which is used to track alive frames for
  // this stream.
  static constexpr size_t kAliveFramesQueueIndex =
      std::numeric_limits<size_t>::max();

  size_t GetLongestReceiverQueue() const;

  // Index of the owner. Owner's queue in `frame_ids_` will keep alive frames.
  const size_t sender_;
  const Timestamp stream_started_time_;
  std::set<size_t> receivers_;
  // To correctly determine dropped frames we have to know sequence of frames
  // in each stream so we will keep a list of frame ids inside the stream.
  // This list is represented by multi head queue of frame ids with separate
  // head for each receiver. When the frame is rendered, we will pop ids from
  // the corresponding head until id will match with rendered one. All ids
  // before matched one can be considered as dropped:
  //
  // | frame_id1 |->| frame_id2 |->| frame_id3 |->| frame_id4 |
  //
  // If we received frame with id frame_id3, then we will pop frame_id1 and
  // frame_id2 and consider those frames as dropped and then compare received
  // frame with the one from `FrameInFlight` with id frame_id3.
  MultiReaderQueue<uint16_t> frame_ids_;
  std::map<size_t, Timestamp> last_rendered_frame_time_;
};

}  // namespace webrtc

#endif  // TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_STREAM_STATE_H_