1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "AudioProxyStreamOut.h"
16 
17 namespace audio_proxy {
18 
AudioProxyStreamOut(audio_proxy_stream_out_t * stream,audio_proxy_device_t * device)19 AudioProxyStreamOut::AudioProxyStreamOut(audio_proxy_stream_out_t* stream,
20                                          audio_proxy_device_t* device)
21     : mStream(stream), mDevice(device) {}
22 
~AudioProxyStreamOut()23 AudioProxyStreamOut::~AudioProxyStreamOut() {
24   mDevice->close_output_stream(mDevice, mStream);
25 }
26 
write(const void * buffer,size_t bytes)27 ssize_t AudioProxyStreamOut::write(const void* buffer, size_t bytes) {
28   return mStream->write(mStream, buffer, bytes);
29 }
30 
getPresentationPosition(int64_t * frames,TimeSpec * timestamp) const31 void AudioProxyStreamOut::getPresentationPosition(int64_t* frames,
32                                                   TimeSpec* timestamp) const {
33   struct timespec ts;
34   mStream->get_presentation_position(mStream,
35                                      reinterpret_cast<uint64_t*>(frames), &ts);
36 
37   timestamp->tvSec = ts.tv_sec;
38   timestamp->tvNSec = ts.tv_nsec;
39 }
40 
standby()41 void AudioProxyStreamOut::standby() { mStream->standby(mStream); }
42 
pause()43 void AudioProxyStreamOut::pause() { mStream->pause(mStream); }
44 
resume()45 void AudioProxyStreamOut::resume() { mStream->resume(mStream); }
46 
drain(AudioDrain type)47 void AudioProxyStreamOut::drain(AudioDrain type) {
48   mStream->drain(mStream, static_cast<audio_proxy_drain_type_t>(type));
49 }
50 
flush()51 void AudioProxyStreamOut::flush() { mStream->flush(mStream); }
52 
setVolume(float left,float right)53 void AudioProxyStreamOut::setVolume(float left, float right) {
54   mStream->set_volume(mStream, left, right);
55 }
56 
getBufferSizeBytes()57 int64_t AudioProxyStreamOut::getBufferSizeBytes() {
58   return mStream->get_buffer_size(mStream);
59 }
60 
getLatencyMs()61 int32_t AudioProxyStreamOut::getLatencyMs() {
62   return mStream->get_latency(mStream);
63 }
64 
start()65 void AudioProxyStreamOut::start() {
66   if (mStream->v2) {
67     mStream->v2->start(mStream->v2);
68   }
69 }
70 
stop()71 void AudioProxyStreamOut::stop() {
72   if (mStream->v2) {
73     mStream->v2->stop(mStream->v2);
74   }
75 }
76 
createMmapBuffer(int32_t minBufferSizeFrames)77 MmapBufferInfo AudioProxyStreamOut::createMmapBuffer(
78     int32_t minBufferSizeFrames) {
79   MmapBufferInfo aidlInfo;
80   if (!mStream->v2) {
81     return aidlInfo;
82   }
83 
84   audio_proxy_mmap_buffer_info_t info =
85       mStream->v2->create_mmap_buffer(mStream->v2, minBufferSizeFrames);
86   aidlInfo.sharedMemoryFd.set(info.shared_memory_fd);
87   aidlInfo.bufferSizeFrames = info.buffer_size_frames;
88   aidlInfo.burstSizeFrames = info.burst_size_frames;
89   aidlInfo.flags = info.flags;
90   return aidlInfo;
91 }
92 
getMmapPosition()93 PresentationPosition AudioProxyStreamOut::getMmapPosition() {
94   PresentationPosition position;
95   if (!mStream->v2) {
96     return position;
97   }
98 
99   int64_t frames = 0;
100   struct timespec ts = {0, 0};
101   mStream->v2->get_mmap_position(mStream->v2, &frames, &ts);
102   position.frames = frames;
103   position.timestamp = {ts.tv_sec, ts.tv_nsec};
104   return position;
105 }
106 }  // namespace audio_proxy
107