RDK Documentation (Open Sourced RDK Components)
AampSecManager.cpp
Go to the documentation of this file.
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 2021 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 /**
21  * @file AampSecManager.cpp
22  * @brief Class impl for AampSecManager
23  */
24 
25 #include "AampSecManager.h"
26 #include <string.h>
27 #include "_base64.h"
28 #include <inttypes.h> // For PRId64
29 
31 
32 /**
33  * @brief To get AampSecManager instance
34  */
36 {
37  if (mInstance == NULL)
38  {
39  mInstance = new AampSecManager();
40  }
41  return mInstance;
42 }
43 
44 /**
45  * @brief To release AampSecManager singelton instance
46  */
48 {
49  if (mInstance)
50  {
51  delete mInstance;
52  mInstance = NULL;
53  }
54 }
55 
56 /**
57  * @brief AampScheduler Constructor
58  */
59 AampSecManager::AampSecManager() : mSecManagerObj(SECMANAGER_CALL_SIGN), mSecMutex(), mSchedulerStarted(false),
60  mRegisteredEvents(), mWatermarkPluginObj(WATERMARK_PLUGIN_CALLSIGN), mWatMutex(), mSpeedStateMutex()
61 {
62 
63  std::lock_guard<std::mutex> lock(mSecMutex);
65  {
66  std::lock_guard<std::mutex> lock(mWatMutex);
68  }
69 
70  /*Start Scheduler for handling RDKShell API invocation*/
71  if(false == mSchedulerStarted)
72  {
74  mSchedulerStarted = true;
75  }
76 
78 }
79 
80 /**
81  * @brief AampScheduler Destructor
82  */
84 {
85  std::lock_guard<std::mutex> lock(mSecMutex);
86 
87  /*Stop Scheduler used for handling RDKShell API invocation*/
88  if(true == mSchedulerStarted)
89  {
90  StopScheduler();
91  mSchedulerStarted = false;
92  }
93 
95 }
96 
97 /**
98  * @brief To acquire license from SecManager
99  */
100 bool AampSecManager::AcquireLicense(PrivateInstanceAAMP* aamp, const char* licenseUrl, const char* moneyTraceMetdata[][2],
101  const char* accessAttributes[][2], const char* contentMetdata, size_t contMetaLen,
102  const char* licenseRequest, size_t licReqLen, const char* keySystemId,
103  const char* mediaUsage, const char* accessToken, size_t accTokenLen,
104  int64_t* sessionId,
105  char** licenseResponse, size_t* licenseResponseLength, int32_t* statusCode, int32_t* reasonCode, int32_t* businessStatus)
106 {
107  // licenseUrl un-used now
108  (void) licenseUrl;
109 
110  bool ret = false;
111  bool rpcResult = false;
112  unsigned int retryCount = 0;
113 
114  //Initializing it with default error codes (which would be sent if there any jsonRPC
115  //call failures to thunder)
116  *statusCode = SECMANGER_DRM_FAILURE;
117  *reasonCode = SECMANGER_DRM_GEN_FAILURE;
118 
119  //Shared memory pointer, key declared here,
120  //Access token, content metadata and licnese request will be passed to
121  //secmanager via shared memory
122  void * shmPt_accToken = NULL;
123  key_t shmKey_accToken = 0;
124  void * shmPt_contMeta = NULL;
125  key_t shmKey_contMeta = 0;
126  void * shmPt_licReq = NULL;
127  key_t shmKey_licReq = 0;
128 
129  const char* apiName = "openPlaybackSession";
130  JsonObject param;
131  JsonObject response;
132  JsonObject sessionConfig;
133  JsonObject aspectDimensions;
134 
135  if(NULL != aamp)
136  mAamp = aamp;
137 
138  sessionConfig["distributedTraceType"] = "money";
139  sessionConfig["distributedTraceId"] = moneyTraceMetdata[0][1];
140  sessionConfig["sessionState"] = "active";
141 
142  // TODO: Remove hardcoded values
143  aspectDimensions["width"] = 1920;
144  aspectDimensions["height"] = 1080;
145 
146  param["clientId"] = "aamp";
147  param["sessionConfiguration"] = sessionConfig;
148  param["contentAspectDimensions"] = aspectDimensions;
149 
150  param["keySystem"] = keySystemId;
151  param["mediaUsage"] = mediaUsage;
152 
153  // If sessionId is present, we are trying to acquire a new license within the same session
154  if (*sessionId != -1)
155  {
156  apiName = "updatePlaybackSession";
157  param["sessionId"] = *sessionId;
158  }
159 
160 #ifdef DEBUG_SECMAMANER
161  std::string params;
162  param.ToString(params);
163  AAMPLOG_WARN("SecManager openPlaybackSession param: %s", params.c_str());
164 #endif
165 
166  {
167  std::lock_guard<std::mutex> lock(mSecMutex);
168  if(accTokenLen > 0 && contMetaLen > 0 && licReqLen > 0)
169  {
170  shmPt_accToken = aamp_CreateSharedMem(accTokenLen, shmKey_accToken);
171  shmPt_contMeta = aamp_CreateSharedMem(contMetaLen, shmKey_contMeta);
172  shmPt_licReq = aamp_CreateSharedMem(licReqLen, shmKey_licReq);
173  }
174 
175  //Set shared memory with the buffer
176  if(NULL != shmPt_accToken && NULL != accessToken &&
177  NULL != shmPt_contMeta && NULL != contentMetdata &&
178  NULL != shmPt_licReq && NULL != licenseRequest)
179  {
180  //copy buffer to shm
181  memcpy(shmPt_accToken, accessToken, accTokenLen);
182  memcpy(shmPt_contMeta, contentMetdata, contMetaLen);
183  memcpy(shmPt_licReq, licenseRequest, licReqLen);
184 
185  AAMPLOG_INFO("Access token, Content metadata and license request are copied to the shared memory successfully, passing details with SecManager");
186 
187  //Set json params to be used by sec manager
188  param["accessTokenBufferKey"] = shmKey_accToken;
189  param["accessTokenLength"] = accTokenLen;
190 
191  param["contentMetadataBufferKey"] = shmKey_contMeta;
192  param["contentMetadataLength"] = contMetaLen;
193 
194  param["licenseRequestBufferKey"] = shmKey_licReq;
195  param["licenseRequestLength"] = licReqLen;
196 
197  //Retry delay
198  int sleepTime ;
199  GETCONFIGVALUE(eAAMPConfig_LicenseRetryWaitTime,sleepTime) ;
200  if(sleepTime<=0) sleepTime = 100;
201  //invoke "openPlaybackSession" with retries for specific error cases
202  do
203  {
204  rpcResult = mSecManagerObj.InvokeJSONRPC(apiName, param, response, 10000);
205  if (rpcResult)
206  {
207  #ifdef DEBUG_SECMAMANER
208  std::string output;
209  response.ToString(output);
210  AAMPLOG_WARN("SecManager openPlaybackSession o/p: %s",output.c_str());
211  #endif
212  if (response["success"].Boolean())
213  {
214  std::string license = response["license"].String();
215  AAMPLOG_TRACE("SecManager obtained license with length: %d and data: %s",license.size(), license.c_str());
216  if (!license.empty())
217  {
218  // Here license is base64 encoded
219  unsigned char * licenseDecoded = NULL;
220  size_t licenseDecodedLen = 0;
221  licenseDecoded = base64_Decode(license.c_str(), &licenseDecodedLen);
222  AAMPLOG_TRACE("SecManager license decoded len: %d and data: %p", licenseDecodedLen, licenseDecoded);
223 
224  if (licenseDecoded != NULL && licenseDecodedLen != 0)
225  {
226  AAMPLOG_INFO("SecManager license post base64 decode length: %d", *licenseResponseLength);
227  *licenseResponse = (char*) malloc(licenseDecodedLen);
228  if (*licenseResponse)
229  {
230  memcpy(*licenseResponse, licenseDecoded, licenseDecodedLen);
231  *licenseResponseLength = licenseDecodedLen;
232  }
233  else
234  {
235  AAMPLOG_ERR("SecManager failed to allocate memory for license!");
236  }
237  free(licenseDecoded);
238  ret = true;
239  }
240  else
241  {
242  AAMPLOG_ERR("SecManager license base64 decode failed!");
243  }
244  }
245  }
246  // Save session ID
247  if (*sessionId == -1)
248  {
249  *sessionId = response["sessionId"].Number();
250  }
251 
252  }
253  // TODO: Sort these values out for backward compatibility
254  if(response.HasLabel("secManagerResultContext"))
255  {
256  JsonObject resultContext = response["secManagerResultContext"].Object();
257 
258  if(resultContext.HasLabel("class"))
259  *statusCode = resultContext["class"].Number();
260  if(resultContext.HasLabel("reason"))
261  *reasonCode = resultContext["reason"].Number();
262  if(resultContext.HasLabel("businessStatus"))
263  *businessStatus = resultContext["businessStatus"].Number();
264  }
265 
266  if(!ret)
267  {
268  //As per Secmanager retry is meaningful only for
269  //Digital Rights Management Failure Class (200) or
270  //Watermarking Failure Class (300)
271  //having the reasons -
272  //DRM license service network timeout / Request/network time out (3).
273  //DRM license network connection failure/Watermark vendor-access service connection failure (4)
274  //DRM license server busy/Watermark service busy (5)
275  if((*statusCode == SECMANGER_DRM_FAILURE || *statusCode == SECMANGER_WM_FAILURE) &&
276  (*reasonCode == SECMANGER_SERVICE_TIMEOUT ||
277  *reasonCode == SECMANGER_SERVICE_CON_FAILURE ||
278  *reasonCode == SECMANGER_SERVICE_BUSY ) && retryCount < MAX_LICENSE_REQUEST_ATTEMPTS)
279  {
280  ++retryCount;
281  AAMPLOG_WARN("SecManager license request failed, response for %s : statusCode: %d, reasonCode: %d, so retrying with delay %d, retry count : %u", apiName, *statusCode, *reasonCode, sleepTime, retryCount );
282  mssleep(sleepTime);
283  }
284  else
285  {
286  AAMPLOG_ERR("SecManager license request failed, response for %s : statusCode: %d, reasonCode: %d", apiName, *statusCode, *reasonCode);
287  break;
288  }
289  }
290  else
291  {
292  AAMPLOG_INFO("SecManager license request success, response for %s : statusCode: %d, reasonCode: %d", apiName, *statusCode, *reasonCode);
293  break;
294  }
295  }
296  while(retryCount < MAX_LICENSE_REQUEST_ATTEMPTS);
297 
298  //Cleanup the shared memory after sharing it with secmanager
299  aamp_CleanUpSharedMem( shmPt_accToken, shmKey_accToken, accTokenLen);
300  aamp_CleanUpSharedMem( shmPt_contMeta, shmKey_contMeta, contMetaLen);
301  aamp_CleanUpSharedMem( shmPt_licReq, shmKey_licReq, licReqLen);
302  }
303  else
304  {
305  AAMPLOG_ERR("SecManager Failed to copy access token to the shared memory, open playback session is aborted statusCode: %d, reasonCode: %d", *statusCode, *reasonCode);
306  }
307  }
308  return ret;
309 }
310 
311 /**
312  * @brief To update session state to SecManager
313  */
314 void AampSecManager::UpdateSessionState(int64_t sessionId, bool active)
315 {
316  bool rpcResult = false;
317  JsonObject result;
318  JsonObject param;
319  param["clientId"] = "aamp";
320  param["sessionId"] = sessionId;
321  AAMPLOG_INFO("%s:%d SecManager call setPlaybackSessionState for ID: %" PRId64 " and state: %d", __FUNCTION__, __LINE__, sessionId, active);
322  if (active)
323  {
324  param["sessionState"] = "active";
325  }
326  else
327  {
328  param["sessionState"] = "inactive";
329  }
330 
331  {
332  std::lock_guard<std::mutex> lock(mSecMutex);
333  rpcResult = mSecManagerObj.InvokeJSONRPC("setPlaybackSessionState", param, result);
334  }
335 
336  if (rpcResult)
337  {
338  if (!result["success"].Boolean())
339  {
340  std::string responseStr;
341  result.ToString(responseStr);
342  AAMPLOG_ERR("%s:%d SecManager setPlaybackSessionState failed for ID: %" PRId64 ", active:%d and result: %s", __FUNCTION__, __LINE__, sessionId, active, responseStr.c_str());
343  }
344  }
345  else
346  {
347  AAMPLOG_ERR("%s:%d SecManager setPlaybackSessionState failed for ID: %" PRId64 " and active: %d", __FUNCTION__, __LINE__, sessionId, active);
348  }
349 }
350 
351 /**
352  * @brief To notify SecManager to release a session
353  */
354 void AampSecManager::ReleaseSession(int64_t sessionId)
355 {
356  bool rpcResult = false;
357  JsonObject result;
358  JsonObject param;
359  param["clientId"] = "aamp";
360  param["sessionId"] = sessionId;
361  AAMPLOG_INFO("%s:%d SecManager call closePlaybackSession for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
362 
363  {
364  std::lock_guard<std::mutex> lock(mSecMutex);
365  rpcResult = mSecManagerObj.InvokeJSONRPC("closePlaybackSession", param, result);
366  }
367 
368  if (rpcResult)
369  {
370  if (!result["success"].Boolean())
371  {
372  std::string responseStr;
373  result.ToString(responseStr);
374  AAMPLOG_ERR("%s:%d SecManager closePlaybackSession failed for ID: %" PRId64 " and result: %s", __FUNCTION__, __LINE__, sessionId, responseStr.c_str());
375  }
376  }
377  else
378  {
379  AAMPLOG_ERR("%s:%d SecManager closePlaybackSession failed for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
380  }
381  /*Clear aampInstanse pointer*/
382  mAamp = NULL;
383 }
384 
385 /**
386  * @brief To update session state to SecManager
387  */
388 bool AampSecManager::setVideoWindowSize(int64_t sessionId, int64_t video_width, int64_t video_height)
389 {
390  bool rpcResult = false;
391  JsonObject result;
392  JsonObject param;
393 
394  param["sessionId"] = sessionId;
395  param["videoWidth"] = video_width;
396  param["videoHeight"] = video_height;
397 
398  AAMPLOG_INFO("%s:%d SecManager call setVideoWindowSize for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
399  {
400  std::lock_guard<std::mutex> lock(mSecMutex);
401  rpcResult = mSecManagerObj.InvokeJSONRPC("setVideoWindowSize", param, result);
402  }
403 
404  if (rpcResult)
405  {
406  if (!result["success"].Boolean())
407  {
408  std::string responseStr;
409  result.ToString(responseStr);
410  AAMPLOG_ERR("%s:%d SecManager setVideoWindowSize failed for ID: %" PRId64 " and result: %s", __FUNCTION__, __LINE__, sessionId, responseStr.c_str());
411  rpcResult = false;
412  }
413 
414  }
415  else
416  {
417  AAMPLOG_ERR("%s:%d SecManager setVideoWindowSize failed for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
418  }
419  return rpcResult;
420 }
421 
422 /**
423  * @brief To set Playback Speed State to SecManager
424  */
425 bool AampSecManager::setPlaybackSpeedState(int64_t sessionId, int64_t playback_speed, int64_t playback_position, bool delayNeeded)
426 {
427  bool rpcResult = false;
428  JsonObject result;
429  JsonObject param;
430  //mSpeedStateMutex is used to avoid any speedstate event to go when a delayed event is in progress results change in order of event call (i.e, if user tries a trickplay within half a second of tune)
431  std::lock_guard<std::mutex> lock(mSpeedStateMutex);
432  if(delayNeeded)
433  {
434  mssleep(SECMANGER_SPEED_SET_DELAY);
435  }
436 
437  param["sessionId"] = sessionId;
438  param["playbackSpeed"] = playback_speed;
439  param["playbackPosition"] = playback_position;
440 
441  AAMPLOG_INFO("%s:%d SecManager call setPlaybackSpeedState for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
442 
443  {
444  std::lock_guard<std::mutex> lock(mSecMutex);
445  rpcResult = mSecManagerObj.InvokeJSONRPC("setPlaybackSpeedState", param, result);
446  }
447 
448  if (rpcResult)
449  {
450  if (!result["success"].Boolean())
451  {
452  std::string responseStr;
453  result.ToString(responseStr);
454  AAMPLOG_ERR("%s:%d SecManager setPlaybackSpeedState failed for ID: %" PRId64 " and result: %s", __FUNCTION__, __LINE__, sessionId, responseStr.c_str());
455  rpcResult = false;
456  }
457  }
458  else
459  {
460  AAMPLOG_ERR("%s:%d SecManager setPlaybackSpeedState failed for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
461  }
462  return rpcResult;
463 }
464 
465 
466 /**
467  * @brief To Load ClutWatermark
468  */
469 bool AampSecManager::loadClutWatermark(int64_t sessionId, int64_t graphicId, int64_t watermarkClutBufferKey, int64_t watermarkImageBufferKey, int64_t clutPaletteSize,
470  const char* clutPaletteFormat, int64_t watermarkWidth, int64_t watermarkHeight, float aspectRatio)
471 {
472  bool rpcResult = false;
473  JsonObject result;
474  JsonObject param;
475 
476  param["sessionId"] = sessionId;
477  param["graphicId"] = graphicId;
478  param["watermarkClutBufferKey"] = watermarkClutBufferKey;
479  param["watermarkImageBufferKey"] = watermarkImageBufferKey;
480  param["clutPaletteSize"] = clutPaletteSize;
481  param["clutPaletteFormat"] = clutPaletteFormat;
482  param["watermarkWidth"] = watermarkWidth;
483  param["watermarkHeight"] = watermarkHeight;
484  param["aspectRatio"] = std::to_string(aspectRatio).c_str();
485 
486  AAMPLOG_INFO("%s:%d SecManager call loadClutWatermark for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
487 
488  {
489  std::lock_guard<std::mutex> lock(mSecMutex);
490  rpcResult = mSecManagerObj.InvokeJSONRPC("loadClutWatermark", param, result);
491  }
492 
493  if (rpcResult)
494  {
495  if (!result["success"].Boolean())
496  {
497  std::string responseStr;
498  result.ToString(responseStr);
499  AAMPLOG_ERR("%s:%d SecManager loadClutWatermark failed for ID: %" PRId64 " and result: %s", __FUNCTION__, __LINE__, sessionId, responseStr.c_str());
500  rpcResult = false;
501  }
502  }
503  else
504  {
505  AAMPLOG_ERR("%s:%d SecManager loadClutWatermark failed for ID: %" PRId64 "", __FUNCTION__, __LINE__, sessionId);
506  }
507  return rpcResult;
508 
509 }
510 
511 /**
512  * @brief Registers Event to input plugin and to mRegisteredEvents list for later use.
513  */
514 void AampSecManager::RegisterEvent (string eventName, std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> functionHandler)
515 {
516  bool bSubscribed;
517  bSubscribed = mSecManagerObj.SubscribeEvent(_T(eventName), functionHandler);
518  if(bSubscribed)
519  {
520  mRegisteredEvents.push_back(eventName);
521  }
522 }
523 
524 /**
525  * @brief Registers all Events to input plugin
526  */
528 {
529  std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> watermarkSessionMethod = std::bind(&AampSecManager::watermarkSessionHandler, this, std::placeholders::_1);
530 
531  RegisterEvent("onWatermarkSession",watermarkSessionMethod);
532 
533  std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> addWatermarkMethod = std::bind(&AampSecManager::addWatermarkHandler, this, std::placeholders::_1);
534 
535  RegisterEvent("onAddWatermark",addWatermarkMethod);
536 
537  std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> updateWatermarkMethod = std::bind(&AampSecManager::updateWatermarkHandler, this, std::placeholders::_1);
538 
539  RegisterEvent("onUpdateWatermark",updateWatermarkMethod);
540 
541  std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> removeWatermarkMethod = std::bind(&AampSecManager::removeWatermarkHandler, this, std::placeholders::_1);
542 
543  RegisterEvent("onRemoveWatermark",removeWatermarkMethod);
544 
545  std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> showWatermarkMethod = std::bind(&AampSecManager::showWatermarkHandler, this, std::placeholders::_1);
546 
547  RegisterEvent("onDisplayWatermark",showWatermarkMethod);
548 
549 }
550 
551 /**
552  * @brief UnRegisters all Events from plugin
553  */
555 {
556  for (auto const& evtName : mRegisteredEvents) {
557  mSecManagerObj.UnSubscribeEvent(_T(evtName));
558  }
559  mRegisteredEvents.clear();
560 }
561 
562 /**
563  * @brief Detects watermarking session conditions
564  */
565 void AampSecManager::watermarkSessionHandler(const JsonObject& parameters)
566 {
567  std::string param;
568  parameters.ToString(param);
569  AAMPLOG_WARN("AampSecManager::%s:%d i/p params: %s", __FUNCTION__, __LINE__, param.c_str());
570  if(NULL != mAamp)
571  {
572  mAamp->SendWatermarkSessionUpdateEvent( parameters["sessionId"].Number(),parameters["conditionContext"].Number(),parameters["watermarkingSystem"].String());
573  }
574 }
575 
576 /**
577  * @brief Gets watermark image details and manages watermark rendering
578  */
579 void AampSecManager::addWatermarkHandler(const JsonObject& parameters)
580 {
581 #ifdef DEBUG_SECMAMANER
582  std::string param;
583  parameters.ToString(param);
584 
585  AAMPLOG_WARN("AampSecManager::%s:%d i/p params: %s", __FUNCTION__, __LINE__, param.c_str());
586 #endif
587  if(mSchedulerStarted)
588  {
589  int graphicId = parameters["graphicId"].Number();
590  int zIndex = parameters["zIndex"].Number();
591  AAMPLOG_WARN("AampSecManager::%s:%d graphicId : %d index : %d ", __FUNCTION__, __LINE__, graphicId, zIndex);
592  ScheduleTask(AsyncTaskObj([graphicId, zIndex](void *data)
593  {
594  AampSecManager *instance = static_cast<AampSecManager *>(data);
595  instance->CreateWatermark(graphicId, zIndex);
596  }, (void *) this));
597 
598  int smKey = parameters["graphicImageBufferKey"].Number();
599  int smSize = parameters["graphicImageSize"].Number();/*ToDo : graphicImageSize (long) long conversion*/
600  AAMPLOG_WARN("AampSecManager::%s:%d graphicId : %d smKey: %d smSize: %d", __FUNCTION__, __LINE__, graphicId, smKey, smSize);
601  ScheduleTask(AsyncTaskObj([graphicId, smKey, smSize](void *data)
602  {
603  AampSecManager *instance = static_cast<AampSecManager *>(data);
604  instance->UpdateWatermark(graphicId, smKey, smSize);
605  }, (void *) this));
606 
607  if (parameters["adjustVisibilityRequired"].Boolean())
608  {
609  int sessionId = parameters["sessionId"].Number();
610  AAMPLOG_WARN("AampSecManager::%s:%d adjustVisibilityRequired is true, invoking GetWaterMarkPalette. graphicId : %d", __FUNCTION__, __LINE__, graphicId);
611  ScheduleTask(AsyncTaskObj([sessionId, graphicId](void *data)
612  {
613  AampSecManager *instance = static_cast<AampSecManager *>(data);
614  instance->GetWaterMarkPalette(sessionId, graphicId);
615  }, (void *) this));
616  }
617  else
618  {
619  AAMPLOG_WARN("AampSecManager::%s:%d adjustVisibilityRequired is false, graphicId : %d", __FUNCTION__, __LINE__, graphicId);
620  }
621 
622 #if 0
623  /*Method to be called only if RDKShell is used for rendering*/
624  ScheduleTask(AsyncTaskObj([show](void *data)
625  {
626  AampSecManager *instance = static_cast<AampSecManager *>(data);
627  instance->AlwaysShowWatermarkOnTop(show);
628  }, (void *) this));
629 #endif
630  }
631 }
632 
633 /**
634  * @brief Gets updated watermark image details and manages watermark rendering
635  */
636 void AampSecManager::updateWatermarkHandler(const JsonObject& parameters)
637 {
638  if(mSchedulerStarted)
639  {
640  int graphicId = parameters["graphicId"].Number();
641  int clutKey = parameters["watermarkClutBufferKey"].Number();
642  int imageKey = parameters["watermarkImageBufferKey"].Number();
643  AAMPLOG_TRACE("graphicId : %d ",graphicId);
644  ScheduleTask(AsyncTaskObj([graphicId, clutKey, imageKey](void *data)
645  {
646  AampSecManager *instance = static_cast<AampSecManager *>(data);
647  instance->ModifyWatermarkPalette(graphicId, clutKey, imageKey);
648  }, (void *) this));
649  }
650 }
651 
652 /**
653  * @brief Removes watermark image
654  */
655 void AampSecManager::removeWatermarkHandler(const JsonObject& parameters)
656 {
657 #ifdef DEBUG_SECMAMANER
658  std::string param;
659  parameters.ToString(param);
660  AAMPLOG_WARN("AampSecManager::%s:%d i/p params: %s", __FUNCTION__, __LINE__, param.c_str());
661 #endif
662  if(mSchedulerStarted)
663  {
664  int graphicId = parameters["graphicId"].Number();
665  AAMPLOG_WARN("AampSecManager::%s:%d graphicId : %d ", __FUNCTION__, __LINE__, graphicId);
666  ScheduleTask(AsyncTaskObj([graphicId](void *data)
667  {
668  AampSecManager *instance = static_cast<AampSecManager *>(data);
669  instance->DeleteWatermark(graphicId);
670  }, (void *) this));
671 #if 0
672  /*Method to be called only if RDKShell is used for rendering*/
673  ScheduleTask(AsyncTaskObj([show](void *data)
674  {
675  AampSecManager *instance = static_cast<AampSecManager *>(data);
676  instance->AlwaysShowWatermarkOnTop(show);
677  }, (void *) this));
678 #endif
679  }
680 
681 }
682 
683 /**
684  * @brief Handles watermark calls to be only once
685  */
686 void AampSecManager::showWatermarkHandler(const JsonObject& parameters)
687 {
688  bool show = true;
689  if(parameters["hideWatermark"].Boolean())
690  {
691  show = false;
692  }
693  AAMPLOG_INFO("Received onDisplayWatermark, show: %d ", show);
694  if(mSchedulerStarted)
695  {
696  ScheduleTask(AsyncTaskObj([show](void *data)
697  {
698  AampSecManager *instance = static_cast<AampSecManager *>(data);
699  instance->ShowWatermark(show);
700  }, (void *) this));
701  }
702 
703  return;
704 }
705 
706 
707 /**
708  * @brief Show watermark image
709  */
711 {
712  JsonObject param;
713  JsonObject result;
714  bool rpcResult = false;
715 
716  AAMPLOG_ERR("AampSecManager %s:%d ", __FUNCTION__, __LINE__);
717  param["show"] = show;
718  {
719  std::lock_guard<std::mutex> lock(mWatMutex);
720  rpcResult = mWatermarkPluginObj.InvokeJSONRPC("showWatermark", param, result);
721  }
722  if (rpcResult)
723  {
724  if (!result["success"].Boolean())
725  {
726  std::string responseStr;
727  result.ToString(responseStr);
728  AAMPLOG_ERR("AampSecManager::%s failed and result: %s", __FUNCTION__, responseStr.c_str());
729  }
730  }
731  else
732  {
733  AAMPLOG_ERR("AampSecManager::%s thunder invocation failed!", __FUNCTION__);
734  }
735 
736  return;
737 }
738 
739 /**
740  * @brief Create Watermark
741  */
742 void AampSecManager::CreateWatermark(int graphicId, int zIndex )
743 {
744  JsonObject param;
745  JsonObject result;
746  bool rpcResult = false;
747 
748  AAMPLOG_ERR("AampSecManager %s:%d ", __FUNCTION__, __LINE__);
749  param["id"] = graphicId;
750  param["zorder"] = zIndex;
751  {
752  std::lock_guard<std::mutex> lock(mWatMutex);
753  rpcResult = mWatermarkPluginObj.InvokeJSONRPC("createWatermark", param, result);
754  }
755  if (rpcResult)
756  {
757  if (!result["success"].Boolean())
758  {
759  std::string responseStr;
760  result.ToString(responseStr);
761  AAMPLOG_ERR("AampSecManager::%s failed and result: %s", __FUNCTION__, responseStr.c_str());
762  }
763  }
764  else
765  {
766  AAMPLOG_ERR("AampSecManager::%s thunder invocation failed!", __FUNCTION__);
767  }
768  return;
769 }
770 
771 /**
772  * @brief Delete Watermark
773  */
775 {
776  JsonObject param;
777  JsonObject result;
778  bool rpcResult = false;
779 
780  AAMPLOG_ERR("AampSecManager %s:%d ", __FUNCTION__, __LINE__);
781  param["id"] = graphicId;
782  {
783  std::lock_guard<std::mutex> lock(mWatMutex);
784  rpcResult = mWatermarkPluginObj.InvokeJSONRPC("deleteWatermark", param, result);
785  }
786  if (rpcResult)
787  {
788  if (!result["success"].Boolean())
789  {
790  std::string responseStr;
791  result.ToString(responseStr);
792  AAMPLOG_ERR("AampSecManager::%s failed and result: %s", __FUNCTION__, responseStr.c_str());
793  }
794  }
795  else
796  {
797  AAMPLOG_ERR("AampSecManager::%s thunder invocation failed!", __FUNCTION__);
798  }
799 
800  return;
801 }
802 
803 /**
804  * @brief Update Wateramrk
805  */
806 void AampSecManager::UpdateWatermark(int graphicId, int smKey, int smSize )
807 {
808  JsonObject param;
809  JsonObject result;
810  bool rpcResult = false;
811 
812  AAMPLOG_ERR("AampSecManager %s:%d ", __FUNCTION__, __LINE__);
813  param["id"] = graphicId;
814  param["key"] = smKey;
815  param["size"] = smSize;
816  {
817  std::lock_guard<std::mutex> lock(mWatMutex);
818  rpcResult = mWatermarkPluginObj.InvokeJSONRPC("updateWatermark", param, result);
819  }
820  if (rpcResult)
821  {
822  if (!result["success"].Boolean())
823  {
824  std::string responseStr;
825  result.ToString(responseStr);
826  AAMPLOG_ERR("AampSecManager::%s failed and result: %s", __FUNCTION__, responseStr.c_str());
827  }
828  }
829  else
830  {
831  AAMPLOG_ERR("AampSecManager::%s thunder invocation failed!", __FUNCTION__);
832  }
833 
834  return;
835 }
836 
837 /**
838  * @brief Show watermark image
839  * This method need to be used only when RDKShell is used for rendering. Not supported by Watermark Plugin
840  */
842 {
843  JsonObject param;
844  JsonObject result;
845  bool rpcResult = false;
846 
847  AAMPLOG_ERR("AampSecManager %s:%d ", __FUNCTION__, __LINE__);
848  param["show"] = show;
849  {
850  std::lock_guard<std::mutex> lock(mWatMutex);
851  rpcResult = mWatermarkPluginObj.InvokeJSONRPC("alwaysShowWatermarkOnTop", param, result);
852  }
853  if (rpcResult)
854  {
855  if (!result["success"].Boolean())
856  {
857  std::string responseStr;
858  result.ToString(responseStr);
859  AAMPLOG_ERR("AampSecManager::%s failed and result: %s", __FUNCTION__, responseStr.c_str());
860  }
861  }
862  else
863  {
864  AAMPLOG_ERR("AampSecManager::%s thunder invocation failed!", __FUNCTION__);
865  }
866 }
867 
868 /**
869  * @brief GetWaterMarkPalette
870  */
871 void AampSecManager::GetWaterMarkPalette(int sessionId, int graphicId)
872 {
873  JsonObject param;
874  JsonObject result;
875  bool rpcResult = false;
876  param["id"] = graphicId;
877  AAMPLOG_WARN("AampSecManager %s:%d Graphic id: %d ", __FUNCTION__, __LINE__, graphicId);
878  {
879  std::lock_guard<std::mutex> lock(mWatMutex);
880  rpcResult = mWatermarkPluginObj.InvokeJSONRPC("getPalettedWatermark", param, result);
881  }
882 
883  if (rpcResult)
884  {
885  if (!result["success"].Boolean())
886  {
887  std::string responseStr;
888  result.ToString(responseStr);
889  AAMPLOG_ERR("AampSecManager::%s failed and result: %s", __FUNCTION__, responseStr.c_str());
890  }
891  else //if success, request sec manager to load the clut into the clut shm
892  {
893 
894  AAMPLOG_WARN("AampSecManager::%s getWatermarkPalette invoke success for graphicId %d, calling loadClutWatermark", __FUNCTION__, graphicId);
895  int clutKey = result["clutKey"].Number();
896  int clutSize = result["clutSize"].Number();
897  int imageKey = result["imageKey"].Number();
898  int imageWidth = result["imageWidth"].Number();
899  int imageHeight = result["imageHeight"].Number();
900  float aspectRatio = (imageHeight != 0) ? (float)imageWidth/(float)imageHeight : 0.0;
901  AampSecManager::GetInstance()->loadClutWatermark(sessionId, graphicId, clutKey, imageKey,
902  clutSize, "RGBA8888", imageWidth, imageHeight,
903  aspectRatio);
904  }
905 
906  }
907  else
908  {
909  AAMPLOG_ERR("AampSecManager::%s thunder invocation failed!", __FUNCTION__);
910  }
911 }
912 
913 /**
914  * @brief ModifyWatermarkPalette
915  */
916 void AampSecManager::ModifyWatermarkPalette(int graphicId, int clutKey, int imageKey)
917 {
918  JsonObject param;
919  JsonObject result;
920 
921  bool rpcResult = false;
922  param["id"] = graphicId;
923  param["clutKey"] = clutKey;
924  param["imageKey"] = imageKey;
925  {
926  std::lock_guard<std::mutex> lock(mWatMutex);
927  rpcResult = mWatermarkPluginObj.InvokeJSONRPC("modifyPalettedWatermark", param, result);
928  }
929  if (rpcResult)
930  {
931  if (!result["success"].Boolean())
932  {
933  std::string responseStr;
934  result.ToString(responseStr);
935  AAMPLOG_ERR("AampSecManager modifyPalettedWatermark failed with result: %s, graphic id: %d", responseStr.c_str(), graphicId);
936  }
937  else
938  {
939  AAMPLOG_TRACE("AampSecManager modifyPalettedWatermark invoke success, graphic id: %d", graphicId);
940  }
941  }
942  else
943  {
944  AAMPLOG_ERR("AampSecManager Thunder invocation for modifyPalettedWatermark failed!, graphic id: %d", graphicId);
945  }
946 }
AampSecManager::ModifyWatermarkPalette
void ModifyWatermarkPalette(int graphicId, int clutKey, int imageKey)
ModifyWatermarkPalette.
Definition: AampSecManager.cpp:916
AampScheduler::ScheduleTask
int ScheduleTask(AsyncTaskObj obj)
To schedule a task to be executed later.
Definition: AampScheduler.cpp:64
AampSecManager::GetWaterMarkPalette
void GetWaterMarkPalette(int sessionId, int graphicId)
GetWaterMarkPalette.
Definition: AampSecManager.cpp:871
eAAMPConfig_LicenseRetryWaitTime
@ eAAMPConfig_LicenseRetryWaitTime
Definition: AampConfig.h:227
AampSecManager::RegisterAllEvents
void RegisterAllEvents()
Registers all Events to input plugin.
Definition: AampSecManager.cpp:527
AampSecManager::mSpeedStateMutex
std::mutex mSpeedStateMutex
Definition: AampSecManager.h:220
AampSecManager::addWatermarkHandler
void addWatermarkHandler(const JsonObject &parameters)
Gets watermark image details and manages watermark rendering.
Definition: AampSecManager.cpp:579
AampSecManager::ReleaseSession
void ReleaseSession(int64_t sessionId)
To notify SecManager to release a session.
Definition: AampSecManager.cpp:354
AampSecManager::~AampSecManager
~AampSecManager()
AampScheduler Destructor.
Definition: AampSecManager.cpp:83
SECMANGER_DRM_GEN_FAILURE
#define SECMANGER_DRM_GEN_FAILURE
Definition: AampDefine.h:168
AampSecManager::UpdateWatermark
void UpdateWatermark(int graphicId, int smKey, int smSize)
Update Wateramrk.
Definition: AampSecManager.cpp:806
ThunderAccessAAMP::SubscribeEvent
bool SubscribeEvent(string eventName, std::function< void(const WPEFramework::Core::JSON::VariantContainer &)> functionHandler)
subscribeEvent
Definition: ThunderAccess.cpp:153
AampSecManager::setVideoWindowSize
bool setVideoWindowSize(int64_t sessionId, int64_t video_width, int64_t video_height)
To update session state to SecManager.
Definition: AampSecManager.cpp:388
SECMANGER_WM_FAILURE
#define SECMANGER_WM_FAILURE
Definition: AampDefine.h:165
AampSecManager::watermarkSessionHandler
void watermarkSessionHandler(const JsonObject &parameters)
Detects watermarking session conditions.
Definition: AampSecManager.cpp:565
PrivateInstanceAAMP::SendWatermarkSessionUpdateEvent
void SendWatermarkSessionUpdateEvent(uint32_t sessionHandle, uint32_t status, const std::string &system)
Generate WatermarkSessionUpdate event based on args passed.
Definition: priv_aamp.cpp:8826
AampSecManager::mInstance
static AampSecManager * mInstance
Definition: AampSecManager.h:214
AampSecManager::UpdateSessionState
void UpdateSessionState(int64_t sessionId, bool active)
To update session state to SecManager.
Definition: AampSecManager.cpp:314
AampSecManager::DestroyInstance
static void DestroyInstance()
To release AampSecManager singelton instance.
Definition: AampSecManager.cpp:47
AampSecManager::DeleteWatermark
void DeleteWatermark(int graphicId)
Delete Watermark.
Definition: AampSecManager.cpp:774
AampSecManager.h
Class to communicate with SecManager Thunder plugin.
AampScheduler::StartScheduler
void StartScheduler()
To start scheduler thread.
Definition: AampScheduler.cpp:52
AampSecManager
Class to get License from Sec Manager.
Definition: AampSecManager.h:41
ThunderAccessAAMP::UnSubscribeEvent
bool UnSubscribeEvent(string eventName)
unSubscribeEvent
Definition: ThunderAccess.cpp:178
AampSecManager::mAamp
PrivateInstanceAAMP * mAamp
Definition: AampSecManager.h:215
AampSecManager::CreateWatermark
void CreateWatermark(int graphicId, int zIndex)
Create Watermark.
Definition: AampSecManager.cpp:742
AsyncTaskObj
Async task operations.
Definition: AampScheduler.h:57
ThunderAccessAAMP::InvokeJSONRPC
bool InvokeJSONRPC(std::string method, const JsonObject &param, JsonObject &result, const uint32_t waitTime=5000)
invokeJSONRPC
Definition: ThunderAccess.cpp:196
AampSecManager::updateWatermarkHandler
void updateWatermarkHandler(const JsonObject &parameters)
Gets updated watermark image details and manages watermark rendering.
Definition: AampSecManager.cpp:636
AampSecManager::mSecMutex
std::mutex mSecMutex
Definition: AampSecManager.h:218
AampSecManager::showWatermarkHandler
void showWatermarkHandler(const JsonObject &parameters)
Handles watermark calls to be only once.
Definition: AampSecManager.cpp:686
_base64.h
base64 source Encoder/Decoder
AampSecManager::AampSecManager
AampSecManager()
AampScheduler Constructor.
Definition: AampSecManager.cpp:59
AampSecManager::GetInstance
static AampSecManager * GetInstance()
To get AampSecManager instance.
Definition: AampSecManager.cpp:35
AampScheduler::StopScheduler
void StopScheduler()
To stop scheduler and associated resources.
Definition: AampScheduler.cpp:173
AampSecManager::AlwaysShowWatermarkOnTop
void AlwaysShowWatermarkOnTop(bool show)
Show watermark image This method need to be used only when RDKShell is used for rendering....
Definition: AampSecManager.cpp:841
AampSecManager::loadClutWatermark
bool loadClutWatermark(int64_t sessionId, int64_t graphicId, int64_t watermarkClutBufferKey, int64_t watermarkImageBufferKey, int64_t clutPaletteSize, const char *clutPaletteFormat, int64_t watermarkWidth, int64_t watermarkHeight, float aspectRatio)
To Load ClutWatermark.
Definition: AampSecManager.cpp:469
AAMPLOG_TRACE
#define AAMPLOG_TRACE(FORMAT,...)
AAMP logging defines, this can be enabled through setLogLevel() as per the need.
Definition: AampLogManager.h:83
PrivateInstanceAAMP
Class representing the AAMP player's private instance, which is not exposed to outside world.
Definition: priv_aamp.h:640
AampSecManager::removeWatermarkHandler
void removeWatermarkHandler(const JsonObject &parameters)
Removes watermark image.
Definition: AampSecManager.cpp:655
AampSecManager::UnRegisterAllEvents
void UnRegisterAllEvents()
UnRegisters all Events from plugin.
Definition: AampSecManager.cpp:554
AampSecManager::setPlaybackSpeedState
bool setPlaybackSpeedState(int64_t sessionId, int64_t playback_speed, int64_t playback_position, bool delayNeeded=false)
To set Playback Speed State to SecManager.
Definition: AampSecManager.cpp:425
AampSecManager::mWatMutex
std::mutex mWatMutex
Definition: AampSecManager.h:219
AampSecManager::RegisterEvent
void RegisterEvent(string eventName, std::function< void(const WPEFramework::Core::JSON::VariantContainer &)> functionHandler)
Registers Event to input plugin and to mRegisteredEvents list for later use.
Definition: AampSecManager.cpp:514
AampSecManager::mSecManagerObj
ThunderAccessAAMP mSecManagerObj
Definition: AampSecManager.h:216
ThunderAccessAAMP::ActivatePlugin
bool ActivatePlugin()
ActivatePlugin.
Definition: ThunderAccess.cpp:120
mssleep
void mssleep(int milliseconds)
Sleep for given milliseconds.
Definition: AampUtils.cpp:1021
AampSecManager::ShowWatermark
void ShowWatermark(bool show)
Show watermark image.
Definition: AampSecManager.cpp:710
AampSecManager::mWatermarkPluginObj
ThunderAccessAAMP mWatermarkPluginObj
Definition: AampSecManager.h:217
AampSecManager::AcquireLicense
bool AcquireLicense(PrivateInstanceAAMP *aamp, const char *licenseUrl, const char *moneyTraceMetdata[][2], const char *accessAttributes[][2], const char *contentMetadata, size_t contentMetadataLen, const char *licenseRequest, size_t licenseRequestLen, const char *keySystemId, const char *mediaUsage, const char *accessToken, size_t accessTokenLen, int64_t *sessionId, char **licenseResponse, size_t *licenseResponseLength, int32_t *statusCode, int32_t *reasonCode, int32_t *businessStatus)
To acquire license from SecManager.
Definition: AampSecManager.cpp:100
base64_Decode
unsigned char * base64_Decode(const char *src, size_t *len, size_t srcLen)
decode base64 encoded data to binary equivalent
Definition: _base64.cpp:91