-/*****************************************************************************\r
-* eyetvplugin.c: Plug-In for the EyeTV software to connect to VLC\r
-*****************************************************************************\r
-* Copyright (C) 2006-2007 the VideoLAN team\r
-* $Id$\r
-*\r
-* Authors: Felix Kühne <fkuehne at videolan dot org>\r
-*\r
-* This program is free software; you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation; either version 2 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* This program is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with this program; if not, write to the Free Software\r
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.\r
-*****************************************************************************/\r
-\r
-#include "eyetvplugin.h"\r
-\r
-#define MAX_PIDS 256\r
-#define MAX_ACTIVE_PIDS 256\r
-#define MAX_DEVICES 16\r
-#define VLC_NOTIFICATION_OBJECT "VLCEyeTVSupport"\r
-\r
-#pragma push\r
-#pragma pack(1)\r
-\r
-\r
-/* Structure for TS-Packets */\r
-typedef struct \r
-{\r
- unsigned long sync_byte : 8,\r
- transport_error_indicator : 1,\r
- payload_unit_start_indicator : 1,\r
- transport_priority : 1,\r
- PID : 13,\r
- transport_scrambling_control : 2,\r
- adaptation_field_control : 2,\r
- continuity_counter : 4;\r
-\r
- unsigned char data[188-4];\r
-\r
-} TransportStreamPacket;\r
-\r
-#pragma pop\r
-\r
-\r
-/* Structure to hold Information on devices */\r
-typedef struct\r
-{\r
- EyeTVPluginDeviceID deviceID;\r
- EyeTVPluginDeviceType deviceType;\r
-\r
- long headendID;\r
- long transponderID;\r
- long serviceID;\r
-\r
- long pidsCount;\r
- long pids[MAX_PIDS];\r
-\r
- EyeTVPluginPIDInfo activePIDs[MAX_ACTIVE_PIDS];\r
- long activePIDsCount;\r
-\r
-} DeviceInfo;\r
-\r
-\r
-/* Structure to hold global data to communicate with EyeTV */\r
-typedef struct \r
-{\r
- EyeTVPluginCallbackProc callback;\r
- long deviceCount;\r
- DeviceInfo devices[MAX_DEVICES];\r
- long long packetCount;\r
-\r
-} VLCEyeTVPluginGlobals_t;\r
-\r
-/* 2nd structure to store our own global data which isn't shared with EyeTV\r
- * a bit empty at the moment, but it will get larger as development progresses */\r
-typedef struct\r
-{\r
- int i_deviceCount;\r
- CFMessagePortRef messagePortToVLC;\r
- bool b_msgPortOpen;\r
-} VLCEyeTVPluginOwnGlobals_t;\r
-\r
-VLCEyeTVPluginOwnGlobals_t *nativeGlobals;\r
-\r
-\r
-/* return the DeviceInfo with ID deviceID */\r
-static DeviceInfo *GetDeviceInfo(VLCEyeTVPluginGlobals_t *globals, EyeTVPluginDeviceID deviceID)\r
-{\r
- int i;\r
- \r
- if( globals ) \r
- {\r
- for( i=0; i<globals->deviceCount; i++) \r
- {\r
- if( globals->devices[i].deviceID == deviceID ) \r
- {\r
- return &globals->devices[i];\r
- }\r
- }\r
- }\r
-\r
- return NULL;\r
-}\r
-\r
-#pragma mark -\r
-\r
-/* initialise the plug-in */\r
-static long VLCEyeTVPluginInitialize(VLCEyeTVPluginGlobals_t** globals, long apiVersion, EyeTVPluginCallbackProc callback)\r
-{\r
- printf("VLC media player Plug-In: Initialize\n");\r
- long result = 0;\r
- \r
- /* init our own storage */\r
- extern VLCEyeTVPluginOwnGlobals_t *nativeGlobals;\r
- nativeGlobals = malloc( sizeof( VLCEyeTVPluginOwnGlobals_t ) );\r
- \r
- /* notify a potential VLC instance about our initialisation */\r
- CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),\r
- CFSTR("PluginInit"), \r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- /*userInfo*/ NULL, \r
- TRUE );\r
- \r
- /* init our notification support */\r
- CFNotificationCenterAddObserver( CFNotificationCenterGetDistributedCenter(),\r
- /* observer */ NULL, \r
- /* callBack */ VLCEyeTVPluginGlobalNotificationReceived,\r
- /* name, NULL==all */ NULL,\r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- CFNotificationSuspensionBehaviorDeliverImmediately );\r
- \r
- *globals = (VLCEyeTVPluginGlobals_t *) calloc(1, sizeof( VLCEyeTVPluginGlobals_t ) );\r
- ( *globals )->callback = callback;\r
- \r
- return result;\r
-}\r
-\r
-/* we will be terminated soon, clean up */\r
-static long VLCEyeTVPluginTerminate(VLCEyeTVPluginGlobals_t *globals)\r
-{\r
- extern VLCEyeTVPluginOwnGlobals_t *nativeGlobals;\r
- \r
- printf("VLC media player Plug-In: Terminate\n");\r
- \r
- long result = 0;\r
- \r
- /* notify a potential VLC instance about our termination */\r
- CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),\r
- CFSTR("PluginQuit"), \r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- /*userInfo*/ NULL, \r
- TRUE );\r
- \r
- /* remove us from the global notification centre */\r
- CFNotificationCenterRemoveEveryObserver( CFNotificationCenterGetDistributedCenter(),\r
- (void *)VLCEyeTVPluginGlobalNotificationReceived );\r
- \r
- /* invalidate and free msg port */\r
- if( nativeGlobals->messagePortToVLC )\r
- {\r
- CFMessagePortInvalidate( nativeGlobals->messagePortToVLC );\r
- free( nativeGlobals->messagePortToVLC );\r
- printf( "msgport invalidated and freed\n" );\r
- }\r
- else\r
- printf( "no msgport to free\n" );\r
- \r
- if( globals ) \r
- {\r
- free( globals );\r
- }\r
-\r
- if( nativeGlobals )\r
- free( nativeGlobals );\r
- \r
- return result;\r
-}\r
-\r
-/* called when EyeTV asks various stuff about us */\r
-static long VLCEyeTVPluginGetInformation(VLCEyeTVPluginGlobals_t *globals, long* outAPIVersion, char* outName, char *outDescription)\r
-{\r
- printf("VLC media player Plug-In: GetInfo\n");\r
- long result = 0;\r
- \r
- if( globals ) \r
- {\r
- if( outAPIVersion )\r
- {\r
- *outAPIVersion = EYETV_PLUGIN_API_VERSION;\r
- }\r
- \r
- if( outName )\r
- {\r
- char* name = "VLC media player Plug-In";\r
- strcpy( &outName[0], name);\r
- }\r
- \r
- if( outDescription )\r
- {\r
- char* desc = "This Plug-In connects EyeTV to the VLC media player for streaming purposes.";\r
- strcpy( &outDescription[0], desc);\r
- }\r
- }\r
- \r
- return result;\r
-}\r
-\r
-/* called if we received a global notification */\r
-void VLCEyeTVPluginGlobalNotificationReceived( CFNotificationCenterRef center, \r
- void *observer, \r
- CFStringRef name, \r
- const void *object, \r
- CFDictionaryRef userInfo )\r
-{\r
- CFIndex maxlen;\r
- char *theName, *theObject;\r
- extern VLCEyeTVPluginOwnGlobals_t *nativeGlobals;\r
-\r
- maxlen = CFStringGetMaximumSizeForEncoding( CFStringGetLength( name ),\r
- kCFStringEncodingUTF8) + 1;\r
- theName = malloc(maxlen);\r
- CFStringGetCString( name, \r
- theName, \r
- maxlen,\r
- kCFStringEncodingUTF8);\r
- \r
- maxlen = CFStringGetMaximumSizeForEncoding( CFStringGetLength( name ),\r
- kCFStringEncodingUTF8) + 1;\r
- theObject = malloc(maxlen);\r
- CFStringGetCString( object, \r
- theObject, \r
- maxlen,\r
- kCFStringEncodingUTF8);\r
- printf( "notication received with name: %s and object: %s\n", theName, theObject );\r
- \r
- /* when VLC launches after us, we need to inform it about our existance and the current state of available devices */\r
- if( CFStringCompare( name, CFSTR( "VLCOSXGUIInit" ), 0) == kCFCompareEqualTo )\r
- {\r
- /* we're here */\r
- CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),\r
- CFSTR("PluginInit"), \r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- /*userInfo*/ NULL, \r
- TRUE );\r
- if( nativeGlobals && ( nativeGlobals->i_deviceCount > 0 ) )\r
- {\r
- /* at least one device is apparently connected */\r
- CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),\r
- CFSTR("DeviceAdded"), \r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- /*userInfo*/ NULL, \r
- TRUE );\r
- }\r
- }\r
- \r
- /* VLC wants us to start sending data */\r
- if( CFStringCompare( name, CFSTR( "VLCAccessStartDataSending" ), 0) == kCFCompareEqualTo )\r
- {\r
- nativeGlobals->messagePortToVLC = CFMessagePortCreateRemote( kCFAllocatorDefault,\r
- CFSTR("VLCEyeTVMsgPort") );\r
- if( nativeGlobals->messagePortToVLC == NULL )\r
- printf( "getting messagePortToVLC failed!\n" );\r
- else\r
- {\r
- nativeGlobals->b_msgPortOpen = TRUE;\r
- printf( "msg port opened / data sending switched on\n" );\r
- }\r
- }\r
- \r
- /* VLC wants us to stop sending data */\r
- if( CFStringCompare( name, CFSTR( "VLCAccessStopDataSending" ), 0) == kCFCompareEqualTo )\r
- {\r
- nativeGlobals->b_msgPortOpen = FALSE;\r
- printf( "data sending switched off\n" );\r
- }\r
-}\r
-\r
-/* called if a device is added */\r
-static long VLCEyeTVPluginDeviceAdded(VLCEyeTVPluginGlobals_t *globals, EyeTVPluginDeviceID deviceID, EyeTVPluginDeviceType deviceType)\r
-{\r
- printf("VLC media player Plug-In: Device with type %i and ID %i added\n", (int)deviceType, (int)deviceID);\r
- \r
- long result = 0;\r
- DeviceInfo *deviceInfo;\r
- extern VLCEyeTVPluginOwnGlobals_t *nativeGlobals;\r
- \r
- \r
- if( globals ) \r
- {\r
- if( globals->deviceCount < MAX_DEVICES ) \r
- {\r
- deviceInfo = &( globals->devices[globals->deviceCount] );\r
- memset(deviceInfo, 0, sizeof(DeviceInfo));\r
- \r
- deviceInfo->deviceID = deviceID;\r
- deviceInfo->deviceType = deviceType;\r
-\r
- globals->deviceCount++;\r
-\r
- if( nativeGlobals )\r
- nativeGlobals->i_deviceCount = globals->deviceCount;\r
-\r
- /* notify a potential VLC instance about the addition */\r
- CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),\r
- CFSTR("DeviceAdded"), \r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- /*userInfo*/ NULL, \r
- TRUE );\r
- }\r
- }\r
-\r
- return result;\r
-}\r
-\r
-/* called if a device is removed */\r
-static long VLCEyeTVPluginDeviceRemoved(VLCEyeTVPluginGlobals_t *globals, EyeTVPluginDeviceID deviceID)\r
-{\r
- printf("VLC media player Plug-In: DeviceRemoved\n");\r
- \r
- extern VLCEyeTVPluginOwnGlobals_t *nativeGlobals;\r
- long result = 0;\r
- int i;\r
- \r
- if( globals ) \r
- {\r
- for( i = 0; i < globals->deviceCount; i++ )\r
- {\r
- if ( globals->devices[i].deviceID == deviceID ) \r
- {\r
- globals->deviceCount--;\r
-\r
- if( i<globals->deviceCount )\r
- {\r
- globals->devices[i] = globals->devices[globals->deviceCount];\r
- }\r
- \r
- if( nativeGlobals )\r
- nativeGlobals->i_deviceCount = globals->deviceCount;\r
- \r
- /* notify a potential VLC instance about the removal */\r
- CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),\r
- CFSTR("DeviceRemoved"), \r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- /*userInfo*/ NULL, \r
- TRUE );\r
- }\r
- }\r
- }\r
- \r
- return result;\r
-}\r
-\r
-/* This function is called, whenever packets are received by EyeTV. For reasons of performance,\r
- * the data is the original data, not a copy. That means, EyeTV waits until this method is \r
- * finished. Therefore all in this method should be as fast as possible. */\r
-int i=0;\r
-static long VLCEyeTVPluginPacketsArrived(VLCEyeTVPluginGlobals_t *globals, EyeTVPluginDeviceID deviceID, long **packets, long packetsCount)\r
-{\r
- long result = 0;\r
- int i, j, isNewPID;\r
- TransportStreamPacket *packet;\r
- extern VLCEyeTVPluginOwnGlobals_t *nativeGlobals;\r
- SInt32 i_returnValue;\r
- CFMutableDataRef theMutableRef;\r
- uint8_t *p_bufferForSending = malloc(4);\r
- bool b_nonSendData;\r
- int i_lastSentPacket;\r
- \r
- if( globals && nativeGlobals ) \r
- {\r
- DeviceInfo *deviceInfo = GetDeviceInfo(globals, deviceID);\r
-\r
- if( deviceInfo ) \r
- {\r
- /* alloc the buffer if wanted */\r
- if( nativeGlobals->b_msgPortOpen == TRUE )\r
- theMutableRef = CFDataCreateMutable( kCFAllocatorDefault, (188) );\r
- \r
- for( i = 0; i < packetsCount; i++ ) \r
- {\r
- packet = ( TransportStreamPacket* )packets[i];\r
- isNewPID = 1;\r
- \r
- /* search for PID */\r
- for( j = 0; j < deviceInfo->pidsCount; j++ ) \r
- {\r
- if( packet->PID == deviceInfo->pids[j] ) \r
- {\r
- isNewPID = 0;\r
- break;\r
- }\r
- }\r
- \r
- /* add new PIDs to the DeviceInfo */\r
- if( isNewPID ) \r
- {\r
- printf ("VLC media player Plug-In: SamplePacketsArrived, newPID = %6d\n", packet->PID);\r
- \r
- if( deviceInfo->pidsCount < MAX_PIDS ) \r
- {\r
- deviceInfo->pids[deviceInfo->pidsCount++] = packet->PID;\r
- }\r
- }\r
- else\r
- {\r
- /* forward data to VLC if wanted */\r
- /* FIXME: we only receive ARD for now */\r
- if( nativeGlobals->b_msgPortOpen == TRUE && (\r
- packet->PID == 1401 ||\r
- packet->PID == 1402 ||\r
- packet->PID == 1400 ||\r
- packet->PID == 1404 ||\r
- packet->PID == 3070 ||\r
- packet->PID == 3072 ||\r
- packet->PID == 3074 ||\r
- packet->PID == 5074 ||\r
- packet->PID == 0 ||\r
- packet->PID == 17 ||\r
- packet->PID == 19 ||\r
- packet->PID == 20 ) )\r
- {\r
- /* in a good world, this wouldn't be necessary */\r
- if( theMutableRef == NULL )\r
- theMutableRef = CFDataCreateMutable( kCFAllocatorDefault, (188) );\r
- \r
- /* collect data to send larger packets */\r
- \r
- /* enlarge buffer if necessary */\r
- if( i > 0 )\r
- CFDataIncreaseLength( theMutableRef, 188 );\r
- \r
- /* add missing header */\r
- memcpy( p_bufferForSending, packet, 4 );\r
- CFDataAppendBytes( theMutableRef, p_bufferForSending, sizeof(p_bufferForSending) );\r
-\r
- free( p_bufferForSending );\r
- p_bufferForSending = malloc(4);\r
- \r
- /* add payload */\r
- CFDataAppendBytes( theMutableRef, packet->data, sizeof(packet->data) );\r
- \r
- b_nonSendData = TRUE;\r
- \r
- }\r
- }\r
- \r
- globals->packetCount++;\r
- \r
- if( globals->packetCount%10000 == 0 ) \r
- printf("-> %lld Packets received so far...\n",globals->packetCount);\r
- }\r
-\r
- if( nativeGlobals->b_msgPortOpen == TRUE )\r
- {\r
- printf( "sending %i bytes of data\n", CFDataGetLength( theMutableRef ) );\r
- i_returnValue = CFMessagePortSendRequest( nativeGlobals->messagePortToVLC,\r
- /* arbitrary int val */ globals->packetCount,\r
- /* the data */ theMutableRef,\r
- /* no timeout for sending */ 0,\r
- /* no timeout for resp */ 0,\r
- /* no resp. wanted */ NULL,\r
- NULL );\r
- b_nonSendData = FALSE;\r
- i_lastSentPacket = globals->packetCount;\r
- if( i_returnValue == kCFMessagePortSendTimeout )\r
- printf( "time out while sending\n" );\r
- else if( i_returnValue == kCFMessagePortReceiveTimeout )\r
- printf( "time out while waiting for resp\n" );\r
- else if( i_returnValue == kCFMessagePortIsInvalid )\r
- {\r
- /* suppress any further attemps */ \r
- printf( "message port is invalid!\n" );\r
- nativeGlobals->b_msgPortOpen = FALSE;\r
- }\r
- else if( i_returnValue == kCFMessagePortTransportError ) \r
- printf( "transport error while sending!\n" );\r
- else\r
- {\r
- //printf( "success, freeing resources\n" );\r
- free( theMutableRef );\r
- theMutableRef = CFDataCreateMutable( kCFAllocatorDefault, (188) );\r
- }\r
- }\r
-\r
- }\r
- }\r
- else\r
- printf( "warning: either globals or nativeGlobals are NIL in VLCEyeTVPluginPacketsArrived" );\r
-\r
- /* clean up before leaving function */\r
- //if( nativeGlobals->b_msgPortOpen == TRUE )\r
- // free( theMutableRef );\r
- \r
- free( p_bufferForSending );\r
- \r
- return result;\r
-}\r
-\r
-/* VLCEyeTVPluginServiceChanged,\r
- *\r
- * - *globals : The plug-in Globals\r
- * - deviceID : Identifies the active Device\r
- * - headendID : The HeadendID, for e300 it's the orbital position of the satelite in \r
- * tenth degrees east\r
- * - transponderID : The Frequency in kHz\r
- * - serviceID : original ServiceID from the DVB-Stream (e300, e400)\r
- * - pidList : List of active PIDs \r
- *\r
- * Whenever a service changes, this function is called. Service-related plug-in data should be updated here.\r
- */\r
-static long VLCEyeTVPluginServiceChanged(VLCEyeTVPluginGlobals_t *globals, \r
- EyeTVPluginDeviceID deviceID, \r
- long headendID, \r
- long transponderID, \r
- long serviceID, \r
- EyeTVPluginPIDInfo *pidList, \r
- long pidsCount)\r
-{\r
- long result = 0;\r
- int i;\r
- \r
- printf("\nVLC media player Plug-In: ServiceChanged:\n");\r
- printf( "=====================================\n");\r
- \r
- if( globals ) \r
- {\r
- DeviceInfo *deviceInfo = GetDeviceInfo( globals, deviceID );\r
- if( deviceInfo ) \r
- {\r
- deviceInfo->headendID = headendID;\r
- printf("HeadendID: %ld, ", headendID);\r
- \r
- deviceInfo->transponderID = transponderID;\r
- printf("TransponderID: %ld, ", transponderID);\r
- \r
- deviceInfo->serviceID = serviceID;\r
- printf("ServiceID: %ld\n\n", serviceID);\r
- \r
- deviceInfo->activePIDsCount = pidsCount;\r
-\r
- for( i = 0; i < pidsCount; i++ )\r
- {\r
- deviceInfo->activePIDs[i] = pidList[i];\r
- printf("Active PID: %ld, type: %ld\n", pidList[i].pid, pidList[i].pidType);\r
- }\r
-\r
- deviceInfo->pidsCount = 0;\r
- \r
- }\r
- }\r
- printf( "=====================================\n");\r
- \r
- /* notify a potential VLC instance about the service change */\r
- CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),\r
- CFSTR("ServiceChanged"), \r
- CFSTR(VLC_NOTIFICATION_OBJECT), \r
- /*userInfo*/ NULL, \r
- TRUE );\r
- \r
- return result;\r
-}\r
-\r
-\r
-#pragma mark -\r
-/* EyeTVPluginDispatcher,\r
- *\r
- * - selector : See 'EyeTVPluginDefs.h'\r
- * - *refCon : The RefCon to the plug-in-related Data\r
- * - deviceID : Identifies the Device\r
- * - params : Parameters for functioncall\r
- *\r
- * This function is a part of the interface for the communication with EyeTV. If something happens,\r
- * EyeTV thinks, we should know of, it calls this function with the corresponding selector. */\r
-\r
-#pragma export on\r
-\r
-long EyeTVPluginDispatcher( EyeTVPluginParams* params )\r
-{\r
- long result = 0;\r
-\r
- switch( params->selector ) \r
- {\r
- case kEyeTVPluginSelector_Initialize:\r
- result = VLCEyeTVPluginInitialize((VLCEyeTVPluginGlobals_t**)params->refCon, \r
- params->initialize.apiVersion, params->initialize.callback);\r
- break;\r
- \r
- case kEyeTVPluginSelector_Terminate:\r
- result = VLCEyeTVPluginTerminate((VLCEyeTVPluginGlobals_t*)params->refCon);\r
- break;\r
-\r
- case kEyeTVPluginSelector_GetInfo:\r
- result = VLCEyeTVPluginGetInformation((VLCEyeTVPluginGlobals_t*)params->refCon, \r
- params->info.pluginAPIVersion, params->info.pluginName, params->info.description);\r
- break;\r
-\r
- case kEyeTVPluginSelector_DeviceAdded:\r
- result = VLCEyeTVPluginDeviceAdded((VLCEyeTVPluginGlobals_t*)params->refCon, \r
- params->deviceID, params->deviceAdded.deviceType);\r
- break;\r
- \r
- case kEyeTVPluginSelector_DeviceRemoved:\r
- result = VLCEyeTVPluginDeviceRemoved((VLCEyeTVPluginGlobals_t*)params->refCon, params->deviceID);\r
- break;\r
-\r
- case kEyeTVPluginSelector_PacketsArrived:\r
- result = VLCEyeTVPluginPacketsArrived((VLCEyeTVPluginGlobals_t*)params->refCon, params->deviceID, \r
- params->packetsArrived.packets, params->packetsArrived.packetCount);\r
- break;\r
-\r
- case kEyeTVPluginSelector_ServiceChanged:\r
- result = VLCEyeTVPluginServiceChanged((VLCEyeTVPluginGlobals_t*)params->refCon, \r
- params->deviceID, params->serviceChanged.headendID, \r
- params->serviceChanged.transponderID, params->serviceChanged.serviceID, \r
- params->serviceChanged.pidList, params->serviceChanged.pidCount);\r
- break;\r
- }\r
-\r
- return result;\r
-}\r
+/*****************************************************************************
+* eyetvplugin.c: Plug-In for the EyeTV software to connect to VLC
+*****************************************************************************
+* Copyright (C) 2006-2007 the VideoLAN team
+* $Id$
+*
+* Authors: Felix Kühne <fkuehne at videolan dot org>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+*****************************************************************************/
+
+#include "eyetvplugin.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#define MAX_PIDS 256
+#define MAX_ACTIVE_PIDS 256
+#define MAX_DEVICES 16
+#define VLC_NOTIFICATION_OBJECT "VLCEyeTVSupport"
+
+#pragma push
+#pragma pack(1)
+
+typedef struct
+{
+ uint32_t sync_byte : 8,
+ transport_error_indicator : 1,
+ payload_unit_start_indicator : 1,
+ transport_priority : 1,
+ PID : 13,
+ transport_scrambling_control : 2,
+ adaptation_field_control : 2,
+ continuity_counter : 4;
+} TransportStreamHeader;
+
+/* Structure for TS-Packets */
+typedef struct
+{
+ TransportStreamHeader header;
+ uint8_t payload[184];
+
+} TransportStreamPacket;
+
+#pragma pop
+
+
+/* Structure to hold global data to communicate with EyeTV */
+typedef struct
+{
+ EyeTVPluginCallbackProc callback;
+ /* Structure to hold current active service */
+ EyeTVPluginDeviceID activeDeviceID;
+ long activePIDsCount;
+ EyeTVPluginPIDInfo activePIDs[MAX_ACTIVE_PIDS];
+ long seenPIDs[MAX_ACTIVE_PIDS];
+} VLCEyeTVPluginGlobals_t;
+
+static int i_deviceCount;
+static int vlcSock;
+
+#pragma mark -
+
+/* initialise the plug-in */
+static long VLCEyeTVPluginInitialize(VLCEyeTVPluginGlobals_t** globals, long apiVersion, EyeTVPluginCallbackProc callback)
+{
+ printf("VLC media player Plug-In: Initialize\n");
+ long result = 0;
+
+ /* init our own storage */
+ i_deviceCount = 0;
+ vlcSock = -1;
+
+ /* notify a potential VLC instance about our initialisation */
+ CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),
+ CFSTR("PluginInit"),
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ /*userInfo*/ NULL,
+ TRUE );
+
+ /* init our notification support */
+ CFNotificationCenterAddObserver( CFNotificationCenterGetDistributedCenter(),
+ /* observer */ NULL,
+ /* callBack */ VLCEyeTVPluginGlobalNotificationReceived,
+ /* name, NULL==all */ NULL,
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ CFNotificationSuspensionBehaviorDeliverImmediately );
+
+ *globals = (VLCEyeTVPluginGlobals_t *) calloc(1, sizeof( VLCEyeTVPluginGlobals_t ) );
+ ( *globals )->callback = callback;
+
+ return result;
+}
+
+/* we will be terminated soon, clean up */
+static long VLCEyeTVPluginTerminate(VLCEyeTVPluginGlobals_t *globals)
+{
+ long result = 0;
+
+ printf("VLC media player Plug-In: Terminate\n");
+
+ /* notify a potential VLC instance about our termination */
+ CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),
+ CFSTR("PluginQuit"),
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ /*userInfo*/ NULL,
+ TRUE );
+
+ /* remove us from the global notification centre */
+ CFNotificationCenterRemoveEveryObserver( CFNotificationCenterGetDistributedCenter(),
+ (void *)VLCEyeTVPluginGlobalNotificationReceived );
+
+ /* close data connection */
+ if( vlcSock != -1 )
+ {
+ close( vlcSock );
+ vlcSock = -1;
+ }
+
+ if( globals )
+ {
+ long i;
+ for( i=0; i<globals->activePIDsCount; ++i )
+ {
+ printf("activePID: %ld, count=%ld\n", globals->activePIDs[i].pid, globals->seenPIDs[i] );
+ }
+ free( globals );
+ }
+
+ return result;
+}
+
+/* called when EyeTV asks various stuff about us */
+static long VLCEyeTVPluginGetInformation(VLCEyeTVPluginGlobals_t *globals, long* outAPIVersion, char* outName, char *outDescription)
+{
+ printf("VLC media player Plug-In: GetInfo\n");
+ long result = 0;
+
+ if( globals )
+ {
+ if( outAPIVersion )
+ {
+ *outAPIVersion = EYETV_PLUGIN_API_VERSION;
+ }
+
+ if( outName )
+ {
+ strcpy( outName, "VLC media player Plug-In");
+ }
+
+ if( outDescription )
+ {
+ strcpy( outDescription, "This Plug-In connects EyeTV to the VLC media player for streaming purposes.");
+ }
+ }
+
+ return result;
+}
+
+/* called if we received a global notification */
+void VLCEyeTVPluginGlobalNotificationReceived( CFNotificationCenterRef center,
+ void *observer,
+ CFStringRef name,
+ const void *object,
+ CFDictionaryRef userInfo )
+{
+ /* when VLC launches after us, we need to inform it about our existance and the current state of available devices */
+ if( CFStringCompare( name, CFSTR( "VLCOSXGUIInit" ), 0) == kCFCompareEqualTo )
+ {
+ /* we're here */
+ CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),
+ CFSTR("PluginInit"),
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ /*userInfo*/ NULL,
+ TRUE );
+ if( i_deviceCount > 0 )
+ {
+ /* at least one device is apparently connected */
+ CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),
+ CFSTR("DeviceAdded"),
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ /*userInfo*/ NULL,
+ TRUE );
+ }
+ }
+
+ /* VLC wants us to start sending data */
+ if( CFStringCompare( name, CFSTR( "VLCAccessStartDataSending" ), 0) == kCFCompareEqualTo )
+ {
+ if( vlcSock == -1 )
+ {
+ int peerSock;
+
+ /* set-up data socket */
+ peerSock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if( peerSock != -1 )
+ {
+ struct sockaddr_un peerAddr;
+ /* set-up connection address */
+ memset(&peerAddr, 0, sizeof(peerAddr));
+ peerAddr.sun_family = AF_UNIX;
+ strncpy(peerAddr.sun_path, "/tmp/.vlc-eyetv-bridge", sizeof(peerAddr.sun_path)-1);
+
+ /* connect */
+ printf("data connect in progess...\n");
+ if( connect(peerSock, (struct sockaddr *)&peerAddr, sizeof(struct sockaddr_un)) != -1 )
+ {
+ printf("data sending switched on\n");
+ vlcSock = peerSock;
+ }
+ else
+ printf("connect data socket failed (errno=%d)\n", errno );
+ }
+ else
+ printf("create data socket failed (errno=%d)\n", errno );
+ }
+ }
+
+ /* VLC wants us to stop sending data */
+ if( CFStringCompare( name, CFSTR( "VLCAccessStopDataSending" ), 0) == kCFCompareEqualTo )
+ {
+ if( vlcSock != -1 )
+ {
+ close( vlcSock );
+ vlcSock = -1;
+ printf( "data sending switched off\n" );
+ }
+ }
+}
+
+/* called if a device is added */
+static long VLCEyeTVPluginDeviceAdded(VLCEyeTVPluginGlobals_t *globals, EyeTVPluginDeviceID deviceID, EyeTVPluginDeviceType deviceType)
+{
+ printf("VLC media player Plug-In: Device with type %i and ID %i added\n", (int)deviceType, (int)deviceID);
+
+ long result = 0;
+
+ if( globals )
+ {
+ ++i_deviceCount;
+ if( 1 == i_deviceCount )
+ {
+ /* notify a potential VLC instance about the addition */
+ CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),
+ CFSTR("DeviceAdded"),
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ /*userInfo*/ NULL,
+ TRUE );
+ }
+ }
+ return result;
+}
+
+/* called if a device is removed */
+static long VLCEyeTVPluginDeviceRemoved(VLCEyeTVPluginGlobals_t *globals, EyeTVPluginDeviceID deviceID)
+{
+ printf("VLC media player Plug-In: DeviceRemoved\n");
+
+ long result = 0;
+
+ --i_deviceCount;
+ if( 0 == i_deviceCount )
+ {
+ /* notify a potential VLC instance about the removal */
+ CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),
+ CFSTR("DeviceRemoved"),
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ /*userInfo*/ NULL,
+ TRUE );
+ }
+ if( (vlcSock != -1) && (deviceID == globals->activeDeviceID) )
+ {
+ close(vlcSock);
+ vlcSock = -1;
+ printf( "data sending switched off\n" );
+ }
+
+ return result;
+}
+
+/* This function is called, whenever packets are received by EyeTV. For reasons of performance,
+ * the data is the original data, not a copy. That means, EyeTV waits until this method is
+ * finished. Therefore all in this method should be as fast as possible. */
+static long VLCEyeTVPluginPacketsArrived(VLCEyeTVPluginGlobals_t *globals, EyeTVPluginDeviceID deviceID, long **packets, long packetsCount)
+{
+ if( globals )
+ {
+ /* check if data connection is active */
+ if( vlcSock != -1 )
+ {
+ if( deviceID == globals->activeDeviceID )
+ {
+ long pidCount = globals->activePIDsCount;
+ if( pidCount )
+ {
+ while( packetsCount )
+ {
+ /* apply PID filtering, only PIDs in active service for device are sent through */
+ long pid = (ntohl(**packets) & 0x001FFF00L)>>8;
+ long i;
+ for( i=0; i<pidCount; ++i )
+ {
+ if( globals->activePIDs[i].pid == pid )
+ {
+ ssize_t sent = write(vlcSock, *packets, sizeof(TransportStreamPacket));
+ if( sent != sizeof(TransportStreamPacket) )
+ {
+ if( sent == -1 )
+ printf("data sending failed (errno=%d)\n", errno);
+ else
+ printf("data sending incomplete (sent=%d)\n", sent);
+ close(vlcSock);
+ vlcSock = -1;
+ return 0;
+ }
+ ++(globals->seenPIDs[i]);
+#if 0
+ if( i > 0 )
+ {
+ /* if we assume that consecutive packets should have the same PID, it would therefore
+ speed up filtering to reorder activePIDs list based on pid occurrences */
+ EyeTVPluginPIDInfo swap = globals->activePIDs[i];
+ memmove(globals->activePIDs+1, globals->activePIDs, sizeof(EyeTVPluginPIDInfo)*i);
+ globals->activePIDs[0] = swap;
+ }
+
+ if( pid && filterPidInfo.pidType != kEyeTVPIDType_PMT )
+ {
+ /* to save on CPU, prevent EyeTV from mirroring that program by blocking video & audio packets
+ by changing PID to NULL PID */
+#if defined(WORDS_BIGENDIAN)
+ **packets |= 0x001FFF00L;
+#else
+ **packets |= 0x00FFF800L;
+#endif
+ }
+#endif
+ /* done filtering on this packet, move on to next packet */
+ break;
+ }
+ }
+ if( i == pidCount )
+ printf("unexpected PID %ld\n", pid);
+ }
+ --packetsCount;
+ ++packets;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+/* VLCEyeTVPluginServiceChanged,
+ *
+ * - *globals : The plug-in Globals
+ * - deviceID : Identifies the active Device
+ * - headendID : The HeadendID, for e300 it's the orbital position of the satelite in
+ * tenth degrees east
+ * - transponderID : The Frequency in kHz
+ * - serviceID : original ServiceID from the DVB-Stream (e300, e400)
+ * - pidList : List of active PIDs
+ *
+ * Whenever a service changes, this function is called. Service-related plug-in data should be updated here.
+ */
+static long VLCEyeTVPluginServiceChanged(VLCEyeTVPluginGlobals_t *globals,
+ EyeTVPluginDeviceID deviceID,
+ long headendID,
+ long transponderID,
+ long serviceID,
+ EyeTVPluginPIDInfo *pidList,
+ long pidsCount)
+{
+ long result = 0;
+ int i;
+
+ printf("\nVLC media player Plug-In: ServiceChanged:\n");
+ printf( "=====================================\n");
+
+ if( globals )
+ {
+ printf("DeviceID: %ld, ", deviceID);
+ printf("HeadendID: %ld, ", headendID);
+ printf("TransponderID: %ld, ", transponderID);
+ printf("ServiceID: %ld\n\n", serviceID);
+
+ globals->activeDeviceID = deviceID;
+ globals->activePIDsCount = pidsCount;
+
+ for( i = 0; i < pidsCount; i++ )
+ {
+ globals->activePIDs[i] = pidList[i];
+ globals->seenPIDs[i] = 0;
+ printf("Active PID: %ld, type: %ld\n", pidList[i].pid, pidList[i].pidType);
+ }
+ }
+ printf( "=====================================\n");
+
+ /* notify a potential VLC instance about the service change */
+ CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter(),
+ CFSTR("ServiceChanged"),
+ CFSTR(VLC_NOTIFICATION_OBJECT),
+ /*userInfo*/ NULL,
+ TRUE );
+
+ return result;
+}
+
+
+#pragma mark -
+/* EyeTVPluginDispatcher,
+ *
+ * - selector : See 'EyeTVPluginDefs.h'
+ * - *refCon : The RefCon to the plug-in-related Data
+ * - deviceID : Identifies the Device
+ * - params : Parameters for functioncall
+ *
+ * This function is a part of the interface for the communication with EyeTV. If something happens,
+ * EyeTV thinks, we should know of, it calls this function with the corresponding selector. */
+
+#pragma export on
+
+long EyeTVPluginDispatcher( EyeTVPluginParams* params )
+{
+ long result = 0;
+
+ switch( params->selector )
+ {
+ case kEyeTVPluginSelector_Initialize:
+ result = VLCEyeTVPluginInitialize((VLCEyeTVPluginGlobals_t**)params->refCon,
+ params->initialize.apiVersion, params->initialize.callback);
+ break;
+
+ case kEyeTVPluginSelector_Terminate:
+ result = VLCEyeTVPluginTerminate((VLCEyeTVPluginGlobals_t*)params->refCon);
+ break;
+
+ case kEyeTVPluginSelector_GetInfo:
+ result = VLCEyeTVPluginGetInformation((VLCEyeTVPluginGlobals_t*)params->refCon,
+ params->info.pluginAPIVersion, params->info.pluginName, params->info.description);
+ break;
+
+ case kEyeTVPluginSelector_DeviceAdded:
+ result = VLCEyeTVPluginDeviceAdded((VLCEyeTVPluginGlobals_t*)params->refCon,
+ params->deviceID, params->deviceAdded.deviceType);
+ break;
+
+ case kEyeTVPluginSelector_DeviceRemoved:
+ result = VLCEyeTVPluginDeviceRemoved((VLCEyeTVPluginGlobals_t*)params->refCon, params->deviceID);
+ break;
+
+ case kEyeTVPluginSelector_PacketsArrived:
+ result = VLCEyeTVPluginPacketsArrived((VLCEyeTVPluginGlobals_t*)params->refCon, params->deviceID,
+ params->packetsArrived.packets, params->packetsArrived.packetCount);
+ break;
+
+ case kEyeTVPluginSelector_ServiceChanged:
+ result = VLCEyeTVPluginServiceChanged((VLCEyeTVPluginGlobals_t*)params->refCon,
+ params->deviceID, params->serviceChanged.headendID,
+ params->serviceChanged.transponderID, params->serviceChanged.serviceID,
+ params->serviceChanged.pidList, params->serviceChanged.pidCount);
+ break;
+ }
+
+ return result;
+}