AClientConnection.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <LibAudio/ABuffer.h>
  2. #include <LibAudio/AClientConnection.h>
  3. #include <SharedBuffer.h>
  4. AClientConnection::AClientConnection()
  5. : IServerConnection(*this, "/tmp/portal/audio")
  6. {
  7. }
  8. void AClientConnection::handshake()
  9. {
  10. auto response = send_sync<AudioServer::Greet>();
  11. set_my_client_id(response->client_id());
  12. }
  13. void AClientConnection::enqueue(const ABuffer& buffer)
  14. {
  15. for (;;) {
  16. const_cast<ABuffer&>(buffer).shared_buffer().share_with(server_pid());
  17. auto response = send_sync<AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
  18. if (response->success())
  19. break;
  20. sleep(1);
  21. }
  22. }
  23. bool AClientConnection::try_enqueue(const ABuffer& buffer)
  24. {
  25. const_cast<ABuffer&>(buffer).shared_buffer().share_with(server_pid());
  26. auto response = send_sync<AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
  27. return response->success();
  28. }
  29. bool AClientConnection::get_muted()
  30. {
  31. return send_sync<AudioServer::GetMuted>()->muted();
  32. }
  33. void AClientConnection::set_muted(bool muted)
  34. {
  35. send_sync<AudioServer::SetMuted>(muted);
  36. }
  37. int AClientConnection::get_main_mix_volume()
  38. {
  39. return send_sync<AudioServer::GetMainMixVolume>()->volume();
  40. }
  41. void AClientConnection::set_main_mix_volume(int volume)
  42. {
  43. send_sync<AudioServer::SetMainMixVolume>(volume);
  44. }
  45. int AClientConnection::get_remaining_samples()
  46. {
  47. return send_sync<AudioServer::GetRemainingSamples>()->remaining_samples();
  48. }
  49. int AClientConnection::get_played_samples()
  50. {
  51. return send_sync<AudioServer::GetPlayedSamples>()->played_samples();
  52. }
  53. void AClientConnection::set_paused(bool paused)
  54. {
  55. send_sync<AudioServer::SetPaused>(paused);
  56. }
  57. void AClientConnection::clear_buffer(bool paused)
  58. {
  59. send_sync<AudioServer::ClearBuffer>(paused);
  60. }
  61. int AClientConnection::get_playing_buffer()
  62. {
  63. return send_sync<AudioServer::GetPlayingBuffer>()->buffer_id();
  64. }
  65. void AClientConnection::handle(const AudioClient::FinishedPlayingBuffer& message)
  66. {
  67. if (on_finish_playing_buffer)
  68. on_finish_playing_buffer(message.buffer_id());
  69. }
  70. void AClientConnection::handle(const AudioClient::MutedStateChanged& message)
  71. {
  72. if (on_muted_state_change)
  73. on_muted_state_change(message.muted());
  74. }