From 0a517afdf8c4824d2961c477bda753ca81e800db Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 3 Nov 2002 11:53:58 +0100 Subject: [PATCH] Implemented audio plugin interface --- HISTORY | 21 ++++++++++++- PLUGINS.html | 78 +++++++++++++++++++++++++++++++++++++-------- audio.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++- audio.h | 44 +++++++++++++++++++++++++- device.c | 14 ++++++--- device.h | 10 ++++-- dvbdevice.c | 8 ++--- dvbdevice.h | 4 +-- dvbplayer.c | 4 +-- player.c | 11 ++++--- player.h | 6 ++-- transfer.c | 4 +-- vdr.c | 14 ++++++--- 13 files changed, 263 insertions(+), 44 deletions(-) diff --git a/HISTORY b/HISTORY index 8d62f406..96eb0706 100644 --- a/HISTORY +++ b/HISTORY @@ -1671,7 +1671,7 @@ Video Disk Recorder Revision History buttons of those that have already been learned (thanks to Oliver Endriss for reporting this one). -2002-11-02: Version 1.1.15 +2002-11-03: Version 1.1.15 - Completely switched to the new CVS HEAD version of the linux-dvb driver. The NEWSTRUCT compile time switch is now obsolete. The required driver is now @@ -1697,3 +1697,22 @@ Video Disk Recorder Revision History your existing plugin directory - make a backup copy first!). - Fixed reading EPG data via the SVDRP command PUTE (it changed the current service ID). +- Fixed closing all dup'ed file descriptors in cPipe. +- Implemented a plugin interface for additional audio processing (see PLUGINS.html + under "Dolby Digital"). + The functionality of the '-a' command line option has been reactivated. + Since the author doesn't have any Dolby Digital equipment this may or may not + work as expected. There may still be some places where a call to cAudios::Clear() + or cAudios::Mute() is necessary. Those with Dolby Digital equipment should please + take a look at this and maybe send patches. + Replaying Dolby Digital in ways other than through VDR's '-a' option will have to + be implemented as plugins. Those who have written patches for VDR version 1.0.x + should convert their work into the proper plugins for version 1.1.x. + Note to authors of cPlayer derived plugins: please read the modified comments + in device.h regarding the member functions cDevice::Clear(), cDevice::Mute() and + cDevice::PlayAudio(). Derived classes must call these base class member functions + to make sure all registered cAudio objects are properly handled. Also note that + the return type of cDevice::PlayAudio() has been changed to 'void', since this + function always has to accept the entire data block immediately and there is + nothing that could be reasonably done in case an error occurs in one of the + cAudio objects. diff --git a/PLUGINS.html b/PLUGINS.html index e71d8b5a..e5948868 100644 --- a/PLUGINS.html +++ b/PLUGINS.html @@ -21,18 +21,18 @@ VDR program and present itself to the user. The inside interface provides the plugin code access to VDR's internal data structures and allows it to hook itself into specific areas to perform special actions.

-
  -Important modifications introduced in version 1.1.11 are marked like this. -
-
  +
  Important modifications introduced in version 1.1.12 are marked like this.
-
  +
  Important modifications introduced in version 1.1.13 are marked like this.
-
  +
  Important modifications introduced in version 1.1.14 are marked like this.
+
  +Important modifications introduced in version 1.1.15 are marked like this. +

Part I - The Outside Interface

@@ -964,7 +964,7 @@ bool DevicePoll(cPoller &Poller, int TimeoutMs = 0);

to determine whether the device is ready for further data. -
  +
 

If the player can provide more than a single audio track, it can implement the following functions to make them available: @@ -977,7 +977,16 @@ virtual void SetAudioTrack(int Index);

-TODO: PlayAudio()??? +
  +If there is an additional audio track that has to be replayed with external hardware, +the player shall call its member function + +


+void PlayAudio(const uchar *Data, int Length); +

+ +where Data points to a complete audio PES packet of Length bytes. +

The second part needed here is a control object that receives user input from the main program loop and reacts on this by telling the player what to do: @@ -1196,7 +1205,7 @@ If the new device can receive, it most likely needs to provide a way of selecting which channel it shall tune to:


