xref: /aosp_15_r20/external/webrtc/sdk/objc/helpers/RTCDispatcher.m (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker/*
2*d9f75844SAndroid Build Coastguard Worker *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker#import "RTCDispatcher+Private.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Workerstatic dispatch_queue_t kAudioSessionQueue = nil;
14*d9f75844SAndroid Build Coastguard Workerstatic dispatch_queue_t kCaptureSessionQueue = nil;
15*d9f75844SAndroid Build Coastguard Workerstatic dispatch_queue_t kNetworkMonitorQueue = nil;
16*d9f75844SAndroid Build Coastguard Worker
17*d9f75844SAndroid Build Coastguard Worker@implementation RTC_OBJC_TYPE (RTCDispatcher)
18*d9f75844SAndroid Build Coastguard Worker
19*d9f75844SAndroid Build Coastguard Worker+ (void)initialize {
20*d9f75844SAndroid Build Coastguard Worker  static dispatch_once_t onceToken;
21*d9f75844SAndroid Build Coastguard Worker  dispatch_once(&onceToken, ^{
22*d9f75844SAndroid Build Coastguard Worker    kAudioSessionQueue = dispatch_queue_create(
23*d9f75844SAndroid Build Coastguard Worker        "org.webrtc.RTCDispatcherAudioSession",
24*d9f75844SAndroid Build Coastguard Worker        DISPATCH_QUEUE_SERIAL);
25*d9f75844SAndroid Build Coastguard Worker    kCaptureSessionQueue = dispatch_queue_create(
26*d9f75844SAndroid Build Coastguard Worker        "org.webrtc.RTCDispatcherCaptureSession",
27*d9f75844SAndroid Build Coastguard Worker        DISPATCH_QUEUE_SERIAL);
28*d9f75844SAndroid Build Coastguard Worker    kNetworkMonitorQueue =
29*d9f75844SAndroid Build Coastguard Worker        dispatch_queue_create("org.webrtc.RTCDispatcherNetworkMonitor", DISPATCH_QUEUE_SERIAL);
30*d9f75844SAndroid Build Coastguard Worker  });
31*d9f75844SAndroid Build Coastguard Worker}
32*d9f75844SAndroid Build Coastguard Worker
33*d9f75844SAndroid Build Coastguard Worker+ (void)dispatchAsyncOnType:(RTCDispatcherQueueType)dispatchType
34*d9f75844SAndroid Build Coastguard Worker                      block:(dispatch_block_t)block {
35*d9f75844SAndroid Build Coastguard Worker  dispatch_queue_t queue = [self dispatchQueueForType:dispatchType];
36*d9f75844SAndroid Build Coastguard Worker  dispatch_async(queue, block);
37*d9f75844SAndroid Build Coastguard Worker}
38*d9f75844SAndroid Build Coastguard Worker
39*d9f75844SAndroid Build Coastguard Worker+ (BOOL)isOnQueueForType:(RTCDispatcherQueueType)dispatchType {
40*d9f75844SAndroid Build Coastguard Worker  dispatch_queue_t targetQueue = [self dispatchQueueForType:dispatchType];
41*d9f75844SAndroid Build Coastguard Worker  const char* targetLabel = dispatch_queue_get_label(targetQueue);
42*d9f75844SAndroid Build Coastguard Worker  const char* currentLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
43*d9f75844SAndroid Build Coastguard Worker
44*d9f75844SAndroid Build Coastguard Worker  NSAssert(strlen(targetLabel) > 0, @"Label is required for the target queue.");
45*d9f75844SAndroid Build Coastguard Worker  NSAssert(strlen(currentLabel) > 0, @"Label is required for the current queue.");
46*d9f75844SAndroid Build Coastguard Worker
47*d9f75844SAndroid Build Coastguard Worker  return strcmp(targetLabel, currentLabel) == 0;
48*d9f75844SAndroid Build Coastguard Worker}
49*d9f75844SAndroid Build Coastguard Worker
50*d9f75844SAndroid Build Coastguard Worker#pragma mark - Private
51*d9f75844SAndroid Build Coastguard Worker
52*d9f75844SAndroid Build Coastguard Worker+ (dispatch_queue_t)dispatchQueueForType:(RTCDispatcherQueueType)dispatchType {
53*d9f75844SAndroid Build Coastguard Worker  switch (dispatchType) {
54*d9f75844SAndroid Build Coastguard Worker    case RTCDispatcherTypeMain:
55*d9f75844SAndroid Build Coastguard Worker      return dispatch_get_main_queue();
56*d9f75844SAndroid Build Coastguard Worker    case RTCDispatcherTypeCaptureSession:
57*d9f75844SAndroid Build Coastguard Worker      return kCaptureSessionQueue;
58*d9f75844SAndroid Build Coastguard Worker    case RTCDispatcherTypeAudioSession:
59*d9f75844SAndroid Build Coastguard Worker      return kAudioSessionQueue;
60*d9f75844SAndroid Build Coastguard Worker    case RTCDispatcherTypeNetworkMonitor:
61*d9f75844SAndroid Build Coastguard Worker      return kNetworkMonitorQueue;
62*d9f75844SAndroid Build Coastguard Worker  }
63*d9f75844SAndroid Build Coastguard Worker}
64*d9f75844SAndroid Build Coastguard Worker
65*d9f75844SAndroid Build Coastguard Worker@end
66