Skip to content

Commit

Permalink
see #1: add support for Face Detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ziodave committed Oct 13, 2015
1 parent 7b87a14 commit 237024a
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 85 deletions.
26 changes: 26 additions & 0 deletions src/includes/class-helixware-mico-face-detection-service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Manage Face Detection fragments.
* @since 1.2.1
*/
class HelixWare_Mico_Face_Detection_Service extends HelixWare_Mico_Fragment_Service {

const FRAGMENTS = 'faceFragments';

/**
* Create an instance of the MICO Fragment service.
*
* @since 1.2.1
*
* @param \HelixWare_HAL_Client $hal_client A HAL client.
* @param string $server_url The server URL.
* @param \HelixWare_Asset_Service $asset_service The Asset service.
*/
public function __construct( $hal_client, $server_url, $asset_service ) {

parent::__construct( self::FRAGMENTS, $hal_client, $server_url, $asset_service );

}

}
75 changes: 15 additions & 60 deletions src/includes/class-helixware-mico-fragment-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
*/
class Helixware_Mico_Fragment_Service {

const FIND_BY_ASSET_GUID_PATH = '/sequenceFragments/search/findByAssetGUID?guid=%s';
const FIND_BY_ASSET_GUID_PATH = '/%s/search/findByAssetGUID?guid=%s';

/**
* The name of the fragments component.
*
* @since 1.2.1
* @access private
* @var string $fragments The name of the fragments component.
*/
private $fragments;

/**
* A HAL client.
Expand Down Expand Up @@ -44,12 +53,14 @@ class Helixware_Mico_Fragment_Service {
*
* @since 1.0.0
*
* @param string $fragments The fragments part in the path.
* @param \HelixWare_HAL_Client $hal_client A HAL client.
* @param string $server_url The server URL.
* @param \HelixWare_Asset_Service $asset_service The Asset service.
*/
public function __construct( $hal_client, $server_url, $asset_service ) {
public function __construct( $fragments, $hal_client, $server_url, $asset_service ) {

$this->fragments = $fragments;
$this->hal_client = $hal_client;
$this->server_url = $server_url;
$this->asset_service = $asset_service;
Expand All @@ -66,14 +77,14 @@ public function __construct( $hal_client, $server_url, $asset_service ) {
*/
public function get_fragments( $guid ) {

$path = sprintf( self::FIND_BY_ASSET_GUID_PATH, urlencode( $guid ) );
$path = sprintf( self::FIND_BY_ASSET_GUID_PATH, $this->fragments, urlencode( $guid ) );
$request = new HelixWare_HAL_Request( 'GET', $this->server_url . $path );

$response = $this->hal_client->execute( $request );

$fragments = array();
do {
$fragments = array_merge( $fragments, $response->get_embedded( 'sequenceFragments' ) );
$fragments = array_merge( $fragments, $response->get_embedded( $this->fragments ) );
} while ( $response->has_next() && $response = $response->get_next() );

return $fragments;
Expand All @@ -95,60 +106,4 @@ public function get_fragments_by_id( $id ) {

}

/**
* Get the VTT chapters URL.
*
* @since 1.2.0
*
* @param int $id The post ID.
*
* @return string The local URL to the VTT chapters URL.
*/
public function get_vtt_chapters_url( $id ) {

return admin_url( "admin-ajax.php?action=hw_vtt_chapters&id=$id" );
}

/**
* Echo a jwplayer:track line linking to the chapters file.
*
* @since 1.2.0
*
* @param WP_Post $post A post instance.
*/
public function playlist_rss_jwplayer_header( $post ) {

echo( '<jwplayer:track file="' . htmlentities( $this->get_vtt_chapters_url( $post->ID ) ) . '" kind="chapters" />' . "\n" );

}

/**
* Outputs a VTT file defining the chapters for the attachment with the provided id.
*
* @since 1.2.0
*/
public function ajax_vtt_chapters() {

// Check that a post ID has been provided.
if ( ! isset( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) {
wp_die( 'A numeric id is required.' );
}

echo( "WEBVTT\n\n" );

$chapter_no = 0;
$fragments = $this->get_fragments_by_id( $_GET['id'] );
array_walk( $fragments, function ( $fragment ) use ( &$chapter_no ) {

echo( 'chapter_' . ( ++ $chapter_no ) . "\n" );
echo( HelixWare_Helper::milliseconds_to_timecode( $fragment->start ) . " --> " . HelixWare_Helper::milliseconds_to_timecode( $fragment->end ) . "\n" );
echo( 'Chapter ' . $chapter_no . "\n" );
echo( "\n" );

} );

wp_die();

}

}
86 changes: 86 additions & 0 deletions src/includes/class-helixware-mico-sequence-service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* This class manages Fragments for assets.
*
* @since 1.0.0
* @package Helixware_Mico
* @subpackage Helixware_Mico/includes
* @author David Riccitelli <[email protected]>
*/
class Helixware_Mico_Sequence_Service extends Helixware_Mico_Fragment_Service {

const FRAGMENTS = "sequenceFragments";

/**
* Create an instance of the MICO Fragment service.
*
* @since 1.0.0
*
* @param \HelixWare_HAL_Client $hal_client A HAL client.
* @param string $server_url The server URL.
* @param \HelixWare_Asset_Service $asset_service The Asset service.
*/
public function __construct( $hal_client, $server_url, $asset_service ) {

parent::__construct( self::FRAGMENTS, $hal_client, $server_url, $asset_service );

}

/**
* Get the VTT chapters URL.
*
* @since 1.2.0
*
* @param int $id The post ID.
*
* @return string The local URL to the VTT chapters URL.
*/
public function get_vtt_chapters_url( $id ) {

return admin_url( "admin-ajax.php?action=hw_vtt_chapters&id=$id" );
}

/**
* Echo a jwplayer:track line linking to the chapters file.
*
* @since 1.2.0
*
* @param WP_Post $post A post instance.
*/
public function playlist_rss_jwplayer_header( $post ) {

echo( '<jwplayer:track file="' . htmlentities( $this->get_vtt_chapters_url( $post->ID ) ) . '" kind="chapters" />' . "\n" );

}

/**
* Outputs a VTT file defining the chapters for the attachment with the provided id.
*
* @since 1.2.0
*/
public function ajax_vtt_chapters() {

// Check that a post ID has been provided.
if ( ! isset( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) {
wp_die( 'A numeric id is required.' );
}

echo( "WEBVTT\n\n" );

$chapter_no = 0;
$fragments = $this->get_fragments_by_id( $_GET['id'] );
array_walk( $fragments, function ( $fragment ) use ( &$chapter_no ) {

echo( 'chapter_' . ( ++ $chapter_no ) . "\n" );
echo( HelixWare_Helper::milliseconds_to_timecode( $fragment->start ) . " --> " . HelixWare_Helper::milliseconds_to_timecode( $fragment->end ) . "\n" );
echo( 'Chapter ' . $chapter_no . "\n" );
echo( "\n" );

} );

wp_die();

}

}
36 changes: 24 additions & 12 deletions src/includes/class-helixware-mico.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,22 @@ class Helixware_Mico {
private $hal_client;

/**
* The MICO Fragment service.
* The MICO Sequence fragment service.
*
* @since 1.2.0
* @since 1.2.1
* @access private
* @var \Helixware_Mico_Sequence_Service $sequence_service The MICO Sequence fragment service.
*/
private $sequence_service;

/**
* The MICO Face Detection fragment service.
*
* @since 1.2.1
* @access private
* @var \Helixware_Mico_Fragment_Service $fragment_service The MICO Fragment service.
* @var \HelixWare_Mico_FaceDetection_Service $face_detection_service The MICO Face Detection fragment service.
*/
private $fragment_service;
private $face_detection_service;

/**
* The hw_fragments shortcode handler.
Expand All @@ -91,7 +100,7 @@ class Helixware_Mico {
* @access private
* @var \HelixWare_Mico_Fragments_Shortcode $fragments_shortcode The hw_fragments shortcode handler.
*/
private $fragments_shortcode;
private $face_detection_shortcode;

/**
* Define the core functionality of the plugin.
Expand Down Expand Up @@ -148,6 +157,8 @@ private function load_dependencies() {
* Load fragments from MICO.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-helixware-mico-fragment-service.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-helixware-mico-sequence-service.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-helixware-mico-face-detection-service.php';

/**
* The class responsible for defining all actions that occur in the admin area.
Expand All @@ -163,7 +174,7 @@ private function load_dependencies() {
/**
* The hw_fragments shortcode.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-helixware-mico-fragments-shortcode.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-helixware-mico-face-detection-shortcode.php';

$this->loader = new Helixware_Mico_Loader();

Expand All @@ -176,9 +187,10 @@ private function load_dependencies() {
$this->http_client = new HelixWare_HTTP_Client( $http_authentication );
$this->hal_client = new HelixWare_HAL_Client( $this->http_client );

$helixware = HelixWare::get_instance();
$this->fragment_service = new Helixware_Mico_Fragment_Service( $this->hal_client, HELIXWARE_MICO_GW_URL, $helixware->get_asset_service() );
$this->fragments_shortcode = new HelixWare_Mico_Fragments_Shortcode( $this->fragment_service, $helixware->get_asset_service(), $helixware->get_asset_image_service() );
$helixware = HelixWare::get_instance();
$this->sequence_service = new Helixware_Mico_Sequence_Service( $this->hal_client, HELIXWARE_MICO_GW_URL, $helixware->get_asset_service() );
$this->face_detection_service = new HelixWare_Mico_Face_Detection_Service( $this->hal_client, HELIXWARE_MICO_GW_URL, $helixware->get_asset_service() );
$this->face_detection_shortcode = new HelixWare_Mico_Face_Detection_Shortcode( $this->face_detection_service, $helixware->get_asset_service(), $helixware->get_asset_image_service() );

}

Expand Down Expand Up @@ -214,7 +226,7 @@ private function define_admin_hooks() {
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

$this->loader->add_action( 'wp_ajax_hw_vtt_chapters', $this->fragment_service, 'ajax_vtt_chapters' );
$this->loader->add_action( 'wp_ajax_hw_vtt_chapters', $this->sequence_service, 'ajax_vtt_chapters' );

}

Expand All @@ -233,11 +245,11 @@ private function define_public_hooks() {
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

// Provide an AJAX end-point to load the video chapters.
$this->loader->add_action( 'wp_ajax_nopriv_hw_vtt_chapters', $this->fragment_service, 'ajax_vtt_chapters' );
$this->loader->add_action( 'wp_ajax_nopriv_hw_vtt_chapters', $this->sequence_service, 'ajax_vtt_chapters' );

// Hook to the hewa_playlist_rss_jwplayer_header action, which is triggered when the RSS/JWPlayer
// playlist is generated. We add the chapters track.
$this->loader->add_action( 'hewa_playlist_rss_jwplayer_header', $this->fragment_service, 'playlist_rss_jwplayer_header' );
$this->loader->add_action( 'hewa_playlist_rss_jwplayer_header', $this->sequence_service, 'playlist_rss_jwplayer_header' );

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
* @subpackage HelixWare/includes
* @author David Riccitelli <[email protected]>
*/
class HelixWare_Mico_Fragments_Shortcode {
class HelixWare_Mico_Face_Detection_Shortcode {

const HANDLE_NAME = 'hw_fragments';
const HANDLE_NAME = 'hw_face_detection';

/**
* The Fragments service.
* The MICO Face Detection service.
*
* @since 1.2.0
* @since 1.2.1
* @access private
* @var \Helixware_Mico_Fragment_Service $fragments_service The Fragments service.
* @var \HelixWare_Mico_Face_Detection_Service $face_detection_service The MICO Face Detection service.
*/
private $fragments_service;
private $face_detection_service;

/**
* The Asset service.
Expand All @@ -43,15 +43,15 @@ class HelixWare_Mico_Fragments_Shortcode {
*
* @since 1.1.0
*
* @param \Helixware_Mico_Fragment_Service $fragments_service The Fragments service.
* @param \Helixware_Mico_Face_Detection_Service $face_detection_service The MICO Face Detection service.
* @param \HelixWare_Asset_Service $asset_service The Asset service.
* @param \HelixWare_Asset_Image_Service $asset_image_service The Asset Image service.
*/
public function __construct( $fragments_service, $asset_service, $asset_image_service ) {
public function __construct( $face_detection_service, $asset_service, $asset_image_service ) {

$this->fragments_service = $fragments_service;
$this->asset_service = $asset_service;
$this->asset_image_service = $asset_image_service;
$this->face_detection_service = $face_detection_service;
$this->asset_service = $asset_service;
$this->asset_image_service = $asset_image_service;

// Register itself as handler for the hw_fragments shortcode.
add_shortcode( self::HANDLE_NAME, array( $this, 'render' ) );
Expand All @@ -78,8 +78,8 @@ public function render( $atts ) {
$id = $atts['id'];

$html = "<ul>";
foreach ( $this->fragments_service->get_fragments_by_id( $id ) as $fragment ) {
$html .= '<li style="float:left;"><img width="200" src="' . $this->asset_image_service->get_local_image_url_by_id( $id, $fragment->start / 1000 ) . '" />' . $fragment->start . '</li>';
foreach ( $this->face_detection_service->get_fragments_by_id( $id ) as $fragment ) {
$html .= '<li style="float:left;"><img width="200" src="' . $this->asset_image_service->get_local_image_url_by_id( $id, $fragment->start / 1000, $fragment->x, $fragment->y, $fragment->width, $fragment->height ) . '" />' . $fragment->start . "($fragment->x, $fragment->y, $fragment->width, $fragment->height)" . '</li>';
}
$html .= "</ul>";

Expand Down

0 comments on commit 237024a

Please sign in to comment.