1 /* This file is part of the Pangolin Project.
2  * http://github.com/stevenlovegrove/Pangolin
3  *
4  * Copyright (c) 2013 Steven Lovegrove
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include <pangolin/video/drivers/truncate.h>
29 #include <pangolin/factory/factory_registry.h>
30 #include <pangolin/video/iostream_operators.h>
31 
32 namespace pangolin
33 {
34 
TruncateVideo(std::unique_ptr<VideoInterface> & src_,size_t begin,size_t end)35 TruncateVideo::TruncateVideo(std::unique_ptr<VideoInterface> &src_, size_t begin, size_t end)
36     : src(std::move(src_)), streams(src->Streams()), begin(begin), end(end), next_frame_to_grab(0)
37 {
38     videoin.push_back(src.get());
39 
40     if(VideoPlaybackInterface* v = GetVideoPlaybackInterface()){
41         // Guard against the obscure case of nested TruncateVideo filters
42         if( !pangolin::FindFirstMatchingVideoInterface<TruncateVideo>(*src_) ) {
43             next_frame_to_grab = v->Seek(begin);
44         }
45     }
46 }
47 
~TruncateVideo()48 TruncateVideo::~TruncateVideo()
49 {
50 }
51 
SizeBytes() const52 size_t TruncateVideo::SizeBytes() const
53 {
54     return videoin[0]->SizeBytes();
55 }
56 
Streams() const57 const std::vector<StreamInfo>& TruncateVideo::Streams() const
58 {
59     return streams;
60 }
61 
Start()62 void TruncateVideo::Start()
63 {
64     videoin[0]->Start();
65 }
66 
Stop()67 void TruncateVideo::Stop()
68 {
69     videoin[0]->Stop();
70 }
71 
GrabNext(unsigned char * image,bool wait)72 bool TruncateVideo::GrabNext( unsigned char* image, bool wait )
73 {
74     if(next_frame_to_grab < end) {
75         bool grab_success = videoin[0]->GrabNext(image, wait);
76         return grab_success && (next_frame_to_grab++) >= begin;
77     }
78     return false;
79 }
80 
GrabNewest(unsigned char * image,bool wait)81 bool TruncateVideo::GrabNewest( unsigned char* image, bool wait )
82 {
83     return videoin[0]->GrabNewest(image, wait);
84 }
85 
InputStreams()86 std::vector<VideoInterface*>& TruncateVideo::InputStreams()
87 {
88     return videoin;
89 }
90 
PANGOLIN_REGISTER_FACTORY(TruncateVideo)91 PANGOLIN_REGISTER_FACTORY(TruncateVideo)
92 {
93     struct TruncateVideoFactory : public FactoryInterface<VideoInterface> {
94         std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
95             std::unique_ptr<VideoInterface> subvid = pangolin::OpenVideo(uri.url);
96             if(subvid->Streams().size() == 0) {
97                 throw VideoException("VideoTruncater input must have at least one stream");
98             }
99 
100             const size_t start = uri.Get<size_t>("begin", 0);
101             const size_t end = uri.Get<size_t>("end", std::numeric_limits<size_t>::max());
102 
103             return std::unique_ptr<VideoInterface>( new TruncateVideo(subvid,start,end) );
104         }
105     };
106 
107     FactoryRegistry<VideoInterface>::I().RegisterFactory(std::make_shared<TruncateVideoFactory>(), 10, "truncate");
108 }
109 
110 }
111