-
  +
  virtual bool ProvidesSource(int Source) const;
virtual bool ProvidesChannel(const cChannel *Channel, int Priority = -1, bool *NeedsDetachReceivers = NULL); @@ -1206,7 +1215,7 @@ virtual bool SetChannelDevice(const cChannel *Channel, bool LiveView); These functions will be called with the desired source or channel and shall return whether this device can provide the requested source or channel and whether tuning to it was successful, repectively. -
  +
 

Audio selection

@@ -1253,7 +1262,7 @@ The functions to implement replaying capabilites are


virtual bool HasDecoder(void) const; -
  +
  virtual bool CanReplay(void) const;
virtual bool SetPlayMode(ePlayMode PlayMode); @@ -1315,7 +1324,51 @@ shut down (delete) all devices when the program terminates. It is therefore important that the devices are created on the heap, using the new operator! -
  +
  +

Dolby Digital

+ +
"The stereo effect may only be experienced if stereo equipment is used!"

+ +There are many different ways to replay additional audio tracks, like Dolby Digital. +So VDR offers a plugin interface that allows for the implementation of any kind of +audio replay facility. +

+To implement a new audio output facility, simply derive a class from cAudio, +as in + +


+#include <vdr/audio.h> +#include <vdr/thread.h> + +class cMyAudio : public cAudio, private cThread { +private: + virtual void Action(void); +public: + cMyAudio(void); + virtual void Play(const uchar *Data, int Length); + virtual void Mute(bool On); + virtual void Clear(void); + }; +

+ +You should create your derived audio object in the +Start() function of your plugin. +Note that the object has to be created on the heap (using new), +and you shall not delete it at any point (it will be deleted automatically +when the program ends). +

+The Play() function will be offered complete audio PES packets +and has to accept each packet immediately. It must return as soon as possible, +in order to not delay the overall replay process. Therefore you may want to +also derive your class from cThread and run the actual audio processing +as a separate thread. Note that the offered data is only valid within the call +to Play(), so if you can't process the entire block immediately, you +will need to copy it for later processing in your thread. +

+The Mute() and Clear() functions will be called whenever the audio shall +be muted, or any buffered data shall be cleared, respectively. +

+

Remote Control

The joy of zapping!

@@ -1427,7 +1480,6 @@ Put(uint64 Code, bool Repeat = false, bool Release = false);

The other parameters have the same meaning as in the first version of this function. -

