summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/test/pc/e2e/analyzer/video/analyzing_video_sinks_helper.cc
blob: 70dc4b00b55c1ff208b4f88e4082754e0ee17687 (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
/*
 *  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.
 */
#include "test/pc/e2e/analyzer/video/analyzing_video_sinks_helper.h"

#include <memory>
#include <set>
#include <string>
#include <utility>

#include "absl/strings/string_view.h"
#include "api/test/pclf/media_configuration.h"
#include "api/test/video/video_frame_writer.h"
#include "rtc_base/synchronization/mutex.h"

namespace webrtc {
namespace webrtc_pc_e2e {

void AnalyzingVideoSinksHelper::AddConfig(absl::string_view sender_peer_name,
                                          VideoConfig config) {
  MutexLock lock(&mutex_);
  auto it = video_configs_.find(*config.stream_label);
  if (it == video_configs_.end()) {
    std::string stream_label = *config.stream_label;
    video_configs_.emplace(
        std::move(stream_label),
        std::pair{std::string(sender_peer_name), std::move(config)});
  } else {
    it->second = std::pair{std::string(sender_peer_name), std::move(config)};
  }
}

absl::optional<std::pair<std::string, VideoConfig>>
AnalyzingVideoSinksHelper::GetPeerAndConfig(absl::string_view stream_label) {
  MutexLock lock(&mutex_);
  auto it = video_configs_.find(std::string(stream_label));
  if (it == video_configs_.end()) {
    return absl::nullopt;
  }
  return it->second;
}

void AnalyzingVideoSinksHelper::RemoveConfig(absl::string_view stream_label) {
  MutexLock lock(&mutex_);
  video_configs_.erase(std::string(stream_label));
}

test::VideoFrameWriter* AnalyzingVideoSinksHelper::AddVideoWriter(
    std::unique_ptr<test::VideoFrameWriter> video_writer) {
  MutexLock lock(&mutex_);
  test::VideoFrameWriter* out = video_writer.get();
  video_writers_.push_back(std::move(video_writer));
  return out;
}

void AnalyzingVideoSinksHelper::CloseAndRemoveVideoWriters(
    std::set<test::VideoFrameWriter*> writers_to_close) {
  MutexLock lock(&mutex_);
  for (auto it = video_writers_.cbegin(); it != video_writers_.cend();) {
    if (writers_to_close.find(it->get()) != writers_to_close.end()) {
      (*it)->Close();
      it = video_writers_.erase(it);
    } else {
      ++it;
    }
  }
}

void AnalyzingVideoSinksHelper::Clear() {
  MutexLock lock(&mutex_);
  video_configs_.clear();
  for (const auto& video_writer : video_writers_) {
    video_writer->Close();
  }
  video_writers_.clear();
}

}  // namespace webrtc_pc_e2e
}  // namespace webrtc