Examples

See the JavaDoc of the IBackend interface for more usage examples.

Query pending recordings

In the following example we connect to a MythTV-backend and fetch the list of pending recordings.

// create a backend object and connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();
   
// register as a playback client
backend.annotatePlayback();
   
// query pending recordings
IRecordingsPending pendingRecordings = backend.queryAllPending();
for (IProgramInfo program : pendingRecordings) {
   // we are only interrested in recordings with "WILL_RECORD" status
   IProgramRecordingStatus recStatus = program.getRecordingStatus();
   if(!recStatus.hasStatus(IProgramRecordingStatus.Status.WILL_RECORD)) continue;
          
   // print out the found recodings
   System.out.println(String.format(
      "%1$tF %1$tT - %2$s (%3$s)",
      program.getStartDateTime(),
      program.getTitle(),
      program.getChannelSign()
   ));
}
   
// close backend connection
backend.close();

The above example will output, e.g.

 MythTV has 4 pending records
 2011-04-17 18:10:00 - Die Simpsons (PRO7)
 2011-04-17 20:15:00 - 10.000 BC (PRO7)
 2011-04-18 22:15:00 - Roter Drache (ZDF)
 2011-04-21 20:15:00 - Dr. House (ORF1)

Query recorded programs

In the following example we print out all already recorded programs.

// connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();
   
// register as a playback client
backend.annotatePlayback();  
  
// query all available recordings
IProgramInfoList allRecordings = backend.queryRecordings();
    
// print the channel, length, file-size and title of all recordings
System.out.println("Channel | Length  | Size     | Title");
for(IProgramInfo program : allRecordings) {
   // print out the found recordings
   System.out.println(String.format(
      "%-5s | %3d min | %8s | %s",
      program.getChannelSign(),
      program.getDuration(),
      EncodingUtils.getFormattedFileSize(Locale.ENGLISH,program.getFileSize()),
      program.getFullTitle()
   ));            
}
  
// close backend connection
backend.close();

The above example will output, e.g.:

Channel | Length  | Size     | Title
ATV+    |  55 min |  3.51 GB | Dr. Quinn - Ärztin aus Leidenschaft - Für das Leben der Cheyenne
RTL2    |  30 min |  2.34 GB | King of Queens - Wer schön sein will ...
RTL2    |  30 min |  2.34 GB | King of Queens - Das Haus am See

Query program infos

In the following example we query the next programs on all known channels.

// connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();
   
// register as a monitoring client
backend.annotateMonitor();  

// specify the time to use for query
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,20);
cal.set(Calendar.MINUTE,15);
    
// fetch the programs of all channels today at 8:15 PM
List<IRecorderNextProgramInfo> nextPrograms = backend.getNextProgramInfos(cal.getTime());
for(IRecorderNextProgramInfo nextProgram : nextPrograms) {
   if(!nextProgram.isValid()) continue; // if there is no valid EPG data for a channel
       
   System.out.println(String.format(
      "%tT | %tT | %-15s | %s",
      nextProgram.getStartDateTime(), nextProgram.getEndDateTime(),
      nextProgram.getChannelSign(), nextProgram.getTitle()
   ));
}

// close backend connection
backend.close();

The above example will output, e.g.:

20:15:01 | 23:05:01 | RTL             | Harry Potter und der Stein der Weisen
20:15:01 | 21:15:01 | SAT.1           | Navy CIS
20:15:01 | 21:45:01 | ARD             | Tatort
20:15:01 | 21:45:01 | ZDF             | Rosamunde Pilcher: Englischer Wein
20:15:01 | 21:45:01 | SWBW            | SonntagAbend
19:45:01 | 21:15:01 | BR3             | Kreuzwege
18:25:00 | 20:15:00 | PULS4           | American Pie Presents Band Camp
20:15:01 | 22:20:01 | PRO7            | Wall-E - Der Letzte räumt die Erde auf
20:15:01 | 22:55:01 | ATV+            | The Missing
20:15:01 | 22:10:01 | RTL2            | Ungeklärte Morde - Dem Täter auf der Spur
20:15:01 | 22:45:01 | ARTE            | Fitzcarraldo
20:15:01 | 21:00:01 | 3SAT            | Neues aus der Anstalt
20:15:01 | 21:50:01 | ORF2            | Tatort
20:15:01 | 21:40:01 | ORF1            | WALL-E - Der Letzte räumt die Erde auf
20:15:01 | 22:45:01 | VOX             | Das perfekte Promi Dinner
20:15:01 | 21:15:01 | SUPRT           | Abgefahren - Der lange Weg zum Führerschein
20:15:01 | 20:45:01 | KABE1           | Two and a Half Men

Download a recording

In the following example a recording file is downloaded to the client.

// connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();
   
// register as a playback client
backend.annotatePlayback();

// getting previously recorded programs
IProgramInfoList programs = backend.queryRecordings();
    
// getting the recording to transfer (we just use the first recording)
IProgramInfo program = programs.get(0);
            
// annotate a new file transfer
IFileTransfer transfer = backend.annotateFileTransfer(program);    
if (transfer.isOpen()) {
   
   // create a local file
   File tempFile = new File(System.getProperty("java.io.tmpdir"), program.getBaseName());
   
   // copy data
   transfer.transferTo(tempFile);
}
            
// close file transfer
transfer.close();

// close backend connection
backend.close();

Query free diskspace on the backend

In this example we query the free diskspace on the backend.

// connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();
   
// register as a monitoring client
backend.annotateMonitor();

