├── Kamera.pro ├── Kamera.pro.user ├── LICENSE ├── app_window.cpp ├── app_window.h ├── dummy_sink.cpp ├── dummy_sink.h ├── main.cpp ├── rtsp_callback.cpp ├── rtsp_callback.h ├── rtsp_client.cpp ├── rtsp_client.h ├── rtsp_session.cpp ├── rtsp_session.h ├── stream_state.cpp └── stream_state.h /Kamera.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = Kamera 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp \ 13 | dummy_sink.cpp \ 14 | stream_state.cpp \ 15 | app_window.cpp \ 16 | rtsp_client.cpp \ 17 | rtsp_callback.cpp \ 18 | rtsp_session.cpp 19 | 20 | # The following define makes your compiler emit warnings if you use 21 | # any feature of Qt which as been marked deprecated (the exact warnings 22 | # depend on your compiler). Please consult the documentation of the 23 | # deprecated API in order to know how to port your code away from it. 24 | DEFINES += QT_DEPRECATED_WARNINGS 25 | 26 | # You can also make your code fail to compile if you use deprecated APIs. 27 | # In order to do so, uncomment the following line. 28 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 29 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 30 | INCLUDEPATH += /usr/local/include 31 | INCLUDEPATH += /usr/local/include/liveMedia 32 | INCLUDEPATH += /usr/local/include/groupsock 33 | INCLUDEPATH += /usr/local/include/UsageEnvironment 34 | INCLUDEPATH += /usr/local/include/BasicUsageEnvironment 35 | LIBS += -L/usr/local/lib 36 | LIBS += -lliveMedia -lgroupsock -lBasicUsageEnvironment -lUsageEnvironment 37 | LIBS += -lavcodec -lavformat -lavutil -lavfilter -lavdevice -lswscale -lswresample 38 | LIBS += -liconv -lx264 -lbz2 -lz -lm 39 | LIBS += -framework AudioToolbox -framework CoreMedia -framework AVFoundation -framework VideoToolbox -framework CoreVideo -framework CoreFoundation 40 | LIBS += -framework Security -framework VideoDecodeAcceleration 41 | 42 | HEADERS += \ 43 | stream_state.h \ 44 | dummy_sink.h \ 45 | app_window.h \ 46 | rtsp_client.h \ 47 | rtsp_callback.h \ 48 | rtsp_session.h 49 | -------------------------------------------------------------------------------- /Kamera.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {e6b1fcc6-2286-469a-b945-f43e1a4ea946} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | 60 | ProjectExplorer.Project.Target.0 61 | 62 | Qt Desktop 63 | Qt Desktop 64 | {ab94d12a-8860-4416-b585-4be4799c2340} 65 | 0 66 | 0 67 | 0 68 | 69 | /Workspace/git/build-Kamera-Qt_Desktop-Debug 70 | 71 | 72 | true 73 | qmake 74 | 75 | QtProjectManager.QMakeBuildStep 76 | true 77 | 78 | false 79 | false 80 | false 81 | 82 | 83 | true 84 | Make 85 | 86 | Qt4ProjectManager.MakeStep 87 | 88 | -w 89 | -r 90 | 91 | false 92 | 93 | 94 | 95 | 2 96 | Build 97 | 98 | ProjectExplorer.BuildSteps.Build 99 | 100 | 101 | 102 | true 103 | Make 104 | 105 | Qt4ProjectManager.MakeStep 106 | 107 | -w 108 | -r 109 | 110 | true 111 | clean 112 | 113 | 114 | 1 115 | Clean 116 | 117 | ProjectExplorer.BuildSteps.Clean 118 | 119 | 2 120 | false 121 | 122 | Debug 123 | 124 | Qt4ProjectManager.Qt4BuildConfiguration 125 | 2 126 | true 127 | 128 | 129 | /Workspace/git/build-Kamera-Qt_Desktop-Release 130 | 131 | 132 | true 133 | qmake 134 | 135 | QtProjectManager.QMakeBuildStep 136 | false 137 | 138 | false 139 | false 140 | false 141 | 142 | 143 | true 144 | Make 145 | 146 | Qt4ProjectManager.MakeStep 147 | 148 | -w 149 | -r 150 | 151 | false 152 | 153 | 154 | 155 | 2 156 | Build 157 | 158 | ProjectExplorer.BuildSteps.Build 159 | 160 | 161 | 162 | true 163 | Make 164 | 165 | Qt4ProjectManager.MakeStep 166 | 167 | -w 168 | -r 169 | 170 | true 171 | clean 172 | 173 | 174 | 1 175 | Clean 176 | 177 | ProjectExplorer.BuildSteps.Clean 178 | 179 | 2 180 | false 181 | 182 | Release 183 | 184 | Qt4ProjectManager.Qt4BuildConfiguration 185 | 0 186 | true 187 | 188 | 189 | /Workspace/git/build-Kamera-Qt_Desktop-Profile 190 | 191 | 192 | true 193 | qmake 194 | 195 | QtProjectManager.QMakeBuildStep 196 | true 197 | 198 | false 199 | true 200 | false 201 | 202 | 203 | true 204 | Make 205 | 206 | Qt4ProjectManager.MakeStep 207 | 208 | -w 209 | -r 210 | 211 | false 212 | 213 | 214 | 215 | 2 216 | Build 217 | 218 | ProjectExplorer.BuildSteps.Build 219 | 220 | 221 | 222 | true 223 | Make 224 | 225 | Qt4ProjectManager.MakeStep 226 | 227 | -w 228 | -r 229 | 230 | true 231 | clean 232 | 233 | 234 | 1 235 | Clean 236 | 237 | ProjectExplorer.BuildSteps.Clean 238 | 239 | 2 240 | false 241 | 242 | Profile 243 | 244 | Qt4ProjectManager.Qt4BuildConfiguration 245 | 0 246 | true 247 | 248 | 3 249 | 250 | 251 | 0 252 | Deploy 253 | 254 | ProjectExplorer.BuildSteps.Deploy 255 | 256 | 1 257 | Deploy locally 258 | 259 | ProjectExplorer.DefaultDeployConfiguration 260 | 261 | 1 262 | 263 | 264 | false 265 | false 266 | 1000 267 | 268 | true 269 | 270 | false 271 | false 272 | false 273 | false 274 | true 275 | 0.01 276 | 10 277 | true 278 | 1 279 | 25 280 | 281 | 1 282 | true 283 | false 284 | true 285 | valgrind 286 | 287 | 0 288 | 1 289 | 2 290 | 3 291 | 4 292 | 5 293 | 6 294 | 7 295 | 8 296 | 9 297 | 10 298 | 11 299 | 12 300 | 13 301 | 14 302 | 303 | 2 304 | 305 | DYLD_LIBRARY_PATH=/System/Library/Frameworks/ImageIO.framework/Resources:/Users/hfli/workspace/Qt5.6.2/5.6/clang_64/lib:/usr/local/lib 306 | 307 | Kamera 308 | 309 | Qt4ProjectManager.Qt4RunConfiguration:/Workspace/git/Kamera/Kamera.pro 310 | true 311 | 312 | Kamera.pro 313 | false 314 | false 315 | 316 | /Workspace/git/build-Kamera-Qt_Desktop-Debug 317 | 3768 318 | false 319 | true 320 | false 321 | false 322 | true 323 | 324 | 1 325 | 326 | 327 | 328 | ProjectExplorer.Project.TargetCount 329 | 1 330 | 331 | 332 | ProjectExplorer.Project.Updater.FileVersion 333 | 18 334 | 335 | 336 | Version 337 | 18 338 | 339 | 340 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 Lesser 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 | {description} 294 | Copyright (C) {year} {fullname} 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 along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /app_window.cpp: -------------------------------------------------------------------------------- 1 | #include "app_window.h" 2 | #include 3 | 4 | //const char * url = "rtsp://192.168.3.99:37441/h264"; 5 | const char * url = "rtsp://10.209.21.189:8554/txdx.mkv"; 6 | 7 | AppWindow::AppWindow(const char* progName, QWidget* parent) 8 | : QMainWindow(parent), 9 | _progName(progName){ 10 | this->setupUi(); 11 | this->_rtspSession = new RtspSession(1024, this->_progName.c_str(), url, this); 12 | this->bindEvents(); 13 | } 14 | 15 | void AppWindow::setupUi(){ 16 | QGridLayout *layout = new QGridLayout; 17 | 18 | this->_cameraScreen = new QLabel("Video 0", this); 19 | //this->_cameraScreen->setg 20 | this->_cameraScreen->setStyleSheet("background:'black';color:'white'"); 21 | 22 | this->_startCaptureButton = new QPushButton(this); 23 | this->_startCaptureButton->setText("Start"); 24 | 25 | this->_stopCaptureButton = new QPushButton(this); 26 | this->_stopCaptureButton->setText("Stop"); 27 | 28 | layout->addWidget(this->_startCaptureButton, 0, 0); 29 | layout->addWidget(this->_stopCaptureButton, 1, 0); 30 | layout->addWidget(this->_cameraScreen, 2, 0); 31 | 32 | QWidget * central = new QWidget(this); 33 | 34 | setCentralWidget(central); 35 | centralWidget()->setLayout(layout); 36 | 37 | setWindowTitle("Camera Window"); 38 | setFixedSize(800, 600); 39 | } 40 | 41 | void AppWindow::bindEvents(){ 42 | connect(this->_startCaptureButton, SIGNAL(clicked()), this, SLOT(onStart())); 43 | connect(this->_stopCaptureButton, SIGNAL(clicked()), this, SLOT(onStop())); 44 | connect(this->_rtspSession, SIGNAL(gotFrame(QImage)), this, SLOT(updateFrame(QImage))); 45 | } 46 | 47 | void AppWindow::onStart(){ 48 | std::cout << "starting..." << std::endl; 49 | this->_rtspSession->start(); 50 | } 51 | 52 | void AppWindow::onStop(){ 53 | std::cout << "stoping..." << std::endl; 54 | this->_rtspSession->stop(); 55 | } 56 | 57 | void AppWindow::updateFrame(const QImage &image) 58 | { 59 | 60 | QPixmap pixmap = QPixmap::fromImage(image.scaled(this->_cameraScreen->size(), Qt::KeepAspectRatio) ); 61 | this->_cameraScreen->setPixmap(pixmap); 62 | } 63 | -------------------------------------------------------------------------------- /app_window.h: -------------------------------------------------------------------------------- 1 | #ifndef APP_WINDOW_H 2 | #define APP_WINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | #include "rtsp_session.h" 14 | 15 | class AppWindow : public QMainWindow 16 | { 17 | Q_OBJECT 18 | public: 19 | explicit AppWindow(const char* progName, QWidget* parent = 0); 20 | virtual ~AppWindow(){}; 21 | private slots: 22 | void onStart(); 23 | void onStop(); 24 | void updateFrame(const QImage& image); 25 | private: 26 | void setupUi(); 27 | void bindEvents(); 28 | private: 29 | std::string _progName; 30 | QLabel* _cameraScreen; 31 | QPushButton* _startCaptureButton; 32 | QPushButton* _stopCaptureButton; 33 | RtspSession* _rtspSession; 34 | }; 35 | 36 | 37 | #endif // APP_WINDOW_H 38 | -------------------------------------------------------------------------------- /dummy_sink.cpp: -------------------------------------------------------------------------------- 1 | #include "dummy_sink.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // Implementation of "DummySink": 8 | 9 | // Even though we're not going to be doing anything with the incoming data, we still need to receive it. 10 | // Define the size of the buffer that we'll use: 11 | #define DUMMY_SINK_RECEIVE_BUFFER_SIZE 100000 12 | 13 | DummySink* DummySink::createNew(RtspSession* session, UsageEnvironment& env, MediaSubsession& subsession, char const* streamId) { 14 | return new DummySink(session, env, subsession, streamId); 15 | } 16 | 17 | DummySink::DummySink(RtspSession* session, 18 | UsageEnvironment& env, MediaSubsession& subsession, char const* streamId) 19 | : MediaSink(env), 20 | session(session), 21 | frameIndex(0), 22 | fSubsession(subsession) { 23 | fStreamId = strDup(streamId); 24 | fReceiveBuffer = new u_int8_t[DUMMY_SINK_RECEIVE_BUFFER_SIZE]; 25 | fReceiveBufferAV = new u_int8_t[DUMMY_SINK_RECEIVE_BUFFER_SIZE + 4]; 26 | 27 | fReceiveBufferAV[0] = 0; 28 | fReceiveBufferAV[1] = 0; 29 | fReceiveBufferAV[2] = 0; 30 | fReceiveBufferAV[3] = 1; 31 | 32 | av_init_packet(&avpkt); 33 | avpkt.flags |= AV_PKT_FLAG_KEY; 34 | avpkt.pts = avpkt.dts = 0; 35 | 36 | memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); 37 | 38 | codec = avcodec_find_decoder(AV_CODEC_ID_H264); 39 | if (!codec) { 40 | envir() << "codec not found!"; 41 | exit(4); 42 | } 43 | 44 | codecContext = avcodec_alloc_context3(codec); 45 | frame = av_frame_alloc(); 46 | rgbFrame = av_frame_alloc(); 47 | avpicture_alloc( ( AVPicture *) rgbFrame, AV_PIX_FMT_RGB24, 1920, 1080); 48 | 49 | if (codec->capabilities & CODEC_CAP_TRUNCATED) { 50 | codecContext->flags |= CODEC_FLAG_TRUNCATED; // we do not send complete frames 51 | } 52 | 53 | codecContext->width = 1920; 54 | codecContext->height = 1080; 55 | codecContext->pix_fmt = AV_PIX_FMT_YUV420P; 56 | 57 | if (avcodec_open2(codecContext,codec,NULL) < 0) { 58 | envir() << "could not open codec"; 59 | exit(5); 60 | } 61 | 62 | this->swsContext = sws_getContext( codecContext->width, codecContext->height, codecContext->pix_fmt, 1920, 1080, 63 | AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); 64 | } 65 | 66 | DummySink::~DummySink() { 67 | delete[] fReceiveBuffer; 68 | delete [] fReceiveBufferAV; 69 | delete[] fStreamId; 70 | } 71 | 72 | void DummySink::afterGettingFrame(void* clientData, unsigned frameSize, unsigned numTruncatedBytes, 73 | struct timeval presentationTime, unsigned durationInMicroseconds) { 74 | DummySink* sink = (DummySink*)clientData; 75 | sink->afterGettingFrame(frameSize, numTruncatedBytes, presentationTime, durationInMicroseconds); 76 | } 77 | 78 | void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, 79 | struct timeval presentationTime, unsigned /*durationInMicroseconds*/) { 80 | // We've just received a frame of data. (Optionally) print out information about it: 81 | if (strcmp(fSubsession.codecName(),"H264") == 0) { 82 | avpkt.data = fReceiveBufferAV; 83 | avpkt.size = frameSize + 4; 84 | memcpy (fReceiveBufferAV + 4, fReceiveBuffer, frameSize); 85 | avpkt.data = fReceiveBufferAV; 86 | len = avcodec_decode_video2 (codecContext, frame, &got_picture, &avpkt); 87 | if (len < 0) { 88 | envir() << "Error while decoding frame " << frameIndex << "\n"; 89 | } 90 | if (got_picture) { 91 | envir() << "->Picture decoded :" << frameIndex << "\n"; 92 | sws_scale(this->swsContext, this->frame->data,this->frame->linesize, 93 | 0, this->codecContext->height, 94 | this->rgbFrame->data, this->rgbFrame->linesize); 95 | QImage *image = new QImage(this->rgbFrame->data[0], 96 | this->codecContext->width, 97 | this->codecContext->height, 98 | QImage::Format_RGB888); 99 | QString name = QString("/Users/hfli/Movies/%1.jpg").arg(frameIndex); 100 | 101 | emit this->session->gotFrame(*image); 102 | frameIndex ++; 103 | } 104 | } 105 | 106 | // Then continue, to request the next frame of data: 107 | continuePlaying(); 108 | } 109 | 110 | Boolean DummySink::continuePlaying() { 111 | if (fSource == NULL) return False; // sanity check (should not happen) 112 | 113 | // Request the next frame of data from our input source. "afterGettingFrame()" will get called later, when it arrives: 114 | fSource->getNextFrame(fReceiveBuffer, DUMMY_SINK_RECEIVE_BUFFER_SIZE, 115 | afterGettingFrame, this, 116 | onSourceClosure, this); 117 | return True; 118 | } 119 | 120 | void DummySink::setSprop(u_int8_t const* prop, unsigned size) { 121 | uint8_t *buf; 122 | uint8_t *buf_start; 123 | buf = (uint8_t *)malloc(1000); 124 | buf_start = buf + 4; 125 | 126 | avpkt.data = buf; 127 | avpkt.data[0] = 0; 128 | avpkt.data[1] = 0; 129 | avpkt.data[2] = 0; 130 | avpkt.data[3] = 1; 131 | memcpy (buf_start, prop, size); 132 | avpkt.size = size + 4; 133 | len = avcodec_decode_video2 (codecContext, frame, &got_picture, &avpkt); 134 | if (len < 0) { 135 | envir() << "Error while decoding frame" << frame; 136 | // exit(6); 137 | } 138 | 139 | envir() << "after setSprop\n"; 140 | // exit (111); 141 | } 142 | -------------------------------------------------------------------------------- /dummy_sink.h: -------------------------------------------------------------------------------- 1 | #ifndef DUMMY_SINK_H 2 | #define DUMMY_SINK_H 3 | 4 | #include "rtsp_session.h" 5 | 6 | #include 7 | #include 8 | 9 | extern "C" { 10 | #include 11 | #include 12 | #include 13 | #include 14 | } 15 | 16 | #define INBUF_SIZE 4096 17 | #define AUDIO_INBUF_SIZE 20480 18 | #define AUDIO_REFILL_THRESH 4096 19 | 20 | // Define a data sink (a subclass of "MediaSink") to receive the data for each subsession (i.e., each audio or video 'substream'). 21 | // In practice, this might be a class (or a chain of classes) that decodes and then renders the incoming audio or video. 22 | // Or it might be a "FileSink", for outputting the received data into a file (as is done by the "openRTSP" application). 23 | // In this example code, however, we define a simple 'dummy' sink that receives incoming data, but does nothing with it. 24 | 25 | class DummySink: public MediaSink { 26 | public: 27 | static DummySink* createNew(RtspSession *session, 28 | UsageEnvironment& env, 29 | MediaSubsession& subsession, // identifies the kind of data that's being received 30 | char const* streamId = NULL); // identifies the stream itself (optional) 31 | 32 | private: 33 | DummySink(RtspSession *session, UsageEnvironment& env, MediaSubsession& subsession, char const* streamId); 34 | // called only by "createNew()" 35 | virtual ~DummySink(); 36 | 37 | static void afterGettingFrame(void* clientData, unsigned frameSize, 38 | unsigned numTruncatedBytes, 39 | struct timeval presentationTime, 40 | unsigned durationInMicroseconds); 41 | void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, 42 | struct timeval presentationTime, unsigned durationInMicroseconds); 43 | //void saveFrameToFile() 44 | private: 45 | // redefined virtual functions: 46 | virtual Boolean continuePlaying(); 47 | 48 | public: 49 | void setSprop(u_int8_t const* prop, unsigned size); 50 | private: 51 | RtspSession* session; 52 | private: 53 | u_int8_t* fReceiveBuffer; 54 | u_int8_t* fReceiveBufferAV; 55 | MediaSubsession& fSubsession; 56 | char* fStreamId; 57 | private: 58 | AVCodec *codec; 59 | AVCodecContext *codecContext; 60 | int frameIndex; 61 | int got_picture; 62 | int len; 63 | AVFrame *frame; 64 | AVFrame *rgbFrame; 65 | SwsContext* swsContext; 66 | uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; 67 | char buf[1024]; 68 | AVPacket avpkt; 69 | }; 70 | 71 | #endif // DUMMY_SINK_H 72 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "app_window.h" 7 | 8 | #include "rtsp_client.h" 9 | #include "dummy_sink.h" 10 | #include "stream_state.h" 11 | 12 | int main(int argc, char** argv) { 13 | avcodec_register_all(); 14 | av_register_all(); 15 | QApplication app(argc, argv); 16 | AppWindow win(argv[0]); 17 | win.show(); 18 | 19 | return app.exec(); 20 | } 21 | -------------------------------------------------------------------------------- /rtsp_callback.cpp: -------------------------------------------------------------------------------- 1 | #include "rtsp_callback.h" 2 | #include "stream_state.h" 3 | #include "rtsp_client.h" 4 | #include "dummy_sink.h" 5 | 6 | #include 7 | 8 | // A function that outputs a string that identifies each stream (for debugging output). Modify this if you wish: 9 | UsageEnvironment& operator<<(UsageEnvironment& env, const RTSPClient& rtspClient) { 10 | return env << "[URL:\"" << rtspClient.url() << "\"]: "; 11 | } 12 | 13 | // A function that outputs a string that identifies each subsession (for debugging output). Modify this if you wish: 14 | UsageEnvironment& operator<<(UsageEnvironment& env, const MediaSubsession& subsession) { 15 | return env << subsession.mediumName() << "/" << subsession.codecName(); 16 | } 17 | 18 | void continueAfterDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString) { 19 | do { 20 | UsageEnvironment& env = rtspClient->envir(); // alias 21 | StreamClientState& scs = ((CustomRTSPClient*)rtspClient)->getStreamClientState(); // alias 22 | 23 | if (resultCode != 0) { 24 | env << *rtspClient << "Failed to get a SDP description: " << resultString << "\n"; 25 | delete[] resultString; 26 | break; 27 | } 28 | 29 | char* const sdpDescription = resultString; 30 | env << *rtspClient << "Got a SDP description:\n" << sdpDescription << "\n"; 31 | 32 | // Create a media session object from this SDP description: 33 | scs.session = MediaSession::createNew(env, sdpDescription); 34 | delete[] sdpDescription; // because we don't need it anymore 35 | if (scs.session == NULL) { 36 | env << *rtspClient << "Failed to create a MediaSession object from the SDP description: " << env.getResultMsg() << "\n"; 37 | break; 38 | } else if (!scs.session->hasSubsessions()) { 39 | env << *rtspClient << "This session has no media subsessions (i.e., no \"m=\" lines)\n"; 40 | break; 41 | } 42 | 43 | // Then, create and set up our data source objects for the session. We do this by iterating over the session's 'subsessions', 44 | // calling "MediaSubsession::initiate()", and then sending a RTSP "SETUP" command, on each one. 45 | // (Each 'subsession' will have its own data source.) 46 | scs.iter = new MediaSubsessionIterator(*scs.session); 47 | setupNextSubsession(rtspClient); 48 | return; 49 | } while (0); 50 | 51 | // An unrecoverable error occurred with this stream. 52 | shutdownStream(rtspClient); 53 | } 54 | 55 | void continueAfterSETUP(RTSPClient* rtspClient, int resultCode, char* resultString) { 56 | do { 57 | UsageEnvironment& env = rtspClient->envir(); // alias 58 | StreamClientState& streamClientState = ((CustomRTSPClient*)rtspClient)->getStreamClientState(); // alias 59 | 60 | if (resultCode != 0) { 61 | env << *rtspClient << "Failed to set up the \"" << *streamClientState.subsession << "\" subsession: " << resultString << "\n"; 62 | break; 63 | } 64 | 65 | env << *rtspClient << "Set up the \"" << *streamClientState.subsession << "\" subsession ("; 66 | if (streamClientState.subsession->rtcpIsMuxed()) { 67 | env << "client port " << streamClientState.subsession->clientPortNum(); 68 | } else { 69 | env << "client ports " << streamClientState.subsession->clientPortNum() << "-" << streamClientState.subsession->clientPortNum()+1; 70 | } 71 | env << ")\n"; 72 | 73 | const char *sprop = streamClientState.subsession->fmtp_spropparametersets(); 74 | uint8_t const* sps = NULL; 75 | unsigned spsSize = 0; 76 | uint8_t const* pps = NULL; 77 | unsigned ppsSize = 0; 78 | 79 | if (sprop != NULL) { 80 | unsigned numSPropRecords; 81 | SPropRecord* sPropRecords = parseSPropParameterSets(sprop, numSPropRecords); 82 | for (unsigned i = 0; i < numSPropRecords; ++i) { 83 | if (sPropRecords[i].sPropLength == 0) continue; // bad data 84 | u_int8_t nal_unit_type = (sPropRecords[i].sPropBytes[0])&0x1F; 85 | if (nal_unit_type == 7/*SPS*/) { 86 | sps = sPropRecords[i].sPropBytes; 87 | spsSize = sPropRecords[i].sPropLength; 88 | } else if (nal_unit_type == 8/*PPS*/) { 89 | pps = sPropRecords[i].sPropBytes; 90 | ppsSize = sPropRecords[i].sPropLength; 91 | } 92 | } 93 | } 94 | 95 | // Having successfully setup the subsession, create a data sink for it, and call "startPlaying()" on it. 96 | // (This will prepare the data sink to receive data; the actual flow of data from the client won't start happening until later, 97 | // after we've sent a RTSP "PLAY" command.) 98 | 99 | streamClientState.subsession->sink = DummySink::createNew(((CustomRTSPClient*)rtspClient)->getSession(), env, *streamClientState.subsession, rtspClient->url()); 100 | // perhaps use your own custom "MediaSink" subclass instead 101 | if (streamClientState.subsession->sink == NULL) { 102 | env << *rtspClient << "Failed to create a data sink for the \"" << *streamClientState.subsession 103 | << "\" subsession: " << env.getResultMsg() << "\n"; 104 | break; 105 | } 106 | 107 | env << *rtspClient << "Created a data sink for the \"" << *streamClientState.subsession << "\" subsession\n"; 108 | streamClientState.subsession->miscPtr = rtspClient; // a hack to let subsession handler functions get the "RTSPClient" from the subsession 109 | 110 | 111 | if (sps != NULL) { 112 | ((DummySink *)streamClientState.subsession->sink)->setSprop(sps, spsSize); 113 | } 114 | if (pps != NULL) { 115 | ((DummySink *)streamClientState.subsession->sink)->setSprop(pps, ppsSize); 116 | } 117 | 118 | streamClientState.subsession->sink->startPlaying(*(streamClientState.subsession->readSource()), 119 | subsessionAfterPlaying, streamClientState.subsession); 120 | 121 | // Also set a handler to be called if a RTCP "BYE" arrives for this subsession: 122 | if (streamClientState.subsession->rtcpInstance() != NULL) { 123 | streamClientState.subsession->rtcpInstance()->setByeHandler(subsessionByeHandler, streamClientState.subsession); 124 | } 125 | 126 | } while (0); 127 | delete[] resultString; 128 | 129 | // Set up the next subsession, if any: 130 | setupNextSubsession(rtspClient); 131 | } 132 | 133 | void continueAfterPLAY(RTSPClient* rtspClient, int resultCode, char* resultString) { 134 | Boolean success = False; 135 | 136 | do { 137 | UsageEnvironment& env = rtspClient->envir(); // alias 138 | StreamClientState& scs = ((CustomRTSPClient*)rtspClient)->getStreamClientState(); // alias 139 | 140 | if (resultCode != 0) { 141 | env << *rtspClient << "Failed to start playing session: " << resultString << "\n"; 142 | break; 143 | } 144 | 145 | // Set a timer to be handled at the end of the stream's expected duration (if the stream does not already signal its end 146 | // using a RTCP "BYE"). This is optional. If, instead, you want to keep the stream active - e.g., so you can later 147 | // 'seek' back within it and do another RTSP "PLAY" - then you can omit this code. 148 | // (Alternatively, if you don't want to receive the entire stream, you could set this timer for some shorter value.) 149 | if (scs.duration > 0) { 150 | unsigned const delaySlop = 2; // number of seconds extra to delay, after the stream's expected duration. (This is optional.) 151 | scs.duration += delaySlop; 152 | unsigned uSecsToDelay = (unsigned)(scs.duration*1000000); 153 | scs.streamTimerTask = env.taskScheduler().scheduleDelayedTask(uSecsToDelay, (TaskFunc*)streamTimerHandler, rtspClient); 154 | } 155 | 156 | env << *rtspClient << "Started playing session"; 157 | if (scs.duration > 0) { 158 | env << " (for up to " << scs.duration << " seconds)"; 159 | } 160 | env << "...\n"; 161 | 162 | success = True; 163 | } while (0); 164 | delete[] resultString; 165 | 166 | if (!success) { 167 | // An unrecoverable error occurred with this stream. 168 | shutdownStream(rtspClient); 169 | } 170 | } 171 | 172 | // By default, we request that the server stream its data using RTP/UDP. 173 | // If, instead, you want to request that the server stream via RTP-over-TCP, change the following to True: 174 | #define REQUEST_STREAMING_OVER_TCP False 175 | 176 | void setupNextSubsession(RTSPClient* rtspClient) { 177 | UsageEnvironment& env = rtspClient->envir(); // alias 178 | StreamClientState& scs = ((CustomRTSPClient*)rtspClient)->getStreamClientState(); // alias 179 | 180 | scs.subsession = scs.iter->next(); 181 | if (scs.subsession != NULL) { 182 | if (!scs.subsession->initiate()) { 183 | env << *rtspClient << "Failed to initiate the \"" << *scs.subsession << "\" subsession: " << env.getResultMsg() << "\n"; 184 | setupNextSubsession(rtspClient); // give up on this subsession; go to the next one 185 | } else { 186 | env << *rtspClient << "Initiated the \"" << *scs.subsession << "\" subsession ("; 187 | if (scs.subsession->rtcpIsMuxed()) { 188 | env << "client port " << scs.subsession->clientPortNum(); 189 | } else { 190 | env << "client ports " << scs.subsession->clientPortNum() << "-" << scs.subsession->clientPortNum()+1; 191 | } 192 | env << ")\n"; 193 | 194 | // Continue setting up this subsession, by sending a RTSP "SETUP" command: 195 | rtspClient->sendSetupCommand(*scs.subsession, continueAfterSETUP, False, REQUEST_STREAMING_OVER_TCP); 196 | } 197 | return; 198 | } 199 | 200 | // We've finished setting up all of the subsessions. Now, send a RTSP "PLAY" command to start the streaming: 201 | if (scs.session->absStartTime() != NULL) { 202 | // Special case: The stream is indexed by 'absolute' time, so send an appropriate "PLAY" command: 203 | rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY, scs.session->absStartTime(), scs.session->absEndTime()); 204 | } else { 205 | scs.duration = scs.session->playEndTime() - scs.session->playStartTime(); 206 | rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY); 207 | } 208 | } 209 | 210 | // Implementation of the other event handlers: 211 | 212 | void subsessionAfterPlaying(void* clientData) { 213 | MediaSubsession* subsession = (MediaSubsession*)clientData; 214 | RTSPClient* rtspClient = (RTSPClient*)(subsession->miscPtr); 215 | 216 | // Begin by closing this subsession's stream: 217 | Medium::close(subsession->sink); 218 | subsession->sink = NULL; 219 | 220 | // Next, check whether *all* subsessions' streams have now been closed: 221 | MediaSession& session = subsession->parentSession(); 222 | MediaSubsessionIterator iter(session); 223 | while ((subsession = iter.next()) != NULL) { 224 | if (subsession->sink != NULL) return; // this subsession is still active 225 | } 226 | 227 | // All subsessions' streams have now been closed, so shutdown the client: 228 | shutdownStream(rtspClient); 229 | } 230 | 231 | void subsessionByeHandler(void* clientData) { 232 | MediaSubsession* subsession = (MediaSubsession*)clientData; 233 | RTSPClient* rtspClient = (RTSPClient*)subsession->miscPtr; 234 | UsageEnvironment& env = rtspClient->envir(); // alias 235 | 236 | env << *rtspClient << "Received RTCP \"BYE\" on \"" << *subsession << "\" subsession\n"; 237 | 238 | // Now act as if the subsession had closed: 239 | subsessionAfterPlaying(subsession); 240 | } 241 | 242 | void streamTimerHandler(void* clientData) { 243 | CustomRTSPClient* rtspClient = (CustomRTSPClient*)clientData; 244 | StreamClientState& scs = rtspClient->getStreamClientState(); // alias 245 | 246 | scs.streamTimerTask = NULL; 247 | 248 | // Shut down the stream: 249 | shutdownStream(rtspClient); 250 | } 251 | 252 | void shutdownStream(RTSPClient* rtspClient){ 253 | UsageEnvironment& env = rtspClient->envir(); // alias 254 | StreamClientState& scs = ((CustomRTSPClient*)rtspClient)->getStreamClientState(); // alias 255 | 256 | // First, check whether any subsessions have still to be closed: 257 | if (scs.session != NULL) { 258 | Boolean someSubsessionsWereActive = False; 259 | MediaSubsessionIterator iter(*scs.session); 260 | MediaSubsession* subsession; 261 | 262 | while ((subsession = iter.next()) != NULL) { 263 | if (subsession->sink != NULL) { 264 | Medium::close(subsession->sink); 265 | subsession->sink = NULL; 266 | 267 | if (subsession->rtcpInstance() != NULL) { 268 | subsession->rtcpInstance()->setByeHandler(NULL, NULL); // in case the server sends a RTCP "BYE" while handling "TEARDOWN" 269 | } 270 | 271 | someSubsessionsWereActive = True; 272 | } 273 | } 274 | 275 | if (someSubsessionsWereActive) { 276 | // Send a RTSP "TEARDOWN" command, to tell the server to shutdown the stream. 277 | // Don't bother handling the response to the "TEARDOWN". 278 | rtspClient->sendTeardownCommand(*scs.session, NULL); 279 | } 280 | } 281 | 282 | std::cout << "Closing the stream.\n"; 283 | Medium::close(rtspClient); 284 | } 285 | -------------------------------------------------------------------------------- /rtsp_callback.h: -------------------------------------------------------------------------------- 1 | #ifndef RTSP_CALLBACK_H 2 | #define RTSP_CALLBACK_H 3 | 4 | #include 5 | #include 6 | 7 | // RTSP 'response handlers': 8 | void continueAfterDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString); 9 | void continueAfterSETUP(RTSPClient* rtspClient, int resultCode, char* resultString); 10 | void continueAfterPLAY(RTSPClient* rtspClient, int resultCode, char* resultString); 11 | 12 | void subsessionAfterPlaying(void* clientData); // called when a stream's subsession (e.g., audio or video substream) ends 13 | void subsessionByeHandler(void* clientData); // called when a RTCP "BYE" is received for a subsession 14 | void streamTimerHandler(void* clientData); 15 | 16 | // Used to iterate through each stream's 'subsessions', setting up each one: 17 | void setupNextSubsession(RTSPClient* rtspClient); 18 | void shutdownStream(RTSPClient* rtspClient); 19 | #endif // RTSP_CALLBACK_H 20 | -------------------------------------------------------------------------------- /rtsp_client.cpp: -------------------------------------------------------------------------------- 1 | #include "rtsp_client.h" 2 | 3 | // Implementation of "ourRTSPClient": 4 | 5 | CustomRTSPClient* CustomRTSPClient::createNew( 6 | RtspSession *session, 7 | UsageEnvironment& env, 8 | char const* rtspURL, 9 | int verbosityLevel, 10 | char const* applicationName, 11 | portNumBits tunnelOverHTTPPortNum) { 12 | return new CustomRTSPClient(session, env, rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum); 13 | } 14 | 15 | CustomRTSPClient::CustomRTSPClient( 16 | RtspSession *session, 17 | UsageEnvironment& env, 18 | char const* rtspURL, 19 | int verbosityLevel, 20 | char const* applicationName, 21 | portNumBits tunnelOverHTTPPortNum) 22 | : RTSPClient(env,rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum, -1) , 23 | session(session){ 24 | } 25 | 26 | CustomRTSPClient::~CustomRTSPClient() { 27 | } 28 | -------------------------------------------------------------------------------- /rtsp_client.h: -------------------------------------------------------------------------------- 1 | #ifndef CAMERA_RTSP_CLIENT_H 2 | #define CAMERA_RTSP_CLIENT_H 3 | 4 | #include 5 | #include 6 | 7 | #include "stream_state.h" 8 | #include "rtsp_session.h" 9 | 10 | // If you're streaming just a single stream (i.e., just from a single URL, once), then you can define and use just a single 11 | // "StreamClientState" structure, as a global variable in your application. However, because - in this demo application - we're 12 | // showing how to play multiple streams, concurrently, we can't do that. Instead, we have to have a separate "StreamClientState" 13 | // structure for each "RTSPClient". To do this, we subclass "RTSPClient", and add a "StreamClientState" field to the subclass: 14 | 15 | class CustomRTSPClient: public RTSPClient { 16 | public: 17 | static CustomRTSPClient* createNew( 18 | RtspSession *session, 19 | UsageEnvironment& env, 20 | char const* rtspURL, 21 | int verbosityLevel = 0, 22 | char const* applicationName = NULL, 23 | portNumBits tunnelOverHTTPPortNum = 0); 24 | 25 | protected: 26 | CustomRTSPClient(RtspSession *session, 27 | UsageEnvironment& env, 28 | char const* rtspURL, 29 | int verbosityLevel, 30 | char const* applicationName, 31 | portNumBits tunnelOverHTTPPortNum); 32 | // called only by createNew(); 33 | virtual ~CustomRTSPClient(); 34 | public: 35 | StreamClientState& getStreamClientState() { return this->streamClientState; } 36 | RtspSession* getSession() { return this->session; } 37 | private: 38 | RtspSession* session; 39 | StreamClientState streamClientState; 40 | }; 41 | 42 | 43 | #endif // CAMERA_RTSP_CLIENT_H 44 | -------------------------------------------------------------------------------- /rtsp_session.cpp: -------------------------------------------------------------------------------- 1 | #include "rtsp_session.h" 2 | #include "rtsp_client.h" 3 | #include "rtsp_callback.h" 4 | 5 | #include 6 | 7 | #define RTSP_CLIENT_VERBOSITY_LEVEL 1 8 | 9 | RtspSession::RtspSession(int id, const char* progName, const char* url, QObject* parent) 10 | : _id(id), 11 | _progName(progName), 12 | _url(url), 13 | _rtspClient(NULL), 14 | _eventLoopWatchVariable(0), 15 | QThread(parent){ 16 | } 17 | 18 | RtspSession::~RtspSession(){ 19 | stop(); 20 | quit(); 21 | wait(); 22 | } 23 | 24 | void RtspSession::stop(){ 25 | this->_eventLoopWatchVariable = 1; 26 | } 27 | 28 | void RtspSession::run(){ 29 | this->_eventLoopWatchVariable = 0; 30 | 31 | TaskScheduler* scheduler= BasicTaskScheduler::createNew(); 32 | UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler); 33 | 34 | if(!this->openURL(*env, this->_progName.c_str(), this->_url.c_str())){ 35 | return; 36 | } 37 | env->taskScheduler().doEventLoop(&this->_eventLoopWatchVariable); 38 | std::cout << "Rtsp event finished" << std::endl; 39 | env->reclaim(); 40 | env = NULL; 41 | delete scheduler; 42 | scheduler = NULL; 43 | } 44 | 45 | bool RtspSession::openURL(UsageEnvironment &env, const char *progName, const char *url){ 46 | this->_rtspClient = CustomRTSPClient::createNew( 47 | this, 48 | env, 49 | url, 50 | RTSP_CLIENT_VERBOSITY_LEVEL, 51 | progName); 52 | if (this->_rtspClient == NULL) { 53 | std::cerr << "Failed to create a RTSP client for URL \"" 54 | << this->_url << "\": " 55 | << env.getResultMsg() << "\n"; 56 | return false; 57 | } 58 | this->_rtspClient->sendDescribeCommand(continueAfterDESCRIBE); 59 | return true; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /rtsp_session.h: -------------------------------------------------------------------------------- 1 | #ifndef RSTP_CLIENT_H 2 | #define RSTP_CLIENT_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | class RtspSession: public QThread{ 11 | Q_OBJECT 12 | public: 13 | explicit RtspSession(int id, const char* progName, const char* url, QObject *parent =NULL); 14 | virtual ~RtspSession(); 15 | public: 16 | void stop(); 17 | signals: 18 | void gotFrame(const QImage& image); 19 | protected: 20 | bool openURL(UsageEnvironment& env, const char* progName, const char * url); 21 | void run(); 22 | private: 23 | int _id; 24 | std::string _progName; 25 | std::string _url; 26 | RTSPClient* _rtspClient; 27 | char _eventLoopWatchVariable; 28 | }; 29 | 30 | #endif // RSTP_CLIENT_H 31 | -------------------------------------------------------------------------------- /stream_state.cpp: -------------------------------------------------------------------------------- 1 | #include "stream_state.h" 2 | 3 | // Implementation of "StreamClientState": 4 | 5 | StreamClientState::StreamClientState() 6 | : iter(NULL), session(NULL), subsession(NULL), streamTimerTask(NULL), duration(0.0) { 7 | } 8 | 9 | StreamClientState::~StreamClientState() { 10 | delete iter; 11 | if (session != NULL) { 12 | // We also need to delete "session", and unschedule "streamTimerTask" (if set) 13 | UsageEnvironment& env = session->envir(); // alias 14 | 15 | env.taskScheduler().unscheduleDelayedTask(streamTimerTask); 16 | Medium::close(session); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stream_state.h: -------------------------------------------------------------------------------- 1 | #ifndef STREAM_STATE_H 2 | #define STREAM_STATE_H 3 | 4 | #include 5 | #include 6 | 7 | // Define a class to hold per-stream state that we maintain throughout each stream's lifetime: 8 | 9 | class StreamClientState { 10 | public: 11 | StreamClientState(); 12 | virtual ~StreamClientState(); 13 | 14 | public: 15 | MediaSubsessionIterator* iter; 16 | MediaSession* session; 17 | MediaSubsession* subsession; 18 | TaskToken streamTimerTask; 19 | double duration; 20 | }; 21 | 22 | 23 | #endif // STREAM_STATE_H 24 | --------------------------------------------------------------------------------