├── LICENSE └── plugin ├── __init__.py ├── airplay.py ├── airplayserver.py ├── openairplay.png └── plugin.py /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 19yy 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) 19yy name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /plugin/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E2OpenPlugins/e2openplugin-OpenAirPlay/0fe2edfa6f859b84752356d148ce7e7a5b2c45b9/plugin/__init__.py -------------------------------------------------------------------------------- /plugin/airplay.py: -------------------------------------------------------------------------------- 1 | # SIFTeam OpenAirPlay 2 | # Copyright (C) <2012> skaman (SIFTeam) 3 | # 4 | # This library is free software; you can redistribute it and/or 5 | # modify it under the terms of the GNU Library General Public 6 | # License as published by the Free Software Foundation; either 7 | # version 2 of the License, or (at your option) any later version. 8 | # 9 | # This library is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | # Library General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU Library General Public 15 | # License along with this library; if not, write to the 16 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 | # Boston, MA 02111-1307, USA. 18 | # 19 | 20 | from enigma import ePicLoad, eServiceReference, eTimer 21 | from Screens.Screen import Screen 22 | from Components.config import config 23 | from Components.Pixmap import Pixmap 24 | from Components.ActionMap import ActionMap 25 | from Components.AVSwitch import AVSwitch 26 | from Components.ServiceEventTracker import ServiceEventTracker 27 | 28 | from airplayserver import APServer, APCallbacks 29 | 30 | 31 | class AirPlayPhoto(Screen): 32 | skin = """ 33 | 34 | 35 | """ 36 | 37 | def __init__(self, session): 38 | Screen.__init__(self, session) 39 | self["image"] = Pixmap() 40 | self.picload = ePicLoad() 41 | self.picload.PictureData.get().append(self.__showPic) 42 | 43 | def __showPic(self, picInfo=""): 44 | ptr = self.picload.getData() 45 | if ptr != None: 46 | self["image"].instance.setPixmap(ptr.__deref__()) 47 | self["image"].show() 48 | 49 | def load(self, data): 50 | open("/tmp/airphoto.jpg", "w").write(data) 51 | #sc = AVSwitch().getFramebufferScale() 52 | self.picload.setPara((self["image"].instance.size().width(), self["image"].instance.size().height(), 1, 1, False, 1, "#FF000000")) 53 | self.picload.startDecode("/tmp/airphoto.jpg") 54 | 55 | def exit(self): 56 | self.close() 57 | 58 | 59 | class AirPlayAudio(Screen): 60 | skin = """ 61 | 62 | """ 63 | 64 | def __init__(self, session): 65 | Screen.__init__(self, session) 66 | self.serviceref = self.session.nav.getCurrentlyPlayingServiceReference() 67 | self.session.nav.stopService() 68 | 69 | def exit(self): 70 | if self.serviceref is not None: 71 | self.session.nav.playService(self.serviceref) 72 | self.close() 73 | 74 | 75 | class AirPlayVideo(Screen): 76 | skin = """ 77 | 78 | """ 79 | 80 | def __init__(self, session): 81 | Screen.__init__(self, session) 82 | self.videomodes = ["4_3_letterbox", "4_3_panscan", "16_9", "16_9_always", "16_10_letterbox", "16_10_panscan", "16_9_letterbox"] 83 | #self.videodescs = ["4:3 Letterbox", "4:3 Panscan", "16:9", "16:9 Always", "16:10 Letterbox", "16:10 Panscan", "16:9 Letterbox"] 84 | self.serviceref = self.session.nav.getCurrentlyPlayingServiceReference() 85 | self.session.nav.stopService() 86 | self.paused = True 87 | self.position = 0 88 | 89 | self["setupActions"] = ActionMap(["ColorActions"], 90 | { 91 | "green": self.changeVideoMode 92 | }, -2) 93 | 94 | def changeVideoMode(self): 95 | iAVSwitch = AVSwitch() 96 | aspectnum = iAVSwitch.getAspectRatioSetting() 97 | aspectnum += 1 98 | if aspectnum >= len(self.videomodes): 99 | aspectnum = 0 100 | iAVSwitch.setAspectRatio(aspectnum) 101 | config.av.aspectratio.setValue(self.videomodes[aspectnum]) 102 | 103 | def open(self, url): 104 | self.session.nav.playService(eServiceReference(4097, 0, url)) 105 | self.paused = False 106 | 107 | def play(self): 108 | service = self.session.nav.getCurrentService() 109 | if service is None: 110 | return 111 | 112 | pauseable = service.pause() 113 | if pauseable is None: 114 | return 115 | 116 | pauseable.unpause() 117 | self.paused = False 118 | if self.position != 0: 119 | self.setPosition(self.position) 120 | 121 | def pause(self): 122 | service = self.session.nav.getCurrentService() 123 | if service is None: 124 | return 125 | 126 | pauseable = service.pause() 127 | if pauseable is None: 128 | return 129 | 130 | pauseable.pause() 131 | self.paused = True 132 | 133 | def getPosition(self): 134 | service = self.session.nav.getCurrentService() 135 | if service is None: 136 | return None 137 | 138 | seek = service.seek() 139 | if seek is None: 140 | return None 141 | 142 | length = 0.0 143 | position = 0.0 144 | llength = seek.getLength() 145 | lposition = seek.getPlayPosition() 146 | 147 | if not llength[0]: 148 | length = float(llength[1]) / 90000.0 149 | 150 | if self.paused: 151 | position = self.position 152 | elif not lposition[0]: 153 | position = float(lposition[1]) / 90000.0 154 | 155 | if position < 0.0: 156 | position = 0.0 157 | 158 | if length < 0.0: 159 | length = 0.0 160 | 161 | # if near the end or over we override the position 162 | if position > length - 1.0: 163 | position = length 164 | 165 | loaded = 0.0 166 | streamed = service.streamed() 167 | if streamed is not None: 168 | charge = streamed.getBufferCharge() 169 | 170 | if charge[2] != 0: 171 | loaded = float(charge[4]) / float(charge[2]) 172 | 173 | return { 174 | "duration": length, 175 | "position": position, 176 | "loaded": loaded, 177 | "paused": self.paused 178 | } 179 | 180 | def setPosition(self, position): 181 | if self.paused: 182 | self.position = position 183 | return 184 | 185 | service = self.session.nav.getCurrentService() 186 | if service is None: 187 | return 188 | 189 | seek = service.seek() 190 | if seek is None: 191 | return 192 | 193 | if not seek.isCurrentlySeekable(): 194 | print "[SIFTeam OpenAirPlay] service not currently seekable" 195 | return 196 | 197 | seek.seekTo(int(position * 90000)) 198 | self.position = 0 199 | 200 | def exit(self): 201 | if self.serviceref is not None: 202 | self.session.nav.playService(self.serviceref) 203 | self.close() 204 | 205 | 206 | class AirPlay(): 207 | def __init__(self, session): 208 | self.session = session 209 | self.callbacks = APCallbacks() 210 | self.callbacks.photo = self.__photo 211 | self.callbacks.audio = self.__audio 212 | self.callbacks.video = self.__video 213 | self.callbacks.videoPlay = self.__videoPlay 214 | self.callbacks.videoPause = self.__videoPause 215 | self.callbacks.videoGetPosition = self.__videoGetPosition 216 | self.callbacks.videoSetPosition = self.__videoSetPosition 217 | self.callbacks.stop = self.__close 218 | self.callbacks.stopAudio = self.__closeAudio 219 | self.server = APServer(self.callbacks) 220 | self.current = None 221 | self.videorequest = False 222 | self.videourl = "" 223 | 224 | def stop(self): 225 | self.server.stop() 226 | 227 | def start(self): 228 | self.server.start() 229 | 230 | def __photo(self, data): 231 | if self.current is None: 232 | self.current = self.session.open(AirPlayPhoto) 233 | 234 | if self.current.__class__.__name__ != "AirPlayPhoto": 235 | print "[SIFTeam OpenAirPlay] is it busy with other contents?" 236 | return 237 | 238 | self.current.load(data) 239 | 240 | def __audioClosed(self): 241 | if self.videorequest: 242 | self.current = self.session.open(AirPlayVideo) 243 | self.current.open(self.videourl) 244 | self.videorequest = False 245 | 246 | def __audio(self): 247 | if self.current is None: 248 | self.current = self.session.openWithCallback(self.__audioClosed, AirPlayAudio) 249 | 250 | def __video(self, url, startposition): 251 | if self.current is None: 252 | self.current = self.session.open(AirPlayVideo) 253 | 254 | if self.current.__class__.__name__ != "AirPlayVideo": 255 | # if audio we replace it 256 | if self.current.__class__.__name__ == "AirPlayAudio": 257 | self.videourl = url 258 | self.videorequest = True 259 | self.current.exit() 260 | return 261 | else: 262 | print "[SIFTeam OpenAirPlay] is it busy with other contents?" 263 | return 264 | 265 | self.current.open(url) 266 | 267 | def __videoPlay(self): 268 | if self.current is not None and self.current.__class__.__name__ == "AirPlayVideo": 269 | self.current.play() 270 | 271 | def __videoPause(self): 272 | if self.current is not None and self.current.__class__.__name__ == "AirPlayVideo": 273 | self.current.pause() 274 | 275 | def __videoGetPosition(self): 276 | if self.current is not None and self.current.__class__.__name__ == "AirPlayVideo": 277 | return self.current.getPosition() 278 | 279 | return None 280 | 281 | def __videoSetPosition(self, position): 282 | if self.current is not None and self.current.__class__.__name__ == "AirPlayVideo": 283 | self.current.setPosition(position) 284 | 285 | def __close(self): 286 | if self.current is not None: 287 | self.current.exit() 288 | self.current = None 289 | self.videorequest = False 290 | 291 | def __closeAudio(self): 292 | if self.current is not None and self.current.__class__.__name__ == "AirPlayAudio": 293 | self.current.exit() 294 | self.current = None 295 | -------------------------------------------------------------------------------- /plugin/airplayserver.py: -------------------------------------------------------------------------------- 1 | # SIFTeam OpenAirPlay 2 | # Copyright (C) <2012> skaman (SIFTeam) 3 | # 4 | # This library is free software; you can redistribute it and/or 5 | # modify it under the terms of the GNU Library General Public 6 | # License as published by the Free Software Foundation; either 7 | # version 2 of the License, or (at your option) any later version. 8 | # 9 | # This library is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | # Library General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU Library General Public 15 | # License along with this library; if not, write to the 16 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 | # Boston, MA 02111-1307, USA. 18 | # 19 | 20 | from twisted.web import server, resource, http, client 21 | from twisted.internet import reactor 22 | from M2Crypto import RSA, X509 23 | from datetime import datetime 24 | from io import BytesIO 25 | from biplist import readPlist, InvalidPlistException, NotBinaryPlistException 26 | 27 | import os 28 | import sys 29 | import uuid 30 | import avahi 31 | import dbus 32 | import platform 33 | import base64 34 | import subprocess 35 | import StringIO 36 | import urlparse 37 | import socket 38 | 39 | AIRPLAY_PORT = 22555 40 | AIRTUNES_PORT = 22556 41 | HAIRTUNES_BINARY = "/usr/bin/hairtunes" 42 | AIRPLAY_BANNER = "SIFTeam AirPlay on " 43 | AIRPORT_PRIVATE_KEY = \ 44 | "-----BEGIN RSA PRIVATE KEY-----\n" \ 45 | "MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt\n" \ 46 | "wC5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDRKSKv6kDqnw4U\n" \ 47 | "wPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuBOitnZ/bDzPHrTOZz0Dew0uowxf\n" \ 48 | "/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJQ+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/\n" \ 49 | "UAaHqn9JdsBWLUEpVviYnhimNVvYFZeCXg/IdTQ+x4IRdiXNv5hEewIDAQABAoIBAQDl8Axy9XfW\n" \ 50 | "BLmkzkEiqoSwF0PsmVrPzH9KsnwLGH+QZlvjWd8SWYGN7u1507HvhF5N3drJoVU3O14nDY4TFQAa\n" \ 51 | "LlJ9VM35AApXaLyY1ERrN7u9ALKd2LUwYhM7Km539O4yUFYikE2nIPscEsA5ltpxOgUGCY7b7ez5\n" \ 52 | "NtD6nL1ZKauw7aNXmVAvmJTcuPxWmoktF3gDJKK2wxZuNGcJE0uFQEG4Z3BrWP7yoNuSK3dii2jm\n" \ 53 | "lpPHr0O/KnPQtzI3eguhe0TwUem/eYSdyzMyVx/YpwkzwtYL3sR5k0o9rKQLtvLzfAqdBxBurciz\n" \ 54 | "aaA/L0HIgAmOit1GJA2saMxTVPNhAoGBAPfgv1oeZxgxmotiCcMXFEQEWflzhWYTsXrhUIuz5jFu\n" \ 55 | "a39GLS99ZEErhLdrwj8rDDViRVJ5skOp9zFvlYAHs0xh92ji1E7V/ysnKBfsMrPkk5KSKPrnjndM\n" \ 56 | "oPdevWnVkgJ5jxFuNgxkOLMuG9i53B4yMvDTCRiIPMQ++N2iLDaRAoGBAO9v//mU8eVkQaoANf0Z\n" \ 57 | "oMjW8CN4xwWA2cSEIHkd9AfFkftuv8oyLDCG3ZAf0vrhrrtkrfa7ef+AUb69DNggq4mHQAYBp7L+\n" \ 58 | "k5DKzJrKuO0r+R0YbY9pZD1+/g9dVt91d6LQNepUE/yY2PP5CNoFmjedpLHMOPFdVgqDzDFxU8hL\n" \ 59 | "AoGBANDrr7xAJbqBjHVwIzQ4To9pb4BNeqDndk5Qe7fT3+/H1njGaC0/rXE0Qb7q5ySgnsCb3DvA\n" \ 60 | "cJyRM9SJ7OKlGt0FMSdJD5KG0XPIpAVNwgpXXH5MDJg09KHeh0kXo+QA6viFBi21y340NonnEfdf\n" \ 61 | "54PX4ZGS/Xac1UK+pLkBB+zRAoGAf0AY3H3qKS2lMEI4bzEFoHeK3G895pDaK3TFBVmD7fV0Zhov\n" \ 62 | "17fegFPMwOII8MisYm9ZfT2Z0s5Ro3s5rkt+nvLAdfC/PYPKzTLalpGSwomSNYJcB9HNMlmhkGzc\n" \ 63 | "1JnLYT4iyUyx6pcZBmCd8bD0iwY/FzcgNDaUmbX9+XDvRA0CgYEAkE7pIPlE71qvfJQgoA9em0gI\n" \ 64 | "LAuE4Pu13aKiJnfft7hIjbK+5kyb3TysZvoyDnb3HOKvInK7vXbKuU4ISgxB2bB3HcYzQMGsz1qJ\n" \ 65 | "2gG0N5hvJpzwwhbhXqFKA4zaaSrw622wDniAK5MlIE0tIAKKP4yxNGjoD2QYjhBGuhvkWKY=\n" \ 66 | "-----END RSA PRIVATE KEY-----" 67 | 68 | SERVER_INFO_TEMPLATE = '\ 69 | \ 70 | \ 71 | \ 72 | deviceid\ 73 | %s\ 74 | features\ 75 | %d\ 76 | model\ 77 | %s\ 78 | protovers\ 79 | 1.0\ 80 | srcvers\ 81 | 101.10\ 82 | \ 83 | ' 84 | 85 | PLAYBACK_INFO_TEMPLATE = '\ 86 | \ 87 | \ 88 | \ 89 | duration\ 90 | %f\ 91 | loadedTimeRanges\ 92 | \ 93 | \ 94 | duration\ 95 | %f\ 96 | start\ 97 | %f\ 98 | \ 99 | \ 100 | playbackBufferEmpty\ 101 | \ 102 | playbackBufferFull\ 103 | \ 104 | playbackLikelyToKeepUp\ 105 | \ 106 | position\ 107 | %f\ 108 | rate\ 109 | %d\ 110 | readyToPlay\ 111 | \ 112 | seekableTimeRanges\ 113 | \ 114 | \ 115 | duration\ 116 | %f\ 117 | start\ 118 | 0.0\ 119 | \ 120 | \ 121 | \ 122 | ' 123 | 124 | PLAYBACK_INFO_NOT_READY_TEMPLATE = '\ 125 | \ 126 | \ 127 | \ 128 | readyToPlay\ 129 | \ 130 | \ 131 | ' 132 | 133 | 134 | class APZeroConf(): 135 | def __init__(self, info): 136 | self.info = info 137 | self.group = None 138 | 139 | def publish(self): 140 | text_ap = ["deviceid=" + self.info.deviceid, "features=" + hex(self.info.features), "model=" + self.info.model] 141 | text_at = ["tp=UDP", "sm=false", "sv=false", "ek=1", "et=0,1", "cn=0,1", "ch=2", "ss=16", "sr=44100", "pw=false", "vn=3", "txtvers=1"] 142 | bus = dbus.SystemBus() 143 | server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) 144 | 145 | self.group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP) 146 | self.group.AddService( 147 | avahi.IF_UNSPEC, 148 | avahi.PROTO_UNSPEC, 149 | dbus.UInt32(0), 150 | AIRPLAY_BANNER + platform.node(), 151 | "_airplay._tcp", 152 | "", 153 | "", 154 | dbus.UInt16(AIRPLAY_PORT), 155 | avahi.string_array_to_txt_array(text_ap) 156 | ) 157 | self.group.AddService( 158 | avahi.IF_UNSPEC, 159 | avahi.PROTO_UNSPEC, 160 | dbus.UInt32(0), 161 | self.info.deviceid + "@" + AIRPLAY_BANNER + platform.node(), 162 | "_raop._tcp", 163 | "", 164 | "", 165 | dbus.UInt16(AIRTUNES_PORT), 166 | avahi.string_array_to_txt_array(text_at) 167 | ) 168 | self.group.Commit() 169 | 170 | def unpublish(self): 171 | if self.group is not None: 172 | self.group.Reset() 173 | 174 | 175 | class RTSPRequest(http.Request): 176 | def process(self): 177 | self.channel.site.resource.render(self) 178 | 179 | 180 | class RTSPChannel(http.HTTPChannel): 181 | requestFactory = RTSPRequest 182 | 183 | def checkPersistence(self, request, version): 184 | if version == "RTSP/1.0": 185 | return 1 186 | return 0 187 | 188 | 189 | class RTSPSite(server.Site): 190 | protocol = RTSPChannel 191 | requestFactory = RTSPRequest 192 | 193 | 194 | class APInfo(): 195 | def __init__(self): 196 | self.deviceid = "%012X" % uuid.getnode() 197 | self.features = 0x77 198 | self.model = "AppleTV2,1" 199 | 200 | 201 | class APRtspRoot(resource.Resource): 202 | isLeaf = True 203 | 204 | def __init__(self, callbacks, info): 205 | resource.Resource.__init__(self) 206 | self.callbacks = callbacks 207 | self.info = info 208 | self.aesiv = None 209 | self.rsaaeskey = None 210 | self.fmtp = None 211 | self.process = None 212 | 213 | def dump(self, data): 214 | dmp = "" 215 | for ch in data: 216 | dmp += "0x%x " % ord(ch) 217 | 218 | dmp = dmp.strip() 219 | print dmp 220 | 221 | def prepareBaseReply(self, request): 222 | request.setETag("RTSP/1.0") 223 | request.setResponseCode(200) 224 | request.setHeader("cseq", request.received_headers["cseq"]) 225 | request.setHeader("audio-jack-status", "connected; type=analog") 226 | 227 | if "apple-challenge" in request.received_headers: 228 | challenge = request.received_headers["apple-challenge"] 229 | if challenge[-2:] != "==": 230 | challenge += "==" 231 | 232 | data = base64.b64decode(challenge) 233 | 234 | host = request.getHost().host 235 | 236 | if (host.split(".")) == 4: # ipv4 237 | data += socket.inet_pton(socket.AF_INET, host) 238 | elif host[:7] == "::ffff:": 239 | data += socket.inet_pton(socket.AF_INET, host[7:]) 240 | else: 241 | data += socket.inet_pton(socket.AF_INET6, host.split("%")[0]) 242 | 243 | hwaddr = self.info.deviceid 244 | for i in range(0, 12, 2): 245 | data += chr(int(hwaddr[i:i + 2], 16)) 246 | 247 | data = data.ljust(32, '\0') 248 | #self.dump(data) 249 | 250 | key = RSA.load_key_string(AIRPORT_PRIVATE_KEY) 251 | signature = base64.b64encode(key.private_encrypt(data, RSA.pkcs1_padding)) 252 | if signature[-2:] == "==": 253 | signature = signature[:-2] 254 | request.setHeader("apple-response", signature) 255 | 256 | def render_OPTIONS(self, request): 257 | print "[SIFTeam OpenAirPlay] " + str(request) 258 | 259 | self.prepareBaseReply(request) 260 | request.setHeader("public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER") 261 | request.write("") 262 | request.finish() 263 | 264 | def render_ANNOUNCE(self, request): 265 | print "[SIFTeam OpenAirPlay] " + str(request) 266 | 267 | self.prepareBaseReply(request) 268 | 269 | content = request.content.read() 270 | for row in content.split("\n"): 271 | row = row.strip() 272 | if row[:2] != "a=": 273 | continue 274 | 275 | row = row[2:] 276 | seppos = row.find(":") 277 | key = row[:seppos].strip() 278 | value = row[seppos + 1:].strip() 279 | 280 | if key == "aesiv" or key == "rsaaeskey": 281 | if value[-2:] != "==": 282 | value += "==" 283 | 284 | if key == "aesiv": 285 | self.aesiv = base64.b64decode(value) 286 | elif key == "rsaaeskey": 287 | self.rsaaeskey = base64.b64decode(value) 288 | key = RSA.load_key_string(AIRPORT_PRIVATE_KEY) 289 | self.rsaaeskey = key.private_decrypt(self.rsaaeskey, RSA.pkcs1_oaep_padding) 290 | elif key == "fmtp": 291 | self.fmtp = value 292 | 293 | request.write("") 294 | request.finish() 295 | 296 | def render_SETUP(self, request): 297 | print "[SIFTeam OpenAirPlay] " + str(request) 298 | 299 | self.prepareBaseReply(request) 300 | 301 | if self.aesiv is not None and self.rsaaeskey is not None and self.fmtp is not None and "transport" in request.received_headers: 302 | data_port = 0 303 | timing_port = 59010 304 | control_port = 59012 305 | for row in request.received_headers["transport"].split(";"): 306 | row = row.strip() 307 | seppos = row.find("=") 308 | if seppos == -1: 309 | continue 310 | 311 | key = row[:seppos].strip() 312 | value = row[seppos + 1:].strip() 313 | 314 | if key == "timing_port": 315 | timing_port = int(value) 316 | elif key == "control_port": 317 | control_port = int(value) 318 | 319 | aesiv = "" 320 | for ch in self.aesiv: 321 | aesiv += "%02X" % ord(ch) 322 | 323 | rsaaeskey = "" 324 | for ch in self.rsaaeskey: 325 | rsaaeskey += "%02X" % ord(ch) 326 | 327 | args = [ 328 | HAIRTUNES_BINARY, 329 | "iv", aesiv, 330 | "key", rsaaeskey, 331 | "fmtp", self.fmtp, 332 | "cport", str(control_port), 333 | "tport", str(timing_port), 334 | "dport", "0" 335 | ] 336 | self.process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) 337 | while self.process.poll() == None: 338 | buff = self.process.stdout.readline() 339 | if len(buff) > 0: 340 | if buff[:6] == "port: ": 341 | data_port = int(buff[6:]) 342 | break 343 | 344 | if data_port != 0: 345 | self.callbacks.audio() 346 | request.setHeader("transport", request.received_headers["transport"] + ";server_port=" + str(data_port)) 347 | request.setHeader("session", "DEADBEEF") 348 | request.write("") 349 | request.finish() 350 | 351 | def render_RECORD(self, request): 352 | print "[SIFTeam OpenAirPlay] " + str(request) 353 | self.prepareBaseReply(request) 354 | request.write("") 355 | request.finish() 356 | 357 | def render_FLUSH(self, request): 358 | print "[SIFTeam OpenAirPlay] " + str(request) 359 | 360 | if self.process is not None and self.process.poll() is None: 361 | self.process.stdin.write("flush\n") 362 | 363 | self.prepareBaseReply(request) 364 | request.write("") 365 | request.finish() 366 | 367 | def render_TEARDOWN(self, request): 368 | print "[SIFTeam OpenAirPlay] " + str(request) 369 | 370 | if self.process != None and self.process.poll() == None: 371 | self.process.stdin.write("exit\n") 372 | self.process.wait() 373 | self.process = None 374 | 375 | self.callbacks.stopAudio() 376 | 377 | self.prepareBaseReply(request) 378 | request.setHeader("connection", "close") 379 | request.write("") 380 | request.finish() 381 | 382 | def render_SET_PARAMETER(self, request): 383 | print "[SIFTeam OpenAirPlay] " + str(request) 384 | buff = request.content.read().split("\n") 385 | for row in buff: 386 | if row[:7] == "volume:": 387 | volume = row[7:].strip() 388 | if self.process is not None and self.process.poll() is None: 389 | self.process.stdin.write("vol: " + volume + "\n") 390 | self.prepareBaseReply(request) 391 | request.write("") 392 | request.finish() 393 | 394 | def render_GET_PARAMETER(self, request): 395 | print "[SIFTeam OpenAirPlay] " + str(request) 396 | self.prepareBaseReply(request) 397 | request.write("") 398 | request.finish() 399 | 400 | def render_DENIED(self, request): 401 | print "[SIFTeam OpenAirPlay] " + str(request) 402 | self.prepareBaseReply(request) 403 | request.write("") 404 | request.finish() 405 | 406 | 407 | class APWebRoot(resource.Resource): 408 | isLeaf = False 409 | 410 | def __init__(self, callbacks, info): 411 | resource.Resource.__init__(self) 412 | self.callbacks = callbacks 413 | self.info = info 414 | 415 | def getChild(self, path, request): 416 | print "[SIFTeam OpenAirPlay] " + str(request) 417 | if path == "server-info": 418 | return APWebServerInfo(self.callbacks, self.info) 419 | elif path == "reverse": 420 | return APWebReverse(self.callbacks, self.info) 421 | elif path == "stop": 422 | return APWebStop(self.callbacks, self.info) 423 | elif path == "photo": 424 | return APWebPhoto(self.callbacks, self.info) 425 | elif path == "slideshow-features": 426 | return APWebSlideShowFeatures(self.callbacks, self.info) 427 | elif path == "play": 428 | return APWebPlay(self.callbacks, self.info) 429 | elif path == "rate": 430 | return APWebRate(self.callbacks, self.info) 431 | elif path == "scrub": 432 | return APWebScrub(self.callbacks, self.info) 433 | elif path == "playback-info": 434 | return APWebPlaybackInfo(self.callbacks, self.info) 435 | elif path == "setProperty": 436 | return APWebSetProperty(self.callbacks, self.info) 437 | elif path == "getProperty": 438 | return APWebGetProperty(self.callbacks, self.info) 439 | 440 | print "[SIFTeam OpenAirPlay] the api '%s' is not yet implemented" % path 441 | return APWebNotFound(self.callbacks, self.info) 442 | 443 | 444 | class APWebBase(resource.Resource): 445 | isLeaf = True 446 | 447 | def __init__(self, callbacks, info): 448 | resource.Resource.__init__(self) 449 | self.callbacks = callbacks 450 | self.info = info 451 | 452 | def getDateTime(self): 453 | return datetime.now().strftime("%a, %d %b %Y %H:%M:%S") + " GMT" 454 | 455 | def commonRender(self, request, body="", retcode=200): 456 | request.setResponseCode(retcode) 457 | if retcode == 101: 458 | request.setHeader("upgrade", "PTTH/1.0") 459 | request.setHeader("connection", "Upgrade") 460 | 461 | request.setHeader("content-length", len(body)) 462 | request.setHeader("date", self.getDateTime()) 463 | request.write(body) 464 | request.finish() 465 | 466 | 467 | class APWebNotFound(APWebBase): 468 | def render(self, request): 469 | self.commonRender(request, "", 404) 470 | return server.NOT_DONE_YET 471 | 472 | 473 | class APWebReverse(APWebBase): 474 | def render(self, request): 475 | self.commonRender(request, "", 101) 476 | return server.NOT_DONE_YET 477 | 478 | 479 | class APWebSlideShowFeatures(APWebBase): 480 | def render(self, request): 481 | # ?? UNKNOW! 482 | self.commonRender(request) 483 | return server.NOT_DONE_YET 484 | 485 | 486 | class APWebServerInfo(APWebBase): 487 | def render(self, request): 488 | request.setHeader("content-type", "text/x-apple-plist+xml") 489 | self.commonRender(request, SERVER_INFO_TEMPLATE % (self.info.deviceid, self.info.features, self.info.model)) 490 | return server.NOT_DONE_YET 491 | 492 | 493 | class APWebStop(APWebBase): 494 | def render(self, request): 495 | self.callbacks.stop() 496 | self.commonRender(request) 497 | return server.NOT_DONE_YET 498 | 499 | 500 | class APWebPhoto(APWebBase): 501 | def render(self, request): 502 | request.setResponseCode(200) 503 | buff = request.content.read() 504 | self.callbacks.photo(buff) 505 | self.commonRender(request) 506 | return server.NOT_DONE_YET 507 | 508 | 509 | class APWebPlay(APWebBase): 510 | def render(self, request): 511 | request.setResponseCode(200) 512 | content = request.content.read() 513 | 514 | url = "" 515 | tmp = StringIO.StringIO() 516 | tmp.write(content) 517 | # TODO: check for content type 'application/x-apple-binary-plist' 518 | try: 519 | plist = readPlist(tmp) 520 | url = plist["Content-Location"] 521 | startposition = float(plist["Start-Position"]) 522 | except (InvalidPlistException, NotBinaryPlistException), e: 523 | startposition = 0.0 524 | for row in content.split("\n"): 525 | row = row.strip() 526 | seppos = row.find(":") 527 | key = row[:seppos].strip() 528 | value = row[seppos + 1:].strip() 529 | if key == "Content-Location": 530 | url = value 531 | elif key == "Start-Position": 532 | startposition = float(value) 533 | 534 | if url != "": 535 | self.callbacks.video(url, startposition) 536 | 537 | self.commonRender(request) 538 | return server.NOT_DONE_YET 539 | 540 | 541 | class APWebRate(APWebBase): 542 | def render(self, request): 543 | if 'value' in request.args: 544 | if float(request.args['value'][0]): 545 | self.callbacks.videoPlay() 546 | else: 547 | self.callbacks.videoPause() 548 | 549 | self.commonRender(request) 550 | return server.NOT_DONE_YET 551 | 552 | 553 | class APWebScrub(APWebBase): 554 | def render_GET(self, request): 555 | position = self.callbacks.videoGetPosition() 556 | if position is not None: 557 | body = "duration: %f\r\nposition: %f\r\n" % (position["duration"], position["position"]) 558 | else: 559 | body = "duration: 0.0\r\nposition: 0.0\r\n" 560 | self.commonRender(request, body) 561 | return server.NOT_DONE_YET 562 | 563 | def render_POST(self, request): 564 | if "position" in request.args: 565 | self.callbacks.videoSetPosition(float(request.args["position"][0])) 566 | self.commonRender(request) 567 | return server.NOT_DONE_YET 568 | 569 | 570 | class APWebPlaybackInfo(APWebBase): 571 | def render(self, request): 572 | info = self.callbacks.videoGetPosition() 573 | if info["duration"] == 0 or info["position"] == info["duration"]: 574 | body = PLAYBACK_INFO_NOT_READY_TEMPLATE 575 | else: 576 | body = PLAYBACK_INFO_TEMPLATE % (info["duration"], info["loaded"], info["position"], info["position"], int(not info["paused"]), info["duration"]) 577 | 578 | request.setHeader("content-type", "text/x-apple-plist+xml") 579 | self.commonRender(request, body) 580 | return server.NOT_DONE_YET 581 | 582 | 583 | class APWebSetProperty(APWebBase): 584 | def render(self, request): 585 | self.commonRender(request) 586 | return server.NOT_DONE_YET 587 | 588 | 589 | class APWebGetProperty(APWebBase): 590 | def render(self, request): 591 | self.commonRender(request) 592 | return server.NOT_DONE_YET 593 | 594 | 595 | class APCallbacks(): 596 | def __init__(self): 597 | self.photo = None 598 | self.audio = None 599 | self.video = None 600 | self.videoPlay = None 601 | self.videoPause = None 602 | self.videoGetPosition = None 603 | self.videoSetPosition = None 604 | self.stop = None 605 | self.stopAudio = None 606 | 607 | 608 | class APServer(): 609 | def __init__(self, apcb): 610 | self.atconn = None 611 | self.apconn = None 612 | 613 | self.apcb = apcb 614 | self.apinfo = APInfo() 615 | self.zeroconf = APZeroConf(self.apinfo) 616 | 617 | self.atroot = APRtspRoot(self.apcb, self.apinfo) 618 | self.aproot = APWebRoot(self.apcb, self.apinfo) 619 | self.atsite = RTSPSite(self.atroot) 620 | self.apsite = server.Site(self.aproot) 621 | 622 | def start(self): 623 | self.zeroconf.publish() 624 | try: 625 | self.atconn = reactor.listenTCP(AIRTUNES_PORT, self.atsite, interface="::") 626 | except Exception: 627 | print "[SIFTeam OpenAirPlay] cannot bind airtunes server on ipv6 interface" 628 | self.atconn = None 629 | 630 | if self.atconn is None: 631 | try: 632 | self.atconn = reactor.listenTCP(AIRTUNES_PORT, self.atsite) 633 | except Exception: 634 | self.atconn = None 635 | self.apconn = None 636 | print "[SIFTeam OpenAirPlay] cannot start airtunes server" 637 | return 638 | 639 | try: 640 | self.apconn = reactor.listenTCP(AIRPLAY_PORT, self.apsite, interface="::") 641 | except Exception: 642 | print "[SIFTeam OpenAirPlay] cannot bind airplay server on ipv6 interface" 643 | self.apconn = None 644 | 645 | if self.apconn is None: 646 | try: 647 | self.apconn = reactor.listenTCP(AIRPLAY_PORT, self.apsite) 648 | except Exception: 649 | self.atconn.stopListening() 650 | self.atconn = None 651 | self.apconn = None 652 | print "[SIFTeam OpenAirPlay] cannot start airplay server" 653 | return 654 | 655 | print "[SIFTeam OpenAirPlay] server started" 656 | 657 | def stop(self): 658 | self.zeroconf.unpublish() 659 | if self.atconn is not None: 660 | self.atconn.stopListening() 661 | if self.apconn is not None: 662 | self.apconn.stopListening() 663 | print "[SIFTeam OpenAirPlay] server stopped" 664 | -------------------------------------------------------------------------------- /plugin/openairplay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E2OpenPlugins/e2openplugin-OpenAirPlay/0fe2edfa6f859b84752356d148ce7e7a5b2c45b9/plugin/openairplay.png -------------------------------------------------------------------------------- /plugin/plugin.py: -------------------------------------------------------------------------------- 1 | # SIFTeam OpenAirPlay 2 | # Copyright (C) <2012> skaman (SIFTeam) 3 | # 4 | # This library is free software; you can redistribute it and/or 5 | # modify it under the terms of the GNU Library General Public 6 | # License as published by the Free Software Foundation; either 7 | # version 2 of the License, or (at your option) any later version. 8 | # 9 | # This library is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | # Library General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU Library General Public 15 | # License along with this library; if not, write to the 16 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 | # Boston, MA 02111-1307, USA. 18 | # 19 | 20 | from Screens.Screen import Screen 21 | from Plugins.Plugin import PluginDescriptor 22 | from Components.Button import Button 23 | from Components.ActionMap import ActionMap 24 | from Components.ConfigList import ConfigListScreen 25 | from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigYesNo 26 | 27 | from airplay import AirPlay 28 | 29 | config.OpenAirPlay = ConfigSubsection() 30 | config.OpenAirPlay.enabled = ConfigYesNo(default=True) 31 | 32 | global_session = None 33 | global_airplay = None 34 | 35 | 36 | class OpenAirPlayConfig(Screen, ConfigListScreen): 37 | skin = """ 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | """ 51 | 52 | def __init__(self, session): 53 | Screen.__init__(self, session) 54 | ConfigListScreen.__init__(self, []) 55 | 56 | self["key_red"] = Button(_("Cancel")) 57 | self["key_green"] = Button(_("Save")) 58 | self["key_yellow"] = Button("") 59 | self["key_blue"] = Button("") 60 | 61 | self["actions"] = ActionMap(["WizardActions", "ColorActions"], 62 | { 63 | "red": self.keyCancel, 64 | "back": self.keyCancel, 65 | "green": self.ok 66 | }, -2) 67 | 68 | self.list = [] 69 | self.list.append(getConfigListEntry(_("OpenAirPlay Enabled"), config.OpenAirPlay.enabled)) 70 | self["config"].setList(self.list) 71 | 72 | def ok(self): 73 | self.keySave() 74 | 75 | if config.OpenAirPlay.enabled.value: 76 | startServer() 77 | else: 78 | stopServer() 79 | 80 | self.close() 81 | 82 | 83 | def startConfiguration(session, **kwargs): 84 | session.open(OpenAirPlayConfig) 85 | 86 | 87 | def startServer(): 88 | global global_session 89 | global global_airplay 90 | 91 | if global_session is None: 92 | return 93 | 94 | if global_airplay is None: 95 | global_airplay = AirPlay(global_session) 96 | global_airplay.start() 97 | 98 | 99 | def stopServer(): 100 | global global_airplay 101 | 102 | if global_airplay is not None: 103 | global_airplay.stop() 104 | global_airplay = None 105 | 106 | 107 | def autoStart(reason, **kwargs): 108 | # we use autostart only for stop server... the start is handled on networkConfigRead 109 | if reason == 1: 110 | stopServer() 111 | 112 | 113 | def networkConfigRead(reason, **kwargs): 114 | if reason is True: 115 | if config.OpenAirPlay.enabled.value: 116 | startServer() 117 | else: 118 | stopServer() 119 | 120 | 121 | def sessionStart(reason, session): 122 | global global_session 123 | global_session = session 124 | 125 | 126 | def Plugins(**kwargs): 127 | return [PluginDescriptor(where=[PluginDescriptor.WHERE_SESSIONSTART], fnc=sessionStart), 128 | PluginDescriptor(where=[PluginDescriptor.WHERE_AUTOSTART], fnc=autoStart), 129 | PluginDescriptor(where=[PluginDescriptor.WHERE_NETWORKCONFIG_READ], fnc=networkConfigRead), 130 | PluginDescriptor(name="OpenAirPlay", description="SIFTeam OpenAirPlay Configuration", icon="openairplay.png", where=[PluginDescriptor.WHERE_PLUGINMENU], fnc=startConfiguration)] 131 | --------------------------------------------------------------------------------