ajelly

live555/mediaServer/live555MediaServer.cpp

Jan 6th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 2.1 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6.  
  7. This library is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
  10. more details.
  11.  
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this library; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  15. **********/
  16. // Copyright (c) 1996-2013, Live Networks, Inc.  All rights reserved
  17. // LIVE555 Media Server
  18. // main program
  19.  
  20. #define ANDROID 1
  21.  
  22. #include <signal.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #include <BasicUsageEnvironment.hh>
  27. #include "DynamicRTSPServer.hh"
  28. #include "version.hh"
  29.  
  30. int p2p_mode=0;
  31. RTSPServer* rtspServer = NULL;
  32.  
  33. void ptime(const char *message) {
  34.     int sec, msec;
  35.     struct timeval tp;
  36.  
  37.     gettimeofday(&tp, NULL);
  38.  
  39.     sec  = tp.tv_sec%1000;
  40.     msec = tp.tv_usec/1000;
  41.  
  42.     printf("[%03d.%03d]: %s",sec,msec,message);
  43.     fflush(stdout);
  44. }
  45.  
  46. static void catch_function(int signal) {
  47.     fprintf(stderr, "Livemedia got [%d] signal, exit after 1 sec!\n", signal);
  48.     delete rtspServer;
  49.     sleep(1);
  50.     exit(13);
  51. }
  52.  
  53. int set_signal_handler(void) {
  54.  
  55.     if (signal(SIGTERM, catch_function) == SIG_ERR) {
  56.         fprintf(stderr, "We cannot set SIGTERM handler!\n");
  57.         return -1;
  58.     }
  59.  
  60.     if (signal(SIGINT, catch_function) == SIG_ERR) {
  61.         fprintf(stderr, "We cannot set SIGINT handler!\n");
  62.         return -1;
  63.     }
  64.  
  65.     ptime("All signal handlers successfully set!\n");
  66.  
  67.     return 0;
  68. }
  69.  
  70. int main(int argc, char** argv) {
  71.     // Begin by setting up our usage environment:
  72.     if (argc>1) {
  73.         if (strcmp(argv[1], "-p")==0) {
  74.             p2p_mode=1;
  75.         }
  76.         else if (strcmp(argv[1], "-q")==0) {
  77.             printf("Quit requested. Ok\n");
  78.             exit(0);
  79.         }
  80.         else {
  81.             printf("Unknown arg\n");
  82.         }
  83.     }
  84.  
  85.     // Set signal processor
  86.     set_signal_handler();
  87.  
  88.     TaskScheduler* scheduler = BasicTaskScheduler::createNew();
  89.     UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
  90.  
  91.     UserAuthenticationDatabase* authDB = NULL;
  92. #ifdef ACCESS_CONTROL
  93.     // To implement client access control to the RTSP server, do the following:
  94.     authDB = new UserAuthenticationDatabase;
  95.     authDB->addUserRecord("username1", "password1"); // replace these with real strings
  96.     // Repeat the above with each <username>, <password> that you wish to allow
  97.     // access to the server.
  98. #endif
  99.  
  100.     // Create the RTSP server.  Try first with the default port number (554),
  101.     // and then with the alternative port number (8554):
  102.     portNumBits rtspServerPortNum = 554;
  103. #ifndef ANDROID
  104.     rtspServer = DynamicRTSPServer::createNew(*env, rtspServerPortNum, authDB);
  105. #endif
  106.     if (rtspServer == NULL) {
  107.         if (p2p_mode==1) {
  108.             rtspServerPortNum = 7236;
  109.         }
  110.         else {
  111.             rtspServerPortNum = 8554;
  112.         }
  113.         rtspServer = DynamicRTSPServer::createNew(*env, rtspServerPortNum, authDB);
  114.     }
  115.     if (rtspServer == NULL) {
  116.         *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
  117.         exit(1);
  118.     }
  119.  
  120.     *env << "LIVE555 Media Server, pid:" << getpid() << "\n";
  121.     *env << "\tversion " << MEDIA_SERVER_VERSION_STRING
  122.          << " (LIVE555 Streaming Media library version "
  123.          << LIVEMEDIA_LIBRARY_VERSION_STRING << ").\n";
  124.  
  125.  
  126.     char* urlPrefix = rtspServer->rtspURLPrefix();
  127.  
  128.     *env << "Play streams from this server using the URL\n\t"
  129.         << urlPrefix << "<filename>\nwhere <filename> is a file present in the current directory.\n";
  130.  
  131.     /*
  132.     *env << "Each file's type is inferred from its name suffix:\n";
  133.     *env << "\t\".264\" => a H.264 Video Elementary Stream file\n";
  134.     *env << "\t\".aac\" => an AAC Audio (ADTS format) file\n";
  135.     *env << "\t\".ac3\" => an AC-3 Audio file\n";
  136.     *env << "\t\".amr\" => an AMR Audio file\n";
  137.     *env << "\t\".dv\" => a DV Video file\n";
  138.     *env << "\t\".m4e\" => a MPEG-4 Video Elementary Stream file\n";
  139.     *env << "\t\".mkv\" => a Matroska audio+video+(optional)subtitles file\n";
  140.     *env << "\t\".mp3\" => a MPEG-1 or 2 Audio file\n";
  141.     *env << "\t\".mpg\" => a MPEG-1 or 2 Program Stream (audio+video) file\n";
  142.     *env << "\t\".ts\" => a MPEG Transport Stream file\n";
  143.     *env << "\t\t(a \".tsx\" index file - if present - provides server 'trick play' support)\n";
  144.     *env << "\t\".vob\" => a VOB (MPEG-2 video with AC-3 audio) file\n";
  145.     *env << "\t\".wav\" => a WAV Audio file\n";
  146.     *env << "\t\".webm\" => a WebM audio(Vorbis)+video(VP8) file\n";
  147.     *env << "See http://www.live555.com/mediaServer/ for additional documentation.\n";
  148.     */
  149.     // Also, attempt to create a HTTP server for RTSP-over-HTTP tunneling.
  150.     // Try first with the default HTTP port (80), and then with the alternative HTTP
  151.     // port numbers (8000 and 8080).
  152.  
  153.     /*
  154.     if (rtspServer->setUpTunnelingOverHTTP(80) || rtspServer->setUpTunnelingOverHTTP(8000) || rtspServer->setUpTunnelingOverHTTP(8080)) {
  155.         *env << "(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling, or for HTTP live streaming (for indexed Transport Stream files only).)\n";
  156.     }
  157.     else {
  158.         *env << "(RTSP-over-HTTP tunneling is not available.)\n";
  159.     }
  160.     */
  161.  
  162.     env->taskScheduler().doEventLoop(); // does not return
  163.  
  164.     return 0; // only to prevent compiler warning
  165. }
Add Comment
Please, Sign In to add comment