⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testamraudiostreamer.cpp

📁 流媒体传输协议的实现代码,非常有用.可以支持rtsp mms等流媒体传输协议
💻 CPP
字号:
/**********This library is free software; you can redistribute it and/or modify it underthe terms of the GNU Lesser General Public License as published by theFree Software Foundation; either version 2.1 of the License, or (at youroption) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)This library is distributed in the hope that it will be useful, but WITHOUTANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License formore details.You should have received a copy of the GNU Lesser General Public Licensealong with this library; if not, write to the Free Software Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA  02111-1307  USA**********/// Copyright (c) 1996-2004, Live Networks, Inc.  All rights reserved// A test program that reads an AMR audio file (as defined in RFC 3267)// and streams it using RTP// main program#include "liveMedia.hh"#include "BasicUsageEnvironment.hh"#include "GroupsockHelper.hh"UsageEnvironment* env;char const* inputFileName = "test.amr";AMRAudioFileSource* audioSource;RTPSink* audioSink;void play(); // forwardint main(int argc, char** argv) {  // Begin by setting up our usage environment:  TaskScheduler* scheduler = BasicTaskScheduler::createNew();  env = BasicUsageEnvironment::createNew(*scheduler);  // Create 'groupsocks' for RTP and RTCP:  struct in_addr destinationAddress;  destinationAddress.s_addr = chooseRandomIPv4SSMAddress(*env);  // Note: This is a multicast address.  If you wish instead to stream  // using unicast, then you should use the "testOnDemandRTSPServer"  // test program - not this test program - as a model.  const unsigned short rtpPortNum = 16666;  const unsigned short rtcpPortNum = rtpPortNum+1;  const unsigned char ttl = 255;  const Port rtpPort(rtpPortNum);  const Port rtcpPort(rtcpPortNum);  Groupsock rtpGroupsock(*env, destinationAddress, rtpPort, ttl);  rtpGroupsock.multicastSendOnly(); // we're a SSM source  Groupsock rtcpGroupsock(*env, destinationAddress, rtcpPort, ttl);  rtcpGroupsock.multicastSendOnly(); // we're a SSM source  // Create a 'AMR Audio RTP' sink from the RTP 'groupsock':  audioSink = AMRAudioRTPSink::createNew(*env, &rtpGroupsock, 96);  // Create (and start) a 'RTCP instance' for this RTP sink:  const unsigned estimatedSessionBandwidth = 10; // in kbps; for RTCP b/w share  const unsigned maxCNAMElen = 100;  unsigned char CNAME[maxCNAMElen+1];  gethostname((char*)CNAME, maxCNAMElen);  CNAME[maxCNAMElen] = '\0'; // just in case  RTCPInstance* rtcp    = RTCPInstance::createNew(*env, &rtcpGroupsock,			      estimatedSessionBandwidth, CNAME,			      audioSink, NULL /* we're a server */,			      True /* we're a SSM source */);  // Note: This starts RTCP running automatically  // Create and start a RTSP server to serve this stream.  RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554);  if (rtspServer == NULL) {    *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";    exit(1);  }  ServerMediaSession* sms    = ServerMediaSession::createNew(*env, "testStream", inputFileName,		   "Session streamed by \"testAMRAudioStreamer\"",					   True /*SSM*/);  sms->addSubsession(PassiveServerMediaSubsession::createNew(*audioSink, rtcp));  rtspServer->addServerMediaSession(sms);  char* url = rtspServer->rtspURL(sms);  *env << "Play this stream using the URL \"" << url << "\"\n";  delete[] url;  // Start the streaming:  *env << "Beginning streaming...\n";  play();  env->taskScheduler().doEventLoop(); // does not return  return 0; // only to prevent compiler warning}void afterPlaying(void* /*clientData*/) {  *env << "...done reading from file\n";  Medium::close(audioSource);  // Note that this also closes the input file that this source read from.  play();}void play() {  // Open the input file as an 'AMR audio file source':  AMRAudioFileSource* audioSource    = AMRAudioFileSource::createNew(*env, inputFileName);  if (audioSource == NULL) {    *env << "Unable to open file \"" << inputFileName	 << "\" as an AMR audio file source: "	 << env->getResultMsg() << "\n";    exit(1);  }    // Finally, start playing:  *env << "Beginning to read from file...\n";  audioSink->startPlaying(*audioSource, afterPlaying, audioSink);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -