mediaplayer: fix race condition with local media and bluetooth a2dp
[apps/mediaplayer.git] / app / MediaPlayer.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import QtQuick 2.6
18 import QtQuick.Layouts 1.1
19 import QtQuick.Controls 2.0
20 import AGL.Demo.Controls 1.0
21
22 ApplicationWindow {
23     id: root
24
25     Item {
26         id: player
27
28         property string title: ""
29         property string album: ""
30         property string artist: ""
31
32         property int duration: 0
33         property int position: 0
34
35         property string cover_art: ""
36         property string status: "stopped"
37
38         function time2str(value) {
39             return Qt.formatTime(new Date(value), 'mm:ss')
40         }
41     }
42
43     Item {
44         id: bluetooth
45
46         property string deviceAddress: ""
47         property bool connected: false
48         property bool av_connected: false
49
50         property int position: 0
51         property int duration: 0
52
53         property string artist: ""
54         property string title: ""
55         property string state: "stopped"
56
57         // AVRCP Target UUID
58         property string avrcp_uuid: "0000110e-0000-1000-8000-00805f9b34fb"
59
60         function connect_profiles() {
61             var address = bluetooth.deviceAddress;
62             bluetooth_connection.connect(address, "a2dp")
63             bluetooth_connection.connect(address, "avrcp")
64         }
65
66         function disconnect_profiles() {
67             var address = bluetooth.deviceAddress;
68             bluetooth_connection.disconnect(address, "a2dp")
69             bluetooth_connection.disconnect(address, "avrcp")
70         }
71
72         function set_avrcp_controls(cmd) {
73             bluetooth_connection.set_avrcp_controls(bluetooth.deviceAddress, cmd)
74         }
75     }
76
77     Connections {
78         target: bluetooth_connection
79
80         onDeviceListEvent: {
81             var address = ""
82             for (var i = 0; i < data.list.length; i++) {
83                 var item = data.list[i]
84                 if (item.Connected == "True" && item.UUIDs.indexOf(bluetooth.avrcp_uuid) >= 0) {
85                     address = item.Address
86
87                     bluetooth.connected = true
88                     mediaplayer.pause()
89
90                     //NOTE: This hack is here for when MediaPlayer is started
91                     //      with an existing connection.
92                     bluetooth.av_connected = item.AVPConnected == "True" 
93                 }
94             }
95             if (!address)
96                 bluetooth.connected = false
97             else
98                 bluetooth.deviceAddress = address
99         }
100
101         onDeviceUpdatedEvent: {
102             var metadata = data.Metadata
103
104             if (data.Connected == "False")
105                 return
106
107             if (!bluetooth.av_connected && data.AVPConnected == "True") {
108                 mediaplayer.pause()
109                 player.status = "stopped"
110             }
111
112             bluetooth.connected = data.Connected == "True"
113             bluetooth.av_connected = data.AVPConnected == "True"
114             bluetooth.deviceAddress = data.Address
115
116             if ('Position' in metadata)
117                 bluetooth.position = metadata.Position
118
119             if ('Duration' in metadata)
120                 bluetooth.duration = metadata.Duration
121
122             if ('Status' in metadata)
123                 bluetooth.state = metadata.Status
124
125             if ('Artist' in metadata)
126                 bluetooth.artist = metadata.Artist
127
128             if ('Title' in metadata)
129                 bluetooth.title = metadata.Title
130         }
131     }
132
133     Connections {
134         target: mediaplayer
135
136         onPlaylistChanged: {
137             playlist_model.clear();
138
139             for (var i = 0; i < playlist.list.length; i++) {
140                 var item = playlist.list[i]
141
142                 playlist_model.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' })
143
144                 if (item.selected) {
145                     playlistview.currentIndex = i
146                 }
147             }
148         }
149
150         onMetadataChanged: {
151             player.title = metadata.title
152             player.album = metadata.album
153             player.artist = metadata.artist
154
155             if (metadata.duration) {
156                 player.duration = metadata.duration
157             }
158
159             if (metadata.position) {
160                 player.position = metadata.position
161             }
162
163             if (metadata.status) {
164                 player.status = metadata.status
165             }
166
167             if (metadata.image) {
168                 player.cover_art = metadata.image
169             }
170
171             playlistview.currentIndex = metadata.index
172         }
173     }
174
175     Timer {
176         id: timer
177         interval: 250
178         running: (bluetooth.av_connected && bluetooth.state == "playing")
179         repeat: true
180         onTriggered: {
181             bluetooth.position = bluetooth.position + 250
182         }
183     }
184
185     ListModel {
186         id: playlist_model
187     }
188
189     ColumnLayout {
190         anchors.fill: parent
191         Item {
192             Layout.fillWidth: true
193             Layout.fillHeight: true
194             Layout.preferredHeight: 1080
195             clip: true
196             Image {
197                 id: albumart
198                 anchors.left: parent.left
199                 anchors.right: parent.right
200                 anchors.bottom: parent.bottom
201                 height: sourceSize.height * width / sourceSize.width
202                 fillMode: Image.PreserveAspectCrop
203                 source: player.cover_art ? player.cover_art : ''
204                 visible: bluetooth.av_connected == false
205             }
206
207             Item {
208                 anchors.left: parent.left
209                 anchors.right: parent.right
210                 anchors.bottom: parent.bottom
211                 height :307
212                 Rectangle {
213                     anchors.fill: parent
214                     color: 'black'
215                     opacity: 0.75
216                 }
217
218                 ColumnLayout {
219                     anchors.fill: parent
220                     anchors.margins: root.width * 0.02
221                     Item {
222                         Layout.fillWidth: true
223                         Layout.fillHeight: true
224                         Row {
225                             spacing: 20
226                             //ToggleButton {
227                             //    id: random
228                             //    visible: bluetooth.connected == false
229                             //    offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
230                             //    onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
231                             //}
232                             ToggleButton {
233                                 id: loop
234                                 visible: bluetooth.connected == false
235                                 //checked: player.loop_state
236                                 offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg'
237                                 onImage: './images/AGL_MediaPlayer_Loop_Active.svg'
238                                 onClicked: { mediaplayer.loop(checked) }
239                             }
240                         }
241                         ColumnLayout {
242                             anchors.fill: parent
243                             Label {
244                                 id: title
245                                 Layout.alignment: Layout.Center
246                                 text: bluetooth.av_connected ? bluetooth.title : (player.title ? player.title : '')
247                                 horizontalAlignment: Label.AlignHCenter
248                                 verticalAlignment: Label.AlignVCenter
249                             }
250                             Label {
251                                 Layout.alignment: Layout.Center
252                                 text: bluetooth.av_connected ? bluetooth.artist : (player.artist ? player.artist : '')
253                                 horizontalAlignment: Label.AlignHCenter
254                                 verticalAlignment: Label.AlignVCenter
255                                 font.pixelSize: title.font.pixelSize * 0.6
256                             }
257                         }
258                     }
259                     Slider {
260                         id: slider
261                         Layout.fillWidth: true
262                         to: bluetooth.av_connected ? bluetooth.duration : player.duration
263                         enabled: bluetooth.av_connected == false
264                         value: bluetooth.av_connected ? bluetooth.position : player.position
265                         function getPosition() {
266                             if (bluetooth.av_connected) {
267                                 return player.time2str(bluetooth.position)
268                             }
269                             return player.time2str(player.position)
270                         }
271                         Label {
272                             id: position
273                             anchors.left: parent.left
274                             anchors.bottom: parent.top
275                             font.pixelSize: 32
276                             text: slider.getPosition()
277                         }
278                         Label {
279                             id: duration
280                             anchors.right: parent.right
281                             anchors.bottom: parent.top
282                             font.pixelSize: 32
283                             text: bluetooth.av_connected ? player.time2str(bluetooth.duration) : player.time2str(player.duration)
284                         }
285                         onPressedChanged: mediaplayer.seek(value)
286                     }
287                     RowLayout {
288                         Layout.fillHeight: true
289 //                        Image {
290 //                            source: './images/AGL_MediaPlayer_Playlist_Inactive.svg'
291 //                        }
292 //                        Image {
293 //                            source: './images/AGL_MediaPlayer_CD_Inactive.svg'
294 //                        }
295                         Item { Layout.fillWidth: true }
296                         ImageButton {
297                             id: previous
298                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
299                             onClicked: {
300                                 if (bluetooth.av_connected) {
301                                     bluetooth.set_avrcp_controls("Previous")
302                                     bluetooth.position = 0
303                                 } else {
304                                     mediaplayer.previous()
305                                 }
306                             }
307                         }
308                         ImageButton {
309                             id: play
310                             states: [
311                                 State {
312                                     when: !bluetooth.av_connected && player.status == "playing"
313                                     PropertyChanges {
314                                         target: play
315                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
316                                         onClicked: {
317                                             player.status = "stopped"
318                                             mediaplayer.pause()
319                                         }
320                                     }
321                                 },
322                                 State {
323                                     when: bluetooth.av_connected && bluetooth.state == "playing"
324                                     PropertyChanges {
325                                         target: play
326                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
327                                         onClicked: bluetooth.set_avrcp_controls("Pause")
328                                     }
329                                 },
330                                 State {
331                                     when: !bluetooth.av_connected && player.status != "playing"
332                                     PropertyChanges {
333                                         target: play
334                                         offImage: './images/AGL_MediaPlayer_Player_Play.svg'
335                                         onClicked: mediaplayer.play()
336                                     }
337                                 },
338                                 State {
339                                     when: bluetooth.av_connected && bluetooth.state != "playing"
340                                     PropertyChanges {
341                                         target: play
342                                         offImage: './images/AGL_MediaPlayer_Player_Play.svg'
343                                         onClicked: bluetooth.set_avrcp_controls("Play")
344                                     }
345                                 }
346                             ]
347                         }
348                         ImageButton {
349                             id: forward
350                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
351                             onClicked: {
352                                 if (bluetooth.av_connected) {
353                                     bluetooth.set_avrcp_controls("Next")
354                                 } else {
355                                     mediaplayer.next()
356                                 }
357                             }
358                         }
359
360                         Item { Layout.fillWidth: true }
361  
362                         ToggleButton {
363                               visible: bluetooth.connected
364                               checked: bluetooth.av_connected
365                               offImage: './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
366                               onImage: './images/AGL_MediaPlayer_Bluetooth_Active.svg'
367
368                               onClicked: {
369                                   if (bluetooth.av_connected) {
370                                       bluetooth.disconnect_profiles()
371                                   } else {
372                                       bluetooth.connect_profiles()
373                                   }
374                               }
375                         }
376                     }
377                 }
378             }
379         }
380         Item {
381             Layout.fillWidth: true
382             Layout.fillHeight: true
383             Layout.preferredHeight: 407
384
385             ListView {
386                 anchors.fill: parent
387                 id: playlistview
388                 visible: bluetooth.av_connected == false
389                 clip: true
390                 header: Label {
391                     x: 50
392                     text: 'PLAYLIST'
393                     opacity: 0.5
394                 }
395                 model: playlist_model
396                 currentIndex: -1
397
398                 delegate: MouseArea {
399                     id: delegate
400                     width: ListView.view.width
401                     height: ListView.view.height / 4
402                     RowLayout {
403                         anchors.fill: parent
404                         anchors.leftMargin: 50
405                         anchors.rightMargin: 50
406                         ColumnLayout {
407                             Layout.fillWidth: true
408                             Label {
409                                 Layout.fillWidth: true
410                                 text: model.title
411                             }
412                             Label {
413                                 Layout.fillWidth: true
414                                 text: model.artist
415                                 color: '#00ADDC'
416                                 font.pixelSize: 32
417                             }
418                         }
419                         //Label {
420                         //    text: player.time2str(model.duration)
421                         //    color: '#00ADDC'
422                         //    font.pixelSize: 32
423                         //}
424                     }
425                     onClicked: {
426                         mediaplayer.picktrack(playlistview.model.get(index).index)
427                     }
428                 }
429
430                 highlight: Rectangle {
431                     color: 'white'
432                     opacity: 0.25
433                 }
434             }
435         }
436     }
437 }