• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 #include <mutex>
21 #include <thread>
22 #include <vector>
23 
24 #include "host/frontend/webrtc/lib/audio_sink.h"
25 #include "host/frontend/webrtc/lib/audio_source.h"
26 #include "host/libs/audio_connector/server.h"
27 
28 namespace cuttlefish {
29 class AudioHandler : public AudioServerExecutor {
30   // TODO(jemoreira): This can probably be avoided if playback goes through the
31   // audio device instead.
32   struct HoldingBuffer {
33     std::vector<uint8_t> buffer;
34     size_t count;
35 
36     void Reset(size_t size);
37     size_t Add(const volatile uint8_t* data, size_t max_len);
38     bool empty() const;
39     bool full() const;
40     uint8_t* data();
41   };
42   struct StreamDesc {
43     std::mutex mtx;
44     int bits_per_sample = -1;
45     int sample_rate = -1;
46     int channels = -1;
47     bool active = false;
48     HoldingBuffer buffer;
49   };
50 
51  public:
52   AudioHandler(std::unique_ptr<AudioServer> audio_server,
53                std::shared_ptr<webrtc_streaming::AudioSink> audio_sink,
54                std::shared_ptr<webrtc_streaming::AudioSource> audio_source);
55   ~AudioHandler() override = default;
56 
57   void Start();
58 
59   // AudioServerExecutor implementation
60   void StreamsInfo(StreamInfoCommand& cmd) override;
61   void SetStreamParameters(StreamSetParamsCommand& cmd) override;
62   void PrepareStream(StreamControlCommand& cmd) override;
63   void ReleaseStream(StreamControlCommand& cmd) override;
64   void StartStream(StreamControlCommand& cmd) override;
65   void StopStream(StreamControlCommand& cmd) override;
66 
67   void OnPlaybackBuffer(TxBuffer buffer) override;
68   void OnCaptureBuffer(RxBuffer buffer) override;
69 
70  private:
71   [[noreturn]] void Loop();
72 
73   std::shared_ptr<webrtc_streaming::AudioSink> audio_sink_;
74   std::unique_ptr<AudioServer> audio_server_;
75   std::thread server_thread_;
76   std::vector<StreamDesc> stream_descs_ = {};
77   std::shared_ptr<webrtc_streaming::AudioSource> audio_source_;
78 };
79 }  // namespace cuttlefish
80