libxcoder  5.2.0
client.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (C) 2022 NETINT Technologies
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18  * SOFTWARE.
19  *
20  ******************************************************************************/
21 
22 /*!*****************************************************************************
23  * \file client.cpp
24  *
25  * \brief client test for NETINT video processing Android service
26  *
27  ******************************************************************************/
28 
29 #include <android/hardware/nidec/1.0/INidec.h>
30 #include <hidl/Status.h>
31 #include <hidl/LegacySupport.h>
32 #include <utils/misc.h>
33 #include <hidl/HidlSupport.h>
34 #include <stdio.h>
35 
36 #include <cutils/properties.h>
37 #include <cutils/ashmem.h>
38 #include <sys/mman.h>
39 
40 using android::sp;
41 using android::hardware::hidl_handle;
42 using android::hardware::nidec::V1_0::INidec;
43 
44 #define UNUSED(x) (void)(x)
45 
46 #define CODERS_SHM_NAME "NI_SHM_CODERS"
47 #define STR_BUFFER_LEN 32
48 
49 using namespace std;
50 
51 int main()
52 {
53  int ret;
54 
55  UNUSED(ret);
56 
57  string param = CODERS_SHM_NAME;
58  int32_t shm_fd = 0;
59 
60  android::sp<INidec> service = INidec::getService();
61  if (service == nullptr)
62  {
63  printf("Failed to get service\n");
64  return -1;
65  }
66 
67  service->GetAppFlag(param, [&](int32_t ret, const hidl_handle &handle) {
68  printf("GetAppFlag: ret %d\n", ret);
69  if (ret > 0)
70  {
71  shm_fd = dup(handle->data[0]);
72  printf("vendor:GetAppFlag shm_fd:%d\n", shm_fd);
73  }
74  });
75 
76  if (shm_fd <= 0)
77  {
78  shm_fd = ashmem_create_region(CODERS_SHM_NAME, sizeof(int32_t));
79  if (shm_fd >= 0)
80  {
81  native_handle_t *handle = native_handle_create(1, 0);
82  handle->data[0] = shm_fd;
83  service->SetAppFlag(param, handle);
84  }
85  printf("Create shm fd %d\n", shm_fd);
86  int32_t *shm_data = (int32_t *)mmap(
87  0, sizeof(int32_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
88  if (MAP_FAILED == shm_data)
89  {
90  printf("Error mmap shm_data ..\n");
91  }
92  *shm_data = 100;
93  printf("set shm data %d\n", *shm_data);
94 
95  //close(shm_fd);
96 
97  shm_data = (int32_t *)mmap(0, sizeof(int32_t), PROT_READ | PROT_WRITE,
98  MAP_SHARED, shm_fd, 0);
99  if (MAP_FAILED == shm_data)
100  {
101  printf("Error mmap shm_data ..\n");
102  }
103  printf("get shm data %d\n", *shm_data);
104 
105  } else
106  {
107  printf("Get shm fd %d\n", shm_fd);
108  int32_t *shm_data = (int32_t *)mmap(
109  0, sizeof(int32_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
110  if (MAP_FAILED == shm_data)
111  {
112  printf("mmap failed errno = %d\n", errno);
113  }
114  printf("get shm data %d\n", *shm_data);
115  }
116 
117  close(shm_fd);
118 
119  return 0;
120 }
CODERS_SHM_NAME
#define CODERS_SHM_NAME
Definition: client.cpp:46
main
int main()
Definition: client.cpp:51
service
android::sp< INidec > service
UNUSED
#define UNUSED(x)
Definition: client.cpp:44