/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.util.*;
import javax.media.*;
import javax.tv.service.*;
import javax.tv.service.selection.*;
import javax.tv.locator.*;

public class Media {
 private static ServiceContext last_ctx;
 private static Service last_srv;
 private static boolean playing;

 public static boolean JMFplay(String url) {
  boolean res=false;

  try {
   MediaLocator ml=new MediaLocator(url);
   Player player=Manager.createPlayer(ml);
   player.start();

   res=true;
  } catch(Throwable t) {}

  return res;
 }

 public static boolean JTVplay(String url) {
  if (!playing) {
   last_ctx=SrvUtils.current_service_context();
   last_srv=SrvUtils.current_service();
  }

  try {
   Locator loc=LocatorFactory.getInstance().createLocator(url);
   Service srv=SIManager.createInstance().getService(loc);

   ServiceContext ctx=ServiceContextFactory.getInstance().getServiceContexts()[0];
   ctx.select(srv);

   playing=true;
  } catch(Throwable t) {}

  return playing;
 }

 public static boolean JTVstop() {
  boolean res=false;

  try {
   if (playing) {
    last_ctx.select(last_srv);
    playing=false;

    res=true;
   }
  } catch(Throwable t) {}

  return res;
 }

}
