public function generatePlaylistMediaItemResponseData($playlistId, $mediaItemId, $showStreamUris, $showVodUris)
 {
     $playlist = Playlist::accessible()->find(intval($playlistId));
     if (is_null($playlist)) {
         return $this->generateNotFound();
     }
     $mediaItem = $playlist->mediaItems()->accessible()->find(intval($mediaItemId));
     if (is_null($mediaItem)) {
         return $this->generateNotFound();
     }
     $mediaItem->load("liveStreamItem", "liveStreamItem.stateDefinition", "liveStreamItem.liveStream", "videoItem", "videoItem.sourceFile.vodData");
     $data = $this->mediaItemTransformer->transform([$playlist, $mediaItem], $this->getMediaItemTransformerOptions($showStreamUris, $showVodUris));
     return new ApiResponseData($data);
 }
Example #2
0
 public function postPlayerInfo($playlistId, $mediaItemId)
 {
     // true if a user is logged into the cms and has permission to view media items.
     $userHasMediaItemsPermission = false;
     // true if a user is logged into the cms and has permission to view playlists.
     $userHasPlaylistsPermission = false;
     if (Auth::isLoggedIn()) {
         $userHasMediaItemsPermission = Auth::getUser()->hasPermission(Config::get("permissions.mediaItems"), 0);
         $userHasPlaylistsPermission = Auth::getUser()->hasPermission(Config::get("permissions.playlists"), 0);
     }
     $playlist = Playlist::accessible();
     if (!$userHasPlaylistsPermission) {
         // current cms user (if logged in) does not have permission to view playlists, so only search playlists accessible to the public.
         $playlist = $playlist->accessibleToPublic();
     }
     $playlist = $playlist->find(intval($playlistId));
     if (is_null($playlist)) {
         App::abort(404);
     }
     $mediaItem = $playlist->mediaItems()->accessible()->find($mediaItemId);
     if (is_null($mediaItem)) {
         App::abort(404);
     }
     $mediaItem->load("likes", "liveStreamItem", "liveStreamItem.stateDefinition", "liveStreamItem.liveStream", "videoItem", "videoItem.chapters", "videoItem.sourceFile");
     $id = intval($mediaItem->id);
     $title = $playlist->generateEpisodeTitle($mediaItem);
     $uri = $playlist->getMediaItemUri($mediaItem);
     $liveStreamItem = $mediaItem->liveStreamItem;
     if (!is_null($liveStreamItem) && !$liveStreamItem->getIsAccessible()) {
         // should not be accessible so pretend doesn't exist
         $liveStreamItem = null;
     }
     $hasLiveStreamItem = !is_null($liveStreamItem);
     $liveStream = $hasLiveStreamItem ? $liveStreamItem->liveStream : null;
     $videoItem = $mediaItem->videoItem;
     if (!is_null($videoItem) && !$videoItem->getIsAccessible()) {
         // should not be accessible so pretend doesn't exist
         $videoItem = null;
     }
     $hasVideoItem = false;
     if (!Config::get("degradedService.enabled")) {
         $hasVideoItem = !is_null($videoItem) && !is_null($videoItem->sourceFile);
         // pretend doesn't exist if no video/video processing
     }
     $publishTime = $mediaItem->scheduled_publish_time;
     if (!is_null($publishTime)) {
         $publishTime = $publishTime->timestamp;
     }
     $coverArtUri = Config::get("custom.default_cover_uri");
     if (!Config::get("degradedService.enabled")) {
         $coverArtResolutions = Config::get("imageResolutions.coverArt");
         $coverArtUri = $playlist->getMediaItemCoverArtUri($mediaItem, $coverArtResolutions['full']['w'], $coverArtResolutions['full']['h']);
     }
     $hasStream = $hasLiveStreamItem;
     $streamInfoMsg = $hasLiveStreamItem ? $liveStreamItem->information_msg : null;
     $streamState = $hasLiveStreamItem ? intval($liveStreamItem->getResolvedStateDefinition()->id) : null;
     $streamEndTime = $streamState === 3 && !is_null($liveStreamItem->end_time) ? $liveStreamItem->end_time->timestamp : null;
     $availableOnDemand = $hasLiveStreamItem ? (bool) $liveStreamItem->being_recorded : null;
     $externalStreamUrl = $hasLiveStreamItem ? $liveStreamItem->external_stream_url : null;
     $streamViewCount = $hasLiveStreamItem ? intval($liveStreamItem->getViewCount()) : null;
     $hasVod = $hasVideoItem;
     $vodLive = $hasVideoItem ? $videoItem->getIsLive() : null;
     $vodViewCount = $hasVideoItem ? intval($videoItem->getViewCount()) : null;
     $vodChapters = null;
     $vodThumbnails = null;
     if ($hasVideoItem && ($vodLive || $userHasMediaItemsPermission)) {
         $vodChapters = array();
         foreach ($videoItem->chapters()->orderBy("time", "asc")->orderBy("title", "asc")->get() as $b => $a) {
             $vodChapters[] = array("title" => $a->title, "time" => intval($a->time));
         }
         $vodThumbnails = $videoItem->getScrubThumbnails();
     }
     $minNumberOfViews = Config::get("custom.min_number_of_views");
     if (!$userHasMediaItemsPermission) {
         $viewCountTotal = 0;
         if (!is_null($vodViewCount)) {
             $viewCountTotal += $vodViewCount;
         }
         if (!is_null($streamViewCount)) {
             $viewCountTotal += $streamViewCount;
         }
         if ($viewCountTotal < $minNumberOfViews) {
             // the combined number of views is less than the amount required for it to be sent to the client
             // send null instead
             $vodViewCount = $streamViewCount = null;
         }
     }
     $minNumWatchingNow = Config::get("custom.min_num_watching_now");
     $numWatchingNow = $mediaItem->getNumWatchingNow();
     if (!$userHasMediaItemsPermission && $numWatchingNow < $minNumWatchingNow) {
         $numWatchingNow = null;
     }
     $user = Facebook::getUser();
     $rememberedPlaybackTime = null;
     if ($hasVideoItem && !is_null($user)) {
         // only retrieve entries where playing is true to prevent fighting if the user has the same video paused in several tabs paused at different points
         $playbackHistory = $videoItem->sourceFile->playbackHistories()->orderBy("updated_at", "desc")->where("user_id", $user->id)->where("playing", true)->first();
         if (!is_null($playbackHistory) && !is_null($playbackHistory->time)) {
             $rememberedPlaybackTime = intval($playbackHistory->time);
         }
     }
     $numLikes = $mediaItem->likes_enabled ? $mediaItem->likes()->where("is_like", true)->count() : null;
     $numDislikes = $mediaItem->likes_enabled ? $mediaItem->likes()->where("is_like", false)->count() : null;
     $likeType = null;
     if (!is_null($user)) {
         $like = $mediaItem->likes()->where("site_user_id", $user->id)->first();
         if (!is_null($like)) {
             $likeType = $like->is_like ? "like" : "dislike";
         }
     }
     $embedData = $playlist->getEmbedData($mediaItem);
     // only return the uris if they are actually needed. Security through obscurity
     // always return uris if there's a cms user with permission logged in because they should be able override the fact that it's not live
     $streamUris = array();
     // return the uris if there is a live stream which is accessible, and the media item live stream is marked as "live"
     // if the media item is in the "show over" state then just the dvr uris need to be served so the dvr recording can still be watched.
     // note $liveStream is the LiveStream model which is attached to the $liveStreamItem which is a MediaItemLiveStream model.
     if ($hasLiveStreamItem && !is_null($liveStream) && $liveStream->getIsAccessible() && ($streamState === 2 || $streamState === 3 || $streamState === 1 && $userHasMediaItemsPermission)) {
         $onlyDvrUris = $streamState === 3;
         foreach ($liveStreamItem->getQualitiesWithUris($onlyDvrUris ? array("dvrBridge") : array("dvrBridge", "live")) as $qualityWithUris) {
             $streamUris[] = array("quality" => array("id" => intval($qualityWithUris['qualityDefinition']->id), "name" => $qualityWithUris['qualityDefinition']->name), "uris" => $qualityWithUris['uris']);
         }
     }
     $videoUris = array();
     $vodSourceId = null;
     // return the uris (and vod source id) if the item is accessible to the public or the logged in cms user has permission
     if ($hasVideoItem && ($vodLive || $userHasMediaItemsPermission)) {
         $vodSourceId = intval($videoItem->sourceFile->id);
         foreach ($videoItem->getQualitiesWithUris() as $qualityWithUris) {
             $videoUris[] = array("quality" => array("id" => intval($qualityWithUris['qualityDefinition']->id), "name" => $qualityWithUris['qualityDefinition']->name), "uris" => $qualityWithUris['uris']);
         }
     }
     $data = array("id" => $id, "title" => $title, "uri" => $uri, "scheduledPublishTime" => $publishTime, "coverUri" => $coverArtUri, "embedData" => $embedData, "hasStream" => $hasStream, "streamInfoMsg" => $streamInfoMsg, "streamState" => $streamState, "streamEndTime" => $streamEndTime, "streamUris" => $streamUris, "availableOnDemand" => $availableOnDemand, "externalStreamUrl" => $externalStreamUrl, "streamViewCount" => $streamViewCount, "hasVod" => $hasVod, "vodSourceId" => $vodSourceId, "vodLive" => $vodLive, "videoUris" => $videoUris, "vodViewCount" => $vodViewCount, "vodChapters" => $vodChapters, "vodThumbnails" => $vodThumbnails, "rememberedPlaybackTime" => $rememberedPlaybackTime, "numWatchingNow" => $numWatchingNow, "numLikes" => $numLikes, "numDislikes" => $numDislikes, "likeType" => $likeType);
     return Response::json($data);
 }