xref: /aosp_15_r20/external/webrtc/sdk/objc/helpers/RTCDispatcher.m (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1/*
2 *  Copyright 2015 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#import "RTCDispatcher+Private.h"
12
13static dispatch_queue_t kAudioSessionQueue = nil;
14static dispatch_queue_t kCaptureSessionQueue = nil;
15static dispatch_queue_t kNetworkMonitorQueue = nil;
16
17@implementation RTC_OBJC_TYPE (RTCDispatcher)
18
19+ (void)initialize {
20  static dispatch_once_t onceToken;
21  dispatch_once(&onceToken, ^{
22    kAudioSessionQueue = dispatch_queue_create(
23        "org.webrtc.RTCDispatcherAudioSession",
24        DISPATCH_QUEUE_SERIAL);
25    kCaptureSessionQueue = dispatch_queue_create(
26        "org.webrtc.RTCDispatcherCaptureSession",
27        DISPATCH_QUEUE_SERIAL);
28    kNetworkMonitorQueue =
29        dispatch_queue_create("org.webrtc.RTCDispatcherNetworkMonitor", DISPATCH_QUEUE_SERIAL);
30  });
31}
32
33+ (void)dispatchAsyncOnType:(RTCDispatcherQueueType)dispatchType
34                      block:(dispatch_block_t)block {
35  dispatch_queue_t queue = [self dispatchQueueForType:dispatchType];
36  dispatch_async(queue, block);
37}
38
39+ (BOOL)isOnQueueForType:(RTCDispatcherQueueType)dispatchType {
40  dispatch_queue_t targetQueue = [self dispatchQueueForType:dispatchType];
41  const char* targetLabel = dispatch_queue_get_label(targetQueue);
42  const char* currentLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
43
44  NSAssert(strlen(targetLabel) > 0, @"Label is required for the target queue.");
45  NSAssert(strlen(currentLabel) > 0, @"Label is required for the current queue.");
46
47  return strcmp(targetLabel, currentLabel) == 0;
48}
49
50#pragma mark - Private
51
52+ (dispatch_queue_t)dispatchQueueForType:(RTCDispatcherQueueType)dispatchType {
53  switch (dispatchType) {
54    case RTCDispatcherTypeMain:
55      return dispatch_get_main_queue();
56    case RTCDispatcherTypeCaptureSession:
57      return kCaptureSessionQueue;
58    case RTCDispatcherTypeAudioSession:
59      return kAudioSessionQueue;
60    case RTCDispatcherTypeNetworkMonitor:
61      return kNetworkMonitorQueue;
62  }
63}
64
65@end
66