ClientConnection.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibAudio/Buffer.h>
  27. #include <LibAudio/ClientConnection.h>
  28. namespace Audio {
  29. ClientConnection::ClientConnection()
  30. : IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>(*this, "/tmp/portal/audio")
  31. {
  32. }
  33. void ClientConnection::handshake()
  34. {
  35. send_sync<Messages::AudioServer::Greet>();
  36. }
  37. void ClientConnection::enqueue(const Buffer& buffer)
  38. {
  39. for (;;) {
  40. auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
  41. if (response->success())
  42. break;
  43. sleep(1);
  44. }
  45. }
  46. bool ClientConnection::try_enqueue(const Buffer& buffer)
  47. {
  48. auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
  49. return response->success();
  50. }
  51. bool ClientConnection::get_muted()
  52. {
  53. return send_sync<Messages::AudioServer::GetMuted>()->muted();
  54. }
  55. void ClientConnection::set_muted(bool muted)
  56. {
  57. send_sync<Messages::AudioServer::SetMuted>(muted);
  58. }
  59. int ClientConnection::get_main_mix_volume()
  60. {
  61. return send_sync<Messages::AudioServer::GetMainMixVolume>()->volume();
  62. }
  63. void ClientConnection::set_main_mix_volume(int volume)
  64. {
  65. send_sync<Messages::AudioServer::SetMainMixVolume>(volume);
  66. }
  67. int ClientConnection::get_remaining_samples()
  68. {
  69. return send_sync<Messages::AudioServer::GetRemainingSamples>()->remaining_samples();
  70. }
  71. int ClientConnection::get_played_samples()
  72. {
  73. return send_sync<Messages::AudioServer::GetPlayedSamples>()->played_samples();
  74. }
  75. void ClientConnection::set_paused(bool paused)
  76. {
  77. send_sync<Messages::AudioServer::SetPaused>(paused);
  78. }
  79. void ClientConnection::clear_buffer(bool paused)
  80. {
  81. send_sync<Messages::AudioServer::ClearBuffer>(paused);
  82. }
  83. int ClientConnection::get_playing_buffer()
  84. {
  85. return send_sync<Messages::AudioServer::GetPlayingBuffer>()->buffer_id();
  86. }
  87. void ClientConnection::handle(const Messages::AudioClient::FinishedPlayingBuffer& message)
  88. {
  89. if (on_finish_playing_buffer)
  90. on_finish_playing_buffer(message.buffer_id());
  91. }
  92. void ClientConnection::handle(const Messages::AudioClient::MutedStateChanged& message)
  93. {
  94. if (on_muted_state_change)
  95. on_muted_state_change(message.muted());
  96. }
  97. void ClientConnection::handle(const Messages::AudioClient::MainMixVolumeChanged& message)
  98. {
  99. if (on_main_mix_volume_change)
  100. on_main_mix_volume_change(message.volume());
  101. }
  102. }