AClientConnection.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <AK/SharedBuffer.h>
  27. #include <LibAudio/ABuffer.h>
  28. #include <LibAudio/AClientConnection.h>
  29. namespace Audio {
  30. ClientConnection::ClientConnection()
  31. : IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>(*this, "/tmp/portal/audio")
  32. {
  33. }
  34. void ClientConnection::handshake()
  35. {
  36. auto response = send_sync<AudioServer::Greet>();
  37. set_my_client_id(response->client_id());
  38. }
  39. void ClientConnection::enqueue(const Buffer& buffer)
  40. {
  41. for (;;) {
  42. const_cast<Buffer&>(buffer).shared_buffer().share_with(server_pid());
  43. auto response = send_sync<AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
  44. if (response->success())
  45. break;
  46. sleep(1);
  47. }
  48. }
  49. bool ClientConnection::try_enqueue(const Buffer& buffer)
  50. {
  51. const_cast<Buffer&>(buffer).shared_buffer().share_with(server_pid());
  52. auto response = send_sync<AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
  53. return response->success();
  54. }
  55. bool ClientConnection::get_muted()
  56. {
  57. return send_sync<AudioServer::GetMuted>()->muted();
  58. }
  59. void ClientConnection::set_muted(bool muted)
  60. {
  61. send_sync<AudioServer::SetMuted>(muted);
  62. }
  63. int ClientConnection::get_main_mix_volume()
  64. {
  65. return send_sync<AudioServer::GetMainMixVolume>()->volume();
  66. }
  67. void ClientConnection::set_main_mix_volume(int volume)
  68. {
  69. send_sync<AudioServer::SetMainMixVolume>(volume);
  70. }
  71. int ClientConnection::get_remaining_samples()
  72. {
  73. return send_sync<AudioServer::GetRemainingSamples>()->remaining_samples();
  74. }
  75. int ClientConnection::get_played_samples()
  76. {
  77. return send_sync<AudioServer::GetPlayedSamples>()->played_samples();
  78. }
  79. void ClientConnection::set_paused(bool paused)
  80. {
  81. send_sync<AudioServer::SetPaused>(paused);
  82. }
  83. void ClientConnection::clear_buffer(bool paused)
  84. {
  85. send_sync<AudioServer::ClearBuffer>(paused);
  86. }
  87. int ClientConnection::get_playing_buffer()
  88. {
  89. return send_sync<AudioServer::GetPlayingBuffer>()->buffer_id();
  90. }
  91. void ClientConnection::handle(const AudioClient::FinishedPlayingBuffer& message)
  92. {
  93. if (on_finish_playing_buffer)
  94. on_finish_playing_buffer(message.buffer_id());
  95. }
  96. void ClientConnection::handle(const AudioClient::MutedStateChanged& message)
  97. {
  98. if (on_muted_state_change)
  99. on_muted_state_change(message.muted());
  100. }
  101. }