RDK Documentation (Open Sourced RDK Components)
subtec_test.cpp
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2020 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #include <PacketSender.hpp>
21 #include <WebVttPacket.hpp>
22 #include <sys/epoll.h>
23 #include <thread>
24 #include <unistd.h>
25 #include <fstream>
26 #include <algorithm>
27 
28 using DataBuffer = std::vector<uint8_t>;
29 
30 void serverThread(const char *socket_path)
31 {
32  int epoll = ::epoll_create(10);
33  int serversock = ::socket(AF_UNIX, SOCK_DGRAM, 0);
34  struct sockaddr_un addr;
35  addr.sun_family = AF_UNIX;
36  if (NULL != socket_path)
37  (void) std::strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
38  else
39  (void) std::strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path));
40  addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
41  if (-1 == ::bind(serversock, (struct sockaddr*)&addr, sizeof(addr)))
42  {
43  printf("Couldn't bind server at %s\n", addr.sun_path);
44  return;
45  }
46  listen(serversock, 1);
47  struct epoll_event event;
48  event.events = EPOLLIN;
49  event.data.fd = serversock;
50  epoll_ctl(epoll, EPOLL_CTL_ADD, serversock, &event);
51 
52  while(1)
53  {
54  struct epoll_event ev;
55  printf("Waiting for client message. Epoll %d serversock %d\n", epoll, serversock);
56  int nfd = epoll_wait(epoll, &ev, 1, 10000);
57  if (nfd > 0 && event.data.fd == serversock)
58  {
59  printf("Message received nfd %d fd %d serversock %d\n", nfd, ev.data.fd, serversock);
60  std::uint32_t type, counter, size;
61  std::uint32_t data[1024];
62  int rd;
63  do {
64  rd = recv(ev.data.fd, data, sizeof(data), 0);
65  printf("Read data %d\n", rd);
66  } while (false);
67  type = data[0];
68  counter = data[1];
69  size = data[2];
70  printf("Packet received: type %d counter %d size %d\n", type, counter, size);
71  }
72  }
73 }
74 
75 int main(int argc, char *argv[])
76 {
77  int opt;
78  bool test_thread = false, ret, send_reset = false;
79  std::thread th;
80  const char *path = NULL;
81  const char *webvtt_file_path = NULL;
82 
83  while ((opt = getopt(argc, argv, "sf:tr")) != -1)
84  {
85  switch(opt) {
86  case 't':
87  test_thread = true;
88  break;
89  case 'f':
90  webvtt_file_path = optarg;
91  break;
92  case 's':
93  path = optarg;
94  printf("path is %s\n", path);
95  break;
96  case 'r':
97  send_reset = true;
98  break;
99  default:
100  break;
101  }
102  }
103 
104 
105  if (test_thread)
106  {
107  if (path)
108  remove(path);
109  else
110  remove(SOCKET_PATH);
111 
112  printf("Starting thread\n");
113  th = std::thread(serverThread, path);
114  sleep(2);
115  }
116 
117  WebVttChannel *channel = new WebVttChannel();
118  std::vector<uint8_t> data;
119 
120  ret = channel->InitComms();
121  if (ret)
122  {
123  channel->SendResetAllPacket();
124  if (!send_reset)
125  {
126  channel->SendResetChannelPacket();
127  channel->SendSelectionPacket(1920, 1080);
128  channel->SendUnmutePacket();
129  channel->SendTimestampPacket(0);
130  if (webvtt_file_path) {
131  auto ifile = std::ifstream(webvtt_file_path, std::ios::in | std::ios::binary);
132 
133  std::for_each(std::istreambuf_iterator<char>(ifile),
134  std::istreambuf_iterator<char>(),
135  [&data](const char c) {
136  data.push_back(c);
137  });
138  } else {
139  data = {'a', 'b', 'c'};
140  }
141  channel->SendDataPacket(std::move(data));
142  sleep(1);
143  }
144  }
145 
146  if (test_thread) th.join();
147 
148  return 0;
149 }
WebVttChannel
Definition: WebVttPacket.hpp:117