Created
July 21, 2016 09:36
-
-
Save Dubiy/afd5c5deb5a5e6d294cc8900e1e675b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User: Igor Dubiy | |
*/ | |
namespace AppBundle\Controller\Api; | |
use Aws\Common\Aws; | |
use Doctrine\ORM\EntityManager; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
use AppBundle\Entity\Device; | |
use AppBundle\Entity\Log; | |
use AppBundle\Entity\Video; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Routing\Annotation\Route; | |
use Aws\Sns\MessageValidator\Message; | |
use Aws\Sns\SnsClient; | |
use Mcfedr\AwsPushBundle\Message\Message as PushMessage; | |
use Guzzle\Http\Client; | |
class HookController extends Controller | |
{ | |
/** | |
* Hooks on video processed | |
* | |
* @Route("/hook/aws_et", name="hook_aws_et") | |
* @Method({"POST"}) | |
* @param Request $request | |
* @return array | |
*/ | |
public function hookAwsEtAction(Request $request) | |
{ | |
$push = Message::fromRawPostData(); | |
$em = $this->getDoctrine()->getManager(); | |
if ($push->get('Type') == 'Notification') { | |
$message = json_decode($push->get('Message')); | |
switch ($message->state) { | |
case 'COMPLETED': | |
case 'WARNING': | |
case (!isset($message->messageDetails) && $message->state == 'ERROR'): | |
{ | |
$video = $this->getDoctrine()->getRepository('AppBundle:Video')->findOneBy(['jobId' => $message->jobId]); | |
if ($video && $video->getStatus() != Video::STATUS_COMPLETE && $video->getStatus() != Video::STATUS_PUBLISHED) { | |
$videoBucket = $this->container->getParameter('amazon_aws.s3.bucket_video'); | |
foreach ($message->outputs as $output) { | |
if ($output->presetId == $this->container->getParameter('amazon_aws.elastic_transcoder.preset_id')) { | |
//main file for web | |
$videoUrl = $this->get('aws.s3')->getUrl($videoBucket, $output->key); | |
if (isset($output->duration)) { | |
$video->setStatus(Video::STATUS_COMPLETE); | |
$video->setDuration($output->duration); | |
$video->setVideo($videoUrl); | |
if (!$video->getThumb() && isset($output->thumbnailPattern)) { | |
$thumbKey = str_replace('{count}', '00001', $output->thumbnailPattern) . '.' . $this->container->getParameter('amazon_aws.elastic_transcoder.preset_thumb_format'); | |
$thumbUrl = $this->get('aws.s3')->getUrl($videoBucket, $thumbKey); | |
$videoThumbnailSize = $this->container->getParameter('video_thumbnail_size'); | |
$video->setThumb($thumbUrl); | |
} | |
if ($video->getUser()->getNotificationsUpload()) { | |
$pushMessage = new PushMessage("Video encoding done!"); | |
$pushMessage->setCustom([ | |
'type' => 'video_encoded', | |
'video' => $video | |
]); | |
$pushMessage->setSound("default"); | |
$pushMessage->setContentAvailable(true); | |
foreach ($video->getUser()->getDevices() as $device) { | |
$this->container->get('service.push')->send($device, $pushMessage); | |
} | |
} | |
} elseif (isset($output->errorCode)) { | |
//handle error | |
$video->setStatus(Video::STATUS_REJECT); | |
$video->setDuration(((isset($output->duration)) ? ($output->duration) : (0))); | |
$video->setDescription(((isset($output->statusDetail)) ? ($output->statusDetail) : (''))); | |
$this->get('aws.s3')->removeFile($videoUrl, $videoBucket); | |
if (!$video->getThumb() && isset($output->thumbnailPattern)) { | |
$thumbPrefix = str_replace('{count}', '', $output->thumbnailPattern); | |
$this->get('aws.s3')->removeFilesByPrefix($thumbPrefix, $videoBucket); | |
} | |
if ($video->getUser()->getNotificationsUpload()) { | |
$pushMessage = new PushMessage("Video encoding format unsupported. Please ensure your video is encoded using a standard compression e.g H.264 or MPEG-2"); | |
$pushMessage->setCustom([ | |
'type' => 'video_rejected', | |
'video' => $video | |
]); | |
$pushMessage->setSound("default"); | |
$pushMessage->setContentAvailable(true); | |
foreach ($video->getUser()->getDevices() as $device) { | |
$this->container->get('service.push')->send($device, $pushMessage); | |
} | |
} | |
} | |
} else { | |
//looks like HLS streams. Catch them in playlist | |
} | |
} | |
if (isset($message->playlists)) { | |
foreach ($message->playlists as $playlist) { | |
$videoHlsUrl = $this->get('aws.s3')->getUrl($videoBucket, $playlist->name . '.m3u8'); | |
$video->setVideoHls($videoHlsUrl); | |
break; | |
} | |
} | |
$em->flush(); | |
} | |
} | |
break; | |
case 'ERROR': { | |
$log = new Log('error', $message->messageDetails, __FILE__ . ':' . __LINE__, json_encode($message)); | |
$em->persist($log); | |
$em->flush(); | |
} | |
break; | |
} | |
} | |
if ($push->get('Type') == 'SubscriptionConfirmation') { | |
$client = new Client(); | |
$client->get($push->get('SubscribeURL'))->send(); | |
return new Response("ok"); | |
} | |
return new Response("smth wrong"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment