Access UIApplication on main thread
Track UIApplication applicationState changes from a C++ class. Uses NSNotificationCenter to access changes on the main thread and exposes a local variable that can be checked from any thread. This fixes a runtime warning on iOS 11 beta. My Objective-C++ is a little rusty so please check if this follows the conventions for C++ code in the project. It also changes the interface exposed by RTCUIApplication.h, not sure if that has impact on any public APIs that needs to be documented somewhere? Bug: webrtc:7773 Change-Id: I9c8ba090ef9f28d812114026a906cef742192c39 Reviewed-on: https://chromium-review.googlesource.com/527442 Reviewed-by: Magnus Jedvert <magjed@webrtc.org> Reviewed-by: Kári Tristan Helgason <kthelgason@webrtc.org> Commit-Queue: Anders Carlsson <andersc@webrtc.org> Cr-Commit-Position: refs/heads/master@{#18558}
This commit is contained in:
committed by
Commit Bot
parent
5b383c0ebd
commit
fc309750a9
@ -47,8 +47,8 @@ if (is_ios || is_mac) {
|
|||||||
"objc/Framework/Classes/Common/RTCDispatcher.m",
|
"objc/Framework/Classes/Common/RTCDispatcher.m",
|
||||||
"objc/Framework/Classes/Common/RTCFieldTrials.mm",
|
"objc/Framework/Classes/Common/RTCFieldTrials.mm",
|
||||||
"objc/Framework/Classes/Common/RTCLogging.mm",
|
"objc/Framework/Classes/Common/RTCLogging.mm",
|
||||||
"objc/Framework/Classes/Common/RTCUIApplication.h",
|
"objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.h",
|
||||||
"objc/Framework/Classes/Common/RTCUIApplication.mm",
|
"objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m",
|
||||||
"objc/Framework/Classes/Common/helpers.h",
|
"objc/Framework/Classes/Common/helpers.h",
|
||||||
"objc/Framework/Classes/Common/helpers.mm",
|
"objc/Framework/Classes/Common/helpers.mm",
|
||||||
"objc/Framework/Headers/WebRTC/RTCDispatcher.h",
|
"objc/Framework/Headers/WebRTC/RTCDispatcher.h",
|
||||||
@ -546,8 +546,8 @@ if (is_ios || is_mac) {
|
|||||||
"objc/Framework/Classes/VideoToolbox/encoder.mm",
|
"objc/Framework/Classes/VideoToolbox/encoder.mm",
|
||||||
"objc/Framework/Classes/VideoToolbox/nalu_rewriter.cc",
|
"objc/Framework/Classes/VideoToolbox/nalu_rewriter.cc",
|
||||||
"objc/Framework/Classes/VideoToolbox/nalu_rewriter.h",
|
"objc/Framework/Classes/VideoToolbox/nalu_rewriter.h",
|
||||||
"objc/Framework/Classes/VideoToolbox/videocodecfactory.cc",
|
|
||||||
"objc/Framework/Classes/VideoToolbox/videocodecfactory.h",
|
"objc/Framework/Classes/VideoToolbox/videocodecfactory.h",
|
||||||
|
"objc/Framework/Classes/VideoToolbox/videocodecfactory.mm",
|
||||||
]
|
]
|
||||||
|
|
||||||
configs += [ "..:common_objc" ]
|
configs += [ "..:common_objc" ]
|
||||||
|
|||||||
@ -1,21 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2016 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_UI_RTCUIAPPLICATION_H_
|
|
||||||
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_UI_RTCUIAPPLICATION_H_
|
|
||||||
|
|
||||||
#include "WebRTC/RTCMacros.h"
|
|
||||||
|
|
||||||
#if defined(WEBRTC_IOS)
|
|
||||||
/** Convenience function to get UIApplicationState from C++. */
|
|
||||||
RTC_EXTERN bool RTCIsUIApplicationActive();
|
|
||||||
#endif // WEBRTC_IOS
|
|
||||||
|
|
||||||
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_UI_RTCUIAPPLICATION_H_
|
|
||||||
@ -8,15 +8,16 @@
|
|||||||
* be found in the AUTHORS file in the root of the source tree.
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "RTCUIApplication.h"
|
|
||||||
|
|
||||||
#if defined(WEBRTC_IOS)
|
#if defined(WEBRTC_IOS)
|
||||||
|
|
||||||
#import <UIKit/UIKit.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
bool RTCIsUIApplicationActive() {
|
@interface RTCUIApplicationStatusObserver : NSObject
|
||||||
UIApplicationState state = [UIApplication sharedApplication].applicationState;
|
|
||||||
return state == UIApplicationStateActive;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // WEBRTC_IOS
|
+ (instancetype)sharedInstance;
|
||||||
|
|
||||||
|
- (BOOL)isApplicationActive;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
#endif // WEBRTC_IOS
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 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 "RTCUIApplicationStatusObserver.h"
|
||||||
|
|
||||||
|
#if defined(WEBRTC_IOS)
|
||||||
|
|
||||||
|
#import <UIKit/UIKit.h>
|
||||||
|
|
||||||
|
@implementation RTCUIApplicationStatusObserver {
|
||||||
|
UIApplicationState _state;
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (instancetype)sharedInstance {
|
||||||
|
static id sharedInstance;
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
sharedInstance = [[self alloc] init];
|
||||||
|
});
|
||||||
|
|
||||||
|
return sharedInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (id)init {
|
||||||
|
if (self = [super init]) {
|
||||||
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
||||||
|
[center addObserverForName:UIApplicationDidBecomeActiveNotification
|
||||||
|
object:nil
|
||||||
|
queue:[NSOperationQueue mainQueue]
|
||||||
|
usingBlock:^(NSNotification *note) {
|
||||||
|
_state = [UIApplication sharedApplication].applicationState;
|
||||||
|
}];
|
||||||
|
|
||||||
|
[center addObserverForName:UIApplicationDidEnterBackgroundNotification
|
||||||
|
object:nil
|
||||||
|
queue:[NSOperationQueue mainQueue]
|
||||||
|
usingBlock:^(NSNotification *note) {
|
||||||
|
_state = [UIApplication sharedApplication].applicationState;
|
||||||
|
}];
|
||||||
|
|
||||||
|
dispatch_block_t initializeBlock = ^{
|
||||||
|
_state = [UIApplication sharedApplication].applicationState;
|
||||||
|
};
|
||||||
|
|
||||||
|
if ([NSThread isMainThread]) {
|
||||||
|
initializeBlock();
|
||||||
|
} else {
|
||||||
|
dispatch_sync(dispatch_get_main_queue(), initializeBlock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)isApplicationActive {
|
||||||
|
return _state == UIApplicationStateActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
#endif // WEBRTC_IOS
|
||||||
@ -13,9 +13,6 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#if defined(WEBRTC_IOS)
|
|
||||||
#include "Common/RTCUIApplication.h"
|
|
||||||
#endif
|
|
||||||
#include "libyuv/convert.h"
|
#include "libyuv/convert.h"
|
||||||
#include "webrtc/api/video/video_frame.h"
|
#include "webrtc/api/video/video_frame.h"
|
||||||
#include "webrtc/base/checks.h"
|
#include "webrtc/base/checks.h"
|
||||||
@ -24,6 +21,10 @@
|
|||||||
#include "webrtc/sdk/objc/Framework/Classes/Video/corevideo_frame_buffer.h"
|
#include "webrtc/sdk/objc/Framework/Classes/Video/corevideo_frame_buffer.h"
|
||||||
#include "webrtc/sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
|
#include "webrtc/sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
|
||||||
|
|
||||||
|
#if defined(WEBRTC_IOS)
|
||||||
|
#import "Common/RTCUIApplicationStatusObserver.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -74,9 +75,7 @@ void VTDecompressionOutputCallback(void* decoder,
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
H264VideoToolboxDecoder::H264VideoToolboxDecoder()
|
H264VideoToolboxDecoder::H264VideoToolboxDecoder()
|
||||||
: callback_(nullptr),
|
: callback_(nullptr), video_format_(nullptr), decompression_session_(nullptr) {}
|
||||||
video_format_(nullptr),
|
|
||||||
decompression_session_(nullptr) {}
|
|
||||||
|
|
||||||
H264VideoToolboxDecoder::~H264VideoToolboxDecoder() {
|
H264VideoToolboxDecoder::~H264VideoToolboxDecoder() {
|
||||||
DestroyDecompressionSession();
|
DestroyDecompressionSession();
|
||||||
@ -97,7 +96,7 @@ int H264VideoToolboxDecoder::Decode(
|
|||||||
RTC_DCHECK(input_image._buffer);
|
RTC_DCHECK(input_image._buffer);
|
||||||
|
|
||||||
#if defined(WEBRTC_IOS)
|
#if defined(WEBRTC_IOS)
|
||||||
if (!RTCIsUIApplicationActive()) {
|
if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
|
||||||
// Ignore all decode requests when app isn't active. In this state, the
|
// Ignore all decode requests when app isn't active. In this state, the
|
||||||
// hardware decoder has been invalidated by the OS.
|
// hardware decoder has been invalidated by the OS.
|
||||||
// Reset video format so that we won't process frames until the next
|
// Reset video format so that we won't process frames until the next
|
||||||
|
|||||||
@ -16,8 +16,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#if defined(WEBRTC_IOS)
|
#if defined(WEBRTC_IOS)
|
||||||
|
#import "Common/RTCUIApplicationStatusObserver.h"
|
||||||
#import "WebRTC/UIDevice+RTCDevice.h"
|
#import "WebRTC/UIDevice+RTCDevice.h"
|
||||||
#include "Common/RTCUIApplication.h"
|
|
||||||
#endif
|
#endif
|
||||||
#include "libyuv/convert_from.h"
|
#include "libyuv/convert_from.h"
|
||||||
#include "webrtc/base/checks.h"
|
#include "webrtc/base/checks.h"
|
||||||
@ -386,7 +386,7 @@ int H264VideoToolboxEncoder::Encode(
|
|||||||
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
|
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
|
||||||
}
|
}
|
||||||
#if defined(WEBRTC_IOS)
|
#if defined(WEBRTC_IOS)
|
||||||
if (!RTCIsUIApplicationActive()) {
|
if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
|
||||||
// Ignore all encode requests when app isn't active. In this state, the
|
// Ignore all encode requests when app isn't active. In this state, the
|
||||||
// hardware encoder has been invalidated by the OS.
|
// hardware encoder has been invalidated by the OS.
|
||||||
return WEBRTC_VIDEO_CODEC_OK;
|
return WEBRTC_VIDEO_CODEC_OK;
|
||||||
|
|||||||
@ -28,8 +28,7 @@ bool IsHighProfileEnabled() {
|
|||||||
|
|
||||||
// VideoToolboxVideoEncoderFactory
|
// VideoToolboxVideoEncoderFactory
|
||||||
|
|
||||||
VideoToolboxVideoEncoderFactory::VideoToolboxVideoEncoderFactory() {
|
VideoToolboxVideoEncoderFactory::VideoToolboxVideoEncoderFactory() {}
|
||||||
}
|
|
||||||
|
|
||||||
VideoToolboxVideoEncoderFactory::~VideoToolboxVideoEncoderFactory() {}
|
VideoToolboxVideoEncoderFactory::~VideoToolboxVideoEncoderFactory() {}
|
||||||
|
|
||||||
@ -43,14 +42,12 @@ VideoEncoder* VideoToolboxVideoEncoderFactory::CreateVideoEncoder(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoToolboxVideoEncoderFactory::DestroyVideoEncoder(
|
void VideoToolboxVideoEncoderFactory::DestroyVideoEncoder(VideoEncoder* encoder) {
|
||||||
VideoEncoder* encoder) {
|
|
||||||
delete encoder;
|
delete encoder;
|
||||||
encoder = nullptr;
|
encoder = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<cricket::VideoCodec>&
|
const std::vector<cricket::VideoCodec>& VideoToolboxVideoEncoderFactory::supported_codecs() const {
|
||||||
VideoToolboxVideoEncoderFactory::supported_codecs() const {
|
|
||||||
supported_codecs_.clear();
|
supported_codecs_.clear();
|
||||||
|
|
||||||
// TODO(magjed): Enumerate actual level instead of using hardcoded level 3.1.
|
// TODO(magjed): Enumerate actual level instead of using hardcoded level 3.1.
|
||||||
@ -59,22 +56,18 @@ VideoToolboxVideoEncoderFactory::supported_codecs() const {
|
|||||||
|
|
||||||
if (IsHighProfileEnabled()) {
|
if (IsHighProfileEnabled()) {
|
||||||
cricket::VideoCodec constrained_high(cricket::kH264CodecName);
|
cricket::VideoCodec constrained_high(cricket::kH264CodecName);
|
||||||
const H264::ProfileLevelId constrained_high_profile(
|
const H264::ProfileLevelId constrained_high_profile(H264::kProfileConstrainedHigh, level);
|
||||||
H264::kProfileConstrainedHigh, level);
|
constrained_high.SetParam(cricket::kH264FmtpProfileLevelId,
|
||||||
constrained_high.SetParam(
|
*H264::ProfileLevelIdToString(constrained_high_profile));
|
||||||
cricket::kH264FmtpProfileLevelId,
|
|
||||||
*H264::ProfileLevelIdToString(constrained_high_profile));
|
|
||||||
constrained_high.SetParam(cricket::kH264FmtpLevelAsymmetryAllowed, "1");
|
constrained_high.SetParam(cricket::kH264FmtpLevelAsymmetryAllowed, "1");
|
||||||
constrained_high.SetParam(cricket::kH264FmtpPacketizationMode, "1");
|
constrained_high.SetParam(cricket::kH264FmtpPacketizationMode, "1");
|
||||||
supported_codecs_.push_back(constrained_high);
|
supported_codecs_.push_back(constrained_high);
|
||||||
}
|
}
|
||||||
|
|
||||||
cricket::VideoCodec constrained_baseline(cricket::kH264CodecName);
|
cricket::VideoCodec constrained_baseline(cricket::kH264CodecName);
|
||||||
const H264::ProfileLevelId constrained_baseline_profile(
|
const H264::ProfileLevelId constrained_baseline_profile(H264::kProfileConstrainedBaseline, level);
|
||||||
H264::kProfileConstrainedBaseline, level);
|
constrained_baseline.SetParam(cricket::kH264FmtpProfileLevelId,
|
||||||
constrained_baseline.SetParam(
|
*H264::ProfileLevelIdToString(constrained_baseline_profile));
|
||||||
cricket::kH264FmtpProfileLevelId,
|
|
||||||
*H264::ProfileLevelIdToString(constrained_baseline_profile));
|
|
||||||
constrained_baseline.SetParam(cricket::kH264FmtpLevelAsymmetryAllowed, "1");
|
constrained_baseline.SetParam(cricket::kH264FmtpLevelAsymmetryAllowed, "1");
|
||||||
constrained_baseline.SetParam(cricket::kH264FmtpPacketizationMode, "1");
|
constrained_baseline.SetParam(cricket::kH264FmtpPacketizationMode, "1");
|
||||||
supported_codecs_.push_back(constrained_baseline);
|
supported_codecs_.push_back(constrained_baseline);
|
||||||
@ -90,8 +83,7 @@ VideoToolboxVideoDecoderFactory::VideoToolboxVideoDecoderFactory() {
|
|||||||
|
|
||||||
VideoToolboxVideoDecoderFactory::~VideoToolboxVideoDecoderFactory() {}
|
VideoToolboxVideoDecoderFactory::~VideoToolboxVideoDecoderFactory() {}
|
||||||
|
|
||||||
VideoDecoder* VideoToolboxVideoDecoderFactory::CreateVideoDecoder(
|
VideoDecoder* VideoToolboxVideoDecoderFactory::CreateVideoDecoder(VideoCodecType type) {
|
||||||
VideoCodecType type) {
|
|
||||||
const rtc::Optional<const char*> codec_name = CodecTypeToPayloadName(type);
|
const rtc::Optional<const char*> codec_name = CodecTypeToPayloadName(type);
|
||||||
if (!codec_name) {
|
if (!codec_name) {
|
||||||
LOG(LS_ERROR) << "Invalid codec type: " << type;
|
LOG(LS_ERROR) << "Invalid codec type: " << type;
|
||||||
@ -106,8 +98,7 @@ VideoDecoder* VideoToolboxVideoDecoderFactory::CreateVideoDecoder(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoToolboxVideoDecoderFactory::DestroyVideoDecoder(
|
void VideoToolboxVideoDecoderFactory::DestroyVideoDecoder(VideoDecoder* decoder) {
|
||||||
VideoDecoder* decoder) {
|
|
||||||
delete decoder;
|
delete decoder;
|
||||||
decoder = nullptr;
|
decoder = nullptr;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user