xref: /aosp_15_r20/external/webrtc/video/adaptation/video_stream_encoder_resource.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2020 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 #include "video/adaptation/video_stream_encoder_resource.h"
12 
13 #include <algorithm>
14 #include <utility>
15 
16 namespace webrtc {
17 
VideoStreamEncoderResource(std::string name)18 VideoStreamEncoderResource::VideoStreamEncoderResource(std::string name)
19     : lock_(),
20       name_(std::move(name)),
21       encoder_queue_(nullptr),
22       listener_(nullptr) {}
23 
~VideoStreamEncoderResource()24 VideoStreamEncoderResource::~VideoStreamEncoderResource() {
25   RTC_DCHECK(!listener_)
26       << "There is a listener depending on a VideoStreamEncoderResource being "
27       << "destroyed.";
28 }
29 
RegisterEncoderTaskQueue(TaskQueueBase * encoder_queue)30 void VideoStreamEncoderResource::RegisterEncoderTaskQueue(
31     TaskQueueBase* encoder_queue) {
32   RTC_DCHECK(!encoder_queue_);
33   RTC_DCHECK(encoder_queue);
34   encoder_queue_ = encoder_queue;
35 }
36 
SetResourceListener(ResourceListener * listener)37 void VideoStreamEncoderResource::SetResourceListener(
38     ResourceListener* listener) {
39   // If you want to change listener you need to unregister the old listener by
40   // setting it to null first.
41   MutexLock crit(&lock_);
42   RTC_DCHECK(!listener_ || !listener) << "A listener is already set";
43   listener_ = listener;
44 }
45 
Name() const46 std::string VideoStreamEncoderResource::Name() const {
47   return name_;
48 }
49 
OnResourceUsageStateMeasured(ResourceUsageState usage_state)50 void VideoStreamEncoderResource::OnResourceUsageStateMeasured(
51     ResourceUsageState usage_state) {
52   MutexLock crit(&lock_);
53   if (listener_) {
54     listener_->OnResourceUsageStateMeasured(rtc::scoped_refptr<Resource>(this),
55                                             usage_state);
56   }
57 }
58 
encoder_queue() const59 TaskQueueBase* VideoStreamEncoderResource::encoder_queue() const {
60   return encoder_queue_;
61 }
62 
63 }  // namespace webrtc
64