RDK Documentation (Open Sourced RDK Components)
admanager_mpd.h
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 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 
20 /**
21  * @file admanager_mpd.h
22  * @brief Client side DAI manger for MPEG DASH
23  */
24 
25 #ifndef ADMANAGER_MPD_H_
26 #define ADMANAGER_MPD_H_
27 
28 #include "AdManagerBase.h"
29 #include <string>
30 #include "libdash/INode.h"
31 #include "libdash/IDASHManager.h"
32 #include "libdash/xml/Node.h"
33 #include "libdash/IMPD.h"
34 
35 using namespace dash;
36 using namespace std;
37 using namespace dash::mpd;
38 
39 /**
40  * @class CDAIObjectMPD
41  *
42  * @brief Client Side DAI object implementation for DASH
43  */
45 {
46  class PrivateCDAIObjectMPD* mPrivObj; /**< Private instance of Client Side DAI object for DASH */
47 public:
48 
49  /**
50  * @fn CDAIObjectMPD
51  *
52  * @param[in] aamp - Pointer to PrivateInstanceAAMP
53  */
55 
56  /**
57  * @fn ~CDAIObjectMPD
58  */
59  virtual ~CDAIObjectMPD();
60 
61  /**
62  * @brief CDAIObject Copy Constructor
63  */
64  CDAIObjectMPD(const CDAIObjectMPD&) = delete;
65 
66  /**
67  * @brief CDAIObject assignment operator overloading
68  */
69  CDAIObjectMPD& operator= (const CDAIObjectMPD&) = delete;
70 
71  /**
72  * @brief Getter for the PrivateCDAIObjectMPD instance
73  *
74  * @return PrivateCDAIObjectMPD object pointer
75  */
77  {
78  return mPrivObj;
79  }
80 
81  /**
82  * @fn SetAlternateContents
83  *
84  * @param[in] periodId - Adbreak's unique identifier; the first period id
85  * @param[in] adId - Individual Ad's id
86  * @param[in] url - Ad URL
87  * @param[in] startMS - Ad start time in milliseconds
88  * @param[in] breakdur - Adbreak's duration in MS
89  */
90  virtual void SetAlternateContents(const std::string &periodId, const std::string &adId, const std::string &url, uint64_t startMS=0, uint32_t breakdur=0) override;
91 };
92 
93 
94 /**
95  * @brief Events to the Ad manager's state machine
96  */
97 enum class AdEvent
98 {
99  INIT, /**< Playback initialization */
100  BASE_OFFSET_CHANGE, /**< Base period's offset change */
101  AD_FINISHED, /**< Ad playback finished */
102  AD_FAILED, /**< Ad playback failed */
103  PERIOD_CHANGE, /**< Period changed */
104  DEFAULT = PERIOD_CHANGE /**< Default event */
105 };
106 
107 #define OFFSET_ALIGN_FACTOR 2000 /**< Observed minor slacks in the ad durations. Align factor used to place the ads correctly. */
108 
109 /**
110  * @struct AdNode
111  * @brief Individual Ad's meta info
112  */
113 struct AdNode {
114  bool invalid; /**< Failed to playback failed once, no need to attempt later */
115  bool placed; /**< Ad completely placed on the period */
116  std::string adId; /**< Ad's identifier */
117  std::string url; /**< Ad's manifest URL */
118  uint64_t duration; /**< Duration of the Ad */
119  std::string basePeriodId; /**< Id of the base period at the beginning of the Ad */
120  int basePeriodOffset; /**< Offset of the base period at the beginning of the Ad */
121  MPD* mpd; /**< Pointer to the MPD object */
122 
123  /**
124  * @brief AdNode default constructor
125  */
126  AdNode() : invalid(false), placed(false), adId(), url(), duration(0), basePeriodId(), basePeriodOffset(0), mpd(nullptr)
127  {
128 
129  }
130 
131  /**
132  * @brief AdNode constructor
133  *
134  * @param[in] invalid - Is Ad valid
135  * @param[in] placed - Is Ad completed placed over underlying period
136  * @param[in] adId - Ad identifier
137  * @param[in] url - Ad's manifest URL
138  * @param[in] duration - Duration of the Ad
139  * @param[in] basePeriodId - Base period id of the Ad
140  * @param[in] basePeriodOffset - Base period offset of the Ad
141  * @param[in] mpd - Pointer to the MPD object
142  */
143  AdNode(bool invalid, bool placed, std::string adId, std::string url, uint64_t duration,
144  std::string basePeriodId, int basePeriodOffset, MPD* mpd)
145  : invalid(invalid), placed(placed), adId(adId), url(url), duration(duration), basePeriodId(basePeriodId),
146  basePeriodOffset(basePeriodOffset), mpd(mpd)
147  {
148 
149  }
150 
151  /**
152  * @brief AdNode Copy Constructor
153  *
154  * @param[in] adNode - Reference to the source AdNode
155  */
156  AdNode(const AdNode& adNode) : invalid(adNode.invalid), placed(adNode.placed), adId(adNode.adId),
157  url(adNode.url), duration(adNode.duration), basePeriodId(adNode.basePeriodId),
158  basePeriodOffset(adNode.basePeriodOffset), mpd(adNode.mpd)
159  {
160  }
161 
162  /**
163  * @brief AdNode assignment operator overloading
164  */
165  AdNode& operator=(const AdNode&) = delete;
166 };
167 /**
168  * @struct AdBreakObject
169  *
170  * @brief AdBreak's metadata object
171  */
173  uint32_t brkDuration; /**< Adbreak's duration */
174  std::shared_ptr<std::vector<AdNode>> ads; /**< Ads in the Adbreak in sequential order */
175  std::string endPeriodId; /**< Base period's id after the adbreak playback */
176  uint64_t endPeriodOffset; /**< Base period's offset after the adbreak playback */
177  uint32_t adsDuration; /**< Ads' duration in the Adbreak */
178  bool adjustEndPeriodOffset; /**< endPeriodOffset needs be re-adjusted or not */
179 
180  /**
181  * @brief AdBreakObject default constructor
182  */
183  AdBreakObject() : brkDuration(0), ads(), endPeriodId(), endPeriodOffset(0), adsDuration(0), adjustEndPeriodOffset(false)
184  {
185  }
186 
187  /**
188  * @brief AdBreakObject constructor
189  *
190  * @param[in] _duration - Adbreak's duration
191  * @param[in] _ads - Ads in the Adbreak
192  * @param[in] _endPeriodId - Base period's id after the adbreak playback
193  * @param[in] _endPeriodOffset - Base period's offset after the adbreak playback
194  * @param[in] _adsDuration - Ads' duration in the Adbreak
195  */
196  AdBreakObject(uint32_t _duration, std::shared_ptr<std::vector<AdNode>> _ads, std::string _endPeriodId,
197  uint64_t _endPeriodOffset, uint32_t _adsDuration)
198  : brkDuration(_duration), ads(_ads), endPeriodId(_endPeriodId), endPeriodOffset(_endPeriodOffset), adsDuration(_adsDuration), adjustEndPeriodOffset(false)
199  {
200  }
201 };
202 
203 /**
204  * @struct AdOnPeriod
205  *
206  * @brief Individual Ad's object placed over the period
207  */
209 {
210  int32_t adIdx; /**< Ad's idx (of vector) */
211  uint32_t adStartOffset; /**< Ad's start offset */
212 };
213 
214 /**
215  * @struct Period2AdData
216  * @brief Meta info corresponding to each period.
217  */
219  bool filled; /**< Period filled with ads or not */
220  std::string adBreakId; /**< Parent Adbreak */
221  uint64_t duration; /**< Period's Duration */
222  std::map<int, AdOnPeriod> offset2Ad; /**< Mapping period's offset to individual Ads */
223 
224  /**
225  * @brief Period2AdData constructor
226  */
227  Period2AdData() : filled(false), adBreakId(), duration(0), offset2Ad()
228  {
229  }
230 };
231 
232 /**
233  * @struct AdFulfillObj
234  *
235  * @brief Temporary object representing currently fulfilling ad (given by setAlternateContent).
236  */
237 struct AdFulfillObj {
238  std::string periodId; /**< Currently fulfilling adbreak id */
239  std::string adId; /**< Currently placing Ad id */
240  std::string url; /**< Current Ad's URL */
241 
242  /**
243  * @brief AdFulfillObj constructor
244  */
245  AdFulfillObj() : periodId(), adId(), url()
246  {
247 
248  }
249 };
250 
251 /**
252  * @struct PlacementObj
253  *
254  * @brief Currently placing Ad's object
255  */
256 struct PlacementObj {
257  std::string pendingAdbrkId; /**< Only one Adbreak will be pending for replacement */
258  std::string openPeriodId; /**< The period in the adbreak that is progressing */
259  uint64_t curEndNumber; /**< Current periods last fragment number */
260  int curAdIdx; /**< Currently placing ad, during MPD progression */
261  uint32_t adNextOffset; /**< Current Ad's offset to be placed in the next iteration of PlaceAds */
262 
263  /**
264  * @brief PlacementObj constructor
265  */
266  PlacementObj(): pendingAdbrkId(), openPeriodId(), curEndNumber(0), curAdIdx(-1), adNextOffset(0)
267  {
268 
269  }
270 };
271 
272 
273 /**
274  * @class PrivateCDAIObjectMPD
275  *
276  * @brief Private Client Side DAI object for DASH
277  */
279 {
280 public:
281  AampLogManager* mLogObj;
282  PrivateInstanceAAMP* mAamp; /**< AAMP player's private instance */
283  std::mutex mDaiMtx; /**< Mutex protecting DAI critical section */
284  bool mIsFogTSB; /**< Channel playing from TSB or not */
285  std::unordered_map<std::string, AdBreakObject> mAdBreaks; /**< Periodid to adbreakobject map*/
286  std::unordered_map<std::string, Period2AdData> mPeriodMap; /**< periodId to Ad map */
287  std::string mCurPlayingBreakId; /**< Currently playing Ad */
288  pthread_t mAdObjThreadID; /**< ThreadId of Ad fulfillment */
289  bool mAdObjThreadStarted; /**< Flag denotes if ad object thread is started */
290  bool mAdFailed; /**< Current Ad playback failed flag */
291  std::shared_ptr<std::vector<AdNode>> mCurAds; /**< Vector of ads from the current Adbreak */
292  int mCurAdIdx; /**< Currently playing Ad index */
293  AdFulfillObj mAdFulfillObj; /**< Temporary object for Ad fulfillment (to pass to the fulfillment thread) */
294  PlacementObj mPlacementObj; /**< Temporary object for Ad placement over period */
295  PlacementObj mAdtoInsertInNextBreak;
296  double mContentSeekOffset; /**< Seek offset after the Ad playback */
297  AdState mAdState; /**< Current state of the CDAI state machine */
298  /**
299  * @fn PrivateCDAIObjectMPD
300  *
301  * @param[in] aamp - Pointer to PrivateInstanceAAMP
302  */
304 
305  /**
306  * @fn ~PrivateCDAIObjectMPD
307  */
309 
310  /**
311  * @brief PrivateCDAIObjectMPD copy constructor
312  */
314 
315  /**
316  * @brief PrivateCDAIObjectMPD assignment operator
317  */
318  PrivateCDAIObjectMPD& operator= (const PrivateCDAIObjectMPD&) = delete;
319 
320  /**
321  * @fn SetAlternateContents
322  *
323  * @param[in] periodId - Adbreak's unique identifier.
324  * @param[in] adId - Individual Ad's id
325  * @param[in] url - Ad URL
326  * @param[in] startMS - Ad start time in milliseconds
327  * @param[in] breakdur - Adbreak's duration in MS
328  */
329  void SetAlternateContents(const std::string &periodId, const std::string &adId, const std::string &url, uint64_t startMS, uint32_t breakdur=0);
330 
331  /**
332  * @fn FulFillAdObject
333  */
334  void FulFillAdObject();
335 
336  /**
337  * @fn GetAdMPD
338  *
339  * @param[in] url - Ad manifest's URL
340  * @param[out] finalManifest - Is final MPD or the final MPD should be downloaded later
341  * @param[in] tryFog - Attempt to download from FOG or not
342  *
343  */
344  MPD* GetAdMPD(std::string &url, bool &finalManifest, bool tryFog = false);
345 
346  /**
347  * @fn InsertToPeriodMap
348  *
349  * @param[in] period - Pointer of the period to be inserted
350  */
351  void InsertToPeriodMap(IPeriod *period);
352 
353  /**
354  * @fn isPeriodExist
355  *
356  * @param[in] periodId - Period id to be checked.
357  * @return true or false
358  */
359  bool isPeriodExist(const std::string &periodId);
360 
361  /**
362  * @brief Method to check the existence of Adbreak object in the AdbreakObject map
363  *
364  * @param[in] adBrkId - Adbreak id to be checked.
365  * @return true or false
366  */
367  inline bool isAdBreakObjectExist(const std::string &adBrkId);
368 
369  /**
370  * @fn PrunePeriodMaps
371  *
372  * @param[in] newPeriodIds - Period ids from the latest manifest
373  */
374  void PrunePeriodMaps(std::vector<std::string> &newPeriodIds);
375 
376  /**
377  * @fn ResetState
378  */
379  void ResetState();
380 
381  /**
382  * @fn ClearMaps
383  */
384  void ClearMaps();
385 
386  /**
387  * @fn PlaceAds
388  */
389  void PlaceAds(dash::mpd::IMPD *mpd);
390 
391  /**
392  * @fn CheckForAdStart
393  *
394  * @param[in] rate - Playback rate
395  * @param[in] periodId - Period id to be checked
396  * @param[in] offSet - Period offset to be checked
397  * @param[out] breakId - Id of the Adbreak, if the period & offset falls in an Adbreak
398  * @param[out] adOffset - Offset of the Ad for that point of the period
399  *
400  */
401  int CheckForAdStart(const float &rate, bool init, const std::string &periodId, double offSet, std::string &breakId, double &adOffset);
402 
403  /**
404  * @fn CheckForAdTerminate
405  *
406  * @param[in] fragmentTime - Current offset in the period
407  *
408  * @return True or false
409  */
410  bool CheckForAdTerminate(double fragmentTime);
411 
412  /**
413  * @fn isPeriodInAdbreak
414  *
415  * @param[in] periodId - Period id
416  *
417  * @return True or false
418  */
419  inline bool isPeriodInAdbreak(const std::string &periodId);
420 };
421 
422 
423 #endif /* ADMANAGER_MPD_H_ */
CDAIObjectMPD
Client Side DAI object implementation for DASH.
Definition: admanager_mpd.h:44
PrivateCDAIObjectMPD::mIsFogTSB
bool mIsFogTSB
Definition: admanager_mpd.h:284
PrivateCDAIObjectMPD
Private Client Side DAI object for DASH.
Definition: admanager_mpd.h:278
AdNode::url
std::string url
Definition: admanager_mpd.h:117
PrivateCDAIObjectMPD::mPeriodMap
std::unordered_map< std::string, Period2AdData > mPeriodMap
Definition: admanager_mpd.h:286
AdNode::mpd
MPD * mpd
Definition: admanager_mpd.h:121
PrivateCDAIObjectMPD::mContentSeekOffset
double mContentSeekOffset
Definition: admanager_mpd.h:296
Period2AdData::filled
bool filled
Definition: admanager_mpd.h:219
AdOnPeriod
Individual Ad's object placed over the period.
Definition: admanager_mpd.h:208
Period2AdData::Period2AdData
Period2AdData()
Period2AdData constructor.
Definition: admanager_mpd.h:227
AdBreakObject
AdBreak's metadata object.
Definition: admanager_mpd.h:172
PrivateCDAIObjectMPD::mAdFulfillObj
AdFulfillObj mAdFulfillObj
Definition: admanager_mpd.h:293
PlacementObj::openPeriodId
std::string openPeriodId
Definition: admanager_mpd.h:258
PlacementObj::curAdIdx
int curAdIdx
Definition: admanager_mpd.h:260
AdFulfillObj
Temporary object representing currently fulfilling ad (given by setAlternateContent).
Definition: admanager_mpd.h:237
PrivateCDAIObjectMPD::mAdState
AdState mAdState
Definition: admanager_mpd.h:297
PrivateCDAIObjectMPD::mCurPlayingBreakId
std::string mCurPlayingBreakId
Definition: admanager_mpd.h:287
AdEvent::INIT
@ INIT
AdEvent::DEFAULT
@ DEFAULT
AdNode::AdNode
AdNode(const AdNode &adNode)
AdNode Copy Constructor.
Definition: admanager_mpd.h:156
AdManagerBase.h
Client side DAI base class, and common data structures.
AdFulfillObj::adId
std::string adId
Definition: admanager_mpd.h:239
AdNode::duration
uint64_t duration
Definition: admanager_mpd.h:118
AdBreakObject::adsDuration
uint32_t adsDuration
Definition: admanager_mpd.h:177
AdEvent
AdEvent
Events to the Ad manager's state machine.
Definition: admanager_mpd.h:97
AdBreakObject::ads
std::shared_ptr< std::vector< AdNode > > ads
Definition: admanager_mpd.h:174
AdBreakObject::endPeriodId
std::string endPeriodId
Definition: admanager_mpd.h:175
AdNode::AdNode
AdNode(bool invalid, bool placed, std::string adId, std::string url, uint64_t duration, std::string basePeriodId, int basePeriodOffset, MPD *mpd)
AdNode constructor.
Definition: admanager_mpd.h:143
AdNode::AdNode
AdNode()
AdNode default constructor.
Definition: admanager_mpd.h:126
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
PlacementObj::PlacementObj
PlacementObj()
PlacementObj constructor.
Definition: admanager_mpd.h:266
AdOnPeriod::adIdx
int32_t adIdx
Definition: admanager_mpd.h:210
AdEvent::BASE_OFFSET_CHANGE
@ BASE_OFFSET_CHANGE
PrivateCDAIObjectMPD::mCurAds
std::shared_ptr< std::vector< AdNode > > mCurAds
Definition: admanager_mpd.h:291
AdFulfillObj::AdFulfillObj
AdFulfillObj()
AdFulfillObj constructor.
Definition: admanager_mpd.h:245
AdEvent::AD_FINISHED
@ AD_FINISHED
Period2AdData
Meta info corresponding to each period.
Definition: admanager_mpd.h:218
AdState
AdState
Ad playback states.
Definition: AdManagerBase.h:35
AdEvent::AD_FAILED
@ AD_FAILED
PrivateCDAIObjectMPD::mAamp
PrivateInstanceAAMP * mAamp
Definition: admanager_mpd.h:282
AdNode
Individual Ad's meta info.
Definition: admanager_mpd.h:113
PrivateCDAIObjectMPD::mPlacementObj
PlacementObj mPlacementObj
Definition: admanager_mpd.h:294
PrivateCDAIObjectMPD::mAdBreaks
std::unordered_map< std::string, AdBreakObject > mAdBreaks
Definition: admanager_mpd.h:285
AdEvent::PERIOD_CHANGE
@ PERIOD_CHANGE
Period2AdData::offset2Ad
std::map< int, AdOnPeriod > offset2Ad
Definition: admanager_mpd.h:222
PrivateCDAIObjectMPD::mAdObjThreadStarted
bool mAdObjThreadStarted
Definition: admanager_mpd.h:289
AdNode::adId
std::string adId
Definition: admanager_mpd.h:116
AdFulfillObj::periodId
std::string periodId
Definition: admanager_mpd.h:238
AdBreakObject::brkDuration
uint32_t brkDuration
Definition: admanager_mpd.h:173
CDAIObjectMPD::GetPrivateCDAIObjectMPD
PrivateCDAIObjectMPD * GetPrivateCDAIObjectMPD()
Getter for the PrivateCDAIObjectMPD instance.
Definition: admanager_mpd.h:76
AdOnPeriod::adStartOffset
uint32_t adStartOffset
Definition: admanager_mpd.h:211
AdBreakObject::AdBreakObject
AdBreakObject(uint32_t _duration, std::shared_ptr< std::vector< AdNode >> _ads, std::string _endPeriodId, uint64_t _endPeriodOffset, uint32_t _adsDuration)
AdBreakObject constructor.
Definition: admanager_mpd.h:196
PrivateInstanceAAMP
Class representing the AAMP player's private instance, which is not exposed to outside world.
Definition: priv_aamp.h:640
PrivateCDAIObjectMPD::operator=
PrivateCDAIObjectMPD & operator=(const PrivateCDAIObjectMPD &)=delete
PrivateCDAIObjectMPD assignment operator.
PlacementObj
Currently placing Ad's object.
Definition: admanager_mpd.h:256
PlacementObj::curEndNumber
uint64_t curEndNumber
Definition: admanager_mpd.h:259
AdNode::invalid
bool invalid
Definition: admanager_mpd.h:114
AdBreakObject::adjustEndPeriodOffset
bool adjustEndPeriodOffset
Definition: admanager_mpd.h:178
PlacementObj::adNextOffset
uint32_t adNextOffset
Definition: admanager_mpd.h:261
AdNode::basePeriodId
std::string basePeriodId
Definition: admanager_mpd.h:119
PlacementObj::pendingAdbrkId
std::string pendingAdbrkId
Definition: admanager_mpd.h:257
CDAIObject
Base class for the client side DAI object.
Definition: AdManagerBase.h:57
AdBreakObject::endPeriodOffset
uint64_t endPeriodOffset
Definition: admanager_mpd.h:176
AdBreakObject::AdBreakObject
AdBreakObject()
AdBreakObject default constructor.
Definition: admanager_mpd.h:183
PrivateCDAIObjectMPD::SetAlternateContents
void SetAlternateContents(const std::string &periodId, const std::string &adId, const std::string &url, uint64_t startMS, uint32_t breakdur=0)
Setting the alternate contents' (Ads/blackouts) URL.
Definition: admanager_mpd.cpp:762
Period2AdData::duration
uint64_t duration
Definition: admanager_mpd.h:221
PrivateCDAIObjectMPD::mAdFailed
bool mAdFailed
Definition: admanager_mpd.h:290
Period2AdData::adBreakId
std::string adBreakId
Definition: admanager_mpd.h:220
AdNode::placed
bool placed
Definition: admanager_mpd.h:115
PrivateCDAIObjectMPD::mCurAdIdx
int mCurAdIdx
Definition: admanager_mpd.h:292
AdNode::basePeriodOffset
int basePeriodOffset
Definition: admanager_mpd.h:120
PrivateCDAIObjectMPD::mAdObjThreadID
pthread_t mAdObjThreadID
Definition: admanager_mpd.h:288
CDAIObjectMPD::mPrivObj
class PrivateCDAIObjectMPD * mPrivObj
Definition: admanager_mpd.h:46
AdFulfillObj::url
std::string url
Definition: admanager_mpd.h:240