ClientConnection.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "ClientConnection.h"
  27. #include "Mixer.h"
  28. #include <AK/SharedBuffer.h>
  29. #include <AudioServer/AudioClientEndpoint.h>
  30. #include <LibAudio/Buffer.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <sys/socket.h>
  34. #include <sys/types.h>
  35. #include <sys/uio.h>
  36. #include <unistd.h>
  37. namespace AudioServer {
  38. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  39. void ClientConnection::for_each(Function<void(ClientConnection&)> callback)
  40. {
  41. NonnullRefPtrVector<ClientConnection> connections;
  42. for (auto& it : s_connections)
  43. connections.append(*it.value);
  44. for (auto& connection : connections)
  45. callback(connection);
  46. }
  47. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id, Mixer& mixer)
  48. : IPC::ClientConnection<AudioClientEndpoint, AudioServerEndpoint>(*this, move(client_socket), client_id)
  49. , m_mixer(mixer)
  50. {
  51. s_connections.set(client_id, *this);
  52. }
  53. ClientConnection::~ClientConnection()
  54. {
  55. }
  56. void ClientConnection::die()
  57. {
  58. s_connections.remove(client_id());
  59. }
  60. void ClientConnection::did_finish_playing_buffer(Badge<BufferQueue>, int buffer_id)
  61. {
  62. post_message(Messages::AudioClient::FinishedPlayingBuffer(buffer_id));
  63. }
  64. void ClientConnection::did_change_muted_state(Badge<Mixer>, bool muted)
  65. {
  66. post_message(Messages::AudioClient::MutedStateChanged(muted));
  67. }
  68. void ClientConnection::did_change_main_mix_volume(Badge<Mixer>, int volume)
  69. {
  70. post_message(Messages::AudioClient::MainMixVolumeChanged(volume));
  71. }
  72. OwnPtr<Messages::AudioServer::GreetResponse> ClientConnection::handle(const Messages::AudioServer::Greet&)
  73. {
  74. return make<Messages::AudioServer::GreetResponse>(client_id());
  75. }
  76. OwnPtr<Messages::AudioServer::GetMainMixVolumeResponse> ClientConnection::handle(const Messages::AudioServer::GetMainMixVolume&)
  77. {
  78. return make<Messages::AudioServer::GetMainMixVolumeResponse>(m_mixer.main_volume());
  79. }
  80. OwnPtr<Messages::AudioServer::SetMainMixVolumeResponse> ClientConnection::handle(const Messages::AudioServer::SetMainMixVolume& message)
  81. {
  82. m_mixer.set_main_volume(message.volume());
  83. return make<Messages::AudioServer::SetMainMixVolumeResponse>();
  84. }
  85. OwnPtr<Messages::AudioServer::EnqueueBufferResponse> ClientConnection::handle(const Messages::AudioServer::EnqueueBuffer& message)
  86. {
  87. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.buffer_id());
  88. if (!shared_buffer) {
  89. // FIXME: The shared buffer should have been retrieved for us already.
  90. // We don't want to do IPC error checking at this layer.
  91. ASSERT_NOT_REACHED();
  92. }
  93. if (!m_queue)
  94. m_queue = m_mixer.create_queue(*this);
  95. if (m_queue->is_full())
  96. return make<Messages::AudioServer::EnqueueBufferResponse>(false);
  97. m_queue->enqueue(Audio::Buffer::create_with_shared_buffer(*shared_buffer, message.sample_count()));
  98. return make<Messages::AudioServer::EnqueueBufferResponse>(true);
  99. }
  100. OwnPtr<Messages::AudioServer::GetRemainingSamplesResponse> ClientConnection::handle(const Messages::AudioServer::GetRemainingSamples&)
  101. {
  102. int remaining = 0;
  103. if (m_queue)
  104. remaining = m_queue->get_remaining_samples();
  105. return make<Messages::AudioServer::GetRemainingSamplesResponse>(remaining);
  106. }
  107. OwnPtr<Messages::AudioServer::GetPlayedSamplesResponse> ClientConnection::handle(const Messages::AudioServer::GetPlayedSamples&)
  108. {
  109. int played = 0;
  110. if (m_queue)
  111. played = m_queue->get_played_samples();
  112. return make<Messages::AudioServer::GetPlayedSamplesResponse>(played);
  113. }
  114. OwnPtr<Messages::AudioServer::SetPausedResponse> ClientConnection::handle(const Messages::AudioServer::SetPaused& message)
  115. {
  116. if (m_queue)
  117. m_queue->set_paused(message.paused());
  118. return make<Messages::AudioServer::SetPausedResponse>();
  119. }
  120. OwnPtr<Messages::AudioServer::ClearBufferResponse> ClientConnection::handle(const Messages::AudioServer::ClearBuffer& message)
  121. {
  122. if (m_queue)
  123. m_queue->clear(message.paused());
  124. return make<Messages::AudioServer::ClearBufferResponse>();
  125. }
  126. OwnPtr<Messages::AudioServer::GetPlayingBufferResponse> ClientConnection::handle(const Messages::AudioServer::GetPlayingBuffer&)
  127. {
  128. int id = -1;
  129. if (m_queue)
  130. id = m_queue->get_playing_buffer();
  131. return make<Messages::AudioServer::GetPlayingBufferResponse>(id);
  132. }
  133. OwnPtr<Messages::AudioServer::GetMutedResponse> ClientConnection::handle(const Messages::AudioServer::GetMuted&)
  134. {
  135. return make<Messages::AudioServer::GetMutedResponse>(m_mixer.is_muted());
  136. }
  137. OwnPtr<Messages::AudioServer::SetMutedResponse> ClientConnection::handle(const Messages::AudioServer::SetMuted& message)
  138. {
  139. m_mixer.set_muted(message.muted());
  140. return make<Messages::AudioServer::SetMutedResponse>();
  141. }
  142. }