Red5下的room
http://blog.csdn.net/whycold/article/details/6142475
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.Red5;
public class RoomApplication extends ApplicationAdapter{
private static HashMap<String,IScope> scopeMap = new HashMap<String,IScope>();
private static HashMap<String,List> userMap = new HashMap<String,List>();
private static HashMap<String,String> videoMap = new HashMap<String,String>();
@Override
public boolean roomConnect(IConnection conn, Object[] arg1) {
// TODO Auto-generated method stub
String userName = arg1[0].toString();
String roomName = arg1[1].toString();
if(userMap.containsKey(roomName)){
List list = userMap.get(roomName);
list.add(userName);
}else{
List<String> list = new ArrayList<String>();
list.add(userName);
userMap.put(roomName, list);
}
conn.getClient().setAttribute("userName", userName);
return super.roomConnect(conn, arg1);
}
@Override
public void roomDisconnect(IConnection client) {
// TODO Auto-generated method stub
super.roomDisconnect(client);
}
@Override
public boolean roomJoin(IClient client, IScope s) {
// TODO Auto-generated method stub
System.out.println("room join" + s.getName());
updateUserList(s.getName());
String roomName = s.getName();
String userName = client.getAttribute("userName").toString();
sendVideo(userName,roomName);
return super.roomJoin(client, s);
}
public void sendVideo(String userName,String roomName){
if(videoMap.containsKey(roomName)){
System.out.println("我要拿视频啦");
IScope s = scopeMap.get(roomName);
System.out.println(s);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
System.out.println("connection");
IConnection conn = iterator.next();
if(conn.getClient().getAttribute("userName").toString().equalsIgnoreCase(userName)){
if (conn instanceof IServiceCapableConnection)
{
//转发消息
System.out.println("conn====" + conn);
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("netStream", new Object[]{videoMap.get(roomName)});
}
}
}
}
}
@Override
public void roomLeave(IClient client, IScope s) {
// TODO Auto-generated method stu
String userName = client.getAttribute("userName").toString();
String roomName = s.getName();
List list = userMap.get(roomName);
list.remove(userName);
updateUserList(roomName);
if(videoMap.containsKey(roomName)){
if(videoMap.get(roomName).toString().equalsIgnoreCase(userName)){
videoMap.remove(roomName);
}
}
super.roomLeave(client, s);
}
@Override
public boolean roomStart(IScope s) {
// TODO Auto-generated method stub
System.out.println("room start" + s.getName());
scopeMap.put(s.getName(), s);
return super.roomStart(s);
}
@Override
public void roomStop(IScope s) {
// TODO Auto-generated method stub
scopeMap.remove(s.getName());
System.out.println("房间彻底关闭了");
super.roomStop(s);
}
public void sendToRoom(String msg,String roomName,String userName,String toUserName){
IScope scope = scopeMap.get(roomName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
String connUserName = conn.getClient().getAttribute("userName").toString();
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
if("all".equalsIgnoreCase(toUserName)){
//转发消息
String str = userName + "说:"+msg;
sc.invoke("sendToRoom", new Object[]{str});
}else if(connUserName.equalsIgnoreCase(userName)){
String str = "你对" + toUserName + "说:"+msg;
sc.invoke("sendToRoom", new Object[]{str});
}else if(connUserName.equalsIgnoreCase(toUserName)){
String str = userName + "对你说:"+msg;
sc.invoke("sendToRoom", new Object[]{str});
}
}
}
}
public void updateUserList(String roomName){
System.out.println("send userlist==" + roomName);
List list = userMap.get(roomName);
System.out.println("有"+list.size()+"个子");
IScope s = scopeMap.get(roomName);
System.out.println(s);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
System.out.println("connection");
IConnection conn = iterator.next();
if (conn instanceof IServiceCapableConnection)
{
//转发消息
System.out.println("conn====" + conn);
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("updateList", new Object[]{list});
}
}
}
public void playVideo(String userName,String roomName){
IScope scope = scopeMap.get(roomName);
videoMap.put(roomName, userName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("netStream", new Object[]{userName});
}
}
}
public void escVideo(String roomName){
IScope scope = scopeMap.get(roomName);
videoMap.remove(roomName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("escVideo", new Object[]{});
}
}
}
public void checkVideo(String userName,String roomName){
System.out.println("checkVideo");
IScope scope = scopeMap.get(roomName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
String connUserName = conn.getClient().getAttribute("userName").toString();
System.out.println("conUserName");
if(userName.equalsIgnoreCase(connUserName)){
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
if(videoMap.containsKey(roomName)){
sc.invoke("canNotVideo", new Object[]{});
}else{
System.out.println("canvideo");
sc.invoke("canVideo", new Object[]{});
}
}
}
}
}
}
mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="con()">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import flash.utils.flash_proxy;
import mx.core.UIComponent;
import mx.messaging.AbstractConsumer;
import mx.collections.ArrayCollection;
import flash.events.*;
import flash.media.Video;
import flash.net.*;
import flash.external.ExternalInterface;
[Bindable]
private var net:NetConnection;
import mx.controls.Alert;
private var username:String;
private var roomname:String;
[Bindable]
private var arrayCon:ArrayCollection;
[Bindable]
private var comboArrayCon:ArrayCollection;
var ns:NetStream;
var nsPlay:NetStream;
public function con():void{
//chat.addEventListener(Event.ENTER_FRAME,onEnterFrame);
username = parameters.name;
roomname = parameters.roomname;
//username="22";
//roomname="11";
if(comboArrayCon==null){
comboArrayCon = new ArrayCollection();
}
comboArrayCon.addItem("all");
//Alert.show(username,roomname);
l.text = "您在"+roomname+"聊天室里!";
if(net==null){
net = new NetConnection();
net.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
net.addEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError);
net.connect("rtmp://192.168.1.32/mychat/"+roomname+"/",username,roomname);
net.client = this;
}
}
private function onAsyncError(event:AsyncErrorEvent):void{
trace("asyncerror");
}
private function onEnterFrame(event:Event):void{
}
public function netStatus(event:NetStatusEvent):void{
trace(event.info.code);
switch(event.info.code){
case "NetConnection.Connect.Success":
ns = new NetStream(net);
var mic:Microphone = Microphone.getMicrophone();
mic.setUseEchoSuppression(true);
mic.setSilenceLevel(0);
ns.attachAudio(mic);
try{
var cam:Camera = Camera.getCamera();
cam.setMode(271,241,15);
cam.setQuality(144000,85 );
ns.attachCamera(cam);
}catch(error:Error){
trace(error);
}
getVideo();
break;
}
}
protected function image1_clickHandler(event:MouseEvent):void
{
ExternalInterface.call("geturl");
}
private function onEnterClick(event:KeyboardEvent):void{
if(event.keyCode==13){
sendMsg();
}
}
private function onSendClick(event:Event):void{
sendMsg();
}
private function sendMsg():void{
if(msg.text==""){
Alert.show("请输入内容!");
}else{
net.call("sendToRoom",null,msg.text,roomname,username,o.selectedItem);
msg.text = "";
this.setFocus();
this.focusManager.setFocus(msg);
msg.setSelection(0,0);
}
}
public function sendToRoom(str:String):void{
c.text = c.text + str+"/n";
this.focusManager.setFocus(msg);
}
public function updateList(user:String):void{
try{
trace("update");
var arr:Array;
if(user==""){
arr = new Array();
}else{
arr = user.split(",");
}
arrayCon = new ArrayCollection(arr);
}catch(err:Error){
trace(err);
}
}
protected function userlist_itemClickHandler(event:ListEvent):void
{
if(!comboArrayCon.contains(userlist.selectedItem)){
comboArrayCon.addItem(userlist.selectedItem);
o.selectedIndex = comboArrayCon.length;
}else{
o.selectedIndex = comboArrayCon.getItemIndex(userlist.selectedItem);
}
}
private function oLabel(object:Object):String{
return object.toString();
}
public function netStream(name:String):void{
nsPlay = new NetStream(net);
nsPlay.play(name);
var video:Video = new Video(v.width,v.height);
v.addChild(video);
video.attachNetStream(nsPlay);
video.width = v.width;
video.height = v.height;
}
public function playVideo():void{
net.call("checkVideo",null,username,roomname);
}
public function getVideo():void{
net.call("sendVideo",null,username,roomname);
}
public function canVideo():void{
ns.publish(username);
w.enabled = false;
s.enabled = true;
net.call("playVideo",null,username,roomname);
}
public function canNotVideo():void{
Alert.show("有人抢先啦,慢慢等吧");
}
public function escVideo():void{
trace("escvideo");
nsPlay.close();
w.enabled = true;
s.enabled = false;
}
]]>
</mx:Script>
<mx:Label width="168" id="l"/>
<mx:TextArea width="472" height="239" id="c" editable="false" x="5" y="26"/>
<mx:TextInput x="67" y="273" width="335" id="msg" keyUp="onEnterClick(event)"/>
<mx:List x="484" y="24" width="122" height="241" id="userlist" dataProvider="{arrayCon}" itemClick="userlist_itemClickHandler(event)"/>
<mx:ComboBox x="-1" y="273" editable="false" width="60" id="o" dataProvider="{comboArrayCon}" />
<mx:Button x="414" y="273" label="send" width="63" id="send" click="onSendClick(event)"/>
<mx:Button x="485" y="273" label="我要视频" click="playVideo()" id="w"/>
<mx:VideoDisplay x="623" y="24" width="271" height="241" id="v"/>
<mx:Button x="563" y="273" label="不视频了" click="net.call('escVideo',null,roomname)" enabled="false" id="s"/>
</mx:Application>
Red5下的room的更多相关文章
- red5下nginx安装配置
http://zfl110.iteye.com/blog/1155149 原址:http://lqw.iteye.com/blog/652763 安装Nginx 1.首先安装pcre-8.02.tar ...
- C++程序结构---1
C++ 基础教程Beta 版 原作:Juan Soulié 翻译:Jing Xu (aqua) 英文原版 本教程根据Juan Soulie的英文版C++教程翻译并改编. 本版为最新校对版,尚未定稿.如 ...
- CentOS下Red5安装
Red5介绍 Red5是一个采用Java开发开源的Flash流媒体服务器.它支持:把音频(MP3)和视频(FLV)转换成播放流: 录制客户端播放流(只支持FLV):共享对象:现场直播流发布:远程调用. ...
- Windows环境搭建Red5流媒体服务器指南
Windows环境搭建Red5流媒体服务器指南 测试环境:Windows 7 一. 下载安装程序 red5-server 下载地址 https://github.com/Red5/red5-ser ...
- Red5 1.0.0RC1 集成到tomcat6.0.35中运行&部署新的red5项目到tomcat中
1.下载red5-war-1.0-RC1.zip 解压之得到 ROOT.war 文件. 2.处理tomcat. 下载apache-tomcat-6.0.35-windows-x86.zip包,解压到你 ...
- ubuntu12.04server下red5-1.0.0RC1的部署
一.搭建环境 Linux版本:ubuntu12.04sever 64位 Java 版本:Java 1.7(jdk+jre) Red5 版本:red5-1.0.0-RC1 二.安装JDK 下载jdk ...
- ubuntu16.04部署RED5流媒体服务器
前提:jdk已经安装 1,下载RED5(https://github.com/Red5/red5-server/releases)选择red5-server-X.X.X-MXX.tar.gz 2,解压 ...
- 开源代码Window下搭建rtmp流媒体服务器
合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综合:有这样需求,将摄像头 ...
- android + red5 + rtmp
背景:在已有的red5服务器环境下实现android客户端的视频直播 要实现客户端视频直播就先先对服务器端有所了解 Red5流媒体服务器是Adboe的产品,免费并且是开源的,与Flash搭配的时候可谓 ...
随机推荐
- oracle .bash_profile
[oracle@redhat4 ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashr ...
- mac下git中文乱码
今天从window切mac,git使用时各种问题.典型的就是,git commit 可以使用中文注释,但是使用 git log 查看的时候发现都是乱码,乱码效果如下: <B1><E0 ...
- Codeforces Beta Round #9 (Div. 2 Only)D
短小精悍的代码 dp[i][j] +=dp[k][j-1]*[i-k-1][j-1] i个结点 J层 #include <iostream> #include<cstdio> ...
- UVa 1646 (递推 JAVA大数) Edge Case
题意: 有n个点围成一圈,这n个点的匹配就是没有公共点的边集(这些边只能连接一圈中相邻的两点),求所有匹配的个数. 额,我不会分析..=_=|| 算了几个数,找找规律发现它满足斐波那契数列的递推关系, ...
- HDU 1358 (所有前缀中的周期串) Period
题意: 给出一个字符串,在所有长度大于1的前缀中,求所有的周期至少为2的周期串,并输出一个周期的长度以及周期的次数. 分析: 有了上一题 HDU 3746 的铺垫,这道题就很容易解决了 把next求出 ...
- span元素定义宽高度
由于span是行内元素,不可能有高度和宽度的,在span标签里添加内容,可以撑出来宽高,想要定义宽高必须转话成块级元素. span{ display:block; } 或者使用 span{ displ ...
- 实现窗口逐渐增大(moveTo(),resizeTo(),resizeBy()方法)
moveTo()方法格式:window.moveTo(x,y); 功能:将窗口移动到指定坐标(x,y)处; resizeTo()方法格式:window.resizeTo(x,y); 功能:将当前窗口改 ...
- Android PRODUCT_COPY_FILES 自动拷贝文件
/********************************************************************** * Android PRODUCT_COPY_FILES ...
- shell脚本实例
备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...
- MySQL基础之第9章 触发器
触发器(TRIGGER)是由事件来触发某个操作.这些事件包括INSERT语句.UPDATE语句和DELETE语句.当数据库系统执行这些事件时,就会激活触发器执行相应的操作.MySQL从5.0.2版本开 ...