1 /*
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 #ifndef _ANDROID_MEDIA_TV_DVR_CLIENT_H_
18 #define _ANDROID_MEDIA_TV_DVR_CLIENT_H_
19 
20 #include <aidl/android/hardware/tv/tuner/DvrSettings.h>
21 #include <aidl/android/hardware/tv/tuner/Result.h>
22 #include <aidl/android/media/tv/tuner/BnTunerDvrCallback.h>
23 #include <aidl/android/media/tv/tuner/ITunerDvr.h>
24 #include <fmq/AidlMessageQueue.h>
25 
26 #include "DvrClientCallback.h"
27 #include "FilterClient.h"
28 
29 using Status = ::ndk::ScopedAStatus;
30 using ::aidl::android::hardware::common::fmq::MQDescriptor;
31 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
32 using ::aidl::android::hardware::tv::tuner::DvrSettings;
33 using ::aidl::android::hardware::tv::tuner::PlaybackStatus;
34 using ::aidl::android::hardware::tv::tuner::RecordStatus;
35 using ::aidl::android::hardware::tv::tuner::Result;
36 using ::aidl::android::media::tv::tuner::BnTunerDvrCallback;
37 using ::aidl::android::media::tv::tuner::ITunerDvr;
38 
39 using namespace std;
40 
41 namespace android {
42 
43 using AidlMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
44 using AidlMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>;
45 
46 class TunerDvrCallback : public BnTunerDvrCallback {
47 
48 public:
49     TunerDvrCallback(sp<DvrClientCallback> dvrClientCallback);
50 
51     Status onRecordStatus(RecordStatus status);
52     Status onPlaybackStatus(PlaybackStatus status);
53 
54 private:
55     sp<DvrClientCallback> mDvrClientCallback;
56 };
57 
58 struct DvrClient : public RefBase {
59 
60 public:
61     DvrClient(shared_ptr<ITunerDvr> tunerDvr);
62     ~DvrClient();
63 
64     /**
65      * Set the DVR file descriptor.
66      */
67     void setFd(int32_t fd);
68 
69     /**
70      * Read data from file with given size. Return the actual read size.
71      */
72     int64_t readFromFile(int64_t size);
73 
74     /**
75      * Read data from the given buffer with given size. Return the actual read size.
76      */
77     int64_t readFromBuffer(int8_t* buffer, int64_t size);
78 
79     /**
80      * Write data to file with given size. Return the actual write size.
81      */
82     int64_t writeToFile(int64_t size);
83 
84     /**
85      * Seeks the Dvr file descriptor from the beginning of the file.
86      */
87     int64_t seekFile(int64_t pos);
88 
89     /**
90      * Write data to the given buffer with given size. Return the actual write size.
91      */
92     int64_t writeToBuffer(int8_t* buffer, int64_t size);
93 
94     /**
95      * Configure the DVR.
96      */
97     Result configure(DvrSettings settings);
98 
99     /**
100      * Attach one filter to DVR interface for recording.
101      */
102     Result attachFilter(sp<FilterClient> filterClient);
103 
104     /**
105      * Detach one filter from the DVR's recording.
106      */
107     Result detachFilter(sp<FilterClient> filterClient);
108 
109     /**
110      * Start DVR.
111      */
112     Result start();
113 
114     /**
115      * Stop DVR.
116      */
117     Result stop();
118 
119     /**
120      * Flush DVR data.
121      */
122     Result flush();
123 
124     /**
125      * close the DVR instance to release resource for DVR.
126      */
127     Result close();
128 
129     /**
130      * Set status check time interval.
131      */
132     Result setStatusCheckIntervalHint(int64_t durationInMs);
133 
134 private:
135     /**
136      * An AIDL Tuner Dvr Singleton assigned at the first time the Tuner Client
137      * opens a dvr. Default null when dvr is not opened.
138      */
139     shared_ptr<ITunerDvr> mTunerDvr;
140 
141     AidlMQ* mDvrMQ;
142     EventFlag* mDvrMQEventFlag;
143     string mFilePath;
144     int32_t mFd;
145 };
146 }  // namespace android
147 
148 #endif  // _ANDROID_MEDIA_TV_DVR_CLIENT_H_
149