blob: 6ff71cd13f82065a67caf09d25a43f3a8f6e6278 [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;
73
Hongguang2ecfc392021-11-23 10:29:15 -080074 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
75
Hongguang4092f2f2021-07-08 18:49:12 -070076 /**
77 * To create a DvrMQ and its Event Flag.
78 *
79 * Return false is any of the above processes fails.
80 */
81 bool createDvrMQ();
82 bool writeRecordFMQ(const std::vector<int8_t>& data);
83 bool addPlaybackFilter(int64_t filterId, std::shared_ptr<IFilter> filter);
84 bool removePlaybackFilter(int64_t filterId);
85 bool readPlaybackFMQ(bool isVirtualFrontend, bool isRecording);
86 bool processEsDataOnPlayback(bool isVirtualFrontend, bool isRecording);
87 bool startFilterDispatcher(bool isVirtualFrontend, bool isRecording);
88 EventFlag* getDvrEventFlag();
89 DvrSettings getSettings() { return mDvrSettings; }
90
91 private:
92 // Demux service
Hongguang Chenff2c6b02021-08-07 00:12:26 +000093 std::shared_ptr<Demux> mDemux;
Hongguang4092f2f2021-07-08 18:49:12 -070094
95 DvrType mType;
96 uint32_t mBufferSize;
97 std::shared_ptr<IDvrCallback> mCallback;
98 std::map<int64_t, std::shared_ptr<IFilter>> mFilters;
99
100 void deleteEventFlag();
101 bool readDataFromMQ();
102 void getMetaDataValue(int& index, int8_t* dataOutputBuffer, int& value);
103 void maySendPlaybackStatusCallback();
104 void maySendRecordStatusCallback();
105 PlaybackStatus checkPlaybackStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
Hongguang11da2cb2021-08-05 19:05:12 -0700106 int64_t highThreshold, int64_t lowThreshold);
Hongguang4092f2f2021-07-08 18:49:12 -0700107 RecordStatus checkRecordStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
Hongguang11da2cb2021-08-05 19:05:12 -0700108 int64_t highThreshold, int64_t lowThreshold);
Hongguang4092f2f2021-07-08 18:49:12 -0700109 /**
110 * A dispatcher to read and dispatch input data to all the started filters.
111 * Each filter handler handles the data filtering/output writing/filterEvent updating.
112 */
113 void startTpidFilter(vector<int8_t> data);
Hongguang4092f2f2021-07-08 18:49:12 -0700114 void playbackThreadLoop();
Hongguang4092f2f2021-07-08 18:49:12 -0700115
116 unique_ptr<DvrMQ> mDvrMQ;
117 EventFlag* mDvrEventFlag;
118 /**
119 * Demux callbacks used on filter events or IO buffer status
120 */
121 bool mDvrConfigured = false;
122 DvrSettings mDvrSettings;
123
124 // Thread handlers
Hongguange423acd2021-07-27 16:56:47 -0700125 std::thread mDvrThread;
Hongguang4092f2f2021-07-08 18:49:12 -0700126
127 // FMQ status local records
128 PlaybackStatus mPlaybackStatus;
129 RecordStatus mRecordStatus;
130 /**
131 * If a specific filter's writing loop is still running
132 */
Hongguange423acd2021-07-27 16:56:47 -0700133 std::atomic<bool> mDvrThreadRunning;
Hongguang901aa7b2021-08-26 12:20:56 -0700134
Hongguang4092f2f2021-07-08 18:49:12 -0700135 /**
136 * Lock to protect writes to the FMQs
137 */
138 std::mutex mWriteLock;
139 /**
140 * Lock to protect writes to the input status
141 */
142 std::mutex mPlaybackStatusLock;
143 std::mutex mRecordStatusLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700144
145 const bool DEBUG_DVR = false;
Hongguang4092f2f2021-07-08 18:49:12 -0700146};
147
148} // namespace tuner
149} // namespace tv
150} // namespace hardware
151} // namespace android
152} // namespace aidl