1 /**
2  * Copyright (C) 2019 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 #include <android/hardware/cas/1.0/IMediaCasService.h>
18 #include <android/hardware/cas/native/1.0/IDescrambler.h>
19 #include <binder/ProcessState.h>
20 
21 using namespace android;
22 using hardware::hidl_vec;
23 using hardware::Return;
24 using namespace hardware::cas::V1_0;
25 using android::hardware::Void;
26 using android::hardware::cas::native::V1_0::IDescrambler;
27 
28 class MyMediaCasListener : public ICasListener {
29  public:
onEvent(int32_t,int32_t,const hidl_vec<uint8_t> &)30   virtual Return<void> onEvent(int32_t, int32_t, const hidl_vec<uint8_t>&) override {
31     return Void();
32   }
33 };
34 
35 static const int32_t sClearKeySystemId = 0xF6D8;
36 static sp<IDescramblerBase> descramblerBase;
37 
thread1(void *)38 static void* thread1(void*) {
39   Return<Status> returnStatus(Status::OK);
40   std::vector<uint8_t> sessionId;
41   sessionId.push_back(1);
42 
43   returnStatus = descramblerBase->setMediaCasSession(sessionId);
44 
45   return NULL;
46 }
47 
main()48 int main() {
49   sp<ICas> cas;
50   Status status;
51   sp<IDescrambler> descrambler;
52   Return<Status> returnStatus(Status::OK);
53   Return<sp<IDescramblerBase>> returnDescrambler(NULL);
54   std::vector<uint8_t> sessionId;
55 
56   android::ProcessState::self()->startThreadPool();
57 
58   sp<IMediaCasService> casService = IMediaCasService::getService("default");
59   if (casService == NULL) {
60     return EXIT_FAILURE;
61   }
62 
63   sp<ICasListener> listener;
64   cas = casService->createPlugin(sClearKeySystemId, listener);
65   if (cas == NULL) {
66     return EXIT_FAILURE;
67   }
68 
69   auto returnVoid = cas->openSession(
70       [&status, &sessionId](Status _status,
71                             const hidl_vec<uint8_t>& _sessionId) {
72         status = _status;
73         sessionId = _sessionId;
74       });
75   if (!returnVoid.isOk() || status != Status::OK) {
76     return EXIT_FAILURE;
77   }
78 
79   returnDescrambler = casService->createDescrambler(sClearKeySystemId);
80   if (!returnDescrambler.isOk()) {
81     return EXIT_FAILURE;
82   }
83 
84   descramblerBase = (sp<IDescramblerBase>)returnDescrambler;
85 
86   if (descramblerBase == NULL) {
87     return EXIT_FAILURE;
88   }
89 
90   returnStatus = descramblerBase->setMediaCasSession(sessionId);
91 
92   if (!returnStatus.isOk() || (Status)returnStatus != Status::OK) {
93     return EXIT_FAILURE;
94   }
95 
96   descrambler = IDescrambler::castFrom(descramblerBase);
97 
98   if (descrambler == NULL) {
99     return EXIT_FAILURE;
100   }
101 
102   pthread_t pt;
103   pthread_create(&pt, NULL, thread1, NULL);
104 
105   descramblerBase->release();
106 
107   return EXIT_FAILURE;
108 }
109