1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-04 10:27:46 +01:00
Telegram/Telegraph/TGAudioPlayer.m

185 lines
4.7 KiB
Mathematica
Raw Normal View History

2014-07-10 16:11:09 +02:00
/*
* This is the source code of Telegram for iOS v. 1.1
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Peter Iakovlev, 2013.
*/
#import "TGAudioPlayer.h"
#import "ASQueue.h"
#import "TGOpusAudioPlayerAU.h"
#import "TGNativeAudioPlayer.h"
2015-10-01 18:19:52 +02:00
#import "TGObserverProxy.h"
#import "TGAppDelegate.h"
#import <SSignalKit/SSignalKit.h>
2014-07-10 16:11:09 +02:00
#import <AVFoundation/AVFoundation.h>
2015-10-01 18:19:52 +02:00
#import "TGAudioSessionManager.h"
2014-07-10 16:11:09 +02:00
@interface TGAudioPlayer ()
{
2015-10-01 18:19:52 +02:00
bool _proximityState;
TGObserverProxy *_proximityChangedNotification;
TGHolder *_proximityChangeHolder;
SMetaDisposable *_currentAudioSession;
bool _changingProximity;
2014-07-10 16:11:09 +02:00
}
@end
@implementation TGAudioPlayer
+ (TGAudioPlayer *)audioPlayerForPath:(NSString *)path
{
if (path == nil)
return nil;
if ([TGOpusAudioPlayerAU canPlayFile:path])
return [[TGOpusAudioPlayerAU alloc] initWithPath:path];
else
return [[TGNativeAudioPlayer alloc] initWithPath:path];
}
2015-10-01 18:19:52 +02:00
- (instancetype)init
{
self = [super init];
if (self != nil)
{
_currentAudioSession = [[SMetaDisposable alloc] init];
_proximityState = TGAppDelegateInstance.deviceProximityState;
_proximityChangedNotification = [[TGObserverProxy alloc] initWithTarget:self targetSelector:@selector(proximityChanged:) name:TGDeviceProximityStateChangedNotification object:nil];
_proximityChangeHolder = [[TGHolder alloc] init];
[TGAppDelegateInstance.deviceProximityListeners addHolder:_proximityChangeHolder];
}
return self;
}
- (void)dealloc
{
[TGAppDelegateInstance.deviceProximityListeners removeHolder:_proximityChangeHolder];
}
2014-07-10 16:11:09 +02:00
- (void)play
{
[self playFromPosition:-1.0];
}
- (void)playFromPosition:(NSTimeInterval)__unused position
{
}
- (void)pause
{
}
- (void)stop
{
}
- (NSTimeInterval)currentPositionSync:(bool)__unused sync
{
return 0.0;
}
- (NSTimeInterval)duration
{
return 0.0;
}
+ (ASQueue *)_playerQueue
{
static ASQueue *queue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
queue = [[ASQueue alloc] initWithName:"org.telegram.audioPlayerQueue"];
});
return queue;
}
2015-10-01 18:19:52 +02:00
- (void)proximityChanged:(NSNotification *)__unused notification
{
bool proximityState = TGAppDelegateInstance.deviceProximityState;
[[TGAudioPlayer _playerQueue] dispatchOnQueue:^
{
_proximityState = proximityState;
bool overridePort = _proximityState && ![TGAudioPlayer isHeadsetPluggedIn];
__weak TGAudioPlayer *weakSelf = self;
_changingProximity = true;
[_currentAudioSession setDisposable:[[TGAudioSessionManager instance] requestSessionWithType:overridePort ? TGAudioSessionTypePlayAndRecordHeadphones : TGAudioSessionTypePlayVoice interrupted:^
{
__strong TGAudioPlayer *strongSelf = weakSelf;
if (strongSelf != nil && !strongSelf->_changingProximity)
{
[strongSelf stop];
[strongSelf _notifyFinished];
}
}]];
_changingProximity = false;
}];
}
2014-07-10 16:11:09 +02:00
- (void)_beginAudioSession
{
[[TGAudioPlayer _playerQueue] dispatchOnQueue:^
{
2015-10-01 18:19:52 +02:00
bool overridePort = _proximityState && ![TGAudioPlayer isHeadsetPluggedIn];
__weak TGAudioPlayer *weakSelf = self;
[_currentAudioSession setDisposable:[[TGAudioSessionManager instance] requestSessionWithType:overridePort ? TGAudioSessionTypePlayAndRecordHeadphones : TGAudioSessionTypePlayVoice interrupted:^
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
__strong TGAudioPlayer *strongSelf = weakSelf;
if (strongSelf != nil && !strongSelf->_changingProximity)
{
[strongSelf stop];
[strongSelf _notifyFinished];
}
}]];
2014-07-10 16:11:09 +02:00
}];
}
- (void)_endAudioSession
{
[[TGAudioPlayer _playerQueue] dispatchOnQueue:^
{
2015-10-01 18:19:52 +02:00
[_currentAudioSession setDisposable:nil];
2014-07-10 16:11:09 +02:00
}];
}
- (void)_endAudioSessionFinal
{
2015-10-01 18:19:52 +02:00
SMetaDisposable *currentAudioSession = _currentAudioSession;
2014-07-10 16:11:09 +02:00
[[TGAudioPlayer _playerQueue] dispatchOnQueue:^
{
2015-10-01 18:19:52 +02:00
[currentAudioSession setDisposable:nil];
2014-07-10 16:11:09 +02:00
}];
}
- (void)_notifyFinished
{
id<TGAudioPlayerDelegate> delegate = _delegate;
if ([delegate respondsToSelector:@selector(audioPlayerDidFinishPlaying:)])
[delegate audioPlayerDidFinishPlaying:self];
}
2015-10-01 18:19:52 +02:00
+ (bool)isHeadsetPluggedIn
{
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription *desc in [route outputs])
{
if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
return true;
}
return false;
}
2014-07-10 16:11:09 +02:00
@end