Chinaunix首页 | 论坛 | 博客
  • 博客访问: 277895
  • 博文数量: 182
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1292
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-06 19:02
个人简介

让一切的准备都完美演出,让所有的努力都美好落幕

文章分类

全部博文(182)

文章存档

2016年(60)

2015年(122)

我的朋友

分类: Android平台

2015-07-18 20:54:11

Libstreaming是一个开源的流媒体框架,它可以让手机变成一台流媒体服务器,直接在PC端查看手机摄像头的实时画面。值得一提的是它的作者也是spydroid的作者。按照作者的说法,spydroid是利用该库完成流媒体传输的,但据笔者的分析观察,此说法并不十分确切。Libstreaming是spydroid的抽象与升华,RTSP服务器的实现方式也有很大的不同。
巧妇难为无米之炊,我们先把Libstreaming的源代码下载下来。地址: 下载完毕后导入eclipse,并新建工程引用该库。这里要颇为注意,新建的工程必须和Libstreaming在同一个盘符下,否则可能出现引用失败的问题。

接下来看看官方文档中给出的创建RTSP服务器的步骤:

1、Add this to your manifest:

<service android:name="net.majorkernelpanic.streaming.rtsp.RtspServer"/>

把RtspServer这个服务在androidManifest文件中进行注册。在libstreaming库中,rtsp服务器是作为service组建实现的,这与spydroid的实现方式完全不一样。

2、You can change the port used by the RtspServer:

Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); editor.putString(RtspServer.KEY_PORT, String.valueOf(1234)); editor.commit(); 

The port is indeed stored as a String in the preferences, there is a good reason to that. The EditTextPreference object saves its input as a String and cannot easily (one would need to override it) be configured to store it as an Integer.

可以改变rtsp服务器的端口,当然,这是非必需的。默认端口是8086。若想改变端口,必须通过sharedPreference完成。先获取一个指向本activity的sharedPreference的editor对象,再将指定的端口号put进去。至于为什么用String类型而不是用整形存储端口号,主要是考虑到EditTextPreference对象的保存类型是string。

3、Configure its behavior with the SessionBuilder:

SessionBuilder.getInstance() .setSurfaceHolder(mSurfaceView.getHolder()) .setContext(getApplicationContext()) .setAudioEncoder(SessionBuilder.AUDIO_AAC) .setVideoEncoder(SessionBuilder.VIDEO_H264);
sessionBuilder是session的建造者。而session又是服务器与客户端间通信的载体。此部分主要是设置sessionBuilder的一些选项,如音频编码器、视频编码器等等。注意,sessionBuilder用到了单例设计模式,整个程序共享这一个sessionBuilder对象。此外,值得一提的是sessionBuilder的setSurfaceHolder方法,此方法其实没有太大的用处,不过因为android 某些API的限制,使得如果你要录制视频,必须要有有效的surface。

4、Start and stop the server like this:

// Starts the RTSP server context.startService(new Intent(this,RtspServer.class)); // Stops the RTSP server context.stopService(new Intent(this,RtspServer.class));
最后,启动或停止service。
阅读(3937) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~