功能:让本机的视频在其余设备上播放。java
demo 学习中node
关键词: cling git
demo from: DROID DLNA 服务器
关于投屏的原理,协议相关的知识已经在上一篇中说明。ide
关于设备搜索,查找等不在此处说明。学习
投屏系统中分: 设备 服务 控制点。url
当须要把A 设备中的视频 投屏到 B 设备。rest
重点是:如何生成本地视频的URL, 这样才能经过 控制点 设置play url 到 播放端。code
跟踪demo 代码发现,播放的这个本地视频,返回的是一个xml 数据。节点中提供了这些视频的 thumb , url 。视频
ContentDirectoryService
这个类中的browse 方法,实现了数据查询和生成方法。
中间的代码写的很是复杂,在
MethodActionExecutor
中用了反射最终调用到这里。 其中的两个service(Local & remote 都不是真正的service)
本地文件的信息在初始化的时候就被所有扫描并添加进来:
ContentTree
addNode
而且生成了 每一个节点的url;
这是demo 中生成的 video item 节点
<item id="video-item-27559" parentID="1" restricted="0"> <dc:title>SVID_20170929_115451</dc:title> <dc:creator><unknown></dc:creator> <upnp:class>object.item.videoItem</upnp:class> <upnp:albumArtURI>http://10.4.58.82:8192/storage/emulated/0/msi/.videothumb/video_thumb_video-item-27559.png</upnp:albumArtURI> <dc:description/> <res protocolInfo="http-get:*:video/mp4:*" size="123202814" duration="0:3:1" resolution="1280x720">http://10.4.58.82:8192/video-item-27559</res> </item>
这是demo 中生成单个播放ship video Item 的方法:
DevicesActivity
String id = ContentTree.VIDEO_PREFIX + cursor.getInt(cursor .getColumnIndex(MediaStore.Video.Media._ID)); String title = cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)); String creator = cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST)); String filePath = cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); String mimeType = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)); long size = cursor.getLong(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); long duration = cursor .getLong(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)); String resolution = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.RESOLUTION)); String description = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DESCRIPTION)); Res res = new Res(new MimeType(mimeType.substring(0, mimeType.indexOf('/')), mimeType.substring(mimeType .indexOf('/') + 1)), size, "http://" + mediaServer.getAddress() + "/" + id); res.setDuration(duration / (1000 * 60 * 60) + ":" + (duration % (1000 * 60 * 60)) / (1000 * 60) + ":" + (duration % (1000 * 60)) / 1000); res.setResolution(resolution); VideoItem videoItem = new VideoItem(id, ContentTree.VIDEO_ID, title, creator, res); // add video thumb Property String videoSavePath = ImageUtil.getSaveVideoFilePath(filePath, id); DIDLObject.Property albumArtURI = new DIDLObject.Property.UPNP.ALBUM_ART_URI( URI.create("http://" + mediaServer.getAddress() + videoSavePath)); Property[] properties = { albumArtURI }; videoItem.addProperties(properties); videoItem.setDescription(description); videoContainer.addItem(videoItem); videoContainer .setChildCount(videoContainer.getChildCount() + 1); ContentTree.addNode(id, new ContentNode(id, videoItem, filePath));
HTTP 视频播放服务:
电视会经过以前手机生成的 视频节点信息中的Uri ,到手机端 请求视频。
处理部分: HttpServer,这个文件是本身实现的,而不是cling 提供的。
String itemId = uri.replaceFirst("/", ""); itemId = URLDecoder.decode(itemId); String newUri = null; if( ContentTree.hasNode(itemId) ) { ContentNode node = ContentTree.getNode(itemId); if (node.isItem()) { newUri = node.getFullPath(); } }
一些其余的关键词:
DMS:Digital Media Server 数字媒体服务器
DMP:Digital Meidal Player : 播放器
DMC : Controller 控制器
DMR: Renderer 渲染器