ebebe2e85f0d255014dafb8f77bc5253376c7833
[profile/ivi/MediaPlayer.git] / js / dlna.js
1 /*
2  * Copyright (c) 2013, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 function convertMediaServerContent(item)
11 {
12         console.log(">>> MediaObject2 >>> " + JSON.stringify(item));
13         var content = { id: item.Path, title: item.DisplayName };
14
15         if (item.Type.indexOf("container") == 0)
16         {
17                 content.directoryURI = "";
18                 content.storageType = "EXTERNAL";
19         }
20
21         else {
22                 content.editableAttributes = [];
23                 content.name = content.title;
24                 content.contentURI = item.URLs[0];
25                 content.mimeType = item.MIMEType;
26                 content.size = item.Size;
27                 content.rating = 0;
28                 if (item.Type.indexOf("video") == 0)
29                 {
30                         content.type = "VIDEO";
31                         content.artists = new Array();
32                         content.artists[0] = "Unknown";
33                         content.duration = item.Duration;
34                         content.width = item.Width;
35                         content.height = item.Height;
36                 }
37
38                 else if (item.Type.indexOf("audio") == 0 ||
39                 item.Type.indexOf("music") == 0)
40                 {
41                         content.type = "AUDIO";
42                         content.album = item.Album ? item.Album : "Unknown";
43                         content.coverArt = musicIcon;
44                         content.artists = new Array();
45                         content.artists[0] = item.Artist ? item.Artist : "Unknown";
46                         content.bitrate = item.SampleRate;
47                         content.duration = item.Duration;
48                 }
49
50                 if (item.Type.indexOf("image") == 0)
51                 {
52                         content.type = "IMAGE";
53                         content.width = item.Width;
54                         content.height = item.Height;
55                         content.orientation = "NORMAL";
56                 }
57         }
58
59         return content;
60 }
61
62 function browseCallback(medias)
63 {
64         console.log("MediaPlayer: MediaServer has called browseCallback");
65         console.log("found " + medias.length + " medias.");
66
67         for (var i=0; i<medias.length; i++)
68                 convertMediaServerContent(medias[i]);
69 }
70
71 function findAllCallback(medias)
72 {
73         console.log("MediaServer findAllCallback");
74         console.log("found " + medias.length + " medias.");
75
76         var itemsAdded = 0;
77
78         for (var i=0; i < medias.length; i++)
79         {
80                 var current = convertMediaServerContent(medias[i]);
81
82                 if (current)
83                 {
84                 switch(current.type)
85                 {
86                         case "AUDIO":
87                                 console.log("MediaPlayer: Remote server found audio " + current);
88                                 audioContent.push(current);
89                                 console.log("MediaPlayer: Remote added audiocontent now = " + audioContent);
90                                 itemsAdded += 1;
91                                 break;
92                         case "VIDEO":
93                                 console.log("MediaPlayer: Remote server found video " + current);
94                                 videoContent.push(current);
95                                 console.log("MediaPlayer: Remote added videocontent now = " + videoContent);
96                                 itemsAdded += 1;
97                                 break;
98                         case "IMAGE":
99                                 console.log("MediaPlayer: Remote server found image " + current);
100                                 imageContent.push(current);
101                                 console.log("MediaPlayer: Remote added imagecontent now = " + imageContent);
102                                 itemsAdded += 1;
103                                 break;
104                         default:
105                                 console.log("MediaPlayer: Remote media failed to push content, not == AUDIO/VIDEO/IMAGE");
106                 }
107                 }
108         }
109
110 }
111
112 function foundMediaServer(srv)
113 {
114         console.log("MediaPlayer: New MediaServer Found");
115         console.log("MediaServer id: " + srv.id);
116         console.log("MediaServer friendlyName: " + srv.friendlyName);
117
118         if (srv.root)
119         {
120                 console.log("MediaPlayer: MediaServer root folder: " + srv.root.id);
121                 srv.browse(srv.root.id, "+DisplayName", 0, 0, browseCallback);
122                 srv.find(srv.root.id, "*", "+DisplayName", 0, 0, findAllCallback);
123         }
124         else
125                 console.log("MediaServer not browsable");
126 }