• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef VIDEO_ADAPTATION_VIDEO_STREAM_ENCODER_RESOURCE_H_
12 #define VIDEO_ADAPTATION_VIDEO_STREAM_ENCODER_RESOURCE_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 #include "api/adaptation/resource.h"
19 #include "api/task_queue/task_queue_base.h"
20 #include "call/adaptation/adaptation_constraint.h"
21 #include "call/adaptation/adaptation_listener.h"
22 #include "rtc_base/synchronization/mutex.h"
23 #include "rtc_base/synchronization/sequence_checker.h"
24 
25 namespace webrtc {
26 
27 class VideoStreamEncoderResource : public Resource {
28  public:
29   ~VideoStreamEncoderResource() override;
30 
31   // Registering task queues must be performed as part of initialization.
32   void RegisterEncoderTaskQueue(TaskQueueBase* encoder_queue);
33 
34   // Resource implementation.
35   std::string Name() const override;
36   void SetResourceListener(ResourceListener* listener) override;
37 
38   // Provides a pointer to the adaptation task queue. After this call, all
39   // methods defined in this interface, including
40   // UnregisterAdaptationTaskQueue() MUST be invoked on the adaptation task
41   // queue. Registering the adaptation task queue may, however, happen off the
42   // adaptation task queue.
43   void RegisterAdaptationTaskQueue(TaskQueueBase* resource_adaptation_queue);
44   // Signals that the adaptation task queue is no longer safe to use. No
45   // assumptions must be made as to whether or not tasks in-flight will run.
46   void UnregisterAdaptationTaskQueue();
47 
48  protected:
49   explicit VideoStreamEncoderResource(std::string name);
50 
51   void OnResourceUsageStateMeasured(ResourceUsageState usage_state);
52 
53   // The caller is responsible for ensuring the task queue is still valid.
54   TaskQueueBase* encoder_queue() const;
55   // Validity of returned pointer is ensured by only allowing this method to be
56   // called on the adaptation task queue. Designed for use with RTC_GUARDED_BY.
57   // For posting from a different queue, use
58   // MaybePostTaskToResourceAdaptationQueue() instead, which only posts if the
59   // task queue is currently registered.
60   TaskQueueBase* resource_adaptation_queue() const;
61   template <typename Closure>
MaybePostTaskToResourceAdaptationQueue(Closure && closure)62   void MaybePostTaskToResourceAdaptationQueue(Closure&& closure) {
63     MutexLock lock(&lock_);
64     if (!resource_adaptation_queue_)
65       return;
66     resource_adaptation_queue_->PostTask(ToQueuedTask(closure));
67   }
68 
69  private:
70   mutable Mutex lock_;
71   const std::string name_;
72   // Treated as const after initialization.
73   TaskQueueBase* encoder_queue_;
74   TaskQueueBase* resource_adaptation_queue_ RTC_GUARDED_BY(lock_);
75   mutable Mutex listener_lock_;
76   ResourceListener* listener_ RTC_GUARDED_BY(listener_lock_);
77 };
78 
79 }  // namespace webrtc
80 
81 #endif  // VIDEO_ADAPTATION_VIDEO_STREAM_ENCODER_RESOURCE_H_
82