Dlna (UPnP Media Server) development

Source: Internet
Author: User
With the trend of mobile Internet, multi-device interaction has gradually entered people's lives. For example, file sharing between mobile QQ and PC allows mobile phones to watch videos and smart routers on PCs. Related attempts started a long time ago, such as UPnP and dlna. As a bunch of industry leaders, dlna combines many other protocols to define some devices and interactions, making media interconnection between devices possible. UPnP is the core protocol, which is based on PTC/IP at the underlying layer and involves DHCP. In addition, some dlna devices need to be abstracted at the upper layer to describe the media stream. This is dlna.
UPnP was first initiated by Intel and provided the open-source implementation (libupnp). Later, some changes have taken place. At present, it can still be downloaded to this open-source development package. There are related development documents on the Internet: upnpprogramming examples, UPnP [1]. 0-chinese_upnp Chinese version. For UPnP itself, more important protocols have UPnP-arch-DeviceArchitecture-v1.0-20081015, basically laid the foundation of UPnP, on which some applications are also defined, such as media service, which defines the architecture of the entire media service, this includes media servers, content services, connection management, media transmission, and rendering control to achieve media resource sharing and interaction.
In the basic architecture of UPnP, there are six aspects: Addressing, discovery, description, control, and event display, which solve how to automatically discover devices in the network and know the properties of devices, controls the device to perform certain operations and receive event notifications from the device.

Because the SDK is used on the PC end, the PC is mainly used as the Media Server to share the media on the PC to other devices. (In fact, it can also be compiled as a mobile so)

What kind of Apis does libupnp provide to facilitate UPnP application construction? Here is a simple description based on ushare (which can be considered as a simple introduction of ushare ).

First, the SDK provides an internal mechanism for searching, discovering, and controlling UPnP devices. The interfaces exposed to users are registration devices and their callbacks, device operation APIs, registration control point callbacks, and so on. Secondly, the SDK implements a webserver so that users can implement a web server. Because UPnP is based on HTTP for many data interactions.


However, this SDK is somewhat inadequate: Only one device can be registered and can only work on one network. However, in the SDK scenario, a device is sufficient to make the device scalable by adding more services to the device. However, a single network can also meet the needs of the vast majority of scenarios: In a home network, a PC has multiple IP addresses and each network has media requirements.

So far, the following process is required to use this SDK to implement a Media Server:
Initialize UPnP-> Start webwerver and register callback-> Register device and callback-> Register Control Point callback.

Let's assume that the next folder named server_root on drive D is shared out. What should we do?

First, you need the device description and service description, which is defined in mediaservice.

Next, you need to construct an internal data structure to share the metadata of the file so that there is information for all the files under the folder. You can obtain metadata synchronously when the service starts.

The next step is to implement device callback. The most important thing is to control the callback of an action. You do not need to consider other measures, such as grading and getting variables. After implementing the control action callback, the service is basically available. The most important thing to control the action callback is to implement the browsing function. In this way, the client can know the item list under the root directory and the item attributes under the list. This action is described in the UPnP-av-ContentDirectory-v1-Service-20020625: The content directory service under the media server, the server describes the parameters that the browsing action should carry and the response. To make the corresponding response logic, you only need to know the above file metadata.


Finally, the file content is transmitted. When the returned data of the preceding browsing action is generated, the item in it contains the URL of the file content. When sending a request to the URL, the SDK webserver processes the request and generates a callback. A callback is a file IO, which includes getting file information, opening the file, reading the file, writing the file, seek, and closing the file. The user needs to find the corresponding object meta information based on the URL entered during the callback, and then make the corresponding action. For example, when a file is opened, a file information is created. The file information contains the file handle and read location. Then, the file information is sent to the webserver as a cookie. When reading a file, this cookie is still carried, so you can read a piece of data and pass it back.

So far, it is clear to implement the media server, but only one file directory can be shared. For more functions, You need to modify the definition of the object metadata, such as introducing a virtual directory. Can be invisible in browsing actions, but will user access URL return data? Yes. The implementation is very simple.

However, it takes a long time to construct the object metadata. How can this problem be solved?
You can create information caches. Multipart object metadata can be loaded.

How can I ensure that files in the directory are synchronized with files on the disk?
It can be implemented through some system mechanisms. But it is generally unnecessary.


For more information about the effect of the Media Server, see mini-WiFi (audio and video sharing, transfer objects by air ), after Baidu audio and video enables dlna sharing in the LAN, you can watch Baidu audio and video on the LAN (not only on PC, but also on the Internet ).
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.