# C++ Based Player SDK(Nowtilus SSAI)

### **Step1: Fetching Media Data**

If type of the stream is live fetch the media URL and vast URL through an API call. If type of the stream is VoD fetch the media data in the form of JSON which also contains the ad data.

### Step2: Initialise SSAI Ad Manager

After the fetching the media data, initialise the SSAI Ad Manager by sending the media data into it. Please refer to the below code for initialisation:

```cpp
MMNowtilusSSAIAdManager* adManager = new MMNowtilusSSAIAdManager();

if(adManager){
        adManager->mmSSAIClientInit(<mediaUrl>, <vastUrl>, <isLive>, <json_VOD_response>);
        IMMSSAIEventsListeners* ad_observer = new mmSSAIEventListener;
        if(ad_observer){
                adManager->addListener(ad_observer);
        }
}
```

### Step3: Send Playhead Position to the Ad Manager

For SDK to detect the ads and fire callbacks for ad events, absolute playhead position should be shared to the MediaMelon SDK. This position value should be shared through `reportPlaybackPosition` function call. The position value should be different for live and VoD streams as mentioned below:

**Live Stream:**\
Pass the absolute playhead position of the player in the unix timestamp format in milliseconds.

**VoD Stream:**\
Pass the absolute playhead position of the player in milliseconds.

```cpp
adManager->reportPlaybackPosition(<playheadPosition>);
```

### Step4: Listen for the Ad Callbacks

When the SSAI Ad Manger detects an ad, a series of ad callbacks will be fired and those callbacks can be listened as shown in the below code snippet.

```cpp
class  mmSSAIEventListener : public IMMSSAIEventsListeners
{
private:
	/* data */
public:
	mmSSAIEventListener(){
	}
	
  void onAdCueTimelineAdded(vector<mmAd> ssaiAdInfoTimeline){
		cout << "onAdCueTimelineAdded Callback";
    		cout<<" Ad ID = "<<ssaiAdInfoTimeline[0].getAdId()<<endl;
    		cout<<" Ad Title = "<<ssaiAdInfoTimeline[0].getAdTitle()<<endl;
    		cout<<" Ad Duration = "<<ssaiAdInfoTimeline[0].getAdDuration()<<endl;
    		cout<<" Ad adStartPosition = "<<ssaiAdInfoTimeline[0].getAdStartPosition()<<endl;
    		cout<<" Ad adEndPosition = "<<ssaiAdInfoTimeline[0].getAdEndPosition()<<endl;
    		cout<<" Ad adStartTime = "<<ssaiAdInfoTimeline[0].getAdStartTime()<<endl;
    		cout<<" Ad adEndTime = "<<ssaiAdInfoTimeline[0].getAdEndTime()<<endl;
	}

  void onAdCueTimelineEnter(mmAd ssaiAdInfo){
		cout << "onAdCueTimelineEnter Callback";
	}

  void onAdCueTimelineExit(mmAd ssaiAdInfo){
		cout << "onAdCueTimelineExit Callback";
	}

  void onAdImpression(mmAd ssaiAdInfo) {
  		cout<<" Ad adStartTime = "<<ssaiAdInfo.getAdStartTime()<<endl;
    		cout<<" Ad adEndTime = "<<ssaiAdInfo.getAdEndTime()<<endl;
    
		cout << "onAdImpression Callback";
	}

  void onAdStarted(mmAd ssaiAdInfo) {
		cout << "onAdStarted Callback";
	}

  void onAdFirstQuartile(mmAd ssaiAdInfo){
		cout << "onAdFirstQuartile Callback";
	}

  void onAdMidpoint(mmAd ssaiAdInfo){
		cout << "onAdMidpoint Callback";
	}

  void onAdThirdQuartile(mmAd ssaiAdInfo){
		cout << "onAdThirdQuartile Callback";
	}

  void onAdComplete(mmAd ssaiAdInfo){
		cout << "onAdComplete Callback";
	}
	
  void onAdError(mmAd ssaiAdInfo){
		cout << "onAdError Callback";
	}
};
```

{% hint style="info" %}
**Handling Trick Play Scenarios:**\
The callback associated with the <mark style="color:purple;">`onAdCueTimelineEnter`</mark> can be employed to signal the video player to disable the seek bar, thereby preventing the ad from being skipped. Correspondingly, upon conclusion of the ad break, the video player will receive<mark style="color:purple;">`onAdCueTimelineExit`</mark>, prompting the enable of the seek bar.

```cpp
void onAdCueTimelineEnter(mmAd ssaiAdInfo){
    //Event dispatched for Ad break started
    //playerObject.disableSeek();
}

void onAdCueTimelineExit(mmAd ssaiAdInfo){
    //Event dispatched for Ad break ended
    //playerObject.enableSeek();
}
```

{% endhint %}

### Step5: Ad Playback Error

When an ad playback error occurs, call the function named `signalAdPlaybackError()` from the sample application. Calling this function will trigger the onAdError callback which contains the ad data.

```cpp
adManager->signalAdPlaybackError();
```

### Step6: Stop SSAI Ad Manager

To stop the SSAI Ad Manager, call the function named `stopSSAIAdManager()` from the sample application. Calling this function will stop the manifest and vast network calls as well.

```cpp
adManager->stopSSAIAdManager();
```

<table><thead><tr><th>Variable</th><th>Description</th><th data-hidden></th></tr></thead><tbody><tr><td>mediaUrl</td><td>String containing the media URL.</td><td></td></tr><tr><td>vastUrl</td><td>String containing the vast URL for live streams. For VoD, it should be an empty string.</td><td></td></tr><tr><td>isLive</td><td>A boolean variable. It should be true for live streams and false for VoD streams.</td><td></td></tr><tr><td>json_VOD_response</td><td>VoD stream curl response in JSON format. It should be Empty JSON object for live streams.</td><td></td></tr><tr><td>playheadPosition</td><td>int64 value containing absolute playhead position of the stream playing.</td><td></td></tr></tbody></table>
