72 lines
4.6 KiB
Rust
72 lines
4.6 KiB
Rust
use std::sync::Arc;
|
|
use super::context::RuntimeServices;
|
|
#[cfg(any(target_os="android",target_os="ios"))]
|
|
use super::context::closed;
|
|
use crate::{app::connections::SessionKey,services::Result};
|
|
impl RuntimeServices {
|
|
/// Created only by a current foreground Connect/microphone action, never profile load or resume.
|
|
pub async fn ensure_mobile_call(self:&Arc<Self>,key:&SessionKey,microphone:bool)->Result<()> {
|
|
let configuration=self.audio_configuration.lock().await;
|
|
self.ensure_mobile_call_locked(key,microphone,&configuration).await
|
|
}
|
|
/// Callers retain this guard through capture permit binding and hardware startup.
|
|
pub async fn ensure_mobile_call_locked(self:&Arc<Self>,key:&SessionKey,microphone:bool,_configuration:&tokio::sync::MutexGuard<'_,()>)->Result<()> {
|
|
self.policy.check()?;self.require_key(key,true)?;let generation=self.current_ui_generation()?;
|
|
let mut call=self.mobile_call.lock().await;
|
|
let cached=call.as_ref().is_some_and(|(current,enabled)|current==key&&(*enabled||!microphone));
|
|
#[cfg(target_os="android")]
|
|
let cached=cached&&crate::platform::android::voice_audio_allowed(key,microphone);
|
|
if cached{return Ok(())}
|
|
#[cfg(target_os="android")]
|
|
{
|
|
if let Some((current,_))=call.as_ref(){if current!=key{crate::platform::android::stop_voice(current)?;*call=None;}}
|
|
if call.is_none(){let runtime=self.runtime.upgrade().ok_or_else(closed)?;crate::platform::android::start_voice(runtime,key.clone(),microphone,self.policy).await?;}
|
|
else{crate::platform::android::set_voice_capture(key,microphone,self.policy).await?;}
|
|
}
|
|
#[cfg(target_os="ios")]
|
|
{
|
|
let mut lease=self.apple_call.lock().await;
|
|
if lease.as_ref().is_some_and(|lease|lease.key()!=key){lease.take();}
|
|
if let Some(lease)=lease.as_ref(){lease.set_capture_enabled(microphone,self.policy).await?;}
|
|
else{*lease=Some(crate::platform::apple::begin_call(key.clone(),self.runtime.upgrade().ok_or_else(closed)?,microphone,self.policy).await?);}
|
|
}
|
|
self.require_key(key,true)?;self.require_ui_generation(generation)?;*call=Some((key.clone(),microphone));
|
|
#[cfg(target_os="android")]
|
|
if !crate::platform::android::voice_audio_allowed(key,microphone){return Err(crate::services::ServiceError::new("audio_focus_unavailable","Android revoked call audio authorization during startup"))}
|
|
Ok(())
|
|
}
|
|
#[cfg(target_os="android")]
|
|
pub async fn android_audio_changed(self:&Arc<Self>,key:&SessionKey,token:u64,revision:u64,restart_output:bool)->Result<()> {
|
|
use crate::media::audio::{engine::AudioCommand,devices::StartPermission};
|
|
use std::sync::atomic::Ordering;
|
|
let current=||self.require_key(key,true).is_ok()&&crate::platform::android::voice_audio_current(key,token,revision);
|
|
if !current(){return Ok(())}
|
|
let _configuration=self.audio_configuration.lock().await;
|
|
if !current(){return Ok(())}
|
|
super::controller::release_immediate(self);
|
|
self.audio_input_active.store(false,Ordering::Release);
|
|
self.audio.request(AudioCommand::StopInput).await?;
|
|
if restart_output {
|
|
self.audio_output_active.store(false,Ordering::Release);
|
|
self.audio.request(AudioCommand::StopOutput).await?;
|
|
if current()&&self.output_granted.load(Ordering::Acquire)&&crate::platform::android::voice_audio_allowed(key,false) {
|
|
self.audio.request(AudioCommand::StartOutput(StartPermission::Granted)).await?;
|
|
if !current()||!crate::platform::android::voice_audio_allowed(key,false){self.audio.request(AudioCommand::StopOutput).await?;return Ok(())}
|
|
self.audio_output_active.store(true,Ordering::Release);
|
|
}
|
|
if current(){crate::platform::android::acknowledge_voice_output(key,token,revision,self.audio.output_active());}
|
|
}
|
|
if current()&&self.connected.connections.snapshot().input_id.as_deref()==Some(key.connection_id.as_str()) {
|
|
self.request(key,teamspeach_core::protocol::Command::SetAudio{input_muted:Some(true),output_muted:None}).await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
pub async fn end_mobile_call(&self,key:&SessionKey)->Result<()> {
|
|
let mut call=self.mobile_call.lock().await;
|
|
if call.as_ref().is_none_or(|(current,_)|current!=key){return Ok(())}
|
|
#[cfg(target_os="android")]if self.policy.allow_os_actions{crate::platform::android::stop_voice(key)?;}
|
|
#[cfg(target_os="ios")]self.apple_call.lock().await.take();
|
|
*call=None;Ok(())
|
|
}
|
|
}
|