mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-12-21 15:09:48 +08:00
[refactor] add namespace 'crossdesk' to codebase
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
constexpr int kSampleRate = 48000;
|
||||
constexpr pa_sample_format_t kFormat = PA_SAMPLE_S16LE;
|
||||
constexpr int kChannels = 1;
|
||||
@@ -265,4 +267,5 @@ int SpeakerCapturerLinux::Pause() {
|
||||
int SpeakerCapturerLinux::Resume() {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} // namespace crossdesk
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
#include "speaker_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class SpeakerCapturerLinux : public SpeakerCapturer {
|
||||
public:
|
||||
SpeakerCapturerLinux();
|
||||
@@ -50,5 +52,5 @@ class SpeakerCapturerLinux : public SpeakerCapturer {
|
||||
std::mutex state_mtx_;
|
||||
std::vector<uint8_t> frame_cache_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
@@ -12,6 +12,8 @@
|
||||
|
||||
#include "speaker_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class SpeakerCapturerMacosx : public SpeakerCapturer {
|
||||
public:
|
||||
SpeakerCapturerMacosx();
|
||||
@@ -33,5 +35,5 @@ class SpeakerCapturerMacosx : public SpeakerCapturer {
|
||||
class Impl;
|
||||
Impl* impl_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
@@ -5,13 +5,17 @@
|
||||
#include "rd_log.h"
|
||||
#include "speaker_capturer_macosx.h"
|
||||
|
||||
namespace crossdesk {
|
||||
class SpeakerCapturerMacosx;
|
||||
}
|
||||
|
||||
@interface SpeakerCaptureDelegate : NSObject <SCStreamDelegate, SCStreamOutput>
|
||||
@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;
|
||||
@@ -22,72 +26,32 @@
|
||||
- (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<short> out_pcm16;
|
||||
if (asbd->mFormatFlags & kAudioFormatFlagIsFloat) {
|
||||
int channels = asbd->mChannelsPerFrame;
|
||||
int samples = (int)(length / sizeof(float));
|
||||
float* floatData = (float*)dataPtr;
|
||||
std::vector<short> 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<short> 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;
|
||||
@@ -262,3 +226,4 @@ int SpeakerCapturerMacosx::Destroy() {
|
||||
int SpeakerCapturerMacosx::Pause() { return 0; }
|
||||
|
||||
int SpeakerCapturerMacosx::Resume() { return Start(); }
|
||||
} // namespace crossdesk
|
||||
@@ -9,9 +9,11 @@
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class SpeakerCapturer {
|
||||
public:
|
||||
typedef std::function<void(unsigned char *, size_t, const char *)>
|
||||
typedef std::function<void(unsigned char*, size_t, const char*)>
|
||||
speaker_data_cb;
|
||||
|
||||
public:
|
||||
@@ -23,5 +25,5 @@ class SpeakerCapturer {
|
||||
virtual int Start() = 0;
|
||||
virtual int Stop() = 0;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
@@ -15,6 +15,8 @@
|
||||
#include "speaker_capturer_macosx.h"
|
||||
#endif
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class SpeakerCapturerFactory {
|
||||
public:
|
||||
virtual ~SpeakerCapturerFactory() {}
|
||||
@@ -32,5 +34,5 @@ class SpeakerCapturerFactory {
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#define SAVE_AUDIO_FILE 0
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
static ma_device_config device_config_;
|
||||
static ma_device device_;
|
||||
static ma_format format_ = ma_format_s16;
|
||||
@@ -99,3 +101,4 @@ int SpeakerCapturerWasapi::Destroy() {
|
||||
}
|
||||
|
||||
int SpeakerCapturerWasapi::Pause() { return 0; }
|
||||
} // namespace crossdesk
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "speaker_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class SpeakerCapturerWasapi : public SpeakerCapturer {
|
||||
public:
|
||||
SpeakerCapturerWasapi();
|
||||
@@ -31,5 +33,5 @@ class SpeakerCapturerWasapi : public SpeakerCapturer {
|
||||
private:
|
||||
bool inited_ = false;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
Reference in New Issue
Block a user