  @author        <  > [  ]
     2012  
  @doc         UDP Mpeg - TS reader
%%% @reference  See <a href="/" target="_top">/</a> for more information
%%% @end
%%%
%%%---------------------------------------------------------------------------------------
-module(mpegts_udp).
-author('Max Lapshin <>').
-include_lib("eunit/include/eunit.hrl").

-define(CMD_OPEN, 1).
-define(CMD_ERRORS, 2).
-define(CMD_SCRAMBLED, 3).
-define(CMD_PACKET_COUNT, 4).
-define(CMD_ACTIVE_ONCE, 5).

-export([open/2, close/1, active_once/1]).
-export([control/2]).


control(Socket, errors) ->
  <<Errors:32/little>> = port_control(Socket, ?CMD_ERRORS, <<>>),
  Errors;

control(Socket, packet_count) ->
  <<PacketCount:32/little>> = port_control(Socket, ?CMD_PACKET_COUNT, <<>>),
  PacketCount;

control(Socket, scrambled) ->
  <<ScrambledCount:32/little>> = port_control(Socket, ?CMD_SCRAMBLED, <<>>),
  ScrambledCount.



open(Port, Options) ->
  Path = case code:lib_dir(mpegts,priv) of
    {error, _} -> "priv";
    LibDir -> LibDir
  end,
  Loaded = case erl_ddll:load_driver(Path, mpegts_udp) of
  	ok -> ok;
  	{error, already_loaded} -> ok;
  	{error, Error} -> {error, {could_not_load_driver,erl_ddll:format_error(Error)}}
  end,
  case Loaded of
    ok ->
      Socket = open_port({spawn, mpegts_udp}, [binary]),
      Multicast = case proplists:get_value(multicast_ttl, Options) of
        undefined -> <<>>;
        _ -> case proplists:get_value(ip, Options) of
        undefined -> <<>>;
        MC when is_list(MC) -> 
          {ok, {I1,I2,I3,I4}} = inet_parse:address(MC),
          <<I1, I2, I3, I4>>;
        {I1,I2,I3,I4} -> 
          <<I1, I2, I3, I4>>
      end end,
      <<"ok">> = port_control(Socket, ?CMD_OPEN, <<Port:16, Multicast/binary>>),
      erlang:port_set_data(Socket, inet_udp),
      {ok, Socket};
    Else ->
      Else
  end.


active_once(Port) ->
  <<"ok">> = port_control(Port, ?CMD_ACTIVE_ONCE, <<>>),
  ok.


close(Socket) ->
  erlang:port_close(Socket).





 @reference  See <a href="/" target="_top">/</a> for more information
 @end

---------------------------------------------------------------------------------------
  @author        <  > [  ]
     2012  
  @doc         UDP Mpeg - TS reader
-module(mpegts_udp).
-author('Max Lapshin <>').
-include_lib("eunit/include/eunit.hrl").

-define(CMD_OPEN, 1).
-define(CMD_ERRORS, 2).
-define(CMD_SCRAMBLED, 3).
-define(CMD_PACKET_COUNT, 4).
-define(CMD_ACTIVE_ONCE, 5).

-export([open/2, close/1, active_once/1]).
-export([control/2]).


control(Socket, errors) ->
  <<Errors:32/little>> = port_control(Socket, ?CMD_ERRORS, <<>>),
  Errors;

control(Socket, packet_count) ->
  <<PacketCount:32/little>> = port_control(Socket, ?CMD_PACKET_COUNT, <<>>),
  PacketCount;

control(Socket, scrambled) ->
  <<ScrambledCount:32/little>> = port_control(Socket, ?CMD_SCRAMBLED, <<>>),
  ScrambledCount.



open(Port, Options) ->
  Path = case code:lib_dir(mpegts,priv) of
    {error, _} -> "priv";
    LibDir -> LibDir
  end,
  Loaded = case erl_ddll:load_driver(Path, mpegts_udp) of
  	ok -> ok;
  	{error, already_loaded} -> ok;
  	{error, Error} -> {error, {could_not_load_driver,erl_ddll:format_error(Error)}}
  end,
  case Loaded of
    ok ->
      Socket = open_port({spawn, mpegts_udp}, [binary]),
      Multicast = case proplists:get_value(multicast_ttl, Options) of
        undefined -> <<>>;
        _ -> case proplists:get_value(ip, Options) of
        undefined -> <<>>;
        MC when is_list(MC) -> 
          {ok, {I1,I2,I3,I4}} = inet_parse:address(MC),
          <<I1, I2, I3, I4>>;
        {I1,I2,I3,I4} -> 
          <<I1, I2, I3, I4>>
      end end,
      <<"ok">> = port_control(Socket, ?CMD_OPEN, <<Port:16, Multicast/binary>>),
      erlang:port_set_data(Socket, inet_udp),
      {ok, Socket};
    Else ->
      Else
  end.


active_once(Port) ->
  <<"ok">> = port_control(Port, ?CMD_ACTIVE_ONCE, <<>>),
  ok.


close(Socket) ->
  erlang:port_close(Socket).




