机顶盒 DLNA代码 Linux C
时间: 2024-02-12 16:19:10 浏览: 265
android DLNA代码
3星 · 编辑精心推荐
以下是一个使用Linux C语言实现的DLNA服务器示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <upnp/upnp.h>
#define MAX_NAME_LEN 256
#define MAX_DESC_LEN 512
#define MAX_ICON_LEN 1024
int main(int argc, char* argv[]) {
int ret;
char name[MAX_NAME_LEN] = "My DLNA Server";
char desc[MAX_DESC_LEN] = "A simple DLNA server";
char icon[MAX_ICON_LEN] = "http://192.168.1.100:1234/icon.png";
char *udn = "uuid:01234567-89ab-cdef-0123-456789abcdef";
int port = 1234;
char media_dir[MAX_NAME_LEN] = "/media";
ret = UpnpInit(NULL, 0);
if (ret != UPNP_E_SUCCESS) {
fprintf(stderr, "Error: UpnpInit failed: %d\n", ret);
return -1;
}
UpnpDevice_Handle device_handle;
ret = UpnpRegisterRootDevice(desc, &device_handle, NULL);
if (ret != UPNP_E_SUCCESS) {
fprintf(stderr, "Error: UpnpRegisterRootDevice failed: %d\n", ret);
UpnpFinish();
return -1;
}
char udn_str[MAX_NAME_LEN];
snprintf(udn_str, MAX_NAME_LEN, "uuid:%s", udn);
char *device_type = "urn:schemas-upnp-org:device:MediaServer:1";
char *friendly_name = name;
char *manufacturer = "MyCompany";
char *manufacturer_url = "http://www.mycompany.com";
char *model_name = "My DLNA Server";
char *model_number = "1.0";
char *model_url = "http://www.mycompany.com/mymodel";
char *serial_number = "1234567890";
char *presentation_url = "http://192.168.1.100:1234/";
UpnpDevice_Handle device_handle;
ret = UpnpRegisterRootDevice(desc, &device_handle, NULL);
if (ret != UPNP_E_SUCCESS) {
fprintf(stderr, "Error: UpnpRegisterRootDevice failed: %d\n", ret);
UpnpFinish();
return -1;
}
char *service_type = "urn:schemas-upnp-org:service:ContentDirectory:1";
char *service_id = "urn:upnp-org:serviceId:ContentDirectory";
char *service_desc = "Content Directory Service";
char *service_control_url = "/ContentDirectory/control";
char *service_event_url = "/ContentDirectory/event";
char *service_scpd_url = "/ContentDirectory/scpd.xml";
UpnpService_Handle service_handle;
ret = UpnpAddService(device_handle, service_type, service_id, service_desc,
service_control_url, service_event_url, service_scpd_url,
&service_handle);
if (ret != UPNP_E_SUCCESS) {
fprintf(stderr, "Error: UpnpAddService failed: %d\n", ret);
UpnpFinish();
return -1;
}
UpnpSetServiceDefaultRespMsgTimeout(service_handle, 30);
char *var_name = "SystemUpdateID";
char *var_value = "0";
UpnpAddStateVariable(service_handle, var_name, UPNP_STATE_VAR_TYPE_UI4,
UPNP_STATE_VAR_FLAG_SEND_EVENTS, var_value);
var_name = "ContainerUpdateIDs";
var_value = "";
UpnpAddStateVariable(service_handle, var_name, UPNP_STATE_VAR_TYPE_STRING,
UPNP_STATE_VAR_FLAG_SEND_EVENTS, var_value);
var_name = "SortCapabilities";
var_value = "";
UpnpAddStateVariable(service_handle, var_name, UPNP_STATE_VAR_TYPE_STRING,
UPNP_STATE_VAR_FLAG_SEND_EVENTS, var_value);
var_name = "SystemUpdateID";
var_value = "0";
UpnpAddStateVariable(service_handle, var_name, UPNP_STATE_VAR_TYPE_UI4,
UPNP_STATE_VAR_FLAG_SEND_EVENTS, var_value);
var_name = "ContainerUpdateIDs";
var_value = "";
UpnpAddStateVariable(service_handle, var_name, UPNP_STATE_VAR_TYPE_STRING,
UPNP_STATE_VAR_FLAG_SEND_EVENTS, var_value);
var_name = "SortCapabilities";
var_value = "";
UpnpAddStateVariable(service_handle, var_name, UPNP_STATE_VAR_TYPE_STRING,
UPNP_STATE_VAR_FLAG_SEND_EVENTS, var_value);
UpnpAddAction(service_handle, "Browse", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "Search", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "GetSortCapabilities", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "GetSystemUpdateID", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "GetSearchCapabilities", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "CreateObject", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "DestroyObject", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "UpdateObject", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "ImportResource", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "ExportResource", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "StopTransferResource", NULL, NULL, NULL, NULL, NULL, NULL);
UpnpAddAction(service_handle, "GetTransferProgress", NULL, NULL, NULL, NULL, NULL, NULL);
char *device_desc_template = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n"
" <specVersion>\n"
" <major>1</major>\n"
" <minor>0</minor>\n"
" </specVersion>\n"
" <device>\n"
" <deviceType>%s</deviceType>\n"
" <friendlyName>%s</friendlyName>\n"
" <manufacturer>%s</manufacturer>\n"
" <manufacturerURL>%s</manufacturerURL>\n"
" <modelName>%s</modelName>\n"
" <modelNumber>%s</modelNumber>\n"
" <modelURL>%s</modelURL>\n"
" <UDN>%s</UDN>\n"
" <serialNumber>%s</serialNumber>\n"
" <presentationURL>%s</presentationURL>\n"
" <iconList>\n"
" <icon>\n"
" <mimetype>image/png</mimetype>\n"
" <width>120</width>\n"
" <height>120</height>\n"
" <depth>24</depth>\n"
" <url>%s</url>\n"
" </icon>\n"
" </iconList>\n"
" <serviceList>\n"
" <service>\n"
" <serviceType>%s</serviceType>\n"
" <serviceId>%s</serviceId>\n"
" <SCPDURL>%s</SCPDURL>\n"
" <controlURL>%s</controlURL>\n"
" <eventSubURL>%s</eventSubURL>\n"
" </service>\n"
" </serviceList>\n"
" </device>\n"
"</root>\n";
char device_desc[MAX_DESC_LEN];
snprintf(device_desc, MAX_DESC_LEN, device_desc_template, device_type, friendly_name,
manufacturer, manufacturer_url, model_name, model_number, model_url, udn_str,
serial_number, presentation_url, icon, service_type, service_id, service_scpd_url,
service_control_url, service_event_url);
ret = UpnpSetDeviceDescription(device_handle, device_desc);
if (ret != UPNP_E_SUCCESS) {
fprintf(stderr, "Error: UpnpSetDeviceDescription failed: %d\n", ret);
UpnpFinish();
return -1;
}
UpnpSetWebServerRootDir(media_dir);
printf("DLNA server started on port %d\n", port);
ret = UpnpStartWebServer(port, media_dir);
if (ret != UPNP_E_SUCCESS) {
fprintf(stderr, "Error: UpnpStartWebServer failed: %d\n", ret);
UpnpFinish();
return -1;
}
UpnpFinish();
return 0;
}
```
这个示例代码使用了libupnp库来实现DLNA服务器的功能。在代码中定义了DLNA服务器的名称、描述、图标、UDN、端口号和媒体文件夹路径等参数。在UpnpInit()函数中初始化了libupnp库,在UpnpRegisterRootDevice()函数中注册了根设备,并返回设备句柄。然后在UpnpAddService()函数中添加了ContentDirectory服务,设置了服务的状态变量和动作。在UpnpSetDeviceDescription()函数中设置了设备描述文件。最后在UpnpStartWebServer()函数中启动了Web服务器,开始提供媒体文件的访问服务。
阅读全文