diff --git a/audio.c b/audio.c index 5e7bb73b..c958256d 100644 --- a/audio.c +++ b/audio.c @@ -4,8 +4,95 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: audio.c 1.1 2002/06/10 16:30:00 kls Exp $ + * $Id: audio.c 1.2 2002/11/03 11:53:34 kls Exp $ */ #include "audio.h" +#include "stdlib.h" +// --- cAudio ---------------------------------------------------------------- + +cAudio::cAudio(void) +{ + Audios.Add(this); +} + +cAudio::~cAudio() +{ +} + +// --- cAudios --------------------------------------------------------------- + +cAudios Audios; + +void cAudios::PlayAudio(const uchar *Data, int Length) +{ + for (cAudio *audio = First(); audio; audio = Next(audio)) + audio->Play(Data, Length); +} + +void cAudios::MuteAudio(bool On) +{ + for (cAudio *audio = First(); audio; audio = Next(audio)) + audio->Mute(On); +} + +void cAudios::ClearAudio(void) +{ + for (cAudio *audio = First(); audio; audio = Next(audio)) + audio->Clear(); +} + +// --- cExternalAudio -------------------------------------------------------- + +cExternalAudio::cExternalAudio(const char *Command) +{ + command = strdup(Command); + mute = false; +} + +cExternalAudio::~cExternalAudio() +{ + free(command); +} + +void cExternalAudio::Play(const uchar *Data, int Length) +{ + if (command && !mute) { + if (pipe || pipe.Open(command, "w")) { + if (Data[0] == 0x00 && Data[1] == 0x00 && Data[2] == 0x01) { + if (Data[3] == 0xBD) { // dolby + //XXX??? int written = Data[8] + (skipAC3bytes ? 13 : 9); // skips the PES header + int written = Data[8] + 9; // skips the PES header + Length -= written; + while (Length > 0) { + int w = fwrite(Data + written, 1, Length, pipe); + if (w < 0) { + LOG_ERROR; + break; + } + Length -= w; + written += w; + } + } + } + } + else { + esyslog("ERROR: can't open pipe to audio command '%s'", command); + free(command); + command = NULL; + } + } +} + +void cExternalAudio::Mute(bool On) +{ + mute = On; + if (mute) + Clear(); +} + +void cExternalAudio::Clear(void) +{ + pipe.Close(); +} diff --git a/audio.h b/audio.h index 2541cdcb..565bd972 100644 --- a/audio.h +++ b/audio.h @@ -4,10 +4,52 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: audio.h 1.1 2002/06/10 16:30:00 kls Exp $ + * $Id: audio.h 1.2 2002/11/03 11:50:02 kls Exp $ */ #ifndef __AUDIO_H #define __AUDIO_H +#include "thread.h" +#include "tools.h" + +class cAudio : public cListObject { +protected: + cAudio(void); +public: + virtual ~cAudio(); + virtual void Play(const uchar *Data, int Length) = 0; + // Plays the given block of audio Data. Must return as soon as possible. + // If the entire block of data can't be processed immediately, it must + // be copied and processed in a separate thread. The Data is always a + // complete PES audio packet. + virtual void Mute(bool On) = 0; + // Immediately sets the audio device to be silent (On==true) or to + // normal replay (On==false). + virtual void Clear(void) = 0; + // Clears all data that might still be awaiting processing. + }; + +class cAudios : public cList { +public: + void PlayAudio(const uchar *Data, int Length); + void MuteAudio(bool On); + void ClearAudio(void); + }; + +extern cAudios Audios; + +class cExternalAudio : public cAudio { +private: + char *command; + cPipe pipe; + bool mute; +public: + cExternalAudio(const char *Command); + virtual ~cExternalAudio(); + virtual void Play(const uchar *Data, int Length); + virtual void Mute(bool On); + virtual void Clear(void); + }; + #endif //__AUDIO_H diff --git a/device.c b/device.c index 3bb68c67..69178a9e 100644 --- a/device.c +++ b/device.c @@ -4,13 +4,14 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: device.c 1.33 2002/11/01 11:08:57 kls Exp $ + * $Id: device.c 1.34 2002/11/03 11:51:24 kls Exp $ */ #include "device.h" #include #include #include +#include "audio.h" #include "channels.h" #include "eit.h" #include "i18n.h" @@ -416,6 +417,7 @@ bool cDevice::ToggleMute(void) mute = !mute; SetVolume(0, mute); volume = OldVolume; + Audios.MuteAudio(mute); return mute; } @@ -424,8 +426,10 @@ void cDevice::SetVolume(int Volume, bool Absolute) volume = min(max(Absolute ? Volume : volume + Volume, 0), MAXVOLUME); SetVolumeDevice(volume); cStatus::MsgSetVolume(volume, Absolute); - if (volume > 0) + if (volume > 0) { mute = false; + Audios.MuteAudio(mute); + } } int cDevice::NumAudioTracks(void) const @@ -462,6 +466,7 @@ void cDevice::TrickSpeed(int Speed) void cDevice::Clear(void) { + Audios.ClearAudio(); } void cDevice::Play(void) @@ -474,6 +479,7 @@ void cDevice::Freeze(void) void cDevice::Mute(void) { + Audios.MuteAudio(true); } void cDevice::StillPicture(const uchar *Data, int Length) @@ -529,9 +535,9 @@ int cDevice::PlayVideo(const uchar *Data, int Length) return -1; } -int cDevice::PlayAudio(const uchar *Data, int Length) +void cDevice::PlayAudio(const uchar *Data, int Length) { - return -1; + Audios.PlayAudio(Data, Length); } int cDevice::Ca(void) const diff --git a/device.h b/device.h index 7ac38ab0..cc2be790 100644 --- a/device.h +++ b/device.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: device.h 1.26 2002/11/01 11:03:56 kls Exp $ + * $Id: device.h 1.27 2002/11/03 11:16:11 kls Exp $ */ #ifndef __DEVICE_H @@ -290,6 +290,8 @@ public: // times. virtual void Clear(void); // Clears all video and audio data from the device. + // A derived class must call the base class function to make sure + // all registered cAudio objects are notified. virtual void Play(void); // Sets the device into play mode (after a previous trick // mode). @@ -297,6 +299,8 @@ public: // Puts the device into "freeze frame" mode. virtual void Mute(void); // Turns off audio while replaying. + // A derived class must call the base class function to make sure + // all registered cAudio objects are notified. virtual void StillPicture(const uchar *Data, int Length); // Displays the given I-frame as a still picture. virtual bool Poll(cPoller &Poller, int TimeoutMs = 0); @@ -309,8 +313,10 @@ public: // Actually plays the given data block as video. The data must be // part of a PES (Packetized Elementary Stream) which can contain // one video and one audio strem. - virtual int PlayAudio(const uchar *Data, int Length); + virtual void PlayAudio(const uchar *Data, int Length); // Plays additional audio streams, like Dolby Digital. + // A derived class must call the base class function to make sure data + // is distributed to all registered cAudio objects. bool Replaying(void) const; // Returns true if we are currently replaying. void StopReplay(void); diff --git a/dvbdevice.c b/dvbdevice.c index c22bc6c8..b8f0f27a 100644 --- a/dvbdevice.c +++ b/dvbdevice.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: dvbdevice.c 1.32 2002/11/01 11:24:47 kls Exp $ + * $Id: dvbdevice.c 1.33 2002/11/03 11:51:29 kls Exp $ */ #include "dvbdevice.h" @@ -792,10 +792,10 @@ int cDvbDevice::PlayVideo(const uchar *Data, int Length) return -1; } -int cDvbDevice::PlayAudio(const uchar *Data, int Length) +void cDvbDevice::PlayAudio(const uchar *Data, int Length) { - //XXX+ - return -1; + //XXX actually this function will only be needed to implement replaying AC3 over the DVB card's S/PDIF + cDevice::PlayAudio(Data, Length); } bool cDvbDevice::OpenDvr(void) diff --git a/dvbdevice.h b/dvbdevice.h index 46f1d106..67b11950 100644 --- a/dvbdevice.h +++ b/dvbdevice.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: dvbdevice.h 1.16 2002/11/01 10:05:41 kls Exp $ + * $Id: dvbdevice.h 1.17 2002/11/02 14:32:25 kls Exp $ */ #ifndef __DVBDEVICE_H @@ -103,7 +103,7 @@ public: virtual void StillPicture(const uchar *Data, int Length); virtual bool Poll(cPoller &Poller, int TimeoutMs = 0); virtual int PlayVideo(const uchar *Data, int Length); - virtual int PlayAudio(const uchar *Data, int Length); + virtual void PlayAudio(const uchar *Data, int Length); // Receiver facilities diff --git a/dvbplayer.c b/dvbplayer.c index 0bc75c0d..2d9cc1ce 100644 --- a/dvbplayer.c +++ b/dvbplayer.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: dvbplayer.c 1.15 2002/10/13 16:08:40 kls Exp $ + * $Id: dvbplayer.c 1.16 2002/11/03 11:23:47 kls Exp $ */ #include "dvbplayer.h" @@ -219,7 +219,7 @@ void cDvbPlayer::StripAudioPackets(uchar *b, int Length, uchar Except) switch (c) { case 0xBD: // dolby if (Except) - ;//XXX+ PlayExternalDolby(&b[i], Length - i); + PlayAudio(&b[i], l); // continue with deleting the data - otherwise it disturbs DVB replay case 0xC0 ... 0xC1: // audio if (c == 0xC1) diff --git a/player.c b/player.c index 3eecc2d4..a88c5010 100644 --- a/player.c +++ b/player.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: player.c 1.5 2002/08/15 10:29:17 kls Exp $ + * $Id: player.c 1.6 2002/11/02 14:55:37 kls Exp $ */ #include "player.h" @@ -31,12 +31,13 @@ int cPlayer::PlayVideo(const uchar *Data, int Length) return -1; } -int cPlayer::PlayAudio(const uchar *Data, int Length) +void cPlayer::PlayAudio(const uchar *Data, int Length) { - if (device) - return device->PlayAudio(Data, Length); + if (device) { + device->PlayAudio(Data, Length); + return; + } esyslog("ERROR: attempt to use cPlayer::PlayAudio() without attaching to a cDevice!"); - return -1; } void cPlayer::Detach(void) diff --git a/player.h b/player.h index 1e1fabfa..faeab1a7 100644 --- a/player.h +++ b/player.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: player.h 1.9 2002/10/12 11:17:02 kls Exp $ + * $Id: player.h 1.10 2002/11/03 11:27:30 kls Exp $ */ #ifndef __PLAYER_H @@ -35,8 +35,8 @@ protected: // Sends the given Data to the video device and returns the number of // bytes that have actually been accepted by the video device (or a // negative value in case of an error). - int PlayAudio(const uchar *Data, int Length); - // XXX+ TODO + void PlayAudio(const uchar *Data, int Length); + // Plays additional audio streams, like Dolby Digital. public: cPlayer(ePlayMode PlayMode = pmAudioVideo); virtual ~cPlayer(); diff --git a/transfer.c b/transfer.c index ddfb28cb..6d3ed780 100644 --- a/transfer.c +++ b/transfer.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: transfer.c 1.6 2002/10/26 10:17:17 kls Exp $ + * $Id: transfer.c 1.7 2002/11/03 11:24:02 kls Exp $ */ #include "transfer.h" @@ -123,7 +123,7 @@ void cTransfer::StripAudioPackets(uchar *b, int Length, uchar Except) switch (c) { case 0xBD: // dolby if (Except) - ;//XXX+ PlayExternalDolby(&b[i], Length - i); + PlayAudio(&b[i], l); // continue with deleting the data - otherwise it disturbs DVB replay case 0xC0 ... 0xC1: // audio if (c == 0xC1) diff --git a/vdr.c b/vdr.c index 41d20477..5c634c2e 100644 --- a/vdr.c +++ b/vdr.c @@ -22,7 +22,7 @@ * * The project's page is at http://www.cadsoft.de/people/kls/vdr * - * $Id: vdr.c 1.130 2002/10/27 15:20:56 kls Exp $ + * $Id: vdr.c 1.131 2002/11/03 11:44:17 kls Exp $ */ #include @@ -30,6 +30,7 @@ #include #include #include +#include "audio.h" #include "channels.h" #include "config.h" #include "cutter.h" @@ -87,6 +88,7 @@ int main(int argc, char *argv[]) #define DEFAULTPLUGINDIR "./PLUGINS/lib" int SVDRPport = DEFAULTSVDRPPORT; + const char *AudioCommand = NULL; const char *ConfigDirectory = NULL; bool DisplayHelp = false; bool DisplayVersion = false; @@ -121,10 +123,8 @@ int main(int argc, char *argv[]) int c; while ((c = getopt_long(argc, argv, "a:c:dD:E:hl:L:mp:P:r:s:t:v:Vw:", long_options, NULL)) != -1) { switch (c) { - /*XXX+ - case 'a': cDevice::SetAudioCommand(optarg); + case 'a': AudioCommand = optarg; break; - XXX*/ case 'c': ConfigDirectory = optarg; break; case 'd': DaemonMode = true; break; @@ -365,6 +365,11 @@ int main(int argc, char *argv[]) #endif Interface->LearnKeys(); + // External audio: + + if (AudioCommand) + new cExternalAudio(AudioCommand); + // Channel: Channels.SwitchTo(Setup.CurrentChannel); @@ -676,6 +681,7 @@ int main(int argc, char *argv[]) delete Interface; cOsd::Shutdown(); Remotes.Clear(); + Audios.Clear(); Setup.CurrentChannel = cDevice::CurrentChannel(); Setup.CurrentVolume = cDevice::CurrentVolume(); Setup.Save();