ASClientConnection.cpp 5.9 KB

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