summaryrefslogtreecommitdiff
path: root/audio_proxy/OutputStreamImpl.h
blob: 7a3f7dc3d06c306367ed3a1603a8a9dde183afdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (C) 2021 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <aidl/device/google/atv/audio_proxy/BnOutputStream.h>
#include <fmq/AidlMessageQueue.h>
#include <fmq/EventFlag.h>
#include <utils/Thread.h>

using aidl::android::hardware::common::fmq::MQDescriptor;
using aidl::android::hardware::common::fmq::SynchronizedReadWrite;
using android::AidlMessageQueue;
using android::sp;
using android::Thread;
using android::hardware::EventFlag;

using aidl::device::google::atv::audio_proxy::AudioDrain;
using aidl::device::google::atv::audio_proxy::BnOutputStream;
using aidl::device::google::atv::audio_proxy::MmapBufferInfo;
using aidl::device::google::atv::audio_proxy::PresentationPosition;
using aidl::device::google::atv::audio_proxy::WriteStatus;

namespace audio_proxy {
class AudioProxyStreamOut;

class OutputStreamImpl : public BnOutputStream {
 public:
  using DataMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
  using DataMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>;
  using StatusMQ = AidlMessageQueue<WriteStatus, SynchronizedReadWrite>;
  using StatusMQDesc = MQDescriptor<WriteStatus, SynchronizedReadWrite>;

  explicit OutputStreamImpl(std::unique_ptr<AudioProxyStreamOut> stream);
  ~OutputStreamImpl() override;

  ndk::ScopedAStatus prepareForWriting(int32_t frameSize, int32_t framesCount,
                                       DataMQDesc* dataMQDesc,
                                       StatusMQDesc* statusMQDesc) override;

  ndk::ScopedAStatus standby() override;
  ndk::ScopedAStatus close() override;
  ndk::ScopedAStatus pause() override;
  ndk::ScopedAStatus resume() override;
  ndk::ScopedAStatus drain(AudioDrain type) override;
  ndk::ScopedAStatus flush() override;

  ndk::ScopedAStatus setVolume(float left, float right) override;

  ndk::ScopedAStatus getBufferSizeBytes(int64_t* bufferSizeBytes) override;
  ndk::ScopedAStatus getLatencyMs(int32_t* latencyMs) override;

  ndk::ScopedAStatus start() override;
  ndk::ScopedAStatus stop() override;
  ndk::ScopedAStatus createMmapBuffer(int32_t minBufferSizeFrames,
                                      MmapBufferInfo* info) override;
  ndk::ScopedAStatus getMmapPosition(PresentationPosition* position) override;

 private:
  typedef void (*EventFlagDeleter)(EventFlag*);

  ndk::ScopedAStatus closeImpl();

  std::unique_ptr<AudioProxyStreamOut> mStream;

  std::unique_ptr<DataMQ> mDataMQ;
  std::unique_ptr<StatusMQ> mStatusMQ;
  std::unique_ptr<EventFlag, EventFlagDeleter> mEventFlag;
  std::atomic<bool> mStopWriteThread = false;
  sp<Thread> mWriteThread;
};

}  // namespace audio_proxy