123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775 |
- //
- // MKRWIFIDeviceManager.m
- // MKRAVPlayer
- //
- // Created by yyqxiaoyin on 2019/4/16.
- //
- #import "MKRWIFIDeviceManager.h"
- #import "NSString+MKRRadioManagerAdd.h"
- #import "UIDevice+MKRRadioManagerAdd.h"
- #import <pthread.h>
- #import <libAS-Control/ASControlTools.h>
- #import <libAS-Control/NSDictionary+MKRXml.h>
- #import <NSHashTable+MKRRadioManagerAdd.h>
- static inline void wd_main_queue(void (^block)(void)){
- if (pthread_main_np()) {
- block();
- }else{
- dispatch_async(dispatch_get_main_queue(), block);
- }
- }
- static inline void wd_playControl_queue(void (^block)(void)){
- dispatch_queue_t queue = dispatch_queue_create("com.maoking.playControlQueue", DISPATCH_QUEUE_SERIAL);
- dispatch_async(queue, block);
- }
- @interface MKRWIFIDeviceManager () <ASControlDelegate> {
- int _upnpErrorCode;
- NSMutableArray *_baseUrlArray;
- dispatch_source_t _searchTimer;
- NSString *_ssid;
- BOOL _shutDownUpnping;
- BOOL _updateLocalResourceLock;
- }
- @property (nonatomic, strong) NSHashTable *listeners;
- @end
- @implementation MKRWIFIDeviceManager
- - (instancetype)init {
- if (self = [super init]) {
- _upnpErrorCode = -1;
- wd_playControl_queue(^{
- [self startUpnp];
- });
- [ASControlTools sharedInstanceUPNP].delegate = self;
- }
- return self;
- }
- + (instancetype)shareManager {
- static MKRWIFIDeviceManager *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[MKRWIFIDeviceManager alloc] init];
- });
- return manager;
- }
- - (void)addDeviceManagerListener:(id<MKRWIFIDeviceManagerDelegate>)listener{
- if (![self.listeners containsObject:listener]){
- [self.listeners addObject:listener];
- }
- }
- - (void)removeDeviceManagerListener:(id<MKRWIFIDeviceManagerDelegate>)listener{
- if ([self.listeners containsObject:listener]) {
- [self.listeners removeObject:listener];
- }
- }
- - (NSInteger)startUpnp {
- NSString *userPhoneName = [[UIDevice currentDevice] name];
- NSString *sharePath = NSHomeDirectory();
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *DocumentSharePath = [paths objectAtIndex:0];
- NSString *sharePath2 = NSTemporaryDirectory();
- NSArray *dirArgv =[[NSArray alloc] initWithObjects:DocumentSharePath,sharePath2,nil];
- libupnp_RELoadUpnp();
- _upnpErrorCode = libupnp_UpnpStartTest([UIDevice currentDeviceMacAddress],userPhoneName,2,dirArgv,[UIDevice currentDeviceInfo],7);
- if (_upnpErrorCode == 0) {
- NSLog(@"upnp初始化成功");
- _upnpErrorCode = libupnp_UpnpSetWebServerRootDir(sharePath);
- }else if(_upnpErrorCode == -100) {
- NSLog(@"UpnpStart 无网络连接!! ==> %d ", _upnpErrorCode);
- }else{//UPNP_ERROR
- NSLog(@"UpnpStart Error!! ==> %d" ,_upnpErrorCode);
- }
- return _upnpErrorCode;
- }
- - (void)stopUpnp{
- wd_playControl_queue(^{
- libupnp_StopBuildFilesList();
- [NSThread sleepForTimeInterval:5];
- if(_upnpErrorCode == 0){
- libupnp_UpnpQuickExit();
- }
- });
- }
- - (void)reloadUpnp{
- NSString *ssid = [UIDevice currentSSID];
- if ([ssid isEqualToString:_ssid]) {
- return;
- }
- if (_shutDownUpnping) {
- return;
- }
- _ssid = ssid;
- wd_playControl_queue(^{
- _shutDownUpnping = YES;
- libupnp_StopBuildFilesList();
- int exitCount = 0;
- while (_updateLocalResourceLock) {
- exitCount++;
- [NSThread sleepForTimeInterval:1.0];
- if (exitCount > 10)
- break;
- }
- _updateLocalResourceLock = NO;
- [self searchDeviceWithSearchTime:20 timeBlock:nil];
- });
- }
- - (void)searchDeviceWithSearchTime:(int)searchTime
- timeBlock:(void(^)(NSInteger time))timeBlock {
- wd_playControl_queue(^{
- _updateLocalResourceLock = YES;
- if (_upnpErrorCode != 0) {
- if ([self startUpnp] == 0) {
- [self checkBaseURL];
- libupnp_BuildFilesList();
- }
- }
- libupnp_SearchDevice(20);
- });
- _ssid = [UIDevice currentSSID];
- __block int count = searchTime +1;
- _searchTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
- dispatch_source_set_timer(_searchTimer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
- dispatch_source_set_event_handler(_searchTimer, ^{
- if (timeBlock) {
- wd_main_queue(^{
- timeBlock(count);
- });
- }
- count --;
- if (count == 0) {
- dispatch_source_cancel(self->_searchTimer);
- }
- });
- dispatch_source_set_cancel_handler(_searchTimer, ^{
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"搜索结束");
- });
- });
- dispatch_resume(_searchTimer);
- }
- - (void)checkBaseURL{
- NSMutableArray *baseURLArray = [NSMutableArray array];
- for (NSString *baseValue in _baseUrlArray) {
- NSString *tempStr = [baseValue stringByReplacingOccurrencesOfString:@"49152" withString:@"MaoKing"];
- tempStr = [tempStr stringByReplacingOccurrencesOfString:@"49153" withString:@"MaoKing"];
- tempStr = [tempStr stringByReplacingOccurrencesOfString:@"49154" withString:@"MaoKing"];
- [baseURLArray addObject:[tempStr stringByReplacingOccurrencesOfString:@"MaoKing" withString:@"49152"]];
- [baseURLArray addObject:[tempStr stringByReplacingOccurrencesOfString:@"MaoKing" withString:@"49153"]];
- [baseURLArray addObject:[tempStr stringByReplacingOccurrencesOfString:@"MaoKing" withString:@"49154"]];
- }
- if([baseURLArray count] > 0)libupnp_FirstCheckByBaseURL((int)[baseURLArray count] , baseURLArray);
- }
- #pragma mark - ASControlDelegate
- -(void)ReceivedPlayCallBack:(NSString *)nUDN{
- NSLog(@"收到播放回调%@",nUDN);
- wd_main_queue(^{
- if ([nUDN containsString:self.device.deviceUUID]) {
- self.currentResource.resourceStatus.playState = MKRResourcePlayStatePlaying;
- [self.listeners makeObjectsPerformSelectorWithObjects:@selector(didReceivePlayStateChange:),MKRResourcePlayStatePlaying];
- }
- });
- }
- -(void)ReceivedPauseCallBack:(NSString *)nUDN{
- NSLog(@"收到暂停回调%@",nUDN);
- wd_main_queue(^{
- if ([nUDN containsString:self.device.deviceUUID]) {
- self.currentResource.resourceStatus.playState = MKRResourcePlayStatePause;
- [self.listeners makeObjectsPerformSelectorWithObjects:@selector(didReceivePlayStateChange:),MKRResourcePlayStatePause];
- }
- });
- }
- -(void)ReceivedStopCallBack:(NSString *)nUDN{
- NSLog(@"收到停止回调%@",nUDN);
- wd_main_queue(^{
- if ([nUDN containsString:self.device.deviceUUID]) {
- self.currentResource.resourceStatus.playState = MKRResourcePlayStateStopped;
- [self.listeners makeObjectsPerformSelectorWithObjects:@selector(didReceivePlayStateChange:),MKRResourcePlayStateStopped];
- }
- });
- }
- -(void)ReceivedSetVolumeCallBack:(NSString *)nUDN Volume:(NSInteger)Volume{
- wd_main_queue(^{
- if ([nUDN containsString:self.device.deviceUUID]) {
- NSLog(@"收到设备音量回调SetZoneVolumeCallBack:%zd",Volume);
- [self.listeners makeObjectsPerformSelectorWithObjects:@selector(didReceiveVolumeChange:),(float)Volume];
- }
- });
- }
- -(void)ReceivedSetMuteCallBack:(NSString *)nUDN Mute:(NSInteger)Mute{
- NSLog(@"%s",__func__);
- }
- -(void)ReceivedSetZoneMuteCallBack:(NSString *)nUDN Mute:(NSInteger)Mute{
- NSLog(@"%s",__func__);
- }
- -(void)ReceivedSetGroupIdCallBack:(NSString *)nUDN GrouId:(NSString *)GroupId{
- NSLog(@"%s",__func__);
- }
- -(void)ReceivedSetSleepStateCallBack:(NSString *)nUDN SleepState:(NSString *)SleepState{
- NSLog(@"%s",__func__);
- }
- -(void)ReceivedSetSoundSourceCallBack:(NSString *)nUDN SoundSource:(NSString *)SoundSource{
- NSLog(@"%s",__func__);
- }
- -(void)ReceivedSetVolumeChannelCallBack:(NSString *)nUDN Channel:(NSString *)Channel {
- NSLog(@"ReceivedSetVolumeChannelCallBack-------------%@ ",Channel);
- }
- -(void)ReceivedSetZoneVolumeCallBack:(NSString *)nUDN Volume:(NSInteger)Volume {
- NSLog(@"%s",__func__);
- }
- -(void)CallBackRemoveZoneMedia:(NSString *)nByeUDN BaseURL:(NSString *)BaseURL{
- NSLog(@"%s",__func__);
- wd_main_queue(^{
- [self.listeners makeObjectsPerformSelectorWithObjects:@selector(didReceiveDeviceOffline:),nByeUDN];
- });
- }
- -(void)CallBackRemoveRenderer:(NSString *)nByeUDN BaseURL:(NSString *)BaseURL
- {
- NSLog(@"CallBackRemoveRenderer---------%@",nByeUDN);
- NSLog(@"%s",__func__);
- }
- -(void)CallBackRemoveDMS:(NSString *)nByeUDN BaseURL:(NSString *)BaseURL{
- NSLog(@"%s",__func__);
- }
- -(void)CallBackAddZoneMedia:(NSArray *)nStr Size:(NSInteger)size{
- NSLog(@"搜索到设备:%@",nStr);
- NSLog(@"%s",__func__);
- wd_playControl_queue(^{
- [self addDevice:nStr];
- });
- }
- - (void)updateClockID:(NSString *)clockID clockDic:(NSDictionary *)clockDic clockTime:(NSString *)clockTime operationType:(BOOL)isOn deleteClock:(BOOL)isDelete{
- NSLog(@"%s",__func__);
- }
- -(void)CallBackAddMediaServer:(NSArray *)nStr Size:(NSInteger)size{
- NSLog(@"%s",__func__);
- }
- -(void)CallBackAddMediaRenderer:(NSArray *)nStr Size:(NSInteger)size{
- NSLog(@"%s",__func__);
- }
- -(void)ReceivedChangeChannelCallBack:(NSString *)nUDN Channel:(NSString *)channel{
- NSLog(@"%s",__func__);
- NSLog(@"收到频道改变回调%@",nUDN);
- wd_main_queue(^{
- if ([nUDN containsString:self.device.deviceUUID]) {
- [self.listeners makeObjectsPerformSelectorWithObjects:@selector(didReceivePlayingChannelChange:),[channel integerValue]];
- }
- });
- }
- -(void)getLocalIp:(NSString *)Ip{
- NSLog(@"%s",__func__);
- }
- -(void)AddLocalResource:(NSArray *)oneSongID3Info{
- NSLog(@"%s",__func__);
- }
- -(void)UpdateLocalResourceFinish{
- NSLog(@"%s",__func__);
- }
- - (void)addDevice:(NSArray *)array {
- NSMutableDictionary *deviceDict = [NSMutableDictionary dictionary];
- [deviceDict setObject:array[0] forKey:@"deviceName"];
- [deviceDict setObject:array[1] forKey:@"deviceUUID"];
- [deviceDict setObject:array[2] forKey:@"deviceBaseUrl"];
- [deviceDict setObject:array[5] forKey:@"connectionManagerUrl"];
- [deviceDict setObject:array[7] forKey:@"avTransportUrl"];
- [deviceDict setObject:array[9] forKey:@"renderingControlUrl"];
- [deviceDict setObject:array[11] forKey:@"groupManagementUrl"];
- NSArray *deviceInfo = [[ASControlTools sharedInstanceUPNP] libupnp_GetDeviceInfo:array[11] ServiceTYPE:array[10]];
- if(deviceInfo == nil || [deviceInfo isEqual:[NSNull null]]){
- return;
- }
- [deviceDict setObject:deviceInfo[0] forKey:@"devGId"];
- [deviceDict setObject:deviceInfo[1] forKey:@"devWiFiDB"];
- [deviceDict setObject:[deviceInfo[16] length]?deviceInfo[16]: @"AWAKE" forKey:@"devSleepStatus"];
- [deviceDict setObject:[deviceInfo[17] length]?deviceInfo[17]: @"WIFI" forKey:@"devSource"];
- [deviceDict setObject:[deviceInfo[28] length]?deviceInfo[28]:@"WIFI" forKey:@"devSourceList"];
- [deviceDict setObject:deviceInfo[12] forKey:@"channelLR"];
- [deviceDict setObject:[deviceInfo[29] length] ? deviceInfo[29]: @"0.0.0.0" forKey:@"devType"];
- [deviceDict setObject:[deviceInfo[32] length] ? deviceInfo[32]: @"" forKey:@"deviceClockInfo"];
- [deviceDict setObject:[deviceInfo[33] length]? deviceInfo[33]:@"NULL" forKey:@"devPower"];
- [deviceDict setObject:[deviceInfo[36] length] ? deviceInfo[36]:@"NULL" forKey:@"devUserID"];
- NSString *devFlag = [NSString stringWithFormat:@"%@-%@",deviceInfo[9],deviceInfo[10]];
- [deviceDict setObject:devFlag forKey:@"devFlag"];
- [deviceDict setObject:[deviceInfo[18] length] ?deviceInfo[18]:@"MJ_M" forKey:@"devProductModel"];
- [deviceDict setObject:[deviceInfo[39] length] ?deviceInfo[39] :@"0" forKey:@"devOpenLowPowerNotice"];
- [deviceDict setObject:[deviceInfo[19] length] ? deviceInfo[19] : @"1.0.0" forKey:@"devVersion"];
- [deviceDict setObject:[deviceInfo[41] length] ? deviceInfo[41] :@"" forKey:@"devDuerAccessToken"];
- [deviceDict setObject:[deviceInfo[42] length] ?deviceInfo[42] : @"" forKey:@"devDuerRefreshToken"];
- [deviceDict setObject:deviceInfo[13] forKey:@"volume"];
- [deviceDict setObject:deviceInfo[14] forKey:@"mute"];
- [deviceDict setObject:[deviceInfo[40] length] ?deviceInfo[40] :@"-1" forKey:@"devPauseShutDownTime"];
- NSString *devNowStatus = [deviceInfo[2] length] ? deviceInfo[2] : @"STOPPED";
- [deviceDict setObject:devNowStatus forKey:@"devNowStatus"];
- [deviceDict setObject:[deviceInfo[30] length] ?deviceInfo[30] :@"REPEAT_ALL" forKey:@"devNowPlayMode"];
- NSURL *url = [NSURL URLWithString:array[2]];
- [deviceDict setObject:url.URLByDeletingLastPathComponent.absoluteString?:@"" forKey:@"devIP"];
- if([devNowStatus isEqualToString:@"PAUSED_PLAYBACK"]){
- NSString *devNowPlaying = [NSString stringWithFormat:@"Pause:%@",deviceInfo[3]];
- [deviceDict setObject:devNowPlaying forKey:@"devNowPlaying"];
- }else if([devNowStatus isEqualToString:@"PLAYING"]){
- [deviceDict setObject:deviceInfo[3] forKey:@"devNowPlaying"];
- }
- [deviceDict setObject:deviceInfo[4] forKey:@"devNowArtist"];
- if([deviceDict[@"devNowPlaying"] isEqualToString:@"Unknown:TrackMetaData Incomplete"]){
- [deviceDict setObject:@"No Music" forKey:@"devNowPlaying"];
- }
- if([deviceDict[@"devNowArtist"] isEqualToString:@"Unknown:TrackMetaData Incomplete"])
- {
- [deviceDict setObject:@"Unknown" forKey:@"devNowArtist"];
- }
- if(deviceDict[@"devNowArtist"] == nil||[deviceDict[@"devNowArtist"] isEqual:[NSNull null]]){
- [deviceDict setObject:@"Unknown" forKey:@"devNowArtist"];
- }
- [deviceDict setObject:@"1" forKey:@"isOnLine"];
- wd_main_queue(^{
- id<MKRWIFIDeviceManagerDelegate> obj;
- NSEnumerator *enumerator = [self.listeners objectEnumerator];
- while ((obj = [enumerator nextObject]) != nil) {
- if ([obj respondsToSelector:@selector(didSearchNewDevice:)]) {
- [obj didSearchNewDevice:deviceDict];
- }
- }
- });
- }
- - (void)setM3UList:(MKRM3UListModel *)m3uListModel{
- self.currentTempM3UListModel = m3uListModel;
- libupnp_SetNewM3UListV2(m3uListModel.secondsList,
- m3uListModel.artistList,
- m3uListModel.titleList,
- m3uListModel.urlList,
- m3uListModel.songFromList,
- m3uListModel.vipCanPlay,
- m3uListModel.channelNO,
- m3uListModel.playModeString,
- m3uListModel.timeString,
- m3uListModel.startIndex,
- m3uListModel.endIndex,
- m3uListModel.m3uSize,
- m3uListModel.channelID,
- m3uListModel.channelInfoString);
- }
- - (void)setAVTransportURIWithSong:(id <MKRRadioResorce>)song
- device:(id <MKRUPnPDevice>)device{
- NSString *timestamp = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
- NSString *channelNumString = @(song.songChannel).stringValue;
- NSString *userID = self.userID;
- NSString *songUrlString = [NSString stringWithFormat:@"AIRSMART//ID_%zd//",song.songID];
- NSString *songFrom = [NSString stringWithFormat:@"1//%zd//%zd//",[MKRM3UListModel convertAudioTypeWithSorceType:song.sourceType],song.songID];
- libupnp_SetAVTransportURIV2(
- device.avTransportUrl,
- songUrlString,
- song.songName?:@"",
- song.songArtist?:@"",
- song.songAlbum?:@"",
- song.songAlbumCover?:@"",
- @"0",
- @"null",
- @"null",
- timestamp,
- channelNumString,
- @"0",
- userID?:@"",
- @(song.songID).stringValue,
- songFrom,
- song.songAlbumID?:@""
- );
-
- }
- - (void)playWithDevice:(id <MKRUPnPDevice>)device {
- wd_playControl_queue(^{
- libupnp_Play(device.avTransportUrl);
- });
- }
- - (void)_playPlayFlag:(NSString *)playFlag
- device:(id <MKRUPnPDevice>)device
- timeStamp:(NSTimeInterval)timeStamp{
- NSString *playListUrl = [NSString stringWithFormat:@"%@/web/playlist",[ASControlTools sharedInstanceUPNP].myIp];
- NSString *timestamp = [NSString stringWithFormat:@"%.0f", timeStamp];
- NSString *userID = self.userID;
- libupnp_PlayNumFlagV2(
- device.avTransportUrl,
- playFlag,
- timestamp,
- userID,
- playListUrl,
- @"0"
- );
- }
- - (void)playSongWithChannelNO:(NSInteger)channelNO
- firstPlayingSong:(id<MKRRadioResorce>)firstPlayingSong
- device:(id<MKRUPnPDevice>)device
- timeStamp:(NSTimeInterval)timeStamp{
- self.currentTempM3UListModel = nil;
- wd_playControl_queue(^{
- NSString *playFlag = [NSString stringWithFormat:@"%zd",channelNO];
- [self _playPlayFlag:playFlag device:device timeStamp:timeStamp];
- });
- }
- - (void)playSongWithChannelNO:(NSInteger)channelNO
- firstPlayingSong:(id<MKRRadioResorce>)firstPlayingSong
- index:(NSInteger)index
- device:(id<MKRUPnPDevice>)device
- timeStamp:(NSTimeInterval)timeStamp{
- self.currentTempM3UListModel = nil;
- wd_playControl_queue(^{
- if (firstPlayingSong && firstPlayingSong.songID) {
- [self setAVTransportURIWithSong:firstPlayingSong device:device];
- }
- NSString *playFlag = [NSString stringWithFormat:@"%zd-%zd",channelNO,index];
- [self _playPlayFlag:playFlag device:device timeStamp:timeStamp];
- });
- }
- - (void)playTempChannelSongs:(NSArray<id<MKRRadioResorce>> *)songs
- index:(NSInteger)index
- device:(id<MKRUPnPDevice>)device
- timeStamp:(NSTimeInterval)timeStamp{
- [self playTempChannelSongs:songs index:index device:device timeStamp:timeStamp playMode:MKRRadioPlayModeOrder];
- }
- - (void)playTempChannelSongs:(NSArray<id<MKRRadioResorce>> *)songs
- index:(NSInteger)index
- device:(id<MKRUPnPDevice>)device
- timeStamp:(NSTimeInterval)timeStamp
- playMode:(MKRRadioPlayMode)playMode{
- MKRM3UListModel *m3uModel = [MKRM3UListModel tempChannelM3UModelWithSongs:songs startIndex:index];
- m3uModel.userID = self.userID;
- m3uModel.playModeString = [self playModeStringWithPlayMode:playMode];
- [MKRWIFIDeviceManager shareManager].currentTempM3UListModel = m3uModel;
- wd_playControl_queue(^{
- [[MKRWIFIDeviceManager shareManager] setM3UList:m3uModel];
- id <MKRRadioResorce> song = songs[index];
- song.songChannel = 13;
- [self setAVTransportURIWithSong:song device:device];
- NSString *playFlag = [NSString stringWithFormat:@"13-%zd",index];
- [self _playPlayFlag:playFlag device:device timeStamp:timeStamp];
- });
- }
- - (void)playTempChannelSongs:(NSArray<id<MKRRadioResorce>> *)songs
- device:(id<MKRUPnPDevice>)device
- timeStamp:(NSTimeInterval)timeStamp{
- [self playTempChannelSongs:songs device:device timeStamp:timeStamp playMode:MKRRadioPlayModeOrder];
- }
- - (void)playTempChannelSongs:(NSArray<id<MKRRadioResorce>> *)songs
- device:(id<MKRUPnPDevice>)device
- timeStamp:(NSTimeInterval)timeStamp
- playMode:(MKRRadioPlayMode)playMode{
- MKRM3UListModel *m3uModel = [MKRM3UListModel tempChannelM3UModelWithSongs:songs startIndex:index];
- m3uModel.userID = self.userID;
- m3uModel.playModeString = [self playModeStringWithPlayMode:playMode];
- [MKRWIFIDeviceManager shareManager].currentTempM3UListModel = m3uModel;
- wd_playControl_queue(^{
- [[MKRWIFIDeviceManager shareManager] setM3UList:m3uModel];
- id <MKRRadioResorce> song = songs[0];
- song.songChannel = 13;
- [self setAVTransportURIWithSong:song device:device];
- [self _playPlayFlag:@"13" device:device timeStamp:timeStamp];
- });
- }
- - (void)pauseWithDevice:(id <MKRUPnPDevice>)device {
- wd_playControl_queue(^{
- libupnp_Pause(device.avTransportUrl);
- });
- }
- - (void)nextWithDevice:(id <MKRUPnPDevice>)device{
- wd_playControl_queue(^{
- libupnp_Next(device.avTransportUrl);
- });
- }
- - (void)previousWithDevice:(id <MKRUPnPDevice>)device{
- wd_playControl_queue(^{
- libupnp_Previous(device.avTransportUrl);
- });
-
- }
- - (void)stopWithDevice:(id <MKRUPnPDevice>)device{
- wd_playControl_queue(^{
- libupnp_Stop(device.avTransportUrl);
- });
- }
- - (void)setVolume:(int)volume device:(id <MKRUPnPDevice>)device{
- wd_playControl_queue(^{
- libupnp_SetZoneVolume(
- device.groupManagementUrl,
- @"urn:schemas-upnp-org:service:GroupManagement:1",
- @"Master",
- volume
- );
- });
-
- }
- - (void)seekToTimeInterval:(NSInteger)timeInterval device:(id <MKRUPnPDevice>)device {
- NSString *timeString = [NSString HHmmssFormatStrig:timeInterval];
- wd_playControl_queue(^{
- libupnp_SetMediaSeek(device.avTransportUrl, timeString);
- });
- }
- - (void)clearChannelWithChannelNO:(NSString *)channelNO device:(id <MKRUPnPDevice>)device {
- NSString *channelID = [NSString stringWithFormat:@"CHANNEL_UPDATE_%@",channelNO];
- NSString *timestamp = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
- wd_playControl_queue(^{
- libupnp_SetChannelClear(device.groupManagementUrl, channelID, timestamp);
- });
- }
- - (void)updateChannel:(NSString *)channelFlag device:(id<MKRUPnPDevice>)device{
- wd_playControl_queue(^{
- NSString *timestamp = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
- NSInteger channelNo = [channelFlag integerValue];
- NSString *playListUrl = @"null";
- if (channelNo >= 13) {
- playListUrl = [NSString stringWithFormat:@"%@/web/playlist",[ASControlTools sharedInstanceUPNP].myIp];
- }
- int result = libupnp_SetChannelUpdateV2(device.groupManagementUrl, self.userID, timestamp, channelFlag, playListUrl);
- });
- }
- - (void)setPlayMode:(MKRRadioPlayMode)playMode device:(id<MKRUPnPDevice>)device channelNO:(NSInteger)channelNO{
- NSString *playModeString = @"";
- switch (playMode) {
- case MKRRadioPlayModeOrder:{
- playModeString = @"REPEAT_ALL";
- }
- break;
- case MKRRadioPlayModeSingle:{
- playModeString = @"REPEAT_ONE";
- }
- break;
- case MKRRadioPlayModeRandom:{
- playModeString = @"SHUFFLE";
- }
- break;
- default:{
- playModeString = @"REPEAT_ALL";
- }
- break;
- }
- NSString *channelFlag = [NSString stringWithFormat:@"CHANNEL_UPDATE_%zd",channelNO];
- wd_playControl_queue(^{
- int result = libupnp_SetChannelPlayModeV2(device.groupManagementUrl, channelFlag, playModeString, self.userID);
- });
- }
- - (void)updateTempChannelPlayMode:(MKRRadioPlayMode)playMode songs:(NSArray <id <MKRRadioResorce>> *)songs index:(NSInteger)index device:(id<MKRUPnPDevice>)device{
- if (!self.currentTempM3UListModel) {
- MKRM3UListModel *m3uModel = [MKRM3UListModel tempChannelM3UModelWithSongs:songs startIndex:index];
- m3uModel.userID = self.userID;
- m3uModel.playModeString = [self playModeStringWithPlayMode:playMode];
- self.currentTempM3UListModel = m3uModel;
- }
- self.currentTempM3UListModel.playModeString = [self playModeStringWithPlayMode:playMode];
- wd_playControl_queue(^{
- [self setM3UList:self.currentTempM3UListModel];
- [self updateChannel:@(13).stringValue device:device];
- });
- }
- - (void)setLowPowerNoticeEnable:(BOOL)enable device:(id<MKRUPnPDevice>)device{
- wd_playControl_queue(^{
- libupnp_SetLowBatteryNotification(device.groupManagementUrl,enable);
- });
- }
- - (NSDictionary *)parsePlayInfoString:(NSString *)playInfoXmlString{
- NSMutableDictionary *resultDic = [NSMutableDictionary dictionaryWithDictionary:[NSDictionary mkr_dictionaryWithXML:playInfoXmlString]];
- NSString *metaDataString = [resultDic objectForKey:@"TrackMetaData"];
- if ([metaDataString isKindOfClass:NSString.class]) {
- metaDataString = [metaDataString stringByReplacingOccurrencesOfString:@"&" withString:@"mkr_***"];
- NSDictionary *metaDic = [NSDictionary mkr_dictionaryWithXML:metaDataString];
- NSDictionary *metaItemDic = metaDic[@"item"];
- if (![metaItemDic isKindOfClass:NSDictionary.class]) {
- metaItemDic = @{};
- }
- [resultDic setObject:metaDic[@"item"]?:@{} forKey:@"TrackMetaData"];
- return resultDic;
- }else{
- return nil;
- }
- }
- - (id <MKRRadioResorce>)getPlayingStatusWithDevice:(id <MKRUPnPDevice>)device {
- wd_playControl_queue(^{
- NSString *resultString = [[ASControlTools sharedInstanceUPNP] libupnp_getPlayInfoString:device.avTransportUrl];
- NSMutableDictionary *resultDic = [self parsePlayInfoString:resultString];
- if (!resultDic || !resultDic.allKeys.count) {
- return ;
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songInfoID"] isKindOfClass:NSString.class]) {
- NSInteger songID = [resultDic[@"TrackMetaData"][@"upnp:songInfoID"] integerValue];
- if (songID != self.currentResource.songID) {
- self.currentResource = [self defaultSong];
- }
- }
- @synchronized (self.currentResource) {
- if ([resultDic[@"TrackMetaData"][@"upnp:songType"] isKindOfClass:NSString.class]) {
- NSInteger sourceType = [MKRM3UListModel convertSorceTypeWithAudioType:[resultDic[@"TrackMetaData"][@"upnp:songType"] integerValue]];
- self.currentResource.sourceType = sourceType;
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songName"] isKindOfClass:NSString.class]) {
- self.currentResource.songName = resultDic[@"TrackMetaData"][@"upnp:songName"];
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songPlayer"] isKindOfClass:NSString.class]) {
- if (self.currentResource.sourceType == MKRSourceTypeHandPick || self.currentResource.sourceType == MKRSourceTypeBroadcast) {
- self.currentResource.songName = resultDic[@"TrackMetaData"][@"upnp:songPlayer"];
- }else{
- self.currentResource.songArtist = resultDic[@"TrackMetaData"][@"upnp:songPlayer"];
- }
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songThumb"] isKindOfClass:NSString.class]) {
- self.currentResource.songAlbumCover = resultDic[@"TrackMetaData"][@"upnp:songThumb"];
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songInfoID"] isKindOfClass:NSString.class]) {
- self.currentResource.songID = [resultDic[@"TrackMetaData"][@"upnp:songInfoID"] integerValue];
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songPlatform"] isKindOfClass:NSString.class]) {
- self.currentResource.songFrom = [resultDic[@"TrackMetaData"][@"upnp:songPlatform"] integerValue];
- }
- if ([resultDic[@"TrackDuration"] isKindOfClass:NSString.class]) {
- self.currentResource.resourceStatus.totalTime = [NSString secondsFromHHmmssFormatStrig:resultDic[@"TrackDuration"]];
- }
- if ([resultDic[@"RelTime"] isKindOfClass:NSString.class]) {
- self.currentResource.resourceStatus.currentTime = [NSString secondsFromHHmmssFormatStrig:resultDic[@"RelTime"]];
- }
- if ([resultDic[@"CurrentTransportState"] isKindOfClass:NSString.class]) {
- self.currentResource.resourceStatus.playState = [self.currentResource.resourceStatus updatePlayState:resultDic[@"CurrentTransportState"]];
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songAlbumID"] isKindOfClass:NSString.class]) {
- id songAlbumID = resultDic[@"TrackMetaData"][@"upnp:songAlbumID"];
- if ([songAlbumID isKindOfClass:NSString.class]) {
- self.currentResource.songAlbumID = resultDic[@"TrackMetaData"][@"upnp:songAlbumID"];
- }
- }
- if ([resultDic[@"TrackMetaData"][@"upnp:songAlbumID"] isKindOfClass:NSString.class]) {
- id songAlbumID = resultDic[@"TrackMetaData"][@"upnp:songAlbumID"];
- if ([songAlbumID isKindOfClass:NSString.class]) {
- self.currentResource.songAlbumID = resultDic[@"TrackMetaData"][@"upnp:songAlbumID"];
- }
- }
- NSString *channelString = resultDic[@"TrackMetaData"][@"upnp:songFlag"];
- NSInteger songChannel = 0;
- if ([channelString containsString:@"-"]) {
- NSArray *channelStringArr = [channelString componentsSeparatedByString:@"-"];
- songChannel = [[channelStringArr firstObject] integerValue];
- }else{
- songChannel = [channelString integerValue];
- }
- self.currentResource.songChannel = songChannel;
- NSString *songChannelUid = [NSString stringWithFormat:@"%@-%zd",self.userID,songChannel];
- self.currentResource.songChannelUid = songChannelUid;
- self.currentResource.songFlag = [NSString stringWithFormat:@"%@_%zd",songChannelUid,self.currentResource.songID];
- wd_main_queue(^{
- [self.listeners makeObjectsPerformSelectorWithObjects:@selector(didGetPlayInfo:),self.currentResource];
- });
- }
- });
- return self.currentResource;
- }
- - (void)mkr_setObject:(id)obj forKey:(NSString *)key{
-
- }
- - (NSString *)playModeStringWithPlayMode:(MKRRadioPlayMode)playmode{
- switch (playmode) {
- case MKRRadioPlayModeOrder:{
- return @"REPEAT_ALL";
- }
- break;
- case MKRRadioPlayModeSingle:{
- return @"REPEAT_ONE";
- }
- break;
- case MKRRadioPlayModeRandom:{
- return @"SHUFFLE";
- }
- break;
- default:{
- return @"REPEAT_ALL";
- }
- break;
- }
- }
- - (id<MKRRadioResorce>)currentResource{
- if (!_currentResource) {
- _currentResource = [[MKRRadioResorce alloc] init];
- _currentResource.songName = @"猫王妙播";
- _currentResource.songArtist = @"Keep radio on!";
- if (!_currentResource.resourceStatus) {
- _currentResource.resourceStatus = [[MKRPlayResourceStatus alloc] init];
- }
- }
- return _currentResource;
- }
- - (id<MKRRadioResorce>)defaultSong{
- MKRRadioResorce *song = [[MKRRadioResorce alloc] init];
- song.songName = @"猫王妙播";
- song.songArtist = @"Keep radio on!";
- song.resourceStatus = [[MKRPlayResourceStatus alloc] init];
- return song;
- }
- - (NSHashTable *)listeners{
- if (!_listeners) {
- _listeners = [NSHashTable weakObjectsHashTable];
- }
- return _listeners;
- }
- @end
|