/** * Process a single audio stream's NowPlaying info. * * @param StationStream $stream * @param Station $station * @return array Structured NowPlaying Data */ public static function processAudioStream(StationStream $stream, Station $station, $force = false) { $current_np_data = (array) $stream->nowplaying_data; // Only process non-default streams on odd-numbered "segments" to improve performance. if (!$stream->is_default && !$force && NOWPLAYING_SEGMENT % 2 == 0 && !empty($current_np_data)) { return $current_np_data; } $np = StationStream::api($stream); $custom_class = Station::getStationClassName($station->name); $custom_adapter = '\\PVL\\RadioAdapter\\' . $custom_class; if (class_exists($custom_adapter)) { $np_adapter = new $custom_adapter($stream, $station); } elseif ($stream->type == "icecast") { $np_adapter = new \PVL\RadioAdapter\IceCast($stream, $station); } elseif ($stream->type == "icebreath") { $np_adapter = new \PVL\RadioAdapter\IceBreath($stream, $station); } elseif ($stream->type == "shoutcast2") { $np_adapter = new \PVL\RadioAdapter\ShoutCast2($stream, $station); } elseif ($stream->type == "shoutcast1") { $np_adapter = new \PVL\RadioAdapter\ShoutCast1($stream, $station); } else { return array(); } Debug::log('Adapter Class: ' . get_class($np_adapter)); $stream_np = $np_adapter->process(); $np = array_merge($np, $stream_np['meta']); $np['listeners'] = $stream_np['listeners']; // Pull from current NP data if song details haven't changed. $current_song_hash = Song::getSongHash($stream_np['current_song']); if (strcmp($current_song_hash, $current_np_data['current_song']['id']) == 0) { $np['current_song'] = $current_np_data['current_song']; $np['song_history'] = $current_np_data['song_history']; } else { if (empty($stream_np['current_song']['text'])) { $np['current_song'] = array(); $np['song_history'] = $station->getRecentHistory($stream); } else { // Register a new item in song history. $np['current_song'] = array(); $np['song_history'] = $station->getRecentHistory($stream); // Determine whether to log this song play for analytics. $log_radio_play = $stream->is_default && $station->category == 'audio'; $song_obj = Song::getOrCreate($stream_np['current_song'], $log_radio_play); $sh_obj = SongHistory::register($song_obj, $station, $stream, $np); // Compose "current_song" object for API. $current_song = Song::api($song_obj); $current_song['sh_id'] = $sh_obj->id; $current_song['score'] = SongVote::getScoreForStation($song_obj, $station); $vote_urls = array(); $vote_functions = array('like', 'dislike', 'clearvote'); foreach ($vote_functions as $vote_function) { $vote_urls[$vote_function] = \PVL\Url::api(array('module' => 'api', 'controller' => 'song', 'action' => $vote_function, 'sh_id' => $sh_obj->id)); } $current_song['vote_urls'] = $vote_urls; $external = $song_obj->getExternal(); if ($external) { $current_song['external'] = $song_obj->getExternal(); } $np['current_song'] = $current_song; } } $stream->nowplaying_data = $np; return $np; }