blob: 293c533cfcfdb5096561e099947a8f50ed628a2a [file] [log] [blame]
Hongguang4092f2f2021-07-08 18:49:12 -07001/*
2 * Copyright 2021 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 <aidl/android/hardware/tv/tuner/BnDvr.h>
20#include <aidl/android/hardware/tv/tuner/RecordStatus.h>
21
22#include <fmq/AidlMessageQueue.h>
23#include <math.h>
Hongguange423acd2021-07-27 16:56:47 -070024#include <atomic>
Hongguang4092f2f2021-07-08 18:49:12 -070025#include <set>
Hongguange423acd2021-07-27 16:56:47 -070026#include <thread>
Hongguang4092f2f2021-07-08 18:49:12 -070027#include "Demux.h"
28#include "Frontend.h"
29#include "Tuner.h"
30
31using namespace std;
32
33namespace aidl {
34namespace android {
35namespace hardware {
36namespace tv {
37namespace tuner {
38
39using ::aidl::android::hardware::common::fmq::MQDescriptor;
40using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
41using ::android::AidlMessageQueue;
42using ::android::hardware::EventFlag;
43
44using DvrMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
45
46struct MediaEsMetaData {
47 bool isAudio;
48 int startIndex;
49 int len;
50 int pts;
51};
52
53class Demux;
54class Filter;
55class Frontend;
56class Tuner;
57
58class Dvr : public BnDvr {
59 public:
Hongguang Chenff2c6b02021-08-07 00:12:26 +000060 Dvr(DvrType type, uint32_t bufferSize, const std::shared_ptr<IDvrCallback>& cb,
61 std::shared_ptr<Demux> demux);
Hongguang4092f2f2021-07-08 18:49:12 -070062 ~Dvr();
63
64 ::ndk::ScopedAStatus getQueueDesc(
65 MQDescriptor<int8_t, SynchronizedReadWrite>* out_queue) override;
66 ::ndk::ScopedAStatus configure(const DvrSettings& in_settings) override;
67 ::ndk::ScopedAStatus attachFilter(const std::shared_ptr<IFilter>& in_filter) override;
68 ::ndk::ScopedAStatus detachFilter(const std::shared_ptr<IFilter>& in_filter) override;
69 ::ndk::ScopedAStatus start() override;
70 ::ndk::ScopedAStatus stop() override;
71 ::ndk::ScopedAStatus flush() override;
72 ::ndk::ScopedAStatus close() override;
Ray Chin62ab6c92022-09-15 15:07:33 +080073 ::ndk::ScopedAStatus setStatusCheckIntervalHint(int64_t in_milliseconds) override;
Hongguang4092f2f2021-07-08 18:49:12 -070074
Hongguang2ecfc392021-11-23 10:29:15 -080075 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
76
Hongguang4092f2f2021-07-08 18:49:12 -070077 /**
78 * To create a DvrMQ and its Event Flag.
79 *
80 * Return false is any of the above processes fails.
81 */
82 bool createDvrMQ();
83 bool writeRecordFMQ(const std::vector<int8_t>& data);
84 bool addPlaybackFilter(int64_t filterId, std::shared_ptr<IFilter> filter);
85 bool removePlaybackFilter(int64_t filterId);
86 bool readPlaybackFMQ(bool isVirtualFrontend, bool isRecording);
87 bool processEsDataOnPlayback(bool isVirtualFrontend, bool isRecording);
88 bool startFilterDispatcher(bool isVirtualFrontend, bool isRecording);
89 EventFlag* getDvrEventFlag();
90 DvrSettings getSettings() { return mDvrSettings; }
91
92 private:
93 // Demux service
Hongguang Chenff2c6b02021-08-07 00:12:26 +000094 std::shared_ptr<Demux> mDemux;
Hongguang4092f2f2021-07-08 18:49:12 -070095
96 DvrType mType;
97 uint32_t mBufferSize;
98 std::shared_ptr<IDvrCallback> mCallback;
99 std::map<int64_t, std::shared_ptr<IFilter>> mFilters;
100
101 void deleteEventFlag();
102 bool readDataFromMQ();
103 void getMetaDataValue(int& index, int8_t* dataOutputBuffer, int& value);
104 void maySendPlaybackStatusCallback();
105 void maySendRecordStatusCallback();
106 PlaybackStatus checkPlaybackStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
Hongguang11da2cb2021-08-05 19:05:12 -0700107 int64_t highThreshold, int64_t lowThreshold);
Hongguang4092f2f2021-07-08 18:49:12 -0700108 RecordStatus checkRecordStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
Hongguang11da2cb2021-08-05 19:05:12 -0700109 int64_t highThreshold, int64_t lowThreshold);
Hongguang4092f2f2021-07-08 18:49:12 -0700110 /**
111 * A dispatcher to read and dispatch input data to all the started filters.
112 * Each filter handler handles the data filtering/output writing/filterEvent updating.
113 */
114 void startTpidFilter(vector<int8_t> data);
Hongguang4092f2f2021-07-08 18:49:12 -0700115 void playbackThreadLoop();
Hongguang4092f2f2021-07-08 18:49:12 -0700116
117 unique_ptr<DvrMQ> mDvrMQ;
118 EventFlag* mDvrEventFlag;
119 /**
120 * Demux callbacks used on filter events or IO buffer status
121 */
122 bool mDvrConfigured = false;
123 DvrSettings mDvrSettings;
124
125 // Thread handlers
Hongguange423acd2021-07-27 16:56:47 -0700126 std::thread mDvrThread;
Hongguang4092f2f2021-07-08 18:49:12 -0700127
128 // FMQ status local records
129 PlaybackStatus mPlaybackStatus;
130 RecordStatus mRecordStatus;
131 /**
132 * If a specific filter's writing loop is still running
133 */
Hongguange423acd2021-07-27 16:56:47 -0700134 std::atomic<bool> mDvrThreadRunning;
Hongguang901aa7b2021-08-26 12:20:56 -0700135
Hongguang4092f2f2021-07-08 18:49:12 -0700136 /**
137 * Lock to protect writes to the FMQs
138 */
139 std::mutex mWriteLock;
140 /**
141 * Lock to protect writes to the input status
142 */
143 std::mutex mPlaybackStatusLock;
144 std::mutex mRecordStatusLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700145
146 const bool DEBUG_DVR = false;
Hongguang4092f2f2021-07-08 18:49:12 -0700147};
148
149} // namespace tuner
150} // namespace tv
151} // namespace hardware
152} // namespace android
153} // namespace aidl