rtmp.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:14k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * rtmp.c: RTMP output.
  3.  *****************************************************************************
  4.  * Copyright (C) URJC - LADyR - Luis Lopez Fernandez
  5.  *
  6.  * Author: Miguel Angel Cabrera Moya
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  21.  *****************************************************************************/
  22. /*****************************************************************************
  23.  * Preamble
  24.  *****************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. # include "config.h"
  27. #endif
  28. #include <vlc_common.h>
  29. #include <vlc_plugin.h>
  30. #include <vlc_sout.h>
  31. #include <vlc_network.h> /* DOWN: #include <network.h> */
  32. #include <vlc_url.h>
  33. #include <vlc_block.h>
  34. #include "../access/rtmp/rtmp_amf_flv.h"
  35. /*****************************************************************************
  36.  * Module descriptor
  37.  *****************************************************************************/
  38. #define RTMP_CONNECT_TEXT N_( "Active TCP connection" )
  39. #define RTMP_CONNECT_LONGTEXT N_( 
  40.     "If enabled, VLC will connect to a remote destination instead of " 
  41.     "waiting for an incoming connection." )
  42. static int  Open ( vlc_object_t * );
  43. static void Close( vlc_object_t * );
  44. #define SOUT_CFG_PREFIX "sout-rtmp-"
  45. vlc_module_begin ()
  46.     set_description( N_("RTMP stream output") )
  47.     set_shortname( N_("RTMP" ) )
  48.     set_capability( "sout access", 0 )
  49.     set_category( CAT_SOUT )
  50.     set_subcategory( SUBCAT_SOUT_STREAM )
  51.     add_shortcut( "rtmp" )
  52.     set_callbacks( Open, Close )
  53.     add_bool( "rtmp-connect", false, NULL, RTMP_CONNECT_TEXT,
  54.               RTMP_CONNECT_LONGTEXT, false )
  55. vlc_module_end ()
  56. /*****************************************************************************
  57.  * Local prototypes
  58.  *****************************************************************************/
  59. static ssize_t Write( sout_access_out_t *, block_t * );
  60. static int     Seek ( sout_access_out_t *, off_t  );
  61. static void* ThreadControl( vlc_object_t * );
  62. struct sout_access_out_sys_t
  63. {
  64.     int active;
  65.     /* thread for filtering and handling control messages */
  66.     rtmp_control_thread_t *p_thread;
  67. };
  68. /*****************************************************************************
  69.  * Open: open the rtmp connection
  70.  *****************************************************************************/
  71. static int Open( vlc_object_t *p_this )
  72. {
  73.     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
  74.     sout_access_out_sys_t *p_sys;
  75.     char *psz, *p;
  76.     int length_path, length_media_name;
  77.     int i;
  78.     if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
  79.         return VLC_ENOMEM;
  80.     p_access->p_sys = p_sys;
  81.     p_sys->p_thread =
  82.         vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
  83.     if( !p_sys->p_thread )
  84.         return VLC_ENOMEM;
  85.     vlc_object_attach( p_sys->p_thread, p_access );
  86.     /* Parse URI - remove spaces */
  87.     p = psz = strdup( p_access->psz_path );
  88.     while( ( p = strchr( p, ' ' ) ) != NULL )
  89.         *p = '+';
  90.     vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
  91.     free( psz );
  92.     if( p_sys->p_thread->url.psz_host == NULL
  93.         || *p_sys->p_thread->url.psz_host == '' )
  94.     {
  95.          msg_Warn( p_access, "invalid host" );
  96.          goto error;
  97.     }
  98.     if( p_sys->p_thread->url.i_port <= 0 )
  99.         p_sys->p_thread->url.i_port = 1935;
  100.     if ( p_sys->p_thread->url.psz_path == NULL )
  101.     {
  102.         msg_Warn( p_access, "invalid path" );
  103.         goto error;
  104.     }
  105.     length_path = strlen( p_sys->p_thread->url.psz_path );
  106.     char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' );
  107.     if( !psz_tmp )
  108.         goto error;
  109.     length_media_name = strlen( psz_tmp ) - 1;
  110.     p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
  111.     p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
  112.     msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
  113.              p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
  114.     if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
  115.     {
  116.         msg_Dbg( p_access, "      user='%s'", p_sys->p_thread->url.psz_username );
  117.     }
  118.     /* Initialize thread variables */
  119.     p_sys->p_thread->b_die = 0;
  120.     p_sys->p_thread->b_error= 0;
  121.     p_sys->p_thread->p_fifo_input = block_FifoNew();
  122.     p_sys->p_thread->p_empty_blocks = block_FifoNew();
  123.     p_sys->p_thread->has_audio = 0;
  124.     p_sys->p_thread->has_video = 0;
  125.     p_sys->p_thread->metadata_received = 0;
  126.     p_sys->p_thread->first_media_packet = 1;
  127.     p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
  128.     p_sys->p_thread->flv_body = rtmp_body_new( -1 );
  129.     p_sys->p_thread->flv_length_body = 0;
  130.     p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
  131.     p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
  132.     for(i = 0; i < 64; i++)
  133.     {
  134.         memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
  135.         p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
  136.         p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
  137.         p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
  138.         p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
  139.         p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
  140.         p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
  141.         p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
  142.         p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
  143.         p_sys->p_thread->rtmp_headers_send[i].body = NULL;
  144.     }
  145.     vlc_cond_init( &p_sys->p_thread->wait );
  146.     vlc_mutex_init( &p_sys->p_thread->lock );
  147.     p_sys->p_thread->result_connect = 1;
  148.     /* p_sys->p_thread->result_publish = only used on access */
  149.     p_sys->p_thread->result_play = 1;
  150.     p_sys->p_thread->result_stop = 0;
  151.     p_sys->p_thread->fd = -1;
  152.     /* Open connection */
  153.     if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 )
  154.     {
  155. #if 0
  156.         p_sys->p_thread->fd = net_ConnectTCP( p_access,
  157.                                               p_sys->p_thread->url.psz_host,
  158.                                               p_sys->p_thread->url.i_port );
  159. #endif
  160.         msg_Err( p_access, "to be implemented" );
  161.         goto error2;
  162.     }
  163.     else
  164.     {
  165.         int *p_fd_listen;
  166.         p_sys->active = 0;
  167.         p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host,
  168.                                      p_sys->p_thread->url.i_port );
  169.         if( p_fd_listen == NULL )
  170.         {
  171.             msg_Warn( p_access, "cannot listen to %s port %i",
  172.                       p_sys->p_thread->url.psz_host,
  173.                       p_sys->p_thread->url.i_port );
  174.             goto error2;
  175.         }
  176.         do
  177.             p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 );
  178.         while( p_sys->p_thread->fd == -1 );
  179.         net_ListenClose( p_fd_listen );
  180.         if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
  181.         {
  182.             msg_Err( p_access, "handshake passive failed");
  183.             goto error2;
  184.         }
  185.     }
  186.     if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
  187.                            VLC_THREAD_PRIORITY_INPUT ) )
  188.     {
  189.         msg_Err( p_access, "cannot spawn rtmp control thread" );
  190.         goto error2;
  191.     }
  192.     if( !p_sys->active )
  193.     {
  194.         if( rtmp_connect_passive( p_sys->p_thread ) < 0 )
  195.         {
  196.             msg_Err( p_access, "connect passive failed");
  197.             goto error2;
  198.         }
  199.     }
  200.     p_access->pf_write = Write;
  201.     p_access->pf_seek = Seek;
  202.     return VLC_SUCCESS;
  203. error2:
  204.     vlc_cond_destroy( &p_sys->p_thread->wait );
  205.     vlc_mutex_destroy( &p_sys->p_thread->lock );
  206.     free( p_sys->p_thread->psz_application );
  207.     free( p_sys->p_thread->psz_media );
  208.     if( p_sys->p_thread->fd != -1 )
  209.         net_Close( p_sys->p_thread->fd );
  210. error:
  211.     vlc_object_detach( p_sys->p_thread );
  212.     vlc_object_release( p_sys->p_thread );
  213.     vlc_UrlClean( &p_sys->p_thread->url );
  214.     free( p_sys );
  215.     return VLC_EGENERIC;
  216. }
  217. /*****************************************************************************
  218.  * Close: close the target
  219.  *****************************************************************************/
  220. static void Close( vlc_object_t * p_this )
  221. {
  222.     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
  223.     sout_access_out_sys_t *p_sys = p_access->p_sys;
  224.     int i;
  225. //    p_sys->p_thread->b_die = true;
  226.     vlc_object_kill( p_sys->p_thread );
  227.     block_FifoWake( p_sys->p_thread->p_fifo_input );
  228.     vlc_thread_join( p_sys->p_thread );
  229.     vlc_cond_destroy( &p_sys->p_thread->wait );
  230.     vlc_mutex_destroy( &p_sys->p_thread->lock );
  231.     block_FifoRelease( p_sys->p_thread->p_fifo_input );
  232.     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
  233.     for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
  234.     {
  235.         if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
  236.         {
  237.             free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
  238.             free( p_sys->p_thread->rtmp_headers_recv[i].body );
  239.         }
  240.     }
  241.     net_Close( p_sys->p_thread->fd );
  242.     vlc_object_detach( p_sys->p_thread );
  243.     vlc_object_release( p_sys->p_thread );
  244.     vlc_UrlClean( &p_sys->p_thread->url );
  245.     free( p_sys->p_thread->psz_application );
  246.     free( p_sys->p_thread->psz_media );
  247.     free( p_sys );
  248. }
  249. /*****************************************************************************
  250.  * Write: standard write on a file descriptor.
  251.  *****************************************************************************/
  252. static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
  253. {
  254.     rtmp_packet_t *rtmp_packet;
  255.     uint8_t *tmp_buffer;
  256.     ssize_t i_ret;
  257.     ssize_t i_write = 0;
  258.     if( p_access->p_sys->p_thread->first_media_packet )
  259.     {
  260.         /* 13 == FLV_HEADER_SIZE + PreviousTagSize*/
  261.         memmove( p_buffer->p_buffer, p_buffer->p_buffer + 13, p_buffer->i_buffer - 13 );
  262.         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer - 13 );
  263.         p_access->p_sys->p_thread->first_media_packet = 0;
  264.     }
  265.     while( p_buffer )
  266.     {
  267.         block_t *p_next = p_buffer->p_next;
  268. //////////////////////////////
  269. /*msg_Warn(p_access, "XXXXXXXXXXXXXXXXX");
  270. int i;
  271. for(i = 0; i < p_buffer->i_buffer; i += 16)
  272. {
  273.     msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
  274. p_buffer->p_buffer[i], p_buffer->p_buffer[i+1], p_buffer->p_buffer[i+2], p_buffer->p_buffer[i+3], p_buffer->p_buffer[i+4], p_buffer->p_buffer[i+5], p_buffer->p_buffer[i+6], p_buffer->p_buffer[i+7],
  275. p_buffer->p_buffer[i+8], p_buffer->p_buffer[i+9], p_buffer->p_buffer[i+10], p_buffer->p_buffer[i+11], p_buffer->p_buffer[i+12], p_buffer->p_buffer[i+13], p_buffer->p_buffer[i+14], p_buffer->p_buffer[i+15]);
  276. }*/
  277. ////////////////////////
  278.         msg_Warn(p_access, "rtmp.c:360 i_dts %"PRIu64" i_pts %"PRIu64,
  279.                  p_buffer->i_dts, p_buffer->i_pts);
  280.         rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer );
  281.         if( rtmp_packet )
  282.         {
  283.             tmp_buffer = rtmp_encode_packet( p_access->p_sys->p_thread, rtmp_packet );
  284.             i_ret = net_Write( p_access->p_sys->p_thread, p_access->p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
  285.             if( i_ret != rtmp_packet->length_encoded )
  286.             {
  287.                 free( rtmp_packet->body->body );
  288.                 free( rtmp_packet->body );
  289.                 free( rtmp_packet );
  290.                 free( tmp_buffer );
  291.                 msg_Err( p_access->p_sys->p_thread, "failed send flv packet" );
  292.                 return -1;
  293.             }
  294.             free( rtmp_packet->body->body );
  295.             free( rtmp_packet->body );
  296.             free( rtmp_packet );
  297.             free( tmp_buffer );
  298.         }
  299.         i_write += p_buffer->i_buffer;
  300.         p_buffer = p_next;
  301.     }
  302.     return i_write;
  303. }
  304. /********************a*********************************************************
  305.  * Seek: seek to a specific location in a file
  306.  *****************************************************************************/
  307. static int Seek( sout_access_out_t *p_access, off_t i_pos )
  308. {
  309.     (void)i_pos;
  310.     msg_Err( p_access, "RTMP sout access cannot seek" );
  311.     return -1;
  312. }
  313. /*****************************************************************************
  314.  * ThreadControl: manage control messages and pipe media to Read
  315.  *****************************************************************************/
  316. static void* ThreadControl( vlc_object_t *p_this )
  317. {
  318.     rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
  319.     rtmp_packet_t *rtmp_packet;
  320.     int canc = vlc_savecancel ();
  321.     rtmp_init_handler( p_thread->rtmp_handler );
  322.     while( vlc_object_alive (p_thread) )
  323.     {
  324.         rtmp_packet = rtmp_read_net_packet( p_thread );
  325.         if( rtmp_packet != NULL )
  326.         {
  327.             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
  328.                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
  329.             {
  330.                 free( rtmp_packet->body->body );
  331.                 free( rtmp_packet->body );
  332.                 free( rtmp_packet );
  333.                 msg_Warn( p_thread, "unknown content type received" );
  334.             }
  335.             else
  336.                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
  337.         }
  338.         else
  339.         {
  340.             /* Sometimes server close connection too soon */
  341.             if( p_thread->result_connect )
  342.             {
  343.                 vlc_mutex_lock( &p_thread->lock );
  344.                 vlc_cond_signal( &p_thread->wait );
  345.                 vlc_mutex_unlock( &p_thread->lock );
  346.             }
  347.             p_thread->b_die = 1;
  348.         }
  349.     }
  350.     vlc_restorecancel (canc);
  351.     return NULL;
  352. }