You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
4.3 KiB
111 lines
4.3 KiB
#import "OpusPlugin.h"
|
|
#import "OpusBridge.h"
|
|
|
|
@interface OpusPlugin ()
|
|
@property (nonatomic, strong) OpusBridge *opusBridge;
|
|
@end
|
|
|
|
@implementation OpusPlugin
|
|
|
|
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
|
FlutterMethodChannel* channel = [FlutterMethodChannel
|
|
methodChannelWithName:@"com.yunqiinnovation.opus"
|
|
binaryMessenger:[registrar messenger]];
|
|
OpusPlugin* instance = [[OpusPlugin alloc] init];
|
|
[registrar addMethodCallDelegate:instance channel:channel];
|
|
}
|
|
|
|
- (instancetype)init {
|
|
self = [super init];
|
|
if (self) {
|
|
_opusBridge = [[OpusBridge alloc] init];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
|
|
if ([@"createEncoder" isEqualToString:call.method]) {
|
|
NSDictionary *args = call.arguments;
|
|
NSNumber *sampleRateValue = args[@"sampleRate"] ?: @48000;
|
|
NSNumber *channelsValue = args[@"channels"] ?: @2;
|
|
NSNumber *applicationValue = args[@"application"] ?: @2048;
|
|
NSNumber *complexityValue = args[@"complexity"] ?: @10;
|
|
NSNumber *frameSizeValue = args[@"frameSize"] ?: @960;
|
|
|
|
BOOL success = [self.opusBridge createEncoderWithSampleRate:sampleRateValue.intValue
|
|
channels:channelsValue.intValue
|
|
application:applicationValue.intValue
|
|
complexity:complexityValue.intValue
|
|
frameSize:frameSizeValue.intValue];
|
|
|
|
result(@(success));
|
|
|
|
} else if ([@"createDecoder" isEqualToString:call.method]) {
|
|
NSDictionary *args = call.arguments;
|
|
NSNumber *sampleRateValue = args[@"sampleRate"] ?: @48000;
|
|
NSNumber *channelsValue = args[@"channels"] ?: @2;
|
|
|
|
BOOL success = [self.opusBridge createDecoderWithSampleRate:sampleRateValue.intValue
|
|
channels:channelsValue.intValue];
|
|
|
|
result(@(success));
|
|
|
|
} else if ([@"encodeData" isEqualToString:call.method]) {
|
|
NSDictionary *args = call.arguments;
|
|
FlutterStandardTypedData *inputData = args[@"input"];
|
|
|
|
if (!inputData) {
|
|
result([FlutterError errorWithCode:@"INVALID_INPUT"
|
|
message:@"Input data is null"
|
|
details:nil]);
|
|
return;
|
|
}
|
|
|
|
NSData *pcmData = inputData.data;
|
|
NSData *encodedData = [self.opusBridge encodePCMData:pcmData];
|
|
|
|
if (encodedData) {
|
|
result([FlutterStandardTypedData typedDataWithBytes:encodedData]);
|
|
} else {
|
|
result([FlutterError errorWithCode:@"ENCODE_FAIL"
|
|
message:@"Failed to encode data"
|
|
details:nil]);
|
|
}
|
|
|
|
} else if ([@"decodeData" isEqualToString:call.method]) {
|
|
NSDictionary *args = call.arguments;
|
|
FlutterStandardTypedData *inputData = args[@"input"];
|
|
NSNumber *frameSizeValue = args[@"frameSize"] ?: @960;
|
|
|
|
if (!inputData) {
|
|
result([FlutterError errorWithCode:@"INVALID_INPUT"
|
|
message:@"Input data is null"
|
|
details:nil]);
|
|
return;
|
|
}
|
|
|
|
NSData *opusData = inputData.data;
|
|
NSData *pcmData = [self.opusBridge decodeOpusData:opusData frameSize:frameSizeValue.intValue];
|
|
|
|
if (pcmData) {
|
|
result([FlutterStandardTypedData typedDataWithBytes:pcmData]);
|
|
} else {
|
|
result([FlutterError errorWithCode:@"DECODE_FAIL"
|
|
message:@"Failed to decode data"
|
|
details:nil]);
|
|
}
|
|
|
|
} else if ([@"disposeEncoder" isEqualToString:call.method]) {
|
|
[self.opusBridge disposeEncoder];
|
|
result(nil);
|
|
|
|
} else if ([@"disposeDecoder" isEqualToString:call.method]) {
|
|
[self.opusBridge disposeDecoder];
|
|
result(nil);
|
|
|
|
} else {
|
|
result(FlutterMethodNotImplemented);
|
|
}
|
|
}
|
|
|
|
@end
|