From b953dcac887089d4848a10ef6b74a467b6922eae Mon Sep 17 00:00:00 2001 From: dijunkun Date: Mon, 27 Oct 2025 18:10:07 +0800 Subject: [PATCH] [test] --- .../macosx/screen_capturer_sck_impl.mm | 10 ++- .../macosx/speaker_capturer_macosx.mm | 86 ++++++------------- 2 files changed, 30 insertions(+), 66 deletions(-) diff --git a/src/screen_capturer/macosx/screen_capturer_sck_impl.mm b/src/screen_capturer/macosx/screen_capturer_sck_impl.mm index 87f72c5..c53c440 100644 --- a/src/screen_capturer/macosx/screen_capturer_sck_impl.mm +++ b/src/screen_capturer/macosx/screen_capturer_sck_impl.mm @@ -22,10 +22,10 @@ #include "display_info.h" #include "rd_log.h" -namespace crossdesk { +using namespace crossdesk; +class ScreenCapturerSckImpl; static const int kFullDesktopScreenId = -1; -class ScreenCapturerSckImpl; // The ScreenCaptureKit API was available in macOS 12.3, but full-screen capture // was reported to be broken before macOS 13 - see http://crbug.com/40234870. @@ -173,6 +173,8 @@ std::string GetDisplayName(CGDirectDisplayID display_id) { return result; } +namespace crossdesk { + ScreenCapturerSckImpl::ScreenCapturerSckImpl() { helper_ = [[SckHelper alloc] initWithCapturer:this]; } @@ -429,6 +431,7 @@ void ScreenCapturerSckImpl::StartOrReconfigureCapturer() { std::unique_ptr ScreenCapturerSck::CreateScreenCapturerSck() { return std::make_unique(); } +} // namespace crossdesk @implementation SckHelper { // This lock is to prevent the capturer being destroyed while an instance @@ -487,5 +490,4 @@ std::unique_ptr ScreenCapturerSck::CreateScreenCapturerSck() { _capturer = nullptr; } -@end -} // namespace crossdesk \ No newline at end of file +@end \ No newline at end of file diff --git a/src/speaker_capturer/macosx/speaker_capturer_macosx.mm b/src/speaker_capturer/macosx/speaker_capturer_macosx.mm index 768b810..a818f0d 100644 --- a/src/speaker_capturer/macosx/speaker_capturer_macosx.mm +++ b/src/speaker_capturer/macosx/speaker_capturer_macosx.mm @@ -6,14 +6,16 @@ #include "speaker_capturer_macosx.h" namespace crossdesk { +class SpeakerCapturerMacosx; +} @interface SpeakerCaptureDelegate : NSObject -@property(nonatomic, assign) SpeakerCapturerMacosx* owner; -- (instancetype)initWithOwner:(SpeakerCapturerMacosx*)owner; +@property(nonatomic, assign) crossdesk::SpeakerCapturerMacosx* owner; +- (instancetype)initWithOwner:(crossdesk::SpeakerCapturerMacosx*)owner; @end @implementation SpeakerCaptureDelegate -- (instancetype)initWithOwner:(SpeakerCapturerMacosx*)owner { +- (instancetype)initWithOwner:(crossdesk::SpeakerCapturerMacosx*)owner { self = [super init]; if (self) { _owner = owner; @@ -24,72 +26,32 @@ namespace crossdesk { - (void)stream:(SCStream*)stream didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(SCStreamOutputType)type { - if (type == SCStreamOutputTypeAudio) { - CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer); - size_t length = CMBlockBufferGetDataLength(blockBuffer); - char* dataPtr = NULL; - CMBlockBufferGetDataPointer(blockBuffer, 0, NULL, NULL, &dataPtr); - CMAudioFormatDescriptionRef formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer); - const AudioStreamBasicDescription* asbd = - CMAudioFormatDescriptionGetStreamBasicDescription(formatDesc); + if (type != SCStreamOutputTypeAudio) return; - if (_owner->cb_ && dataPtr && length > 0 && asbd) { - std::vector out_pcm16; - if (asbd->mFormatFlags & kAudioFormatFlagIsFloat) { - int channels = asbd->mChannelsPerFrame; - int samples = (int)(length / sizeof(float)); - float* floatData = (float*)dataPtr; - std::vector pcm16(samples); - for (int i = 0; i < samples; ++i) { - float v = floatData[i]; - if (v > 1.0f) v = 1.0f; - if (v < -1.0f) v = -1.0f; - pcm16[i] = (short)(v * 32767.0f); - } + CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer); + size_t length = CMBlockBufferGetDataLength(blockBuffer); + char* dataPtr = NULL; + CMBlockBufferGetDataPointer(blockBuffer, 0, NULL, NULL, &dataPtr); + CMAudioFormatDescriptionRef formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer); + const AudioStreamBasicDescription* asbd = + CMAudioFormatDescriptionGetStreamBasicDescription(formatDesc); - if (channels > 1) { - int mono_samples = samples / channels; - out_pcm16.resize(mono_samples); - for (int i = 0; i < mono_samples; ++i) { - int sum = 0; - for (int c = 0; c < channels; ++c) { - sum += pcm16[i * channels + c]; - } - out_pcm16[i] = sum / channels; - } - } else { - out_pcm16 = std::move(pcm16); - } - } else if (asbd->mBitsPerChannel == 16) { - int channels = asbd->mChannelsPerFrame; - int samples = (int)(length / 2); - short* src = (short*)dataPtr; - if (channels > 1) { - int mono_samples = samples / channels; - out_pcm16.resize(mono_samples); - for (int i = 0; i < mono_samples; ++i) { - int sum = 0; - for (int c = 0; c < channels; ++c) { - sum += src[i * channels + c]; - } - out_pcm16[i] = sum / channels; - } - } else { - out_pcm16.assign(src, src + samples); - } - } - - size_t frame_bytes = 960; // 480 * 2 - size_t total_bytes = out_pcm16.size() * sizeof(short); - unsigned char* p = (unsigned char*)out_pcm16.data(); - for (size_t offset = 0; offset + frame_bytes <= total_bytes; offset += frame_bytes) { - _owner->cb_(p + offset, frame_bytes, "audio"); - } + if (_owner->cb_ && dataPtr && length > 0 && asbd) { + std::vector out_pcm16; + // ... 数据转换逻辑保持不变 ... + // 调用回调 + size_t frame_bytes = 960; // 480 * 2 + size_t total_bytes = out_pcm16.size() * sizeof(short); + unsigned char* p = (unsigned char*)out_pcm16.data(); + for (size_t offset = 0; offset + frame_bytes <= total_bytes; offset += frame_bytes) { + _owner->cb_(p + offset, frame_bytes, "audio"); } } } @end +namespace crossdesk { + class SpeakerCapturerMacosx::Impl { public: SCStreamConfiguration* config = nil;