RDK Documentation (Open Sourced RDK Components)
main.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 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 #include <unistd.h>
20 #include <stdio.h>
21 #include <sys/prctl.h>
22 #include <sys/stat.h>
23 #include <glib.h>
24 #include <gst/gst.h>
25 #ifdef ENABLE_BREAKPAD
26 #include <client/linux/handler/exception_handler.h>
27 #endif
28 #include <rtRemote.h>
29 #include "rdkmediaplayer.h"
30 #include "logger.h"
31 #include "glib_tools.h"
32 #include "hangdetector_utils.h"
33 
34 #ifdef BCM_SVP_ENABLED
35 #include "nexus_config.h"
36 #include "nexus_platform.h"
37 #include "nxclient.h"
38 #endif
39 
40 #ifdef ENABLE_SIGNAL_HANDLE
41 #include <signal.h>
42 #ifdef Q_OS_LINUX
43 //#include <unistd.h>
44 #endif
45 #endif
46 
47 GMainLoop *gMainLoop = 0;
48 int gPipefd[2];
49 
50 #ifdef ENABLE_BREAKPAD
51 
52 namespace
53 {
54 
55 bool breakpadCallback(const google_breakpad::MinidumpDescriptor& /*descriptor*/, void* /*context*/, bool succeeded) {
56  rtRemoteShutdown();
57  return succeeded;
58 }
59 
60 void installExceptionHandler() {
61  static google_breakpad::ExceptionHandler* excHandler = NULL;
62  delete excHandler;
63  excHandler = new google_breakpad::ExceptionHandler(
64  google_breakpad::MinidumpDescriptor("/opt/minidumps"),
65  NULL,
66  breakpadCallback,
67  NULL,
68  true,
69  -1);
70 }
71 
72 } // namespace
73 #endif
74 
75 #ifdef ENABLE_SIGNAL_HANDLER
76 void signalHandler(int signum)
77 {
78  printf("signalHandler %d\n", signum);
79  rtError e = rtRemoteShutdown();
80  if (e != RT_OK)
81  {
82  printf("rtRemoteShutdown failed: %s \n", rtStrError(e));
83  } //CID:103340 - Checked return
84  signal(signum, SIG_DFL);
85  kill(getpid(), signum);
86 }
87 #endif
88 
89 void rtMainLoopCb(void*)
90 {
91  rtError err = rtRemoteProcessSingleItem();
92  if(err != RT_OK && err != RT_ERROR_QUEUE_EMPTY)
93  {
94  LOG_WARNING("rtRemoteProcessSingleItem() returned %d", err);
95  }
96 }
97 
98 void rtRemoteCallback(void*)
99 {
100  //LOG_TRACE("queueReadyHandler entered");
101  static char temp[1];
102  int ret = HANDLE_EINTR_EAGAIN(write(gPipefd[PIPE_WRITE], temp, 1));
103  if (ret == -1)
104  perror("can't write to pipe");
105 }
106 
107 int main(int argc, char *argv[]) {
108  Utils::HangDetector hangDetector;
109  hangDetector.start();
110  prctl(PR_SET_PDEATHSIG, SIGHUP);
111 
112  #ifdef ENABLE_BREAKPAD
113  installExceptionHandler();
114  #endif
115 
116  #ifdef ENABLE_SIGNAL_HANDLER
117  signal(SIGINT, signalHandler);
118  signal(SIGQUIT, signalHandler);
119  signal(SIGTERM, signalHandler);
120  signal(SIGILL, signalHandler);
121  signal(SIGABRT, signalHandler);
122  signal(SIGFPE, signalHandler);
123  signal(SIGSEGV, signalHandler);
124  #endif
125  #ifdef BCM_SVP_ENABLED
126  NEXUS_Error rt;
127  rt = NxClient_Join(NULL);
128  ASSERT(rt == NEXUS_SUCCESS);
129  //CID:96870 - checked return
130  #endif
131  gst_init(0, 0);
132  log_init();
133 
134  LOG_INFO("Init rtRemote");
135  rtError rc;
136  gMainLoop = g_main_loop_new(0, FALSE);
137 
138  auto *source = pipe_source_new(gPipefd, rtMainLoopCb, nullptr);
139  g_source_attach(source, g_main_loop_get_context(gMainLoop));
140 
141  rtRemoteRegisterQueueReadyHandler( rtEnvironmentGetGlobal(), rtRemoteCallback, nullptr );
142 
143  rc = rtRemoteInit();
144  ASSERT(rc == RT_OK);
145 
146  const char* objectName = getenv("PX_WAYLAND_CLIENT_REMOTE_OBJECT_NAME");
147  if (!objectName) objectName = "rdkmediaplayer";
148  LOG_INFO("Register: %s", objectName);
149 
150  rtObjectRef playerRef = new RDKMediaPlayer;
151  rc = rtRemoteRegisterObject(objectName, playerRef);
152  ASSERT(rc == RT_OK);
153 
154  {
155  LOG_INFO("Start main loop");
156  g_main_loop_run(gMainLoop);
157  g_main_loop_unref(gMainLoop);
158  gMainLoop = 0;
159  }
160 
161  playerRef = nullptr;
162 
163  LOG_INFO("Shutdown");
164  rc = rtRemoteShutdown();
165  ASSERT(rc == RT_OK);
166 
167  gst_deinit();
168  #ifdef BCM_SVP_ENABLED
169  NxClient_Uninit();
170  #endif
171  g_source_unref(source);
172 
173  return 0;
174 }
pipe_source_new
GSource * pipe_source_new(int pipefd[2], PipeSourceCallback cb, void *ctx)
This API creates a new event source "rtRemoteSource".
Definition: glib_tools.cpp:81
Utils::HangDetector
Definition: hangdetector_utils.h:33
RDKMediaPlayer
Definition: rdkmediaplayer.h:52
LOG_INFO
#define LOG_INFO(AAMP_JS_OBJECT, FORMAT,...)
Definition: jsutils.h:39