// query the free space
IBasicFreeSpace freeSpaceOverview = backend.getFreeSpaceOverview();
System.out.println(String.format(
   "MythTV has used %s space out of %s.",
   EncodingUtils.getFormattedFileSize(freeSpaceOverview.getUsedSpace()),
   EncodingUtils.getFormattedFileSize(freeSpaceOverview.getTotalSpace())
));

// close backend connection
backend.close();

The above example will output, e.g.:

MythTV has used 3,31 GB space out of 3,72 GB.

Query the status of all recorders

In this example we determine the recording status of all MythTV recorders.

// connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();
   
// register as a monitoring client
backend.annotateMonitor();

// get the IDs of all available (busy or idle) recorders
int[] allRecorderIDs = backend.getRecorderIDs();
System.out.println(String.format(
   "%d recorders are available:", allRecorderIDs.length
));
    
for(int recorderId : allRecorderIDs) {
   // get the recorder info
   IRecorderInfo recorderInfo = backend.getRecorderForNum(recorderId);
       
   // connect to the recorder
   IRecorder recorder = backend.getRecorder(recorderInfo);
       
   // get the recorder status
   boolean isRecording = recorder.isRecording();
   System.out.println(String.format(
      "Recorder %d is %s",
      recorderId,
      isRecording? "busy" : "idle"
   ));
   
   // disconnect from the recorder
   recorder.close();
}

// close backend connection
backend.close();

The above example will output, e.g.:

3 recorders are available:
Recorder 1 is busy
Recorder 2 is idle
Recorder 3 is idle

Stream Live-TV

In the following example we reserve a free recorder and stream Live-TV data to the client.

// connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();
   
// register as a playback client
backend.annotatePlayback();
    
// requesting a free recorder
IRecorderInfo recorderInfo = backend.getNextFreeRecorder();
if(recorderInfo != null) {
   // connect to the recorder
   IRecorder recorder = backend.getRecorder(recorderInfo);
   
   // initialize Live-TV
   String channelNr = "2";
   boolean pictureInPicture = false;
   if (recorder.spawnLiveTV(pictureInPicture,channelNr)) {
      // wait (max. 10 seconds) for the recorder to enter the recording state
      recorder.waitForIsRecording(10000);
      
      // getting the program info to determine the streaming file
      IProgramInfo programInfo = recorder.getRecording();
      
      // annotate a new file transfer to receive the streaming data
      IFileTransfer transfer = backend.annotateFileTransfer(programInfo);      
      if (transfer.isOpen()) {
      
         // create a local file to copy Live-TV data into
         File tempFile = File.createTempFile("livetv", ".mpg");
         BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(tempFile));
    
         // initialize a byte buffer
         int blockSize = 65536/2;
         byte[] buffer = new byte[blockSize];
        
         // receive data in a loop
         boolean stopped = false;
         while(!stopped) {
            // reading the next block
            int readSize = transfer.readBlock(buffer,blockSize);            
                            
            // writing the block into the file
            fileOut.write(buffer, 0, readSize);
            fileOut.flush();
            
            // TODO: do some check if the user has aborted Live-TV
            if(tempFile.length() > 20*1024*1024) {
                stopped = true;
            }
         }
      
         // close file stream
         fileOut.close();
      } else {
         System.err.println("Unable to open a file transfer connection.");
      }
      
      // close file transfer connection
      transfer.close();
      
      // shutdown live tv
      recorder.stopLiveTv();
      
   } else {
      System.err.println("Unable to spawn Live-TV.");
   }
   
   // disconnect from the recorder
   recorder.close();
} else {
   System.err.println("No free recorder available.");
}
    
// close backend connection
backend.close();

Add a EPG recording schedule

In the following example add a new recording schedule into the database and inform the MythTV backend about the new scheduling rule.

// the mythtv database to connect to
IDatabase db = new DataBase("mythbox",IDatabase.DEFAULT_PORT,IDatabase.DEFAULT_DB,"mythtv","RhXhWF12");

// connect to the backend
IBackend backend = BackendFactory.createBackend("mythbox");
backend.connect();

// register as a client and enable events
backend.annotatePlayback(RequestUtils.getHostname(),EPlaybackSockEventsMode.NORMAL);

// the date of the program to record
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,20);
cal.set(Calendar.MINUTE,15);

// the channel of the program to record
String callSign = "ARD";
final List<IRecorderChannelInfo> channelInfos = db.getChannelInfosByCallSign(callSign);
if(channelInfos.isEmpty()) {
   System.err.println("No channel found for the given channel sign.");
   return;
}
final IRecorderChannelInfo channelInfo = channelInfos.get(0);

// load the program to record
IRecorderNextProgramInfo programInfo = db.queryNextProgramInfo(channelInfo, cal.getTime());
if(programInfo == null) {
   System.err.println("No program found for the given channel and time");
   return;
}    

// create the recording schedule object
Schedule schedule = Schedule.valueOf(backend.getVersionNr(),db.getDbVersion(),programInfo);

// change some properties (e.g. the priority and recording offset)
schedule.setRecordingPriority(10);
schedule.setStartOffset(Integer.valueOf(5));
schedule.setEndOffset(Integer.valueOf(15));

// add the recording schedule into the database
final Integer recordingId = db.addSchedule(schedule);

// inform the backend about the new scheduling rule
Boolean scheduled = backend.rescheduleRecordingsAndWait(recordingId,20,TimeUnit.SECONDS);
if(scheduled == null) {
   System.err.println("Timeout while scheduling of recording.");
} else if(!scheduled.booleanValue()) {
   System.err.println("Unable to schedule the recording.");
} else {
   System.out.println("Recording successfully scheduled.");
}

// close backend connection
backend.close();