diff --git a/switch/include/discoverymanager.h b/switch/include/discoverymanager.h index 35f354d4..190b24a8 100644 --- a/switch/include/discoverymanager.h +++ b/switch/include/discoverymanager.h @@ -26,6 +26,8 @@ #include +static void Discovery(ChiakiDiscoveryHost*, void*); + class DiscoveryManager { private: @@ -37,6 +39,7 @@ class DiscoveryManager int Discover(const char *discover_ip_dest="255.255.255.255"); int ParseSettings(); Host* GetHost(std::string); + void DiscoveryCB(ChiakiDiscoveryHost*); }; #endif //CHIAKI_DISCOVERYMANAGER_H diff --git a/switch/include/host.h b/switch/include/host.h index 929f14bc..0adfad92 100644 --- a/switch/include/host.h +++ b/switch/include/host.h @@ -40,11 +40,11 @@ extern "C" #include "exception.h" class DiscoveryManager; -static void DiscoveryCB(ChiakiDiscoveryHost*, void*); -static void RegistCB(ChiakiRegistEvent*, void*); -static bool VideoCB(uint8_t*, size_t, void*); -static void InitAudioCB(unsigned int, unsigned int, void*); -static void AudioCB(int16_t*, size_t, void*); +static void Discovery(ChiakiDiscoveryHost*, void*); +static void Regist(ChiakiRegistEvent*, void*); +static bool Video(uint8_t*, size_t, void*); +static void InitAudio(unsigned int, unsigned int, void*); +static void Audio(int16_t*, size_t, void*); class Host @@ -85,19 +85,12 @@ class Host AVCodec *codec; AVCodecContext *codec_context; struct SwsContext * sws_context = NULL; - int UpdateFrame(); // audio vars SDL_AudioDeviceID audio_device_id; // use friend class and methodes // to allow private var access friend class DiscoveryManager; friend class Settings; - friend void DiscoveryCB(ChiakiDiscoveryHost*, void*); - friend void RegistCB(ChiakiRegistEvent*, void*); - friend bool VideoCB(uint8_t*, size_t, void*); - friend void InitAudioCB(unsigned int, unsigned int, void*); - friend void AudioCB(int16_t*, size_t, void*); - public: // internal state bool discovered = false; @@ -108,14 +101,21 @@ class Host // share picture frame // with main function AVFrame *pict; - Host(); - static Host * GetOrCreate(std::map*, std::string*); + Host(ChiakiLog *log):log(log) {}; + Host(){}; + static Host * GetOrCreate(ChiakiLog*, std::map*, std::string*); + void InitVideo(); int Register(std::string pin); int Wakeup(); int ConnectSession(); void StartSession(); bool ReadGameKeys(SDL_Event*, ChiakiControllerState*); void SendFeedbackState(ChiakiControllerState*); + // callback methods + void RegistCB(ChiakiRegistEvent*); + bool VideoCB(uint8_t*, size_t); + void InitAudioCB(unsigned int, unsigned int); + void AudioCB(int16_t*, size_t); }; #endif diff --git a/switch/include/settings.h b/switch/include/settings.h index b1415dfd..8bdb01cf 100644 --- a/switch/include/settings.h +++ b/switch/include/settings.h @@ -46,15 +46,15 @@ class Settings { // the aim is not to have bulletproof parser // the goal is to read/write inernal flat configuration file const std::map re_map = { - { HOST_NAME, std::regex("\\[\\s*(.+)\\s*\\]") }, - { HOST_IP, std::regex("\\s*host_ip\\s*=\\s*\"?(\\d+\\.\\d+\\.\\d+\\.\\d+)\"?") }, - { PSN_ONLINE_ID, std::regex("\\s*psn_online_id\\s*=\\s*\"?(\\w+)\"?") }, - { PSN_ACCOUNT_ID, std::regex("\\s*psn_account_id\\s*=\\s*\"?([\\w/=]+)\"?") }, - { RP_KEY, std::regex("\\s*rp_key\\s*=\\s*\"?(\\w+)\"?") }, - { RP_KEY_TYPE, std::regex("\\s*rp_key_type\\s*=\\s*\"?(\\d)\"?") }, - { RP_REGIST_KEY, std::regex("\\s*rp_regist_key\\s*=\\s*\"?(\\w+)\"?") }, - { VIDEO_RESOLUTION, std::regex("\\s*video_resolution\\s*=\\s*\"?(1080p|720p|540p|360p)\"?") }, - { VIDEO_FPS, std::regex("\\s*video_fps\\s*=\\s*\"?(60|30)\"?") }, + { HOST_NAME, std::regex("^\\[\\s*(.+)\\s*\\]") }, + { HOST_IP, std::regex("^\\s*host_ip\\s*=\\s*\"?(\\d+\\.\\d+\\.\\d+\\.\\d+)\"?") }, + { PSN_ONLINE_ID, std::regex("^\\s*psn_online_id\\s*=\\s*\"?(\\w+)\"?") }, + { PSN_ACCOUNT_ID, std::regex("^\\s*psn_account_id\\s*=\\s*\"?([\\w/=]+)\"?") }, + { RP_KEY, std::regex("^\\s*rp_key\\s*=\\s*\"?(\\w+)\"?") }, + { RP_KEY_TYPE, std::regex("^\\s*rp_key_type\\s*=\\s*\"?(\\d)\"?") }, + { RP_REGIST_KEY, std::regex("^\\s*rp_regist_key\\s*=\\s*\"?(\\w+)\"?") }, + { VIDEO_RESOLUTION, std::regex("^\\s*video_resolution\\s*=\\s*\"?(1080p|720p|540p|360p)\"?") }, + { VIDEO_FPS, std::regex("^\\s*video_fps\\s*=\\s*\"?(60|30)\"?") }, }; ConfigurationItem ParseLine(std::string *line, std::string *value); diff --git a/switch/src/discoverymanager.cpp b/switch/src/discoverymanager.cpp index 75665959..1d61ce4f 100644 --- a/switch/src/discoverymanager.cpp +++ b/switch/src/discoverymanager.cpp @@ -23,81 +23,11 @@ #include -static void DiscoveryCB(ChiakiDiscoveryHost *host, void *user) -{ - // the user ptr is passed as - // chiaki_discovery_thread_start arg - std::map *hosts = (std::map*) user; - std::string key = host->host_name; - Host *n_host = Host::GetOrCreate(hosts, &key); - - ChiakiLog* log = nullptr; - CHIAKI_LOGI(log, "--"); - CHIAKI_LOGI(log, "Discovered Host:"); - CHIAKI_LOGI(log, "State: %s", chiaki_discovery_host_state_string(host->state)); - /* host attr - uint32_t host_addr; - int system_version; - int device_discovery_protocol_version; - std::string host_name; - std::string host_type; - std::string host_id; - */ - n_host->state = host->state; - // add host ptr to list - if(host->system_version){ - // example: 07020001 - n_host->system_version = atoi(host->system_version); - CHIAKI_LOGI(log, "System Version: %s", host->system_version); - } - if(host->device_discovery_protocol_version) - n_host->device_discovery_protocol_version = atoi(host->device_discovery_protocol_version); - CHIAKI_LOGI(log, "Device Discovery Protocol Version: %s", host->device_discovery_protocol_version); - - if(host->host_request_port) - CHIAKI_LOGI(log, "Request Port: %hu", (unsigned short)host->host_request_port); - - if(host->host_addr) - n_host->host_addr = host->host_addr; - CHIAKI_LOGI(log, "Host Addr: %s", host->host_addr); - - if(host->host_name) - // FIXME - n_host->host_name = host->host_name; - CHIAKI_LOGI(log, "Host Name: %s", host->host_name); - - if(host->host_type) - CHIAKI_LOGI(log, "Host Type: %s", host->host_type); - - if(host->host_id) - CHIAKI_LOGI(log, "Host ID: %s", host->host_id); - - if(host->running_app_titleid) - CHIAKI_LOGI(log, "Running App Title ID: %s", host->running_app_titleid); - - if(host->running_app_name) - CHIAKI_LOGI(log, "Running App Name: %s", host->running_app_name); - - CHIAKI_LOGI(log, "--"); +static void Discovery(ChiakiDiscoveryHost *discovered_host, void *user){ + DiscoveryManager *dm = (DiscoveryManager*) user; + dm->DiscoveryCB(discovered_host); } -int DiscoveryManager::ParseSettings(){ - /* - std::string ap_ssid; - std::string ap_bssid; - std::string ap_key; - std::string ap_name; - std::string ps4_nickname; - // mac address = 48 bits - uint8_t ps4_mac[6]; - char rp_regist_key[CHIAKI_SESSION_AUTH_SIZE]; - uint32_t rp_key_type; - uint8_t rp_key[0x10]; - */ - return 0; -} - - int DiscoveryManager::Discover(const char *discover_ip_dest) { // use broadcast address to discover host form local network @@ -111,7 +41,7 @@ int DiscoveryManager::Discover(const char *discover_ip_dest) } ChiakiDiscoveryThread thread; - err = chiaki_discovery_thread_start(&thread, &discovery, DiscoveryCB, hosts); + err = chiaki_discovery_thread_start(&thread, &discovery, Discovery, this); if(err != CHIAKI_ERR_SUCCESS) { CHIAKI_LOGE(log, "Discovery thread init failed"); @@ -174,3 +104,61 @@ Host* DiscoveryManager::GetHost(std::string ps4_nickname){ return 0; } } + +void DiscoveryManager::DiscoveryCB(ChiakiDiscoveryHost *discovered_host){ + + // the user ptr is passed as + // chiaki_discovery_thread_start arg + + std::string key = discovered_host->host_name; + Host *host = Host::GetOrCreate(this->log, hosts, &key); + + CHIAKI_LOGI(this->log, "--"); + CHIAKI_LOGI(this->log, "Discovered Host:"); + CHIAKI_LOGI(this->log, "State: %s", chiaki_discovery_host_state_string(discovered_host->state)); + /* host attr + uint32_t host_addr; + int system_version; + int device_discovery_protocol_version; + std::string host_name; + std::string host_type; + std::string host_id; + */ + host->state = discovered_host->state; + // add host ptr to list + if(discovered_host->system_version){ + // example: 07020001 + host->system_version = atoi(discovered_host->system_version); + CHIAKI_LOGI(this->log, "System Version: %s", discovered_host->system_version); + } + if(discovered_host->device_discovery_protocol_version) + host->device_discovery_protocol_version = atoi(discovered_host->device_discovery_protocol_version); + CHIAKI_LOGI(this->log, "Device Discovery Protocol Version: %s", discovered_host->device_discovery_protocol_version); + + if(discovered_host->host_request_port) + CHIAKI_LOGI(this->log, "Request Port: %hu", (unsigned short)discovered_host->host_request_port); + + if(discovered_host->host_addr) + host->host_addr = discovered_host->host_addr; + CHIAKI_LOGI(this->log, "Host Addr: %s", discovered_host->host_addr); + + if(discovered_host->host_name) + // FIXME + host->host_name = discovered_host->host_name; + CHIAKI_LOGI(this->log, "Host Name: %s", discovered_host->host_name); + + if(discovered_host->host_type) + CHIAKI_LOGI(this->log, "Host Type: %s", discovered_host->host_type); + + if(discovered_host->host_id) + CHIAKI_LOGI(this->log, "Host ID: %s", discovered_host->host_id); + + if(discovered_host->running_app_titleid) + CHIAKI_LOGI(this->log, "Running App Title ID: %s", discovered_host->running_app_titleid); + + if(discovered_host->running_app_name) + CHIAKI_LOGI(this->log, "Running App Name: %s", discovered_host->running_app_name); + + CHIAKI_LOGI(this->log, "--"); +} + diff --git a/switch/src/host.cpp b/switch/src/host.cpp index 198151e2..3edbfd3f 100644 --- a/switch/src/host.cpp +++ b/switch/src/host.cpp @@ -25,138 +25,38 @@ #include -static void RegistCB(ChiakiRegistEvent *event, void *user){ - // Chiaki callback fuction - // fuction called by lib chiaki regist - // durring client pin code registration - // - // read data from lib and record secrets into Host object +static void Regist(ChiakiRegistEvent *event, void *user){ Host *host = (Host*) user; - host->registered = false; - switch(event->type) - { - case CHIAKI_REGIST_EVENT_TYPE_FINISHED_CANCELED: - //FIXME - break; - case CHIAKI_REGIST_EVENT_TYPE_FINISHED_FAILED: - //FIXME - CHIAKI_LOGI(host->log, "Register failed %s", host->host_name.c_str()); - break; - case CHIAKI_REGIST_EVENT_TYPE_FINISHED_SUCCESS: - { - ChiakiRegisteredHost *r_host = event->registered_host; - // copy values form ChiakiRegisteredHost object - host->ap_ssid = r_host->ap_ssid; - host->ap_key = r_host->ap_key; - host->ap_name = r_host->ap_name; - memcpy( &(host->ps4_mac), &(r_host->ps4_mac), sizeof(host->ps4_mac) ); - host->ps4_nickname = r_host->ps4_nickname; - memcpy( &(host->rp_regist_key), &(r_host->rp_regist_key), sizeof(host->rp_regist_key) ); - host->rp_key_type = r_host->rp_key_type; - memcpy( &(host->rp_key), &(r_host->rp_key), sizeof(host->rp_key) ); - // mark host as registered - host->registered = true; - host->rp_key_data = true; - CHIAKI_LOGI(host->log, "Register Success %s", host->host_name.c_str()); - break; - } - } + host->RegistCB(event); } -static bool VideoCB(uint8_t *buf, size_t buf_size, void *user){ - // callback function to decode video buffer - - // access chiaki session from Host object +static void InitAudio(unsigned int channels, unsigned int rate, void *user){ Host *host = (Host*) user; - AVPacket packet; - av_init_packet(&packet); - packet.data = buf; - packet.size = buf_size; - int r; - -send_packet: - // TODO AVCodec internal buffer is full removing frames before pushing - r = avcodec_send_packet(host->codec_context, &packet); - if(r != 0) { - if(r == AVERROR(EAGAIN)){ - CHIAKI_LOGE(host->log, "AVCodec internal buffer is full removing frames before pushing"); - AVFrame *frame = av_frame_alloc(); - - if(!frame){ - CHIAKI_LOGE(host->log, "Failed to alloc AVFrame"); - return false; - } - - r = avcodec_receive_frame(host->codec_context, frame); - // send decoded frame for sdl texture update - av_frame_free(&frame); - if(r != 0){ - CHIAKI_LOGE(host->log, "Failed to pull frame"); - return false; - } - goto send_packet; - } else { - char errbuf[128]; - av_make_error_string(errbuf, sizeof(errbuf), r); - CHIAKI_LOGE(host->log, "Failed to push frame: %s", errbuf); - return false; - } - } - // FramesAvailable - host->UpdateFrame(); - return true; + host->InitAudioCB(channels, rate); } -static void InitAudioCB(unsigned int channels, unsigned int rate, void *user){ +static bool Video(uint8_t *buf, size_t buf_size, void *user){ Host *host = (Host*) user; - SDL_AudioSpec want, have, test; - SDL_memset(&want, 0, sizeof(want)); - - //source - //[I] Audio Header: - //[I] channels = 2 - //[I] bits = 16 - //[I] rate = 48000 - //[I] frame size = 480 - //[I] unknown = 1 - want.freq = rate; - want.format = AUDIO_S16SYS; - // 2 == stereo - want.channels = channels; - want.samples = 1024; - want.callback = NULL; - - ChiakiLog *log = nullptr; - host->audio_device_id = SDL_OpenAudioDevice(NULL, 0, &want, NULL, 0); - if(host->audio_device_id < 0){ - CHIAKI_LOGE(host->log, "SDL_OpenAudioDevice failed: %s\n", SDL_GetError()); - } else { - SDL_PauseAudioDevice(host->audio_device_id, 0); - } + return host->VideoCB(buf, buf_size); } -static void AudioCB(int16_t *buf, size_t samples_count, void *user){ +static void Audio(int16_t *buf, size_t samples_count, void *user){ Host *host = (Host*) user; - //int az = SDL_GetQueuedAudioSize(host->audio_device_id); - // len the number of bytes (not samples!) to which (data) points - int success = SDL_QueueAudio(host->audio_device_id, buf, sizeof(int16_t)*samples_count*2); - if(success != 0){ - CHIAKI_LOGE(host->log, "SDL_QueueAudio failed: %s\n", SDL_GetError()); - } + host->AudioCB(buf, samples_count); } -Host * Host::GetOrCreate(std::map *hosts, std::string *key){ +Host * Host::GetOrCreate(ChiakiLog *log, std::map *hosts, std::string *key){ // update of create Host instance if ( hosts->find(*key) == hosts->end() ) { // create host if udefined - (*hosts)[*key] = Host(); + (*hosts)[*key] = Host(log); } Host *ret = &(hosts->at(*key)); ret->host_name = *key; return ret; } -Host::Host(){ +void Host::InitVideo(){ // set libav video context // for later stream @@ -246,13 +146,7 @@ Host::Host(){ this->codec_context->height, 32 ); -/* - * CHIAKI_VIDEO_RESOLUTION_PRESET_360p = 1, - * CHIAKI_VIDEO_RESOLUTION_PRESET_540p = 2, - * CHIAKI_VIDEO_RESOLUTION_PRESET_720p = 3, - * CHIAKI_VIDEO_RESOLUTION_PRESET_1080p = 4 - * CHIAKI_VIDEO_FPS_PRESET_30; - */ + chiaki_connect_video_profile_preset(&(this->video_profile), this->video_resolution, this->video_fps); @@ -305,7 +199,7 @@ int Host::Register(std::string pin){ regist_info.broadcast = false; CHIAKI_LOGI(this->log, "Registering to host `%s` `%s` with PSN AccountID `%s` pin `%s`", this->host_name.c_str(), this->host_addr.c_str(), psn_account_id.c_str(), pin.c_str()); - chiaki_regist_start(®ist, this->log, ®ist_info, RegistCB, this); + chiaki_regist_start(®ist, this->log, ®ist_info, Regist, this); //FIXME poll host->registered sleep(1); chiaki_regist_stop(®ist); @@ -321,17 +215,8 @@ int Host::ConnectSession() { chiaki_connect_info.host = this->host_addr.c_str(); chiaki_connect_info.video_profile = this->video_profile; - /* - * if(strlen(this->rp_regist_key) != sizeof(chiaki_connect_info.regist_key)) - throw Exception("RegistKey invalid"); -*/ memcpy(chiaki_connect_info.regist_key, this->rp_regist_key, sizeof(chiaki_connect_info.regist_key)); - // morning == rp-key - /* - if(sizeof(this->rp_key) != sizeof(chiaki_connect_info.morning)) - throw Exception("Morning invalid"); - */ memcpy(chiaki_connect_info.morning, this->rp_key, sizeof(chiaki_connect_info.morning)); // set keybord state to 0 @@ -341,16 +226,15 @@ int Host::ConnectSession() { if(err != CHIAKI_ERR_SUCCESS) throw Exception(chiaki_error_string(err)); // audio setting_cb and frame_cb - chiaki_opus_decoder_set_cb(&this->opus_decoder, InitAudioCB, AudioCB, this); + chiaki_opus_decoder_set_cb(&this->opus_decoder, InitAudio, Audio, this); chiaki_opus_decoder_get_sink(&this->opus_decoder, &audio_sink); chiaki_session_set_audio_sink(&(this->session), &audio_sink); - chiaki_session_set_video_sample_cb(&(this->session), VideoCB, this); + chiaki_session_set_video_sample_cb(&(this->session), Video, this); // TODO chiaki_session_set_event_cb(&(this->session), NULL, this); return 0; } - void Host::StartSession() { ChiakiErrorCode err = chiaki_session_start(&this->session); @@ -362,56 +246,6 @@ void Host::StartSession() sleep(1); } -int Host::UpdateFrame(){ - - AVFrame *frame = av_frame_alloc(); - //AVFrame *next_frame = av_frame_alloc(); - //AVFrame *tmp_swp = frame; - - if(!frame){ - CHIAKI_LOGE(this->log, "UpdateFrame Failed to alloc AVFrame"); - return -1; - } - - int ret; - ret = avcodec_receive_frame(this->codec_context, frame); - - if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) - { - CHIAKI_LOGE(this->log, "Error while decoding: EAGAIN or AVERROR_EOF"); - return ret; - } - else if (ret < 0) - { - CHIAKI_LOGE(this->log, "Error while decoding."); - return -1; - } - /* - // decode frame - do { - tmp_swp = frame; - frame = next_frame; - next_frame = tmp_swp; - ret = avcodec_receive_frame(this->codec_context, next_frame); - } while(ret == 0); - */ - - // adjust frame to pict - sws_scale( - this->sws_context, - (uint8_t const * const *)frame->data, - frame->linesize, - 0, - this->codec_context->height, - pict->data, - pict->linesize - ); - - av_frame_free(&frame); - //av_frame_free(&next_frame); - return 0; -} - bool Host::ReadGameKeys(SDL_Event *event, ChiakiControllerState *state){ bool ret = true; switch(event->type){ @@ -511,6 +345,166 @@ bool Host::ReadGameKeys(SDL_Event *event, ChiakiControllerState *state){ } void Host::SendFeedbackState(ChiakiControllerState *state){ + // send controller/joystick key chiaki_session_set_controller_state(&this->session, state); } +void Host::RegistCB(ChiakiRegistEvent *event){ + // Chiaki callback fuction + // fuction called by lib chiaki regist + // durring client pin code registration + // + // read data from lib and record secrets into Host object + + this->registered = false; + switch(event->type) + { + case CHIAKI_REGIST_EVENT_TYPE_FINISHED_CANCELED: + //FIXME + break; + case CHIAKI_REGIST_EVENT_TYPE_FINISHED_FAILED: + //FIXME + CHIAKI_LOGI(this->log, "Register failed %s", this->host_name.c_str()); + break; + case CHIAKI_REGIST_EVENT_TYPE_FINISHED_SUCCESS: + { + ChiakiRegisteredHost *r_host = event->registered_host; + // copy values form ChiakiRegisteredHost object + this->ap_ssid = r_host->ap_ssid; + this->ap_key = r_host->ap_key; + this->ap_name = r_host->ap_name; + memcpy( &(this->ps4_mac), &(r_host->ps4_mac), sizeof(this->ps4_mac) ); + this->ps4_nickname = r_host->ps4_nickname; + memcpy( &(this->rp_regist_key), &(r_host->rp_regist_key), sizeof(this->rp_regist_key) ); + this->rp_key_type = r_host->rp_key_type; + memcpy( &(this->rp_key), &(r_host->rp_key), sizeof(this->rp_key) ); + // mark host as registered + this->registered = true; + this->rp_key_data = true; + CHIAKI_LOGI(this->log, "Register Success %s", this->host_name.c_str()); + break; + } + } +} + +bool Host::VideoCB(uint8_t *buf, size_t buf_size){ + // callback function to decode video buffer + // access chiaki session from Host object + AVPacket packet; + av_init_packet(&packet); + packet.data = buf; + packet.size = buf_size; + int r; + +send_packet: + // TODO AVCodec internal buffer is full removing frames before pushing + r = avcodec_send_packet(this->codec_context, &packet); + if(r != 0) { + if(r == AVERROR(EAGAIN)){ + CHIAKI_LOGE(this->log, "AVCodec internal buffer is full removing frames before pushing"); + AVFrame *frame = av_frame_alloc(); + + if(!frame){ + CHIAKI_LOGE(this->log, "Failed to alloc AVFrame"); + return false; + } + + r = avcodec_receive_frame(this->codec_context, frame); + // send decoded frame for sdl texture update + av_frame_free(&frame); + if(r != 0){ + CHIAKI_LOGE(this->log, "Failed to pull frame"); + return false; + } + goto send_packet; + } else { + char errbuf[128]; + av_make_error_string(errbuf, sizeof(errbuf), r); + CHIAKI_LOGE(this->log, "Failed to push frame: %s", errbuf); + return false; + } + } + + // FramesAvailable + AVFrame *frame = av_frame_alloc(); + AVFrame *next_frame = av_frame_alloc(); + AVFrame *tmp_swp = frame; + + if(!frame){ + CHIAKI_LOGE(this->log, "UpdateFrame Failed to alloc AVFrame"); + return -1; + } + + int ret; + /* + ret = avcodec_receive_frame(this->codec_context, frame); + + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + { + CHIAKI_LOGE(this->log, "Error while decoding: EAGAIN or AVERROR_EOF"); + return ret; + } + else if (ret < 0) + { + CHIAKI_LOGE(this->log, "Error while decoding."); + return -1; + } + */ + // decode frame + do { + tmp_swp = frame; + frame = next_frame; + next_frame = tmp_swp; + ret = avcodec_receive_frame(this->codec_context, next_frame); + } while(ret == 0); + + // adjust frame to pict + sws_scale( + this->sws_context, + (uint8_t const * const *)frame->data, + frame->linesize, + 0, + this->codec_context->height, + pict->data, + pict->linesize + ); + + av_frame_free(&frame); + av_frame_free(&next_frame); + return true; +} + +void Host::InitAudioCB(unsigned int channels, unsigned int rate){ + SDL_AudioSpec want, have, test; + SDL_memset(&want, 0, sizeof(want)); + + //source + //[I] Audio Header: + //[I] channels = 2 + //[I] bits = 16 + //[I] rate = 48000 + //[I] frame size = 480 + //[I] unknown = 1 + want.freq = rate; + want.format = AUDIO_S16SYS; + // 2 == stereo + want.channels = channels; + want.samples = 1024; + want.callback = NULL; + + this->audio_device_id = SDL_OpenAudioDevice(NULL, 0, &want, NULL, 0); + if(this->audio_device_id < 0){ + CHIAKI_LOGE(this->log, "SDL_OpenAudioDevice failed: %s\n", SDL_GetError()); + } else { + SDL_PauseAudioDevice(this->audio_device_id, 0); + } +} + +void Host::AudioCB(int16_t *buf, size_t samples_count){ + //int az = SDL_GetQueuedAudioSize(host->audio_device_id); + // len the number of bytes (not samples!) to which (data) points + int success = SDL_QueueAudio(this->audio_device_id, buf, sizeof(int16_t)*samples_count*2); + if(success != 0){ + CHIAKI_LOGE(this->log, "SDL_QueueAudio failed: %s\n", SDL_GetError()); + } +} diff --git a/switch/src/main.cpp b/switch/src/main.cpp index 66a86128..15c7c7a1 100644 --- a/switch/src/main.cpp +++ b/switch/src/main.cpp @@ -244,11 +244,15 @@ int main(int argc, char* argv[]){ return 1; } + //FIXME //if(host->rp_key_data) { //CHIAKI_LOGI(&log, "Call Wakeup"); //int w = host->Wakeup(); //} + //simple video var init + host->InitVideo(); + if(!host->rp_key_data) { CHIAKI_LOGI(&log, "Call register"); char pin_input[9]; diff --git a/switch/src/settings.cpp b/switch/src/settings.cpp index be492f4b..fb9c9c35 100644 --- a/switch/src/settings.cpp +++ b/switch/src/settings.cpp @@ -61,7 +61,7 @@ void Settings::ParseFile(){ case HOST_NAME: CHIAKI_LOGV(this->log, "HOST_NAME %s", value.c_str()); // current host is in context - current_host = Host::GetOrCreate(this->hosts, &value); + current_host = Host::GetOrCreate(this->log, this->hosts, &value); // all following case will edit the current_host config break; case HOST_IP: