MediaPlayer will now remember where the user was on restart. Also able to reconnect...
[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 var stopConnectionSearch = undefined;
10 var remoteMediaPreviouslyLoaded = false;
11
12 function convertMediaServerContent(item)
13 {
14         console.log(">>> MediaObject2 >>> " + JSON.stringify(item));
15         var content = { id: item.Path, title: item.DisplayName };
16
17         if (item.Type.indexOf("container") == 0)
18         {
19                 content.directoryURI = "";
20                 content.storageType = "EXTERNAL";
21         }
22
23         else
24         {
25                 content.editableAttributes = [];
26                 content.name = content.title;
27                 content.contentURI = item.URLs[0];
28                 content.mimeType = item.MIMEType;
29                 content.size = item.Size;
30                 content.rating = 0;
31                 content.remoteFile = true;
32
33                 if (item.Type.indexOf("video") == 0)
34                 {
35                         content.type = "VIDEO";
36                         content.artists = new Array();
37                         content.artists[0] = "Unknown";
38                         content.duration = item.Duration;
39                         content.width = item.Width;
40                         content.height = item.Height;
41                 }
42
43                 else if (item.Type.indexOf("audio") == 0 ||
44                 item.Type.indexOf("music") == 0)
45                 {
46                         content.type = "AUDIO";
47                         content.album = item.Album ? item.Album : "Unknown";
48                         content.coverArt = musicIcon;
49                         content.artists = new Array();
50                         content.artists[0] = item.Artist ? item.Artist : "Unknown";
51                         content.bitrate = item.SampleRate;
52                         content.duration = item.Duration;
53                 }
54
55                 if (item.Type.indexOf("image") == 0)
56                 {
57                         content.type = "IMAGE";
58                         content.width = item.Width;
59                         content.height = item.Height;
60                         content.orientation = "NORMAL";
61                 }
62         }
63
64         return content;
65 }
66
67 function browseCallback(medias)
68 {
69         console.log("MediaPlayer: MediaServer has called browseCallback");
70         console.log("found " + medias.length + " medias.");
71
72         for (var i=0; i<medias.length; i++)
73                 convertMediaServerContent(medias[i]);
74 }
75
76 function findAllCallback(medias)
77 {
78         console.log("MediaServer findAllCallback");
79         console.log("found " + medias.length + " medias.");
80
81         clearInterval(stopConnectionSearch);
82         stopConnectionSearch = undefined;
83
84         var itemsAdded = 0;
85
86         for (var i=0; i < medias.length; i++)
87         {
88                 var current = convertMediaServerContent(medias[i]);
89
90                 if (current)
91                 {
92                 switch(current.type)
93                 {
94                         case "AUDIO":
95                                 audioContent.push(current);
96                                 itemsAdded += 1;
97                                 break;
98                         case "VIDEO":
99                                 videoContent.push(current);
100                                 itemsAdded += 1;
101                                 break;
102                         case "IMAGE":
103                                 imageContent.push(current);
104                                 itemsAdded += 1;
105                                 break;
106                         default:
107                 }
108                 }
109         }
110
111         //Force MediaPlayer to reload the lists so the new items show
112         audioMediaListLoaded = false;
113         videoMediaListLoaded = false;
114         imageMediaListLoaded = false;
115 }
116
117 function restartMedia (medias)
118 {
119         clearInterval(stopConnectionSearch);
120         loadPrevVideo = true;
121         $("#videoSrc").attr("src", videoContent[videoIndex].contentURI);
122 }
123
124 function foundMediaServer(srv)
125 {
126         console.log("MediaPlayer: New MediaServer Found");
127         console.log("MediaServer id: " + srv.id);
128         console.log("MediaServer friendlyName: " + srv.friendlyName);
129
130         clearInterval(stopServerSearch);
131
132         if (srv.root)
133         {
134                 console.log("MediaPlayer: MediaServer root folder: " + srv.root.id);
135                 srv.browse(srv.root.id, "+DisplayName", 0, 0, browseCallback);
136                 srv.find(srv.root.id, "*", "+DisplayName", 0, 0, findAllCallback);
137
138                 if (stopConnectionSearch !== undefined)
139                         clearInterval(stopConnectionSearch);
140
141                 stopConnectionSearch = setInterval(function(){console.log("MediaPlayer searching for remote media..."); tizen.mediaserver.scanNetwork(foundMediaServer);}, 3000);
142         }
143         else
144                 console.log("MediaServer not browsable");
145 }
146
147 function reconnectServer(srv)
148 {
149         if (srv.root)
150         {
151                 srv.find(srv.root.id, "*", "+DisplayName", 0, 0, restartMedia);
152                 stopConnectionSearch = setInterval(function(){console.log("MediaPlayer searching for remote media for reconnect..."); tizen.mediaserver.scanNetwork(reconnectServer);}, 1000);
153         }
154         else
155                 console.log("MediaServer not reloadable");
156 }