summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Fischer <joeljfischer@gmail.com>2019-05-30 09:58:34 -0400
committerJoel Fischer <joeljfischer@gmail.com>2019-05-30 09:58:34 -0400
commitfff87c17f1edd794fbaa7888871b8129c877dfbf (patch)
tree7d0091ec72f25a8aa17979bc45201f8b3f316427
parent3a8bd00d7b10689a178b6ae575656dcd53945e31 (diff)
downloadsdl_ios-fff87c17f1edd794fbaa7888871b8129c877dfbf.tar.gz
Add audio data queueing to the audio stream manager
-rwxr-xr-xSmartDeviceLink/SDLAudioFile.h32
-rwxr-xr-xSmartDeviceLink/SDLAudioFile.m9
-rwxr-xr-xSmartDeviceLink/SDLAudioStreamManager.h7
-rwxr-xr-xSmartDeviceLink/SDLAudioStreamManager.m30
4 files changed, 75 insertions, 3 deletions
diff --git a/SmartDeviceLink/SDLAudioFile.h b/SmartDeviceLink/SDLAudioFile.h
index e34c37a8d..794c50cab 100755
--- a/SmartDeviceLink/SDLAudioFile.h
+++ b/SmartDeviceLink/SDLAudioFile.h
@@ -12,21 +12,49 @@ NS_ASSUME_NONNULL_BEGIN
@interface SDLAudioFile : NSObject
-@property (copy, nonatomic, readonly) NSURL *inputFileURL;
+/**
+ If initialized with a file URL, the file URL it came from
+ */
+@property (nullable, copy, nonatomic, readonly) NSURL *inputFileURL;
-@property (copy, nonatomic, readonly) NSURL *outputFileURL;
+/**
+ If initialized with a file URL, where the transcoder should produce the transcoded PCM audio file
+ */
+@property (nullable, copy, nonatomic, readonly) NSURL *outputFileURL;
/**
In seconds. UINT32_MAX if unknown.
*/
@property (assign, nonatomic) UInt32 estimatedDuration;
+/**
+ The PCM audio data to be transferred and played
+ */
@property (copy, nonatomic, readonly) NSData *data;
+/**
+ The size of the PCM audio data in bytes
+ */
@property (assign, nonatomic, readonly) unsigned long long fileSize;
+/**
+ Initialize an audio file to be queued and played
+
+ @param inputURL The file that exists on the device to be transcoded and queued
+ @param outputURL The target URL that the transcoded file will be output to
+ @param duration The duration of the file
+ @return The audio file object
+ */
- (instancetype)initWithInputFileURL:(NSURL *)inputURL outputFileURL:(NSURL *)outputURL estimatedDuration:(UInt32)duration;
+/**
+ Initialize a buffer of PCM audio data to be queued and played
+
+ @param data The PCM audio data buffer
+ @return The audio file object
+ */
+- (instancetype)initWithData:(NSData *)data;
+
@end
NS_ASSUME_NONNULL_END
diff --git a/SmartDeviceLink/SDLAudioFile.m b/SmartDeviceLink/SDLAudioFile.m
index 1d0e2ef98..46b50f540 100755
--- a/SmartDeviceLink/SDLAudioFile.m
+++ b/SmartDeviceLink/SDLAudioFile.m
@@ -32,6 +32,15 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
+- (instancetype)initWithData:(NSData *)data {
+ self = [super init];
+ if (!self) { return nil; }
+
+ _data = data;
+
+ return self;
+}
+
- (NSData *)data {
if (_data.length == 0) {
return [NSData dataWithContentsOfURL:_outputFileURL];
diff --git a/SmartDeviceLink/SDLAudioStreamManager.h b/SmartDeviceLink/SDLAudioStreamManager.h
index e6608f8e1..bb44ef978 100755
--- a/SmartDeviceLink/SDLAudioStreamManager.h
+++ b/SmartDeviceLink/SDLAudioStreamManager.h
@@ -67,6 +67,13 @@ typedef NS_ENUM(NSInteger, SDLAudioStreamManagerError) {
- (void)pushWithFileURL:(NSURL *)fileURL;
/**
+ Push a new audio buffer onto the queue. Call `playNextWhenReady` to start playing the pushed audio buffer.
+
+ @param data The audio buffer to be pushed onto the queue
+ */
+- (void)pushWithData:(NSData *)data;
+
+/**
Play the next item in the queue. If an item is currently playing, it will continue playing and this item will begin playing after it is completed.
When complete, this will callback on the delegate.
diff --git a/SmartDeviceLink/SDLAudioStreamManager.m b/SmartDeviceLink/SDLAudioStreamManager.m
index 69312b0b5..f91db09c5 100755
--- a/SmartDeviceLink/SDLAudioStreamManager.m
+++ b/SmartDeviceLink/SDLAudioStreamManager.m
@@ -46,10 +46,15 @@ NSString *const SDLErrorDomainAudioStreamManager = @"com.sdl.extension.pcmAudioS
return self;
}
+#pragma mark - Getters
+
- (NSArray<SDLFile *> *)queue {
return [_mutableQueue copy];
}
+#pragma mark - Pushing to the Queue
+#pragma mark Files
+
- (void)pushWithFileURL:(NSURL *)fileURL {
dispatch_async(_audioQueue, ^{
[self sdl_pushWithContentsOfURL:fileURL];
@@ -79,6 +84,21 @@ NSString *const SDLErrorDomainAudioStreamManager = @"com.sdl.extension.pcmAudioS
}
}
+#pragma mark Raw Data
+
+- (void)pushWithData:(NSData *)data {
+ dispatch_async(_audioQueue, ^{
+ [self sdl_pushWithData:data];
+ });
+}
+
+- (void)sdl_pushWithData:(NSData *)data {
+ SDLAudioFile *audioFile = [[SDLAudioFile alloc] initWithData:data];
+ [self.mutableQueue addObject:audioFile];
+}
+
+#pragma mark Playing from the Queue
+
- (void)playNextWhenReady {
dispatch_async(_audioQueue, ^{
[self sdl_playNextWhenReady];
@@ -104,8 +124,14 @@ NSString *const SDLErrorDomainAudioStreamManager = @"com.sdl.extension.pcmAudioS
[self.mutableQueue removeObjectAtIndex:0];
// Strip the first bunch of bytes (because of how Apple outputs the data) and send to the audio stream, if we don't do this, it will make a weird click sound
+ NSData *audioData = nil;
+ if (file.inputFileURL != nil) {
+ audioData = [file.data subdataWithRange:NSMakeRange(5760, (file.data.length - 5760))];
+ } else {
+ audioData = file.data;
+ }
+
SDLLogD(@"Playing audio file: %@", file);
- NSData *audioData = [file.data subdataWithRange:NSMakeRange(5760, (file.data.length - 5760))];
__block BOOL success = [self.streamManager sendAudioData:audioData];
self.playing = YES;
@@ -125,6 +151,8 @@ NSString *const SDLErrorDomainAudioStreamManager = @"com.sdl.extension.pcmAudioS
});
}
+#pragma mark - Stopping Playback
+
- (void)stop {
dispatch_async(_audioQueue, ^{
self.shouldPlayWhenReady = NO;