forked from aws-amplify/aws-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWSPollySynthesizeSpeechURLBuilder.m
249 lines (193 loc) · 10.8 KB
/
AWSPollySynthesizeSpeechURLBuilder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/apache2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
#import "AWSPollySynthesizeSpeechURLBuilder.h"
static NSString *const AWSInfoPollySynthesizeSpeechURLBuilder = @"PollySynthesizeSpeechUrlBuilder";
static NSString *const AWSPollySDKVersion = @"2.12.1";
NSString *const AWSPollySynthesizeSpeechURLBuilderErrorDomain = @"com.amazonaws.AWSPollySynthesizeSpeechURLBuilderErrorDomain";
NSString *const AWSPollyPresignedUrlPath = @"v1/speech";
@interface AWSPollySynthesizeSpeechURLBuilder()
@property (nonatomic, strong) AWSServiceConfiguration *configuration;
@end
@interface AWSServiceConfiguration()
@property (nonatomic, strong) AWSEndpoint *endpoint;
@end
@interface AWSPollyStartSpeechSynthesisTaskInput()
+ (NSValueTransformer *)engineJSONTransformer;
+ (NSValueTransformer *)languageCodeJSONTransformer;
+ (NSValueTransformer *)outputFormatJSONTransformer;
+ (NSValueTransformer *)textTypeJSONTransformer;
+ (NSValueTransformer *)voiceIdJSONTransformer;
@end
@implementation AWSPollySynthesizeSpeechURLBuilderRequest
- (void)setLexiconNames:(NSArray<NSString *> *)lexiconNames {
_lexiconNames = lexiconNames;
}
- (void)setSpeechMarkTypes:(NSArray<NSString *> *)speechMarkTypes {
_speechMarkTypes = speechMarkTypes;
}
@end
@implementation AWSPollySynthesizeSpeechURLBuilder
+ (void)initialize {
[super initialize];
if (![AWSiOSSDKVersion isEqualToString:AWSPollySDKVersion]) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"AWSCore and AWSPolly versions need to match. Check your SDK installation. AWSCore: %@ AWSPolly: %@", AWSiOSSDKVersion, AWSPollySDKVersion]
userInfo:nil];
}
}
#pragma mark - Setup
static AWSSynchronizedMutableDictionary *_serviceClients = nil;
+ (instancetype)defaultPollySynthesizeSpeechURLBuilder {
static AWSPollySynthesizeSpeechURLBuilder *_defaultPolly = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
AWSServiceConfiguration *serviceConfiguration = nil;
AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoPollySynthesizeSpeechURLBuilder];
if (serviceInfo) {
serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region
credentialsProvider:serviceInfo.cognitoCredentialsProvider];
}
if (!serviceConfiguration) {
serviceConfiguration = [AWSServiceManager defaultServiceManager].defaultServiceConfiguration;
}
if (!serviceConfiguration) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"The service configuration is `nil`. You need to configure `Info.plist` or set `defaultServiceConfiguration` before using this method."
userInfo:nil];
}
_defaultPolly = [[AWSPollySynthesizeSpeechURLBuilder alloc] initWithConfiguration:serviceConfiguration];
});
return _defaultPolly;
}
+ (void)registerPollySynthesizeSpeechURLBuilder:(AWSServiceConfiguration *)configuration forKey:(NSString *)key{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_serviceClients = [AWSSynchronizedMutableDictionary new];
});
[_serviceClients setObject:[[AWSPollySynthesizeSpeechURLBuilder alloc] initWithConfiguration:configuration]
forKey:key];
}
+ (instancetype)PollySynthesizeSpeechURLBuilderForKey:(NSString *)key {
@synchronized(self) {
AWSPollySynthesizeSpeechURLBuilder *serviceClient = [_serviceClients objectForKey:key];
if (serviceClient) {
return serviceClient;
}
AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] serviceInfo:AWSInfoPollySynthesizeSpeechURLBuilder
forKey:key];
if (serviceInfo) {
AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region
credentialsProvider:serviceInfo.cognitoCredentialsProvider];
[AWSPollySynthesizeSpeechURLBuilder registerPollySynthesizeSpeechURLBuilder:serviceConfiguration
forKey:key];
}
return [_serviceClients objectForKey:key];
}
}
+ (void)removePollySynthesizeSpeechURLBuilderForKey:(NSString *)key{
[_serviceClients removeObjectForKey:key];
}
- (instancetype)init {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"`- init` is not a valid initializer. Use `+ defaultPolly` or `+ PollyForKey:` instead."
userInfo:nil];
return nil;
}
#pragma mark -
- (instancetype)initWithConfiguration:(AWSServiceConfiguration *)configuration {
if (self = [super init]) {
_configuration = [configuration copy];
_configuration.endpoint = [[AWSEndpoint alloc] initWithRegion:_configuration.regionType
service:AWSServicePolly
useUnsafeURL:NO];
}
return self;
}
- (AWSTask<NSURL *> *)getPreSignedURL:(AWSPollySynthesizeSpeechURLBuilderRequest *)preSignedURLRequest{
return [[AWSTask taskWithResult:nil] continueWithSuccessBlock:^id _Nullable(AWSTask *task) {
id<AWSCredentialsProvider> credentialProvider = self.configuration.credentialsProvider;
AWSEndpoint *endpoint = self.configuration.endpoint;
//validate expires Date
if (!preSignedURLRequest.expires) {
//set default expiry to 15 mins from now.
[preSignedURLRequest setExpires:[NSDate dateWithTimeIntervalSinceNow:15*60]];
}
if ([preSignedURLRequest.expires timeIntervalSinceNow] < 0.0) {
return [AWSTask taskWithError:[NSError errorWithDomain:AWSPollySynthesizeSpeechURLBuilderErrorDomain
code:AWSPollySynthesizeSpeechURLBuilderInvalidExpiresDate
userInfo:@{NSLocalizedDescriptionKey: @"expires can not be in past"}]
];
}
int32_t expireDuration = [preSignedURLRequest.expires timeIntervalSinceNow];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
[parameters setObject:[self stringFromOutputFormat:preSignedURLRequest.outputFormat] forKey:@"OutputFormat"];
if (preSignedURLRequest.sampleRate) {
[parameters setObject:preSignedURLRequest.sampleRate forKey:@"SampleRate"];
}
[parameters setObject:preSignedURLRequest.text forKey:@"Text"];
if (preSignedURLRequest.textType) {
[parameters setObject:[self stringFromTextType:preSignedURLRequest.textType] forKey:@"TextType"];
}
[parameters setObject:[self stringFromVoiceId:preSignedURLRequest.voiceId] forKey:@"VoiceId"];
if (preSignedURLRequest.lexiconNames && [preSignedURLRequest.lexiconNames count] >= 1) {
[parameters setObject:preSignedURLRequest.lexiconNames forKey:@"LexiconNames"];
}
if (preSignedURLRequest.speechMarkTypes && [preSignedURLRequest.speechMarkTypes count] >= 1) {
[parameters setObject:preSignedURLRequest.speechMarkTypes forKey:@"SpeechMarkTypes"];
}
if (preSignedURLRequest.languageCode) {
[parameters setObject:[self stringFromLanguageCode:preSignedURLRequest.languageCode] forKey:@"LanguageCode"];
}
if (preSignedURLRequest.engine) {
[parameters setObject:[self stringFromEngine:preSignedURLRequest.engine] forKey:@"Engine"];
}
NSMutableDictionary *headers = [NSMutableDictionary new];
[headers setObject:endpoint.hostName forKey:@"host"];
return [AWSSignatureV4Signer generateQueryStringForSignatureV4WithCredentialProvider:credentialProvider
httpMethod:AWSHTTPMethodGET
expireDuration:expireDuration
endpoint:endpoint
keyPath:AWSPollyPresignedUrlPath
requestHeaders:headers
requestParameters:parameters
signBody:YES];
}];
}
- (NSString *)stringFromOutputFormat:(AWSPollyOutputFormat)format {
NSValueTransformer *transformer = [AWSPollyStartSpeechSynthesisTaskInput outputFormatJSONTransformer];
NSString *transformedValue = (NSString *)[transformer reverseTransformedValue:(id)@(format)];
return transformedValue;
}
- (NSString *)stringFromTextType:(AWSPollyTextType)textType {
NSValueTransformer *transformer = [AWSPollyStartSpeechSynthesisTaskInput textTypeJSONTransformer];
NSString *transformedValue = (NSString *)[transformer reverseTransformedValue:(id)@(textType)];
return transformedValue;
}
- (NSString *)stringFromLanguageCode:(AWSPollyLanguageCode)languageCode {
NSValueTransformer *transformer = [AWSPollyStartSpeechSynthesisTaskInput languageCodeJSONTransformer];
NSString *transformedValue = (NSString *)[transformer reverseTransformedValue:(id)@(languageCode)];
return transformedValue;
}
- (NSString *)stringFromVoiceId:(AWSPollyVoiceId)voiceId {
NSValueTransformer *transformer = [AWSPollyStartSpeechSynthesisTaskInput voiceIdJSONTransformer];
NSString *transformedValue = (NSString *)[transformer reverseTransformedValue:(id)@(voiceId)];
return transformedValue;
}
- (NSString *)stringFromEngine:(AWSPollyEngine)engine {
NSValueTransformer *transformer = [AWSPollyStartSpeechSynthesisTaskInput engineJSONTransformer];
NSString *transformedValue = (NSString *)[transformer reverseTransformedValue:(id)@(engine)];
return transformedValue;
}
@end